您的位置:首页 > 移动开发 > 微信开发

php 版本 微信支付 APP 服务端开发

2016-01-01 22:03 696 查看
我们通过 微信支付的文档知道 第一步 服务端需要调用统一下单接口生成预付单,其中主要的参数就是 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 修改即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: