Problems integrating WeChat Pay with PHP

The official documentation is fairly brief, the demo tutorial basically has no parameter comments, so just read the code and figure it out.
Payment Scan-code payment native.php
Mode 2

$input->SetBody(“test”); //description

$input->SetAttach(“test”); //order id, attachment custom data, which will be returned as-is

$input->SetGoods_tag(“test”); //parameters of the voucher or instant discount feature

$input->SetNotify_url(‘url’); //specify the callback address

$input->SetProduct_id(‘test’); //merchant order number. If you use Mode 1 you have to go configure the callback address on the WeChat official platform; Mode 2 does not need configuration

Callback: the official reference given is notify.php, so just define your own callback address based on notify.php. In notify.php, locate the notify() method under WxPay.Api.php //get the notification data//get the notification data

$xml = $GLOBALS[‘HTTP_RAW_POST_DATA’];

But $GLOBALS[‘HTTP_RAW_POST_DATA’];

no matter what, it just couldn’t get the data — the same was true in a simulated environment. After checking the docs, $GLOBALS[‘HTTP_RAW_POST_DATA’] is actually the same as file_get_contents(‘php://input’). The official version is roughly from around 2014, while my server is PHP 7, so I decisively swapped it out and the data was retrieved normally. Once the callback can retrieve data, you need to verify the signature to ensure security. You can’t just write your own verification scheme — it’s needed right away.

You can find a clue in the official demo, which contains an already-written signature verification that you can call directly. The rule is still from the notify() method under WxPay.Api.php //if it returns success, then verify the signature//if it returns success, then verify the signature try { $result = WxPayResults::Init($xml); } catch (WxPayException $e){ $msg = $e->errorMessage(); return false; } As for how to include the files, I won’t explain that — just work through it yourself slowly. When I have time I’ll also try writing a signature verification scheme myself.