1. 程式人生 > >微信支付功能實現

微信支付功能實現

< view class= "container"> < input type= "text" bindinput= "getOrderCode" style= "border:1px solid #ccc;" / > < button bindtap= "pay">立即支付 </ button > </ view >
// pages/pay/pay.js var app = getApp(); Page({ data: {}, onLoad: function
(options) { // 頁面初始化 options為頁面跳轉所帶來的引數 }, /* 微信支付 */ wxpay: function () { var that = this //登陸獲取code wx.login({ success: function (res) { console.log(res.code) //獲取openid that.getOpenId(res.code) } }); }, getOpenId: function
(code) { var that = this; wx.request({ url: " https://api.weixin.qq.com/sns/jscode2session?appid=wxa142513e524e496c&secret=5d6a7d86048884e7c60f84f7aa85253c&js_code=" ; + code + "&grant_type=authorization_code", data: {}, method: 'GET', success: function
(res) { that.generateOrder(res.data.openid) }, fail: function () { // fail }, complete: function () { // complete } }) }, /**生成商戶訂單 */ generateOrder: function (openid) { var that = this //統一支付 wx.request({ url: ' http://localhost:8070/RMS/pay_pay.action' ;, method: 'GET', data: { total_fee: '5', body: '支付測試', attach: '真假酒水' }, success: function (res) { var pay = res.data //發起支付 var timeStamp = pay[ 0].timeStamp; var packages = pay[ 0].package; var paySign = pay[ 0].paySign; var nonceStr = pay[ 0].nonceStr; var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; that.pay(param) }, }) },
/* 支付 */ pay: function (param) { console.log( "支付") console.log(param) wx.requestPayment({ timeStamp: param.timeStamp, nonceStr: param.nonceStr, package: param.package, signType: param.signType, paySign: param.paySign, success: function (res) { // success wx.navigateBack({ delta: 1, // 回退前 delta(預設為1) 頁面 success: function (res) { wx.showToast({ title: '支付成功', icon: 'success', duration: 2000 }) }, fail: function () { // fail
}, complete: function () { // complete } }) }, fail: function (res) { // fail }, complete: function () { // complete } }) } })

1、小程式微信支付API使用的公眾號jssdk的微信支付API,官方文件上有一個引數寫錯了,就是傳的appid中的“I ”必須為大寫,不然一直提示引數錯誤。在這個上面除錯好久,希望看到的小夥伴注意下。