1.申請開通小程序支付,我們正式開通的微信支付是在微信公眾平台上,我們需要綁定之前的微信商戶平台即可,這一點不過多強調
(1)、統一下單
大家看到微信的統一下單接口密密麻麻的一大堆參數,千萬不要害怕,有問題解決就是了。
<?php //微信支付向外暴露的支付接口 public function pay() { $result = $this->weChatPay(); return $result; } //請求微信統一下單接口 private function weChatPay() { $param = array( 'appid' => $this->APPID,//小程序id 'mch_id'=> $this->MCHID,//商戶id 'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],//終端ip 'notify_url'=>$this->APPURL, //回調通知地址 'nonce_str'=> $this->createNoncestr(),//隨機字符串 'out_trade_no'=>$this->outTradeNo,//商戶訂單編號 'total_fee'=>floatval($this->totalFee), //總金額 'openid'=>$this->openid,//用戶openid 'trade_type'=>$this->TRADETYPE,//交易類型 'body' =>$this->BODY, //商品信息 ); //通過簽名算法計算得出的簽名值,詳見簽名生成算法 $param['sign'] = $this->getSign($param); //將數組內容轉為xml格式,向微信發出請求 $xmlData = $this->arrayToXml($param); $xml_result = $this->postXmlCurl($xmlData,'https://api.mch.weixin.qq.com/pay/unifiedorder',60); $result = $this->xmlToArray($xml_result); return $result; // var_dump($result); /* * array(9) { ["return_code"]=> string(7) "SUCCESS" ["return_msg"]=> string(2) "OK" ["appid"]=> string(18) "wx2d4fefcfe1a39c9b" ["mch_id"]=> string(10) "1420537902" ["nonce_str"]=> string(16) "JASf0yXVuPknKm2J" ["sign"]=> string(32) "BE3CF30459D01660BB9AB2DE0AD023CE" ["result_code"]=> string(7) "SUCCESS" ["prepay_id"]=> string(36) "wx29154208585459f31c6875691178935371" ["trade_type"]=> string(5) "JSAPI" } } /* * 對要發送到微信統一下單接口的數據進行簽名 */ protected function getSign($Obj){ foreach ($Obj as $k => $v){ $param[$k] = $v; } //簽名步驟一:按字典序排序參數 ksort($param); $String = $this->formatBizQueryParaMap($param, false); //簽名步驟二:在string后加入KEY $String = $String."&key=".$this->KEY; //簽名步驟三:MD5加密 $String = md5($String); //簽名步驟四:所有字符轉為大寫 $result_ = strtoupper($String); return $result_; } /* *排序並格式化參數方法,簽名時需要使用 */ protected function formatBizQueryParaMap($paraMap, $urlencode) { $buff = ""; ksort($paraMap); foreach ($paraMap as $k => $v) { if($urlencode) { $v = urlencode($v); } //$buff .= strtolower($k) . "=" . $v . "&"; $buff .= $k . "=" . $v . "&"; } $reqPar = ""; if (strlen($buff) > 0) { $reqPar = substr($buff, 0, strlen($buff)-1); } return $reqPar; } /* * 生成隨機字符串方法 */ protected function createNoncestr($length = 32 ){ $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str =""; for ( $i = 0; $i < $length; $i++ ) { $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } //數組轉字符串方法 protected function arrayToXml($arr){ $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; } //將xml字符串轉換為數組 protected static function xmlToArray($xml){ $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $array_data; } //發送xml請求方法 private static function postXmlCurl($xml, $url, $second = 30) { $ch = curl_init(); //設置超時 curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //嚴格校驗 //設置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求結果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //post提交方式 curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_TIMEOUT, 40); set_time_limit(0); //運行curl $data = curl_exec($ch); //返回結果 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); throw new WxPayException("curl出錯,錯誤碼:$error"); } }
(2)、處理微信的notify_url 通知地址
微信會把剛才生成的支付訂單返回來,給我們做校驗
異步接收微信支付結果通知的回調地址,通知url必須為外網可訪問的url,不能攜帶參數。
/* 微信官方提醒:
* 商戶系統對於支付結果通知的內容一定要做【簽名驗證】,
* 並校驗返回的【訂單金額是否與商戶側的訂單金額】一致,
* 防止數據泄漏導致出現“假通知”,造成資金損失。
*/
$post = post_data(); //接受POST數據XML個數 function post_data(){ $receipt = $_REQUEST; if($receipt==null){ $receipt = file_get_contents("php://input"); if($receipt == null){ $receipt = $GLOBALS['HTTP_RAW_POST_DATA']; } } return $receipt; } //調用后台驗證 WeiXinPay::payNotifyUrlCallback($post);
/* * 微信支付結果回調; * */ public static function PayCallBack($result){ //查找對應的功能模塊,返回信息內容 /* * <xml> <appid><![CDATA[wx24123c4370ec43b]]></appid> <attach><![CDATA[測試測試]]></attach> <bank_type><![CDATA[CFT]]></bank_type> <fee_type><![CDATA[CNY]]></fee_type> <is_subscribe><![CDATA[Y]]></is_subscribe> <mch_id><![CDATA[10000100]]></mch_id> <nonce_str><![CDATA[5d2b634f23F23da20af46e531c]]></nonce_str> <openid><![CDATA[oUpF8uMEb4q23FEWG4Q23R268TekukE]]></openid> <out_trade_no><![CDATA[140123153]]></out_trade_no> <result_code><![CDATA[SUCCESS]]></result_code> <sign><![CDATA[B552EDFW23G423G5DD0D78AB241]]></sign> <sub_mch_id><![CDATA[10000100]]></sub_mch_id> <time_end><![CDATA[20140903131540]]></time_end> <total_fee>1</total_fee> <coupon_fee><![CDATA[10]]></coupon_fee> <coupon_count><![CDATA[1]]></coupon_count> <coupon_type><![CDATA[CASH]]></coupon_type> <coupon_id><![CDATA[10000]]></coupon_id> <coupon_fee><![CDATA[100]]></coupon_fee> <trade_type><![CDATA[JSAPI]]></trade_type> <transaction_id><![CDATA[1004400740212312353532168]]></transaction_id> </xml> */ if($result == null){ return; } if($result['result_code'] =='SUCCESS'){ //支付成功
//開始取出數據庫訂單數據進行校驗
//校驗成功向微信發出請求成功
WeiXinPay::return_success(); }
}
/*
* 給微信發送確認訂單金額和簽名正確,SUCCESS信息 -xzz0521
*/
public static function return_success(){
$return['return_code'] = 'SUCCESS';
$return['return_msg'] = 'OK';
$xml_post = '<xml>
<return_code>'.$return['return_code'].'</return_code>
<return_msg>'.$return['return_msg'].'</return_msg>
</xml>';
echo $xml_post;exit;
}
使用本代碼,建議使用單個方法拼接,最終來組成邏輯代碼