微信小程序支付步骤

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

[javascript] view plain copy
 
print?在CODE上查看代码片派生到个人代码片
  1. wx.requestPayment({  
  2.         nonceStr: res.data.nonceStr,  
  3.         package: "prepay_id="+res.data.prepayId,  
  4.         signType: 'MD5',  
  5.         timeStamp: res.data.timestamp,      
  6.         paySign: sign,//<strong><span style="color:#ff0000;">五个字段参与签名(区分大小写):appId,nonceStr,package,signType,timeStamp(须要注意的是,这5个参数签名排序的顺序按照ASCII字典序排序)</span></strong>  
  7.         success: function(res){  
  8.           console.log("支付成功");  
  9.         },  
  10.         fail: function() {  
  11.         },  
  12.         complete: function() {  
  13.         }  
  14.       })  


生成签名sign
微信小程序 MD5加密工具下载地址:https://code.csdn.net/snippets/2019875/master/download
[javascript] view plain copy
 
print?在CODE上查看代码片派生到个人代码片
  1. var MD5Util = require('../../../utils/md5.js');  
[javascript] view plain copy
 
print?在CODE上查看代码片派生到个人代码片
  1. var sign = '';  
  2. //<strong><span style="color:#ff0000;">顺序按照ASCII字典序排序</span></strong>  
  3. var signA = "appId="+app.appId+"&nonceStr="+res.data.nonceStr+"&package=prepay_id="+res.data.prepayId+"&signType=MD5&timeStamp="+res.data.timestamp;  
  4. var signB = signA+"&key="+app.key;  
  5. 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。
最终获得最终发送的数据:
[html] view plain copy
 
print?在CODE上查看代码片派生到个人代码片
  1. <xml>  
  2. <appid>wxd930ea5d5a258f4f</appid>  
  3. <mch_id>10000100</mch_id>  
  4. <device_info>1000<device_info>  
  5. <body>test</body>  
  6. <nonce_str>ibuaiVcKdpRxkhJA</nonce_str>  
  7. <sign>9A0A8659F005D6984697E2CA0A9CF3B7</sign>  
  8. <xml>  
OK, 微信小程序支付完成。66666
相关文章
相关标签/搜索