[transfer] WeChat small program to achieve WeChat payment function (available)

 

Original Bo:

https://blog.csdn.net/fredrik/article/details/79697963

WeChat small program to achieve WeChat payment function

 

It’s OK to replace the inside parameters with yours directly, if you want to open the Wechat payment function, you need the merchant number, appid, appsecret, openid.

 

//Small program end code:

 

pay:function(){
var that=this
wx.getStorage({
key: ‘openid’,
success: function(res) {
wx.request({

//Here’s the background processing method. The URL is self-defined, and you can change it to your own background processing method. Wx_Pay is written below
//Background PHP is used for processing, Java can refer to the method, the reason is the same.

url: url + ‘Wx_Pay’,
data: {
//User openid
openid:res.data,
fee: that.data.totalPrice, //Amount of payment
details: that.data.goodsList[0].goods_name,//Name of commodity paid
},
success:function(result){
if(result.data){
//out_trade_no=res.data[‘out_trade_no’];
wx.requestPayment({
timeStamp: result.data[‘timeStamp’],
nonceStr: result.data[‘nonceStr’],
package: result.data[‘package’],
signType: ‘MD5’,
paySign: result.data[‘paySign’],
‘success’:function(successret){
console.log(‘Successful payment).
//Get information about paying users
wx.getStorage({
key: ‘userInfo’,
success: function (getuser) {
//Add order form to record
wx.request({
url: url + ‘Wx_AddOrder’,
data: {
uname: getuser.data.nickName,
goods: that.data.goodsList[0].goods_name,
price: that.data.totalPrice,
openid:res.data,
},
success: function (lastreturn) {
console.log(“Access success “.
}
})
},
})
},’fail’:function(res){
}
})
}
}
})
},
})
},

//Backstage

 

//WeChat paymentPublic function Wx_Pay () {$request=Request:: instance ();$fee=$request-> PAram ('fee');$details=$request-> param ('details'); / / commodity details, such as iPhone8, purple/ / $fee = 0.01; / / example recharge 0.01$appid ='appid'; //appid$body = $details; / / 'GEIL Hui Mall'; / / '[fill it out yourself]$mch_id ='1486742092'; / /] your business number [fill it out].$nonce_str = $this-> nonce_str (); / / random string$notify_url ='https://zys.jinbh.cn/admin/Api/Wx_Speech'; / / callback URL [fill it out];$openid = $request-> param ('openid'); / /] user's openid [oneself fill].$out_trade_no = $thIs-> order_number ($openid); / / merchant order number$spbill_create_ip ='123.206.45.131'; / /' IP of the server [self fill]];$total_fee = $fee * 100; / / / Because the minimum charge is 1 and the unit is 1 yuan, you need * 100 here$trade_type ='JSAPI'/ / transaction type default/ / here is in order, because the following signatures are sorted in order.$post['appid'] = $appid;$post['body'] = $body;$post['mch_id'] = $mch_id;$post['nonce_str'] = $noNce_str; / / random string$post['notify_url'] = $notify_url;$post['openid'] = $Openid;$post['out_trade_no'] = $out_trade_no;$post['spbill_create_ip] = $spbill_create_ip; / / terminal IP$post['total_fee'] = $total_fee; / / the total amount is the lowest, which must be an integer.$post['trade_type'] = $trade_type;$sign = $this-> sign ($post); / / signature$poSt_xml ='< xml>< appid>'.$appid.'< /appid>< body>'.$bodY.'< /body>< mch_id>'.$mch_id.'< /mch_id>< nonce_str> '. $.Nonce_str.'< /nonce_str>< notify_url>'.$notify_url.'< /notify_url>< openid>'.$openid.'< /openid>< out_trade_no>'.$out_trade_no.'< /oUt_trade_no>< spbill_create_ip>'.$spbill_create_ip.'< /spbill_create_ip>< total_fee>'.$total_fee.'< /total_fee>< trade_type>'.$trade_typE.'< /trade_type>< sign>'.$sign.'< /sign>< /xml> ';/ / unified interface prepay_id$url ='https://api.mch.weixin.qq.com/pay/unifiedorder';$xml = $thIs-> http_request ($url, $post_xml);$array = $this-> XML ($xml); / / all must be capitalized.If ($array['RETURN_CODE'] = ='SUCCESS'& & $array['RESULT_CODE'] = ='SUCCESS') {$time = time ();$tmp=''; / / temporary array for signature$tmp['appId'] = $appid;$tmp['nonceStr'] =$nonce_str;$tmp['package'] ='prepay_id='.$array['PREPAY_ID'];$tmp['signTYpe'] ='MD5';$tmp['timeStamp'] = "$time";$data['state'] = 1;$data['timeStamp'] = "$time"; / / timestamp$data['nonceStr'] = $nonce_str; / / random string$Data['signType'] ='MD5'; / / signature algorithm, temporarily support MD5$data['package'] ='prepay_id='.$array['PREPAY_ID'] / / unified prepay_id parameter value returned by the next interface, submit format such as: prepay_id=*$data['paySign'] = $this-> sign ($tmp);/ / signature, the specific signature scheme is referred to the WeChat public number to pay the help document;$data['out_trade_no'] = $out_trade_no;}else{$data['state'] = 0;$data['text'] = "error";$data['RETURN_CODE'] = $array['RETURN_CODE'];$data['RETURN_MSG'] = $array['RETURN_MSG'];}Echo json_eNcode ($data);}/ / random 32 bit stringPrivate function nonce_str () {$result = '';$stR ='QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';For ($i=0; $i< 32; $i++) {$result. = $str[rand (0,48)];}Return $result;}/ / generate order numberPrivate funcTion order_number ($openid) {//date ('Ymd', time ()).Time ().Rand (10,99); //18 bit.Return MD5($openid.time ().Rand (10,99)); //32 bit.}/ / signature $data should be arranged in good order.Public function sign ($data){$stringA = '';Foreach ($data as $key => $value) {If (($value) conti)Nue;If ($stringA) $stringA. ='&; $key. "=". $value;Else $stringA= $key. "=". $value;}$wx_key ='Zhangyusheng19810318015729366660'; / / after application payment, a merchant is granted.Account and password, set up key after landing.StringSignTemp = $stringA.Set up keyReturn strtoupper (MD5 ($stringSignTemp));}//curl request.Function http_requesT ($url, $data = null, $headers = array ()){$curl = curl_init ();If(count ($headers) > = 1) {Curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers);}Curl_setopt ($curl, CURLOPT_URL, $url);Curl_setopt ($curl, CURLOPT_S)SL_VERIFYPEER, FALSE);Curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, FALSE);IF (empty ($data)) {Curl_setopt ($curl, CURLOPT_POST, 1);Curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}Curl_setopt ($curl, CURLOPT_RETURNTRANS)FER, 1);$output = curl_exec ($curl);Curl_close ($curl);Return $Output;}/ / get XMLPrivate function XML ($xml) {$p = xml_parser_create ();Xml_parse_into_struct ($p, $xml, $vals, $index);Xml_parser_free ($p);$data = "";Foreach ($index as $key=> $value) {If ($key = ='xml')$key (='XML') continue;$tag = $vals[$value[0]]['tag'];$value= $vals[$value[0]]['value'];$data[$tag] = $value;}Return$data;}/ / WeChat payment ended

Leave a Reply

Your email address will not be published. Required fields are marked *