Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize WeChat Mini Programs's payment function

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to realize the payment function of WeChat Mini Programs. The content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to realize the payment function of WeChat Mini Programs. Let's take a look.

The development documents of WeChat Mini Programs's payment function are as follows:

The mini-program side retains most of the console version

/ * WeChat Pay * / goWxPay: function () {var that = this; / / Log in to obtain code wx.login ({success: function (res) {console.log ("get login code", res.code); / / get openid that.getOpenId (res.code);}}), / * get openId * / getOpenId: function (code) {var that = this Wx.request ({url: "https://****?code=" + code, / / request address on server side. Domain name has been added to Mini Program request whitelist method: 'GET', success: function (res) {console.log (" get openid ", res); that.unitedPayRequest (res.data.openid) }, fail: function () {console.log ("failed to acquire openid", res);}, complete: function () {console.log ("acquisition of openid completed", res);}});}, / / getOpenId () / * Unified payment Interface * / unitedPayRequest: function (openid) {var that=this; / / Unified payment signature var appid ='' / / appid is required: var body =''; / / Commodity name is required: var mch_id =''; / / var nonce_str = util.randomString () is required for merchant number; / / Random string, no longer than 32 bits. Var notify_url =''; / / required for notification address: var total_fee = parseInt (0.01x100); / / Price, which is one cent var trade_type = "JSAPI"; var key =''; / / required for merchant key. Get var out_trade_no =''at merchant backend. / / Custom order number var unifiedPayment = 'appid=' + appid +' & body=' + body +'& mch_id=' + mch_id +'& nonce_str=' + nonce_str +'& notify_url=' + notify_url +'& openid=' + openid +'& out_trade_no=' + out_trade_no +'& total_fee=' + total_fee + '™_ type=' + trade_type +' & key=' + key; console.log ("unifiedPayment", unifiedPayment) Var sign = md5.md5 (unifiedPayment). ToUpperCase (); console.log ("signature md5", sign); / / encapsulated unified payment xml parameter var formData = ""; formData + = "" + appid + ""; formData + = "" + body + ""; formData + = "" + mch_id + ""; formData + = "" + nonce_str + ""; formData + = "" + notify_url + ""; formData + = "+ openid +" FormData + = "" + that.data.ordernum + ""; formData + = "" + total_fee + ""; formData + = "" + trade_type + ""; formData + = "" + sign + ""; formData + = ""; console.log ("formData", formData) / / Unified pay wx.request ({url: 'https://api.mch.weixin.qq.com/pay/unifiedorder', / / Don't forget to add the api.mch.weixin.qq.com domain name to the Mini Program request whitelist At present, you can add method: 'POST', head:' application/x-www-form-urlencoded', data: formData, / / set the requested header success: function (res) {console.log ("return to the merchant", res.data) Var result_code = util.getXMLNodeValue ('result_code', res.data.toString ("utf-8")); var resultCode = result_code.split (' [') [2] .split (']') [0]; if (resultCode = = 'FAIL') {var err_code_des = util.getXMLNodeValue (' err_code_des', res.data.toString ("utf-8")) Var errDes = err_code_des.split ('[') [2] .split (']') [0] Wx.showToast ({title: errDes, icon: 'none', duration: 3000})} else {/ / initiate payment var prepay_id = util.getXMLNodeValue (' prepay_id', res.data.toString ("utf-8")); var tmp = prepay_id.split ('[') Var tmp1 = tmp [2] .split (']'); / / signature var key =''; / / required for merchant key, get var appId ='at merchant backend; / / appid required var timeStamp = util.createTimeStamp (); var nonceStr = util.randomString () Var stringSignTemp = "appId=" + appId + "& nonceStr=" + nonceStr + "& package=prepay_id=" + tmp1 [0] + "& signType=MD5&timeStamp=" + timeStamp + "& key=" + key; console.log ("signature string", stringSignTemp); var sign = md5.md5 (stringSignTemp). ToUpperCase (); console.log ("signature", sign) Var param = {"timeStamp": timeStamp, "package": 'prepay_id=' + tmp1 [0], "paySign": sign, "signType": "MD5", "nonceStr": nonceStr} console.log ("param Mini Program payment Interface parameters", param); that.processPay (param) }},})}, / / unitedPayRequest () / * Mini Program pay * / processPay: 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 ("wx.requestPayment returns information", res) Wx.showModal ({title: 'payment succeeded', content: 'you will receive payment voucher in the official account of WeChat Pay', showCancel: false, success: function (res) {if (res.confirm) {} else if (res.cancel) {})} Fail: function () {console.log ("payment failed") }, complete: function () {console.log ("payment completed (success or failure is completed");}})} / / processPay ()

Several methods to be used, in addition to the code MD5 uses to find from Github, are as follows:

/ * timestamp generation function * / function createTimeStamp () {return parseInt (new Date (). GetTime () / 1000) +'} / * random number * / function randomString () {var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; / / the easily confused characters oOLl,9gq,Vv,Uu,I1 var maxPos = chars.length; var pwd =''; for (var I = 0; I) are removed by default

< 32; i++) { pwd += chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd;}/* 获取XML节点信息 */function getXMLNodeValue(node_name, xml) { var tmp = xml.split("") var _tmp = tmp[1].split("") return _tmp[0]}module.exports = { createTimeStamp: createTimeStamp, randomString: randomString, getXMLNodeValue: getXMLNodeValue} 在服务端获取openid的PHP代码 //获取用户openidfunction getPortData($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $r = curl_exec($ch); //$r = json_decode($r); if($error=curl_error($ch)){ die($error); } curl_close($ch); return $r;} function getopenid(){ $code = $_GET["code"]; if(empty($code)) return array('status'=>

0jinfooted = > 'missing js_code'); $appid =''; / required $appsecret =''; / / required $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=authorization_code"; $result = getPortData ($url); / / var_dump ($result); echo $result;} getopenid () This is the end of the article on "how to realize the payment function of WeChat Mini Programs". Thank you for your reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve WeChat Mini Programs payment function". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report