1. 程式人生 > >php 版本 微信支付 APP 服務端開發

php 版本 微信支付 APP 服務端開發

我們通過 微信支付的文件知道 第一步 服務端需要呼叫統一下單介面生成預付單,其中主要的引數就是 prepay_id 這樣 app 通過 prepay_id 就可以發起支付請求了。
我們可以參考 微信支付的 官方SDK(就是個坑)
統一下單介面就是 呼叫函式

WxPayApi::unifiedOrder($input);

其中的一個引數 設定為:

$input->SetTrade_type("APP");

沒有服務端demo 可以參考 JSAPI的修改上面的引數就可以了
統一下單介面定義在 WxPay.Api.php中其中有程式碼

$response = self
::postXmlCurl($xml, $url, false, $timeOut);

即向微信介面傳送所需要的資料 ,這裡面就會出現坑爹的地方。

記得以前版本的 sdk 單詞CURLOPT 少些個T
這次是需要把程式碼

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);

改為

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);

在 WxPay.Data.php中有函式 FromXml($xml)
其中有程式碼
libxml_disable_entity_loader(true);
這個函式可能引起很大的問題
他的初衷本來是為安全考慮,禁止引用外部xml 網上的一個bug 解釋是:

The function 'libxml_disable_entity_loader' is not thread safe so, if a thread configure it to 'true' all the others threads are true too. And it should be at 'false' by default when a new request come. That is why the problem disappears when we restart php-fpm daemon.
If you write this at the beginning of
app.php we can see that all is working again: <?php// app.php libxml_disable_entity_loader(false); The problem is caused by some third-party bundle or library, because it must be changing to true and is not restoring to false again.

一些PHP框架使用了這個函式 就會出現問題,我使用magento框架 測試微信支付,添加了這個函式 結果這個伺服器上的其它web測試環境都不能夠正常訪問了。應該就是破壞了整個伺服器的load xml 邏輯。
重啟伺服器後正常,所以我乾脆就註釋掉了這個函式。
再之後就是得到 prepay_id 後 依照客戶端需要的引數給相應引數
但是給客戶端的時候需要二次加密
就是在走一遍 加密流程
大致 函式是:

if($order['result_code'] == 'SUCCESS' && $order['return_code'] == 'SUCCESS'){
            $data['appid'] = $order['appid'];
            $data['noncestr'] = WxPayApi::getNonceStr();
            $data['package'] = 'Sign=WXPay';
            $data['partnerid'] = WxPayConfig::MCHID;
            $data['prepayid'] = $order['prepay_id'];
            $data['timestamp'] = time();
            $s = $this->MakeSign($data);
            $data['sign'] = $s;
            $resJson = json_encode($data);
            echo $resJson;

這樣客戶端就能夠正常像微信傳送支付請求了。
最後 就是 寫好 服務端的 notify函式
根據業務流程 和 notify demo 修改即可