Implementation of the applet payment function (front-end) and the applet payment function
Implementation of the mini-program payment function (front-end)
Only the code on the applet is provided:
Var app = getApp (); Page ({data :{}, onLoad: function (options) {// The parameter var that = this brought about by PAGE initialization options for page Jump // log on to get code wx. login ({success: function (res) {console. log (res. code) // obtain openid that. getOpenId (res. code) }}) ;}, getOpenId: function (code) {var that = this; wx. request ({url: "https://api.weixin.qq.com/sns/jscode2session? Appid = applet appid & secret = applet application key & js_code = "+ code +" & grant_type = authorization_code ", data :{}, method: 'get', success: function (res) {that. generateOrder (res. data. openid)}, fail: function () {// fail}, complete: function () {// complete})},/** generate Merchant Order */generateOrder: function (openid) {var that = this // unified payment wx. request ({url: 'background path', method: 'get', data: {gbid: 'item price', gname: 'item name', openId: openid (whether to pass the value of the product price and product name according to your own needs. openid is required)}, success: function (res) {var pay = res. data // initiate payment 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 */pay: function (param) {console. log ("Payment") 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, // The success: function (res) {wx. showToast ({title: 'payment successfully', icon: 'success ', duration: 2000})}, fail: function () {// fail}, complete: function () {// complete}, fail: function (res) {// fail}, complete: function () {// complete }})}})
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!