最近负责的一些项目开发,都用到了微信支付(微信公众号支付、微信H5支付、微信扫码支付)。在开发的过程中,在调试支付的过程中,或多或少都遇到了一些问题,今天总结下,分享,留存。代码在文章结尾处,有需要的同学可以下载看下。
先说注意的第一点,所有支付的第一步都是请求统一下单,统一下单,统一下单,请求URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder。
再说一个微信官方提供的一个很重要的工具,微信支付接口签名校验工具(网址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1),此工具旨在帮助开发者检测调用【微信支付接口API】时发送的请求参数中生成的签名是否正确,提交相关信息后可获得签名校验结果。特别实用!特别实用!特别实用!签名只要正确了,一切就OK了!
第一部分 微信公众号支付
微信公众号支付需要配置的参数有:APPID(微信公众号开发者ID)、APPSECRET(微信公众号开发者密码)、MCHID(商户ID)、KEY(商户密钥)。
微信公众号支付应用的场景是在微信内部的H5环境中使用的支付方式。因为要通过网页授权获取用户的OpenId,所以必须要配置网页授权域名。同时要配置JS接口安全域名。
JsApiConfig.cs
1 using System.Web; 2 using System.Text; 3 using System.IO; 4 using System.Net; 5 using System; 6 using System.Xml; 7 using System.Collections.Generic; 8 using Gwbnsh.Common; 9 10 namespace Gwbnsh.API.Payment.wxpay 11 { 12 public class JsApiConfig 13 { 14 #region 字段 15 private string partner = string.Empty; 16 private string key = string.Empty; 17 private string appid = string.Empty; 18 private string appsecret = string.Empty; 19 private string redirect_url = string.Empty; 20 private string notify_url = string.Empty; 21 #endregion 22 23 public JsApiConfig(int site_payment_id) 24 { 25 Model.site_payment model = new BLL.site_payment().GetModel(site_payment_id); //站点支付方式 26 if (model != null) 27 { 28 Model.payment payModel = new BLL.payment().GetModel(model.payment_id); //支付平台 29 Model.sites siteModel = new BLL.sites().GetModel(model.site_id); //站点配置 30 Model.sysconfig sysConfig = new BLL.sysconfig().loadConfig(); //系统配置 31 32 partner = model.key1; //商户号(必须配置) 33 key = model.key2; //商户支付密钥,参考开户邮件设置(必须配置) 34 appid = model.key3; //绑定支付的APPID(必须配置) 35 appsecret = model.key4; //公众帐号secert(仅JSAPI支付的时候需要配置) 36 37 //获取用户的OPENID回调地址及登录后的回调地址 38 redirect_url = "http://m.gwbnsh.net.cn/hd/SellPhone" + payModel.return_url; 39 notify_url = "http://m.gwbnsh.net.cn/hd/SellPhone" + payModel.notify_url; 40 } 41 } 42 } 43 44 #region 属性 45 /// <summary> 46 /// 商户号(必须配置) 47 /// </summary> 48 public string Partner 49 { 50 get { return partner; } 51 set { partner = value; } 52 } 53 54 /// <summary> 55 /// 获取或设交易安全校验码 56 /// </summary> 57 public string Key 58 { 59 get { return key; } 60 set { key = value; } 61 } 62 63 /// <summary> 64 /// 绑定支付的APPID(必须配置) 65 /// </summary> 66 public string AppId 67 { 68 get { return appid; } 69 set { appid = value; } 70 } 71 72 /// <summary> 73 /// 公众帐号secert(仅JSAPI支付的时候需要配置) 74 /// </summary> 75 public string AppSecret 76 { 77 get { return appsecret; } 78 set { appsecret = value; } 79 } 80 81 /// <summary> 82 /// 获取用户的OPENID回调地址 83 /// </summary> 84 public string Redirect_url 85 { 86 get { return redirect_url; } 87 } 88 89 /// <summary> 90 /// 获取服务器异步通知页面路径 91 /// </summary> 92 public string Notify_url 93 { 94 get { return notify_url; } 95 } 96 97 #endregion 98 } 99 }