只提供微信小程序端代码:
1 var app = getApp(); 2 Page({ 3 data: {}, 4 onLoad: function (options) { 5 // 页面初始化 options为页面跳转所带来的参数 6 var that = this 7 //登陆获取code 8 wx.login({ 9 success: function (res) { 10 console.log(res.code) 11 //获取openid 12 that.getOpenId(res.code) 13 } 14 }); 15 }, 16 getOpenId: function (code) { 17 var that = this; 18 wx.request({ 19 url: "https://api.weixin.qq.com/sns/jscode2session?appid=小程序appid&secret=小程序应用密钥&js_code=" + code + "&grant_type=authorization_code", 20 data: {}, 21 method: ‘GET‘, 22 success: function (res) { 23 that.generateOrder(res.data.openid) 24 }, 25 fail: function () { 26 // fail 27 }, 28 complete: function () { 29 // complete 30 } 31 }) 32 }, 33 /**生成商户订单 */ 34 generateOrder: function (openid) { 35 var that = this 36 //统一支付 37 wx.request({ 38 url: ‘后台路径‘, 39 method: ‘GET‘, 40 data: { 41 gfee: ‘商品价钱‘, 42 gname: ‘商品名称‘, 43 openId:openid 44 (商品价钱和商品名称根据自身需要是否传值,openid为必传) 45 }, 46 success: function (res) { 47 var pay = res.data 48 //发起支付 49 var timeStamp = pay[0].timeStamp; 50 var packages = pay[0].package; 51 var paySign = pay[0].paySign; 52 var nonceStr = pay[0].nonceStr; 53 var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; 54 that.pay(param) 55 }, 56 }) 57 }, 58 59 /* 支付 */ 60 pay: function (param) { 61 console.log("支付") 62 console.log(param) 63 wx.requestPayment({ 64 timeStamp: param.timeStamp, 65 nonceStr: param.nonceStr, 66 package: param.package, 67 signType: param.signType, 68 paySign: param.paySign, 69 success: function (res) { 70 // success 71 wx.navigateBack({ 72 delta: 1, // 回退前 delta(默认为1) 页面 73 success: function (res) { 74 wx.showToast({ 75 title: ‘支付成功‘, 76 icon: ‘success‘, 77 duration: 2000 78 }) 79 }, 80 fail: function () { 81 // fail 82 83 }, 84 complete: function () { 85 // complete 86 } 87 }) 88 }, 89 fail: function (res) { 90 // fail 91 }, 92 complete: function () { 93 // complete 94 } 95 }) 96 } 97 })
时间: 2024-10-12 12:48:35