微信小程序支付,php后臺對接完整代碼,全是干貨呀,拿過來可以直接使用。小程序在調(diào)起微信支付之前需要5個(gè)參數(shù),這時(shí)候就需要攜帶code向后臺請求,然后后臺根據(jù)code獲取openid 再進(jìn)行服務(wù)器之間的。
一、準(zhǔn)備工作
1、小程序注冊,要以公司的以身份去注冊一個(gè)小程序,才有微信支付權(quán)限;
2、綁定商戶號。
3、在小程序填寫合法域
二、完成以上條件,你可以得到
小程序appid 小程序秘鑰 這兩個(gè)用于獲取用戶openid;
商戶號id ,商戶號秘鑰 支付接口必須的;
三、開始開發(fā)
前臺代碼
/* 調(diào)起微信支付 @param 支付價(jià)格,不填寫默認(rèn)為1分錢 */ function pay(total_fee) { var total_fee = total_fee; wx.login({ success: res => { //code 用于獲取openID的條件之一 var code = res.code; wx.request({ url: '后臺地址/index.php', method: "POST", data: { total_fee:total_fee, code: code, }, header: { 'content-type': 'application/x-www-form-urlencoded' // 默認(rèn)值 }, success: function (res) { //后端返回的數(shù)據(jù) var data = res.data; console.log(data); console.log(data["timeStamp"]); wx.requestPayment({ timeStamp: data['timeStamp'], nonceStr: data['nonceStr'], package: data['package'], signType: data['signType'], paySign: data['paySign'], success: function (res) { wx.showModal({ title: '支付成功', content: '', }) }, fail: function (res) { console.log(res); } }) } }); } }) }
以下是PHP后臺代碼 ,這里用的是tp框架
<?php namespace Home\Controller; use Think\Controller; class PayController extends Controller { /** * [callback 微信支付回調(diào)處理] * @Author zhengmingzhou * @DateTime 2018-05-22 * @return function [description] */ public function callback(){ vendor("Wechart.WxPay.Api"); vendor("Wechart.NativePay"); vendor("Wechart.WxPay.Data"); vendor("Wechart.WxPay.Notify"); //獲取微信返回支付信息 $xml = $GLOBALS['HTTP_RAW_POST_DATA']; $WxPayData = new \WxPayDataBase(); $result = $WxPayData->FromXml($xml); if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){ //回調(diào)邏輯處理。。 //返回給微信的參數(shù) $data['RETURN_CODE'] = 'SUCCESS'; $data['RETURN_MSG'] = 'OK'; }else{ $data['RETURN_CODE'] = 'FAIL'; $data['RETURN_MSG'] = 'NO'; } //返回給微信 $xml = self::arrtoxml($data); echo $xml; } /** * [arrtoxml 格式化返回給微信的數(shù)據(jù)格式] * @Author zhengmingzhou * @DateTime 2018-05-22 * @param [type] $arr [description] * @return [type] [description] */ private function arrtoxml( $arr ){ if(!$arr){ return ''; }else{ $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } } //微信支付 public function pay(){ //獲取openid if(I("post.code")) { //用code獲取openid $code=I("post.code"); $WX_APPID = '';//appid $WX_SECRET = '';//AppSecret $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $WX_APPID . "&secret=" . $WX_SECRET . "&js_code=" . $code . "&grant_type=authorization_code"; $infos = json_decode(file_get_contents($url)); $openid = $infos->openid; } if(I("post.total_fee")) { $total_fee=I("post.total_fee"); } else { $total_fee=0.01; } $fee = 0.01;//舉例充值0.01 $appid = '';//appid $body = '標(biāo)題'; $mch_id = ''; //商戶號 $nonce_str = $this->nonce_str();//隨機(jī)字符串 $notify_url = ''; //回調(diào)的url【自己填寫】 $openid = $openid; $out_trade_no = $this->order_number();//商戶訂單號 $spbill_create_ip = '';//服務(wù)器的ip【自己填寫】; $total_fee = $fee*100;//這里需要*100 $trade_type = 'JSAPI';//交易類型 默認(rèn) //這里是按照順序的 因?yàn)橄旅娴暮灻前凑枕樞?排序錯(cuò)誤 肯定出錯(cuò) $post['appid'] = $appid; $post['body'] = $body; $post['mch_id'] = $mch_id; $post['nonce_str'] = $nonce_str;//隨機(jī)字符串 $post['notify_url'] = $notify_url; $post['openid'] = $openid; $post['out_trade_no'] = $out_trade_no; $post['spbill_create_ip'] = $spbill_create_ip;//終端的ip $post['total_fee'] = $total_fee;//總金額 $post['trade_type'] = $trade_type; $sign = $this->sign($post);//簽名 $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> '; //print_r($post_xml);die; //統(tǒng)一接口prepay_id $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $xml = $this->http_request($url,$post_xml); $array = $this->xml($xml);//全要大寫 //print_r($array); if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){ $time = time(); $tmp='';//臨時(shí)數(shù)組用于簽名 $tmp['appId'] = $appid; $tmp['nonceStr'] = $nonce_str; $tmp['package'] = 'prepay_id='.$array['PREPAY_ID']; $tmp['signType'] = 'MD5'; $tmp['timeStamp'] = "$time"; $data['state'] = 200; $data['timeStamp'] = "$time";//時(shí)間戳 $data['nonceStr'] = $nonce_str;//隨機(jī)字符串 $data['signType'] = 'MD5';//簽名算法,暫支持 MD5 $data['package'] = 'prepay_id='.$array['PREPAY_ID'];//統(tǒng)一下單接口返回的 prepay_id 參數(shù)值,提交格式如:prepay_id=* $data['paySign'] = $this->sign($tmp);//簽名,具體簽名方案參見微信公眾號支付幫助文檔; $data['out_trade_no'] = $out_trade_no; }else{ $data['state'] = 0; $data['text'] = "錯(cuò)誤"; $data['RETURN_CODE'] = $array['RETURN_CODE']; $data['RETURN_MSG'] = $array['RETURN_MSG']; } echo json_encode($data); } //隨機(jī)32位字符串 private function nonce_str(){ $result = ''; $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz'; for ($i=0;$i<32;$i++){ $result .= $str[rand(0,48)]; } return $result; } //生成訂單號 private function order_number($openid){ //date('Ymd',time()).time().rand(10,99);//18位 return md5($openid.time().rand(10,99));//32位 } //簽名 $data要先排好順序 private function sign($data){ $stringA = ''; foreach ($data as $key=>$value){ if(!$value) continue; if($stringA) $stringA .= '&'.$key."=".$value; else $stringA = $key."=".$value; } $wx_key = '';//申請支付后有給予一個(gè)商戶賬號和密碼,登陸后自己設(shè)置的key $stringSignTemp = $stringA.'&key='.$wx_key; return strtoupper(md5($stringSignTemp)); } //curl請求 public 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_SSL_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_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } //獲取xml private 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; } }
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com