请先看微信支付退款通知文档【https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_11.shtml】
再看这个【https://github.com/lampnick/wxpay-refund-notify-decrypt-helper】
wxPaymentNotice VerifySign 微信支付结果通知
wxPayRefundNotice xmlToArray 微信支付退款结果通知
PHP代码
<?php
namespace App\Http\Controllers\WxPay;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use WechatPay\GuzzleMiddleware\Util\AesUtil;
class WxPayNotice extends Controller
{
// 微信支付结果通知处理
public function wxPaymentNotice(Request $request)
{
$this->validate($request, [
'id' => 'bail|required|string|min:1',
'create_time' => 'required|string|min:1',
'event_type' => 'required|string|min:1',
'resource_type' => 'required|string|min:1',
'resource.algorithm' => 'required|string|min:1',
'resource.ciphertext' => 'required|string|min:1',
'resource.associated_data' => 'sometimes|required|string',
'resource.original_type' => 'required|string|min:1',
'resource.nonce' => 'required|string|min:1',
'summary' => 'required|string|min:1'
]);
$contents = $request['resource'];
$signature = $request->header("Wechatpay-Signature");
$timestamp = $request->header("Wechatpay-Timestamp");
$serial = $request->header("Wechatpay-Serial");
$nonce = $request->header("Wechatpay-Nonce");
$params = file_get_contents('php://input');
$bf_msg = $timestamp . "\n" . $nonce . "\n" . $params . "\n";
$outpath = '日志(看需求).txt';
$handle = fopen($outpath, 'ab+');
if($serial!==config('globaldatas.wechat_no')) {
fwrite($handle, "签名证书序列号不一致" . PHP_EOL);
}
$check_sign = $this->VerifySign($bf_msg, $signature);
$plainCerts='"check_sign":"' . $check_sign . PHP_EOL;
fwrite($handle, $plainCerts);
$apiv = config('globaldatas.apiv');
$decrypter = new AesUtil($apiv);
$plain = $decrypter->decryptToString($contents['associated_data'], $contents['nonce'], $contents['ciphertext']);
if (!$plain) {
fwrite($handle, "encrypted certificate decrypt fail!" . PHP_EOL);
fclose($handle);
exit(1);
}
$plainCerts = '"id":' . json_encode($plain) . PHP_EOL;
fwrite($handle, $plainCerts);
fclose($handle);
// return [
// "code"=> "ERROR_NAME", //SUCCESS
// "message"=> "ERROR_DESCRIPTION"
// ];
// return [
// "code"=> "SUCCESS",
// "message"=> "成功收到微信支付结果通知并解密",
// "ret"=>$plain
// ];
}
// 检验微信支付结果通知签名
public function VerifySign($data, $signature)
{
$signature = base64_decode($signature);//解密应答签名
// 微信支付平台证书
$wechat_no = '微信支付平台证书.pem';
$wechatpayCertificate = openssl_pkey_get_public(file_get_contents($wechat_no));
$retCode = openssl_verify($data, $signature, $wechatpayCertificate, OPENSSL_ALGO_SHA256);
if ($retCode == 1) {
return true;
}
return false;
}
// 微信支付退款结果通知
public function wxPayRefundNotice(Request $request) {
$param = file_get_contents('php://input');
$valueArr=static::xmlToArray($param);
$req_info=$valueArr['req_info'];
$key=config('globaldatas.apivv');
$md5LowerKey = strtolower(md5($key));
$decrypted = openssl_decrypt($req_info, "AES-256-ECB", $md5LowerKey);
$decryptedData =static::xmlToArray($decrypted);
$printStr=json_encode($decryptedData).PHP_EOL;
$outpath = '日志(看需求).txt';
file_put_contents($outpath,$printStr, FILE_APPEND | LOCK_EX);
return $decryptedData;
}
public static function xmlToArray($xml) {
$valueXml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$valueObj = json_encode($valueXml);
$valueArr = json_decode($valueObj, true);
return $valueArr;
}
}
有nodejs的嘛