当前位置:   article > 正文

uniapp+nodejs实现小程序支付

uniapp+nodejs实现小程序支付

1.准备商户号、企业级小程序(或者个体工商户级别的)

2.在小程序端调用uni.login获取code,传递给后端

  1. uni.login({
  2. success: loginRes => {
  3. uni.request({
  4. url: "http://127.0.0.1:3003/wxpay/pay",
  5. data: {
  6. code: loginRes.code
  7. },
  8. method: "get",
  9. success: res => {
  10. },
  11. fail: err => {
  12. }
  13. })
  14. }
  15. })

3.在nodejs端,

  1. let express = require("express");
  2. let router = express.Router();
  3. const WxPay = require('wechatpay-node-v3');
  4. const fs = require('fs');
  5. let api = require("../api/index");
  6. var path = require('path');
  7. let weixinData = require('../utils/weixinData');
  8. const qs = require('querystring')
  9. //根据code查找openid
  10. function getOpenid(code){
  11. return new Promise((resolve, reject) => {
  12. let appid = weixinData.Appid
  13. let secret = weixinData.AppSecret
  14. let params = {
  15. appid,
  16. secret,
  17. js_code:code
  18. }
  19. let formData = qs.stringify(params)
  20. let openIdUrl = "https://api.weixin.qq.com/sns/jscode2session?" + formData + "&grant_type=authorization_code"
  21. api.get(openIdUrl).then( res => {
  22. resolve(res.data.openid)
  23. })
  24. })
  25. }
  26. // 创建支付实例
  27. const pay = new WxPay({
  28. appid: 'wx9fc72986dc9287b6',
  29. mchid: '1674432158',
  30. publicKey: fs.readFileSync(path.resolve(__dirname, 'apiclient_cert.pem')), // 公钥
  31. privateKey: fs.readFileSync(path.resolve(__dirname, 'apiclient_key.pem')), // 秘钥
  32. key: 'zlc010903zlc010903zlc010903wcpwc'
  33. });
  34. // 订单号生成函数
  35. function randomNumber() {
  36. const now = new Date()
  37. let month = now.getMonth() + 1
  38. let day = now.getDate()
  39. let hour = now.getHours()
  40. let minutes = now.getMinutes()
  41. let seconds = now.getSeconds()
  42. month = month < 10 ? "0" + month : month;
  43. day = day < 10 ? "0" + day : day;
  44. hour = hour < 10 ? "0" + hour : hour;
  45. minutes = minutes < 10 ? "0" + minutes : minutes;
  46. seconds = seconds < 10 ? "0" + seconds : seconds;
  47. let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 1000000)).toString();
  48. return orderCode;
  49. }
  50. let notify = async (req, res) => {
  51. // 申请的APIv3
  52. let key = 'zlc010903zlc010903zlc010903wcpwc';
  53. let { ciphertext, associated_data, nonce } = req.body.resource;
  54. // 解密回调信息
  55. const result = pay.decipher_gcm(ciphertext, associated_data, nonce, key);
  56. // 拿到订单号
  57. let { out_trade_no } = result;
  58. if (result.trade_state == 'SUCCESS') {
  59. // 支付成功之后需要进行的业务逻辑
  60. console.log('支付成功')
  61. }
  62. }
  63. // 拿到支付所需参数
  64. let payInfo = (req, res) => {
  65. let { code } = req.query
  66. //获取openid
  67. getOpenid(code).then(async resolve => {
  68. const params = {
  69. mchid: '1674432158',
  70. description: '支付测试', // 订单描述
  71. out_trade_no: randomNumber(), // 订单号,一般每次发起支付都要不一样,可使用随机数生成
  72. notify_url: 'http://127.0.0.1:3003/wxpay/notify_url',
  73. amount: {
  74. total: 1, // 支付金额,单位为分
  75. currency: 'CNY'
  76. },
  77. payer: {
  78. openid: resolve, // 微信小程序用户的openid,一般需要前端发送过来
  79. },
  80. scene_info: {
  81. payer_client_ip: 'ip', // 支付者ip,这个不用理会也没有问题
  82. },
  83. };
  84. const result = await pay.transactions_jsapi(params);
  85. // 将结果响应给微信小程序前端
  86. res.send(result);
  87. })
  88. }
  89. // 回调路由
  90. router.post('/notify_url', notify)
  91. router.get('/pay', payInfo);
  92. module.exports = router

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/794823
推荐阅读
相关标签
  

闽ICP备14008679号