一起源码网

  • www.171739.xyz
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 高级进阶 查看主题

微信小程序支付开发教程

发布者: jackyrong | 发布时间: 2018-5-7 03:35| 查看数: 6411| 评论数: 1|帖子模式

作者:蜗牛呆呆,来自原文地址

微信小程序支付终于踩完坑了,发现里面坑挺大的,现在发个贴,希望以后入坑的同学可以看一下 
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_4&index=2 
业务流程在这里大家看文档的时候可以看到。第一个坑,获取用户的openid,参数一定要拼在url连接上,否则会报{"errcode":40013,"errmsg":"invalid appid, hints: [ req_id: iil1ba0504ns86 ]"}错误

  1. onLoad: function () {
  2. var that = this
  3. wx.login({
  4. success: function (res) {
  5. if (res.code) {
  6. //发起网络请求
  7. wx.request({
  8. url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxaacf22345345cfc7162fe3&secret=83ebd41c3e6f34a49b3a34578063434548ff3f71&js_code=' + res.code + '&grant_type=authorization_code',
  9. method: "POST",
  10. success: function (res) {
  11. that.setData({
  12. openid: res.data.openid
  13. })
  14. }
  15. })
  16. } else {
  17. console.log('获取用户登录态失败!' + res.errMsg)
  18. }
  19. }
  20. });
  21. }

第二个坑,支付统一下单接口,签名这个坑是比较多人遇到问题的这个是MD5加密经常和签名工具里面的加密签名不一样 
签名加密工具地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1 
签名加密的时候要转成utf-8,加密我用自己的接口进行加密的 digest.update(data.getBytes("utf-8"));

  1. // 统一下单接口获取sign(签名)
  2. paysignjsapi: function (appid, attach, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, key) {
  3. var self = this;
  4. //加密签名
  5. wx.request({
  6. url: 'http://localhost:8080/XinXingWXApi/wxXcxApi/Md5Encrypt.do',
  7. method: 'GET',
  8. data: {
  9. appid: appid,
  10. attach: attach,
  11. body: body,
  12. mch_id: mch_id,
  13. nonce_str: nonce_str,
  14. notify_url: notify_url,
  15. openid: openid,
  16. out_trade_no: out_trade_no,
  17. spbill_create_ip: spbill_create_ip,
  18. total_fee: total_fee,
  19. trade_type: trade_type,
  20. key: key
  21. },
  22. //统一下单
  23. success: function (res) {
  24. var sign = res.data.strMd5
  25. var formData = "<xml>"
  26. formData += "<appid>" + appid + "</appid>" //appid
  27. formData += "<attach>" + attach + "</attach>" //附加数据
  28. formData += "<body>" + body + "</body>" //标题
  29. formData += "<mch_id>" + mch_id + "</mch_id>" //商户号
  30. formData += "<nonce_str>" + nonce_str + "</nonce_str>" //随机字符串,不长于32位。
  31. formData += "<notify_url>" + notify_url + "</notify_url>" //异步接收微信支付结果通知的回调地址
  32. formData += "<openid>" + openid + "</openid>" //用户Id
  33. formData += "<out_trade_no>" + out_trade_no + "</out_trade_no>" //商户订单号
  34. formData += "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>"
  35. formData += "<total_fee>" + total_fee + "</total_fee>" //金额
  36. formData += "<trade_type>" + trade_type + "</trade_type>" //公共号支付
  37. formData += "<sign>" + sign + "</sign>"//签名
  38. formData += "</xml>"

返回数据解析xml

  1. //请求统一下单接口
  2. wx.request({
  3. url: "https://api.mch.weixin.qq.com/pay/unifiedorder",
  4. method: 'POST',
  5. data: formData,
  6. success: function (data) {
  7. wx.request({
  8. url: "http://localhost:8080/XinXingWXApi/wxXcxApi/xmlAnalyze.do?strXml=" + data.data,
  9. method: 'POST',
  10. success: function (res) {
  11. var pk = 'prepay_id=' + res.data.prepayId;
  12. var timeStamp = self.createTimeStamp();
  13. //获取支付签名,并支付
  14. self.getsignType(appid, timeStamp, nonce_str, pk, "MD5", key);
  15. }
  16. })
  17. }
  18. })
  19. }
  20. });
  21. }

第三就是调用支付了,这里也有几个小坑,第一就是appId很多写成appid就不行了,第二个就是preoatid 的参数格式要写对prepay_id=wx2017011711060194dccf725232155886323 第三个就是调用支付的时候报支付签名错误,也需要到签名接口查看签名是否一致,查看参数是否是对的,调用微信支付的时候必须加上appId

  1. getsignType: function (appid, timeStamp, nonce_str, pk, signType, key) {
  2. var that = this;
  3. wx.request({
  4. url: "http://localhost:8080/XinXingWXApi/wxXcxApi/getSignType.hn",
  5. method: 'GET',
  6. data: {
  7. appId: appid,
  8. timeStamp: timeStamp,
  9. nonceStr: nonce_str,
  10. pk: pk,
  11. signType: signType,
  12. key: key
  13. },
  14. success: function (res) {
  15. console.log(res.data.paySign)
  16. var paySign = res.data.paySign
  17. //调用微信支付
  18. wx.requestPayment({
  19. 'appId': appid,
  20. 'timeStamp': timeStamp,
  21. 'nonceStr': nonce_str,
  22. 'package': pk,
  23. 'signType': 'MD5',
  24. 'paySign': paySign,
  25. 'success': function (res) {
  26. console.log(res);
  27. console.log('success');
  28. },
  29. 'fail': function (res) {
  30. console.log(res);
  31. console.log('fail');
  32. },
  33. 'complete': function (res) {
  34. // console.log(res);
  35. console.log('complete');
  36. }
  37. });
  38. }
  39. })
  40. }

大致就是这样了,如果有同学遇到问题可以下面留言可以一起探讨一下

最新评论

一杯咖_PT0tk 发表于 2022-6-13 15:24
通过源代码免费下载文档

浏览过的版块

一起源码让程序更轻更快

www.171739.xyz

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表