http://blog.csdn.net/wangsf789/article/details/53419781javascript
最近开发微信小程序进入到支付阶段,一直以来从事App开发,因此支付流程仍是熟记于心的。可是微信小程序的支付就有点奇怪了,应用的建立是在公众号里,可是文档的介绍却在公众号中没法找到直接入口,甚是不解,并且小程序的师傅究竟是属于公众号支付范畴仍是app支付范畴也成疑问。下面是小程序支付文档的入口(嵌套在小程序api中):php
小程序支付步骤:
1,预支付
2,根据预支付数据+签名——>发起支付
3,支付回调
下面对这3个步骤进行简单描述:
1,预支付。该接口经过前端提交需支付的信息(订单号、价格等)给开发者服务器,开发者服务器在提交到微信,然后返回真正前端须要进行支付的一些信息;eg:
开发者服务器的预支付接口:
https://()htm?
total_fee=100&cid=6001&orderCodes=2016120119
{
"sign":"A2****************A6",
"timestamp":"14
****************68",
"package":"Sign=WXPay",
"partnerId":"14
****************02",
"appid":"wx
****************ab",
"nonceStr":"9f
****************37",
"prepayId":"wx
****************54"
}
2,发起支付(须要注意的是发起支付是不须要上传appid, 可是签名paySign须要appid ,并且放在第一个)html
- wx.requestPayment({
- nonceStr: res.data.nonceStr,
- package: "prepay_id="+res.data.prepayId,
- signType: 'MD5',
- timeStamp: res.data.timestamp,
- paySign: sign,
- success: function(res){
- console.log("支付成功");
- },
- fail: function() {
- },
- complete: function() {
- }
- })
生成签名sign
微信小程序
MD5加密工具下载地址:https://code.csdn.net/snippets/2019875/master/download
- var MD5Util = require('../../../utils/md5.js');
- var sign = '';
- var signA = "appId="+app.appId+"&nonceStr="+res.data.nonceStr+"&package=prepay_id="+res.data.prepayId+"&signType=MD5&timeStamp="+res.data.timestamp;
- var signB = signA+"&key="+app.key;
- sign = MD5Util.MD5(signB).toUpperCase();
生成签名上面是个人代码,不是很清晰, 下面列出来官方文档的详细描述:
假设传送的参数以下:
appid: wxd930ea5d5a258f4f(须要注意的是appid 在wx.requestPayment({})发起支付是不上传,可是签名时须要)
mch_id: 10000100
device_info: 1000
body: test
nonce_str: ibuaiVcKdpRxkhJA
第一步:对参数按照key=value的格式,并按照参数名ASCII字典序排序以下:
stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";
第二步:拼接API密钥:
stringSignTemp="stringA&key=192006250b4c09247ec02edce69f6a2d"
sign=MD5(stringSignTemp).toUpperCase()="9A0A8659F005D6984697E2CA0A9CF3B7"
此时的sign 用于wx.requestPayment 上传参数paySign。
最终获得最终发送的数据:
- <xml>
- <appid>wxd930ea5d5a258f4f</appid>
- <mch_id>10000100</mch_id>
- <device_info>1000<device_info>
- <body>test</body>
- <nonce_str>ibuaiVcKdpRxkhJA</nonce_str>
- <sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign>
- <xml>
OK, 微信小程序支付完成。66666