您好,登錄后才能下訂單哦!
小編給大家分享一下微信支付統一下單用PHP的實現方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
H5支付時出現“簽名錯誤”怎么辦?注意調用微信“統一下單”接口后返回pre_pay_id后,需要用戶再次進行簽名返回給HTML; “支付目錄沒有授權”,進入微信商家管理后臺設置即可。
一、Model層,Wxpay.php
<?php namespace app\common\model; class Wxpay { private $appid = 'wxe8*****d4'; //微信公眾號appid private $secret = '37c4*******5f0'; //微信公眾號appsecret private $mchid = '13******02'; //商家號 private $key = '5363e******49e8'; //支付密鑰 private $sslcert_path = 'apiclient_cert.pem'; //證書所在絕對路徑 private $sslkey_path = 'apiclient_key.pem'; //證書所在絕對路徑 public function __construct($appid = '', $secret = '', $mchid = '', $key = '') { if(!empty($appid)) $this->appid = $appid; if(!empty($secret)) $this->secret = $secret; if(!empty($mchid)) $this->mchid = $mchid; if(!empty($key)) $this->key= $key; } /* * 微信內H5調起支付 * @params string $openid : 微信用戶openid * @params string $out_trade_no : 商家生成的訂單號(唯一性) * @params int $total_fee : 支付金額,單位分 * return array $ret : 返回支付時所需要的數據 * */ public function payForWeixin($openid,$out_trade_no,$total_fee,$attach='微信支付',$body='微信支付') { //支付數據 $data['openid'] = $openid; $data['out_trade_no'] = $out_trade_no; $data['total_fee'] = $total_fee*100; $data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"]; $data['attach'] = $attach; $data['body'] = $body; $data['appid'] = $this->appid; $data['mch_id'] = $this->mchid; $data['nonce_str'] = random(12); $data['trade_type'] = "JSAPI"; $data['notify_url'] = "http://****.com/home/wxpaynofiy/notify.html"; $sign = $this->getParam($data); $dataXML = "<xml> <appid>".$data['appid']."</appid> <attach>".$data['attach']."</attach> <body>".$data['body']."</body> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <notify_url>".$data['notify_url']."</notify_url> <openid>".$data['openid']."</openid> <out_trade_no>".$data['out_trade_no']."</out_trade_no> <spbill_create_ip>".$data['spbill_create_ip']."</spbill_create_ip> <total_fee>".$data['total_fee']."</total_fee> <trade_type>".$data['trade_type']."</trade_type> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $result = $this->https_post($url,$dataXML); $ret = $this->xmlToArray($result); if($ret['return_code'] == 'SUCCESS' && $ret['return_msg'] == 'OK') { return array( 'appId' => $this->appid, 'timeStamp' => time(), 'nonceStr' => $data['nonce_str'], 'package' => 'prepay_id='.$ret['prepay_id'], 'signType' => 'MD5', 'paySign' => $sign ); } else { $this->errorLog("微信支付失敗,",$ret); return null; } } /* * 微信二維碼支付 * @params string $openid : 用戶的openid * @params string $out_trade_no : 商戶訂單號 * @params number $total_fee : 訂單金額,單位分 * return string $code_url : 二維碼URL鏈接 */ public function payForQrcode($out_trade_no,$total_fee,$body="魔盒CMS",$attach="微信支付") { //支付數據 $data['out_trade_no'] = $out_trade_no; $data['total_fee'] = $total_fee*100; $data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"]; $data['attach'] = $attach; $data['body'] = $body; $data['appid'] = $this->appid; $data['mch_id'] = $this->mchid; $data['nonce_str'] = random(12); $data['trade_type'] = "NATIVE"; $data['notify_url'] = "http://****.com/home/wxpaynofiy/notify.html"; $sign = $this->getParam($data); $dataXML = "<xml> <appid>".$data['appid']."</appid> <attach>".$data['attach']."</attach> <body>".$data['body']."</body> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <notify_url>".$data['notify_url']."</notify_url> <out_trade_no>".$data['out_trade_no']."</out_trade_no> <spbill_create_ip>".$data['spbill_create_ip']."</spbill_create_ip> <total_fee>".$data['total_fee']."</total_fee> <trade_type>".$data['trade_type']."</trade_type> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $result = $this->https_post($url,$dataXML); $ret = $this->xmlToArray($result); if($ret['return_code'] == 'SUCCESS' && $ret['return_msg'] == 'OK') { return $ret['code_url']; } else { $this->errorLog("獲取微信支付二維碼失敗,",$ret); return null; } } /* * 訂單查詢 * @params string $transaction_id : 微信訂單號 * @params string $out_trade_no : 商家訂單號(與微信訂單號二選一) * */ public function findOrder($out_trade_no) { $data['appid'] = $this->appid; $data['mch_id'] = $this->mchid; $data['nonce_str'] = random(12); $data['out_trade_no'] = $out_trade_no; $sign = $this->getParam($data); $dataXML = "<xml> <appid>".$data['appid']."</appid> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <out_trade_no>".$data['out_trade_no']."</out_trade_no> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/pay/orderquery'; $result = $this->https_post($url,$dataXML); $ret = $this->xmlToArray($result); if($ret['return_code'] == 'SUCCESS' && $ret['return_msg'] == 'OK') { return $ret; } else { $this->errorLog("查詢微信支付訂單失敗,",$ret); return null; } } /* * 退款訂單查詢 * @params string $transaction_id : 微信訂單號 * @params string $out_trade_no : 商家訂單號(與微信訂單號二選一) * */ public function findRefundOrder($out_trade_no) { $data['appid'] = $this->appid; $data['mch_id'] = $this->mchid; $data['nonce_str'] = random(12); $data['out_trade_no'] = $out_trade_no; $sign = $this->getParam($data); $dataXML = "<xml> <appid>".$data['appid']."</appid> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <out_trade_no>".$data['out_trade_no']."</out_trade_no> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/pay/refundquery'; $result = $this->https_post($url,$dataXML); $ret = $this->xmlToArray($result); if($ret['return_code'] == 'SUCCESS' && $ret['return_msg'] == 'OK') { return $ret; } else { $this->errorLog("查詢微信支付退款訂單失敗,",$ret); return $ret['err_code_des']; } } /* * 申請退款 * @params string $out_trade_no : 商戶訂單號 * @params string $out_refund_no : 商戶退款單號 * @params int $total_fee : 訂單金額 * @params int $refund_fee : 退款金額 * @params string $refund_desc : 退款原因 * */ public function refund($out_trade_no,$out_refund_no,$total_fee,$refund_fee,$refund_desc='退款') { $data['appid'] = $this->appid; $data['mch_id'] = $this->mchid; $data['nonce_str'] = random(12); $data['out_trade_no'] = $out_trade_no; $data['out_refund_no'] = $out_refund_no; $data['total_fee'] = $total_fee*100; $data['refund_fee'] = $refund_fee*100; $data['refund_desc'] = $refund_desc; $data['notify_url'] = "http://*****.com/home/wxpaynofiy/refund.html"; $sign = $this->getParam($data); $dataXML = "<xml> <appid>".$data['appid']."</appid> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <out_trade_no>".$data['out_trade_no']."</out_trade_no> <out_refund_no>".$data['out_refund_no']."</out_refund_no> <total_fee>".$data['total_fee']."</total_fee> <refund_fee>".$data['refund_fee']."</refund_fee> <refund_desc>".$data['refund_desc']."</refund_desc> <notify_url>".$data['notify_url']."</notify_url> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; $result = $this->https_post($url,$dataXML,true); $ret = $this->xmlToArray($result); if($ret['return_code'] == 'SUCCESS' && $ret['return_msg'] == 'OK') { return $ret; } else { $this->errorLog("微信退款失敗,",$ret); return null; } } /* * 企業付款至用戶零錢 * @params string $openid : 用戶openid * @params int $total_fee : 付款金額,單位分 * @params string $out_trade_no : 商家訂單號 * @params string $username : 微信用戶名稱(注意微信昵稱若為空時支付會出錯) * @params string $desc : 付款描述 * @params string $check_name : 是否檢測用戶名 * */ public function payForUser($openid,$total_fee,$out_trade_no,$username='魔盒CMS',$desc='魔盒CMS付款給用戶',$check_name='NO_CHECK') { $data['amount'] = $total_fee*100; $data['check_name'] = $check_name; $data['desc'] = $desc; $data['mch_appid'] = $this->appid; $data['mchid'] = $this->mchid; $data['nonce_str'] = random(12); $data['openid'] = $openid; $data['partner_trade_no'] = $out_trade_no; $data['re_user_name'] = $username; $data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"]; $sign = $this->getParam($data); $dataXML="<xml> <mch_appid>".$data['mch_appid']."</mch_appid> <mchid>".$data['mchid']."</mchid> <nonce_str>".$data['nonce_str']."</nonce_str> <partner_trade_no>".$data['partner_trade_no']."</partner_trade_no> <openid>".$data['openid']."</openid> <check_name>".$data['check_name']."</check_name> <re_user_name>".$data['re_user_name']."</re_user_name> <amount>".$data['amount']."</amount> <desc>".$data['desc']."</desc> <spbill_create_ip>".$data['spbill_create_ip']."</spbill_create_ip> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; $result = $this->https_post($url,$dataXML,true); $ret = $this->xmlToArray($result); if($ret['return_code']=='SUCCESS' && $ret['result_code'] == 'SUCCESS') { //支付成功返回商戶訂單號、微信訂單號、微信支付成功時間 $result['partner_trade_no'] = $ret['partner_trade_no']; $result['payment_no'] = $ret['payment_no']; $result['payment_time'] = $ret['payment_time']; return $ret; } else { $this->errorLog('付款給用戶失敗',$ret); return null; } } /* * 普通紅包 * @params string $out_trade_no : 商家訂單號 * @params string $openid : 接收紅包用戶的openid * @params int $total_fee : 紅包金額,單位分 * @params int $total_num : 紅包發放總人數 * @params string $wishing : 紅包祝福語 * @params string $act_name : 活動名稱 * @params string $remark : 備注 * @params string $scene_id :場景值ID。發放紅包使用場景,紅包金額大于200或者小于1元時必傳。PRODUCT_1:商品促銷、PRODUCT_2:抽獎、PRODUCT_3:虛擬物品兌獎 、PRODUCT_4:企業內部福利、PRODUCT_5:渠道分潤、PRODUCT_6:保險回饋、PRODUCT_7:彩票派獎、PRODUCT_8:稅務刮獎 * */ public function redPack($openid,$total_fee,$out_trade_no,$total_num = 1,$wishing = '感謝您光臨***平臺進行購物',$act_name='***購物發紅包',$remark = '購物領紅包') { $data['mch_billno'] = $out_trade_no; $data['mch_id'] = $this->mchid; $data['wxappid'] = $this->appid; $data['send_name'] = '發送紅包者的名稱'; $data['re_openid'] = $openid; $data['total_amount'] = $total_fee; $data['total_num'] = $total_num; $data['wishing'] = $wishing; $data['client_ip'] = $_SERVER["REMOTE_ADDR"]; $data['act_name'] = $act_name; $data['remark'] = $remark; $data['nonce_str'] = random(12); $sign = $this->getParam($data); $dataXML="<xml> <sign>".$sign."</sign> <mch_billno>".$data['mch_billno']."</mch_billno> <mch_id>".$data['mch_id']."</mch_id> <wxappid>".$data['wxappid']."</wxappid> <send_name>".$data['send_name']."</send_name> <re_openid>".$data['re_openid']."</re_openid> <total_amount>".$data['total_amount']."</total_amount> <total_num>".$data['total_num']."</total_num> <wishing>".$data['wishing']."</wishing> <client_ip>".$data['client_ip']."</client_ip> <act_name>".$data['act_name']."</act_name> <remark>".$data['remark']."</remark> <nonce_str>".$data['nonce_str']."</nonce_str> </xml>"; $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; $result = $this->https_post($url,$dataXML,true); $ret = $this->xmlToArray($result); if($ret['return_code']=='SUCCESS' && $ret['result_code'] == 'SUCCESS') { return $ret; } else { $this->errorLog('發放普通紅包失敗',$ret); return $ret['err_code_des']; } } /* * 裂變紅包:一次可以發放一組紅包。首先領取的用戶為種子用戶,種子用戶領取一組紅包當中的一個,并可以通過社交分享將剩下的紅包給其他用戶。 * 裂變紅包充分利用了人際傳播的優勢。 * @params string $out_trade_no : 商家訂單號 * @params string $openid : 接收紅包用戶的openid * @params int $total_fee : 紅包金額,單位分 * @params int $total_num : 紅包發放總人數 * @params string $wishing : 紅包祝福語 * @params string $act_name : 活動名稱 * @params string $remark : 備注 * @params string $scene_id :場景值ID。發放紅包使用場景,紅包金額大于200或者小于1元時必傳。PRODUCT_1:商品促銷、PRODUCT_2:抽獎、PRODUCT_3:虛擬物品兌獎 、PRODUCT_4:企業內部福利、PRODUCT_5:渠道分潤、PRODUCT_6:保險回饋、PRODUCT_7:彩票派獎、PRODUCT_8:稅務刮獎 * */ public function redPackGroup($openid,$total_fee,$out_trade_no,$total_num,$wishing = '感謝您光臨***進行購物',$act_name='**購物發紅包',$remark = '購物領紅包') { $data['mch_billno'] = $out_trade_no; $data['mch_id'] = $this->mchid; $data['wxappid'] = $this->appid; $data['send_name'] = '發送紅包者的名稱'; $data['re_openid'] = $openid; $data['total_amount'] = $total_fee; $data['amt_type'] = 'ALL_RAND'; //ALL_RAND—全部隨機,商戶指定總金額和紅包發放總人數,由微信支付隨機計算出各紅包金額 $data['total_num'] = $total_num; $data['wishing'] = $wishing; $data['client_ip'] = $_SERVER["REMOTE_ADDR"]; $data['act_name'] = $act_name; $data['remark'] = $remark; $data['nonce_str'] = random(12); $sign = $this->getParam($data); $dataXML="<xml> <sign>".$sign."</sign> <mch_billno>".$data['mch_billno']."</mch_billno> <mch_id>".$data['mch_id']."</mch_id> <wxappid>".$data['wxappid']."</wxappid> <send_name>".$data['send_name']."</send_name> <re_openid>".$data['re_openid']."</re_openid> <total_amount>".$data['total_amount']."</total_amount> <amt_type>".$data['amt_type']."</amt_type> <total_num>".$data['total_num']."</total_num> <wishing>".$data['wishing']."</wishing> <client_ip>".$data['client_ip']."</client_ip> <act_name>".$data['act_name']."</act_name> <remark>".$data['remark']."</remark> <nonce_str>".$data['nonce_str']."</nonce_str> </xml>"; $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack'; $result = $this->https_post($url,$dataXML,true); $ret = $this->xmlToArray($result); if($ret['return_code']=='SUCCESS' && $ret['result_code'] == 'SUCCESS') { return $ret; } else { $this->errorLog('發放裂變紅包失敗',$ret); return $ret['err_code_des']; } } /* * 查詢紅包記錄 * @params string $out_trade_no : 商家訂單號 * */ public function findRedPack($out_trade_no) { $data['mch_billno'] = $out_trade_no; $data['mch_id'] = $this->mchid; $data['appid'] = $this->appid; $data['bill_type'] = 'MCHT'; //MCHT:通過商戶訂單號獲取紅包信息。 $data['nonce_str'] = random(12); $sign = $this->getParam($data); $dataXML="<xml> <sign>".$sign."</sign> <mch_billno>".$data['mch_billno']."</mch_billno> <mch_id>".$data['mch_id']."</mch_id> <appid>".$data['appid']."</appid> <bill_type>".$data['bill_type']."</bill_type> <nonce_str>".$data['nonce_str']."</nonce_str> </xml>"; $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo'; $result = $this->https_post($url,$dataXML,true); $ret = $this->xmlToArray($result); if($ret['return_code']=='SUCCESS' && $ret['result_code'] == 'SUCCESS') { return $ret; } else { $this->errorLog('查詢紅包記錄失敗',$ret); return $ret['err_code_des']; } } /* * 獲取用戶微信的OPENID * */ public function openid($c=false) { if($_GET['state']!="zgm"){ $t = $c ? "snsapi_userinfo" : "snsapi_base"; $url=urlencode(get_url()); $url="https://open.weixin.qq.com/connect/oauth3/authorize?appid=".$this->appid."&redirect_uri=".$url."&response_type=code&scope=".$t."&state=zgm#wechat_redirect"; echo "<html><script>window.location.href='$url';</script></html>"; exit; } if($_GET['code']){ $url="https://api.weixin.qq.com/sns/oauth3/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$_GET['code']."&grant_type=authorization_code"; $wx_db=json_decode($this->https_get($url)); if($c){ $url_2="https://api.weixin.qq.com/sns/userinfo?access_token=".$wx_db->access_token."&openid=".$wx_db->openid."&lang=zh_CN"; $db=json_decode($this->https_get($url_2)); return $db; }else{ return $wx_db->openid; } } } /* * 發起網絡GET請求 * @params string $url : URL鏈接 */ private function https_get($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_HEADER, FALSE) ; curl_setopt($curl, CURLOPT_TIMEOUT,60); if (curl_errno($curl)) { return 'Errno'.curl_error($curl); } else{$result=curl_exec($curl);} curl_close($curl); return $result; } //對參數排序,生成MD5加密簽名 private function getParam($paramArray, $isencode=false) { $paramStr = ''; ksort($paramArray); $i = 0; foreach ($paramArray as $key => $value) { if ($key == 'Signature'){ continue; } if ($i == 0){ $paramStr .= ''; }else{ $paramStr .= '&'; } $paramStr .= $key . '=' . ($isencode?urlencode($value):$value); ++$i; } $stringSignTemp=$paramStr."&key=".$this->key; $sign=strtoupper(md5($stringSignTemp)); return $sign; } //POST提交數據 private function https_post($url,$data,$ssl = false) { $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE ); if($ssl) { curl_setopt ( $ch,CURLOPT_SSLCERT,$this->sslcert_path); curl_setopt ( $ch,CURLOPT_SSLKEY,$this->sslkey_path); } curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 ); curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); $result = curl_exec($ch); if (curl_errno($ch)) { return 'Errno: '.curl_error($ch); } curl_close($ch); return $result; } /* * XML轉array * @params xml $xml : xml 數據 * return array $data : 轉義后的array數組 */ private function xmlToArray($xml) { libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring),true); return $val; } /* * 記錄日志 * @params string $msg : 提示語句 * @params array $ret : 錯誤結果 */ private function errorLog($msg,$ret) { file_put_contents(ROOT_PATH . 'runtime/error/wxpay.log', "[" . date('Y-m-d H:i:s') . "] ".$msg."," .json_encode($ret).PHP_EOL, FILE_APPEND); } }
二、Controller層,Test.php
<?php namespace app\goods\controller; use app\common\model\Wxpay; class Test { /* * 調用微信支付 * 一、獲取微信用戶的openid; * 二、調用微信支付接口 * 三、生成H5中使用的簽名內容 */ public function wx() { $wxpay = new Wxpay(); $openid = $wxpay->openid(); $pay = $wxpay->payForWeixin($openid,date('YmdHis').rand(1,5),'0.1'); $paySign =strtoupper(MD5('appId='.$pay['appId'].'&nonceStr='.$pay['nonceStr'].'&package='.$pay['package'].'&signType=MD5&timeStamp='.$pay['timeStamp'].'&key=536*****9e8')); $h6 = array( 'appId' => $pay['appId'], 'timeStamp' => $pay['timeStamp'], 'nonceStr' => $pay['nonceStr'], 'package' => $pay['package'], 'signType' => $pay['signType'], 'paySign' => $paySign ); $this->assign('wxpay',$h6); return view(); } }
三、HTML中調用微信支付,wx.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>訂單示例</title> </head> <body> <script type="text/javascript"> function onBridgeReady(){ WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId":"{$wxpay['appId']}", "timeStamp":"{$wxpay['timeStamp']}", "nonceStr":"{$wxpay['nonceStr']}", "package":"{$wxpay['package']}", "signType":"MD5", "paySign":"{$wxpay['paySign']}" }, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ){ // 使用以上方式判斷前端返回,微信團隊鄭重提示: //res.err_msg將在用戶支付成功后返回ok,但并不保證它絕對可靠。 } }); } if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } }else{ onBridgeReady(); } </script> </body> </html>
四、接收微信支付返回結果,支付時設置的notify_url值,確保此鏈接外網能訪問。wxpaynofiy.php
<?php namespace app\home\controller; class Wxpaynofiy { public function notify() { $xml = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input"); $data = xmlToArray($xml); if($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS'){ if($this->checkSign($data)) { $transaction_id = $data['transaction_id']; //微信支付訂單號 $out_trade_no = $data['out_trade_no']; //商家訂單號 $this->errorLog('微信支付返回結果,微信支付訂單號:'.$transaction_id.',商家訂單號:'.$out_trade_no,[]); } else { $this->errorLog('微信支付返回結果簽名驗證失敗',$data); } } else { $this->errorLog('微信支付返回結果',$data); } echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'; } /* * 驗證簽名 * @params array $result : 微信支付成功返回的結果數組 * return bool $ret : 成功true,失敗false * */ private function checkSign(array $data) { $str = ''; ksort($data); foreach ($data as $k => $v) { if($k != 'sign') $str .= $k.'='.$v.'&'; } $temp = $str . 'key=5363******49e8'; //key:商戶支付密鑰 $sign = strtoupper(md5($temp)); return $sign == $data['sign'] ? true : false; } private function errorLog($msg,$ret) { file_put_contents(ROOT_PATH . 'runtime/error/wxpaynofiy.log', "[" . date('Y-m-d H:i:s') . "] ".$msg."," .json_encode($ret).PHP_EOL, FILE_APPEND); } }
以上是“微信支付統一下單用PHP的實現方法”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。