This article introduces the small program: the payment skip process first takes a look at the small program development steps
Okay, go to the code (the code is very primitive and not encapsulated)
/* Pay */wxpay: function () {var that = this // log on to get code wx. login ({success: function (res) {console. log (res. code) // Obtain openid that. getOpenId (res. code) }});},/* get openId */getOpenId: function (code) {var that = this wx. request ({url :" https://api.weixin.qq.com/sns/jscode2session?appid=wxbd5a8270399d41d9&secret=d8aac26a5a9c16266d1a23851ebb7d9b&js_code= "+ Code +" & grant_type = authorization_code ", method: 'GET', success: function (res) {// unified payment signature var appid = ''; // appid var body = ''; // merchant name var mch_id =''; // merchant ID var nonce_str = that. randomString; // random string, not longer than 32 characters. Var yy_url = ''; // notification address var spbill_create_ip =''; // ip // var total_region = parseInt (that. data. wxPayMoney) * 100; var total_amount = 100; var trade_type = "JSAPI"; var key = ''; var uniiedpayment = 'appid = '+ appid +' & body = '+ body +' & mch_id = '+ mch_id +' & nonce_str = '+ nonce_str + 'verify ify _ url = '+ yy_url +' & openid = '+ res. data. openid + '& out_trade_no =' + that. data. paySn + '& spbill_create_ip =' + spbill_create_ip + '& total_bandwidth =' + total_bandwidth + '& trade_type =' + trade_type + '& key =' + key var sign = MD5.MD5 (unifiedPayment). toUpperCase () console. log (sign) // encapsulate the unified payment xml parameter var formData = "" formData + = "" + appid + "" formData + = "" + body + "" formData + = "" + mch_id + "" formData + = "" + nonce_str + "" formData + = "" + policy_url + "" formData + = "" + res. data. openid + "" formData + = "" + that. data. paySn + "" formData + = "" + spbill_create_ip + "" formData + = "" + total_fee + "" formData + = "[tr]" + trade_type + "" formData + = "" + sign + "" formData + = "" // unified payment for wx. request ({url :' https://api.mch.weixin.qq.com/pay/unifiedorder ', Method: 'post', head: 'application/x-www-form-urlencoded', data: formData, // Set the request header success: function (res) {console. log (res. data) var result_code = that. getXMLNodeValue ('result _ code', res. data. toString ("UTF-8") var resultCode = result_code.split ('[') [2]. split (']') [0] if (resultCode = 'fail ') {var err_code_des = that. getXMLNodeValue ('err _ code_des ', res. data. toString ("UTF-8") var errDes = err_code_des.split ('[') [2]. split (']') [0] wx. navigateBack ({delta: 1, // The success: function (res) {wx. showToast ({title: errDes, icon: 'success', duration: 2000}) },} else {// initiate a payment var prepay_id = that. getXMLNodeValue ('prepay _ id', res. data. toString ("UTF-8") var tmp = prepay_id.split ('[') var tmp1 = tmp [2]. split (']') // sign var key = ''; var appId =''; var timeStamp = that. createTimeStamp (); var nonceStr = that. randomString (); var stringSignTemp = "appId = & nonceStr =" + nonceStr + "& package = prepay_id =" + tmp1 [0] + "& signType = MD5 & timeStamp =" + timeStamp + "& key = "var sign = MD5.MD5 (stringSignTemp ). toUpperCase () console. log (sign) var param = {"timeStamp": timeStamp, "package": 'prepay _ id = '+ tmp1 [0], "paySign": sign, "signType ": "MD5", "nonceStr": nonceStr} that. pay (param) }}})}, fail: function () {// fail}, complete: function () {// complete }})}, /* random number */randomString: function () {var chars = 'handler';/*** the easily obfuscated characters oOLl, 9gq, Vv, and Uu are removed by default, i1 *****/var maxPos = chars. length; var pwd = ''; for (var I = 0; I <32; I ++) {pwd + = chars. charAt (Math. floor (Math. random () * maxPos);} return pwd;},/* get prepay_id */getXMLNodeValue: function (node_name, xml) {var tmp = xml. split ("<" + node_name + ">") var _ tmp = tmp [1]. split ("") return _ tmp [0]},/* timestamp generation function */createTimeStamp: function () {return parseInt (new Date (). getTime ()/1000) + ''},/* pay */pay: function (param) {wx. requestPayment ({timeStamp: param. timeStamp, nonceStr: param. nonceStr, package: param. package, signType: param. signType, paySign: param. paySign, success: function (res) {// success console. log (res) 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 () {// fail console. log ("payment failed")}, complete: function () {// complete console. log ("pay complete ")}})}
For more small programs: Please pay attention to the PHP Chinese network for articles related to the payment skip process!