小程序整个支付流程接口(包含后台chuanshu乱码的处理)
1,获取:openid接口; ,2,完成整套支付流程接口 // 用code换取openId openIdUrl: `https://developers.weixin.qq.com/`, getOpenId: function (code) { var that = this; wx.request({ url: openIdUrl+"openid", data: { code: code }, method: 'GET', success: function (res) { console.log('返回openId') console.log(res.data) that.generateOrder(res.data.openid) }, fail: function () { // fail }, complete: function () { // complete } }) }, // 生成支付订单的接口 paymentUrl: openIdUrl+`payment`, /**生成商户订单 */ generateOrder: function (openid) { var that = this //统一支付 wx.request({ url: openIdUrl+'wxpay', method: 'GET', data: { openid: openid, total_fee: '5', // body: '支付测试', // attach: '真假酒水' body: 'zhifuceshi', attach: 'zhenjiajiushui' }, success: function (res) { console.log(res) var pay = res.data //发起支付 var timeStamp = pay[0].timeStamp; console.log("timeStamp:" + timeStamp) var packages = pay[0].package; console.log("package:" + packages) var paySign = pay[0].paySign; console.log("paySign:" + paySign) var nonceStr = pay[0].nonceStr; console.log("nonceStr:" + nonceStr) var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; that.pay(param) }, }) }, 注:中文乱码,tomcat 容器,配置 /* 支付 */ 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 console.log("支付") console.log(res) 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 console.log("支付失败") console.log(res) }, complete: function () { // complete console.log("pay complete") } }) } 成功跳转版 // pages/pay/pay.js var app = getApp(); var openid; 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:openIdUrl+ "openid", data: { code: code }, success: function (res) { console.log('返回openId') console.log(res.data) that.generateOrder(res.data) }, fail: function () { // fail }, complete: function () { // complete } }) }, /**生成商户订单 */ generateOrder: function (openid) { var that = this //统一支付 wx.request({ url: openIdUrl+'wxpay', method: 'GET', data: { openid: openid, total_fee: '1', body: '50套亲测小程序源码', attach: '行业源码' }, success: function (res) { console.log(res) var pay = res.data //发起支付 var timeStamp = pay[0].timeStamp; console.log("timeStamp:" + timeStamp) var packages = pay[0].package; console.log("package:" + packages) var paySign = pay[0].paySign; console.log("paySign:" + paySign) var nonceStr = pay[0].nonceStr; console.log("nonceStr:" + 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 //付款成功,这里可以写你的业务代码 console.log("支付") console.log(res) // 写入订单号,更新订单状态 // 下单成功,跳转到订单管理界面 wx.redirectTo({ url: "/pages/order-list/index" }); 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 console.log("支付失败") console.log(res) }, complete: function () { // complete console.log("pay complete") } }) } })