NativePay.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. namespace LYFZ.WxPayAPI
  5. {
  6. public class NativePay
  7. {
  8. /**
  9. * 生成扫描支付模式一URL
  10. * @param productId 商品ID
  11. * @return 模式一URL
  12. */
  13. public string GetPrePayUrl(string productId)
  14. {
  15. Log.Info(this.GetType().ToString(), "Native pay mode 1 url is producing...");
  16. WxPayData data = new WxPayData();
  17. data.SetValue("appid", WxPayConfig.APPID);//公众帐号id
  18. data.SetValue("mch_id", WxPayConfig.MCHID);//商户号
  19. data.SetValue("time_stamp", WxPayApi.GenerateTimeStamp());//时间戳
  20. data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串
  21. data.SetValue("product_id", productId);//商品ID
  22. data.SetValue("sign", data.MakeSign());//签名
  23. string str = ToUrlParams(data.GetValues());//转换为URL串
  24. string url = "weixin://wxpay/bizpayurl?" + str;
  25. Log.Info(this.GetType().ToString(), "Get native pay mode 1 url : " + url);
  26. return url;
  27. }
  28. /**
  29. * 生成直接支付url,支付url有效期为2小时,模式二
  30. * @param productId 商品ID
  31. * @return 模式二URL
  32. */
  33. public string GetPayUrl(string productId)
  34. {
  35. Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing...");
  36. WxPayData data = new WxPayData();
  37. data.SetValue("body", "test");//商品描述
  38. data.SetValue("attach", "test");//附加数据
  39. data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());//随机字符串
  40. data.SetValue("total_fee", 1);//总金额
  41. data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
  42. data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
  43. data.SetValue("goods_tag", "jjj");//商品标记
  44. data.SetValue("trade_type", "NATIVE");//交易类型
  45. data.SetValue("product_id", productId);//商品ID
  46. WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
  47. string url = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接
  48. Log.Info(this.GetType().ToString(), "Get native pay mode 2 url : " + url);
  49. return url;
  50. }
  51. /**
  52. * 参数数组转换为url格式
  53. * @param map 参数名与参数值的映射表
  54. * @return URL字符串
  55. */
  56. private string ToUrlParams(SortedDictionary<string, object> map)
  57. {
  58. string buff = "";
  59. foreach (KeyValuePair<string, object> pair in map)
  60. {
  61. buff += pair.Key + "=" + pair.Value + "&";
  62. }
  63. buff = buff.Trim('&');
  64. return buff;
  65. }
  66. }
  67. }