微信小程序 支付功能(前端)的实现
程序员文章站
2023-11-22 09:38:04
微信小程序 支付功能(前端)的实现
只提供微信小程序端代码:
var app = getapp();
page({
data: {},
o...
微信小程序 支付功能(前端)的实现
只提供微信小程序端代码:
var app = getapp(); page({ data: {}, onload: function (options) { // 页面初始化 options为页面跳转所带来的参数 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=小程序appid&secret=小程序应用密钥&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: '后台路径', method: 'get', data: { gfee: '商品价钱', gname: '商品名称', openid:openid (商品价钱和商品名称根据自身需要是否传值,openid为必传) }, 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 } }) } })
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!