PHP 微信支付回调 原始参数处理

微信官方支付回调文档:

https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3

回调参数为xml格式。

<xml><appid><![CDATA[wx3c10c1xxxxxxxx]]></appid>
<bank_type><![CDATA[OTHERS]]></bank_type>
<cash_fee><![CDATA[1]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[N]]></is_subscribe>
<mch_id><![CDATA[1613681045]]></mch_id>
<nonce_str><![CDATA[6151235fa93fc]]></nonce_str>
<openid><![CDATA[oajZ_5I49bcCLj1jkkO6rm4GB8-4]]></openid>
<out_trade_no><![CDATA[20210927095023713696]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[FE459532F0CC6FC1F3FAA1D11AD43B2C]]></sign>
<time_end><![CDATA[20210927095042]]></time_end>
<total_fee>1</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<transaction_id><![CDATA[4200001158202109278494646037]]></transaction_id>
</xml>

PHP 处理回调回来的xml数据 demo:

//获取返回的xml
$notify_xml = file_get_contents("php://input");

Seaslog::info('[debug] 微信支付回调参数 xml:' . $notify_xml);    // debug

//将xml转化为json格式
$json_xml = json_encode(simplexml_load_string($notify_xml, 'SimpleXMLElement', LIBXML_NOCDATA));

//转成数组
$post_data = json_decode($json_xml, true);

Seaslog::info('[debug] 微信支付回调参数:' . json_encode($post_data, JSON_UNESCAPED_UNICODE));    // debug

//var_dump($post_data['out_trade_no']);
$order_info = ActiveOrder::where('order_no', $post_data['out_trade_no'])->find();
//var_dump($order_info);exit();

获取到回调回来的订单号后 $post_data['out_trade_no'] 处理自己业务逻辑。

原文地址:https://www.cnblogs.com/starfish29/p/15341632.html