如题:
https://pay.weixin.qq.com/wik...
以上为微信官方的文档,想要实现微信退款通知处理业务,使用的thinkphp3.2
通过对上述完成
使用php 无论如何对都req_info 无法解密
求大神指导 ,或提供一个dmeo
const CIPHER = MCRYPT_RIJNDAEL_256;
const MCRYPT_MODE = MCRYPT_MODE_ECB;
$tools = new \WxPayNotify();
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];//获取xml回调数据
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);//转换xml数据为数组
// $data = json_decode('{"return_code":"SUCCESS","appid":"wxa88a657b659971cd","mch_id":"1290531301","nonce_str":"c88807107134237f13d7d6e6cdd1b3a1","req_info":"ujyWCvgLmXzJdHIrf1ltoHtIJX5u\/EBepMYNSXPp9ZsgieGhhdXMtfXF8eL82Gbk1fb4cjk2tFX0GNkKRilfcglxDQVEVqYhMGIIyqTfEYs+So\/V\/EYSvYCOM9TjlzJ4sXzqLOwTh4PaPRAYRN9RFy1w3uN7pmYiDC7mhB2WrhTvTJl5j22u+Ywpb9xk96vMDxo62nlJl7XohdYIPY2TPbYOQp0sfeMCeWoKilUGjJcadjcSiF2LUfrjOdW+AD3Ow6axOFbjpwV\/rheoUzt1REN1jZWD23bwmVrwvz4ZVoBKcUG7F\/9MU8LdI7HDuk2EzICr8R6Uj4rpN8zvm4RHb853bJ6W1Vctvphy5N66q\/DFKu0Xl\/z+Kxn38LEzANomFuV2XF8vdZumWnqVVVUmyQkSXmXSj2lyONkK9jUQgQBsDHa1Gq6e3g54gDqgQdmEYIk5FmSU\/xn\/1pPZB3\/jOG6qXm03VEmD6sw8jSnAypl0vBDJfH7If5bQXihYDmFoipTzhaWOQJO6UTmj1gk\/nAq3BlBz4KuOIFWM7PsyGjzlf+SpCbIlzEje+uYKcrqVRr8qpZSCtRlS59crk3LrHiNfPGImthhHIZO+l9zbmf5LQnQKqCvvwxy7M8Izu\/iVyXjeNz0pN+6oeZ\/VCYAGGCmhSVkT3Pau8ta3xZGUMqduPMTCEp+IkU46a02m2FWArK9vXKoZpehyvCpCSlyrSy6zxUJYQ\/mE8bIuHAJBS7oN9lvltw4\/mRsFSjJ1v0H5F7IU4vZJ7O+wnpnxTmmUa5ziaLEZ\/YQe5AGo78\/ozg8I5toEJzXV3n5bVrNjgSb5b7Y3yaKTXibylfaeV98OzRz09DqJRqUdRWoO2BLU3KtA+1loEQkEoh5pKAEBcmXII4w5CN7Y7A9aP2qXt6Bf\/atP5eJBvspKTZk4aWsfnSbXey961bO6zVA9Ge08965gCObaBCc11d5+W1azY4Em+VaUiNnQCKRVQ9XDZcR96AQ2gnLDZtUg+pqCt2x+pfaOl+4vCVDeWhUkjbfl48UtEwUPCxHlm7Ip69wxYSXFT1hMTSO6OnzGXMH9Q\/6VbNhN7k6KYqB\/W3cMuAFEOthqX4XkmzustCzymRiNslCmuDo="}',true);
// $data = $tools->handle($array_data);
$wxpay = C('payment.wxpay');
//指定初始化向量iv的大小:
$encryptData = base64_decode($array_data['req_info']);
$decryptedData = $this->_decryptAesData($encryptData);
/**
* decrypt data
* @param string $encryptData
* @param string $md5LowerKey
* @return array
*/
private function _decryptAesData( $encryptData, $md5LowerKey = '')
{
if (empty($md5LowerKey)) {
$wxpay = C('payment.wxpay');
$md5LowerKey = strtolower(md5($wxpay['APPID']));
}
// var_dump($wxpay);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER, self::MCRYPT_MODE), MCRYPT_RAND);
$decrypted = mcrypt_decrypt(self::CIPHER, $md5LowerKey, $encryptData, self::MCRYPT_MODE, $iv);
return $this->xml2array($decrypted);
}
/**
* reply to wechat
* @param $xml
*/
public function replyNotify($xml)
{
if (is_array($xml)) {
$xml = $this->toXml($xml);
}
echo $xml;
}
/**
* @param string $xml
* @return array
* @throws \Exception
*/
public function xml2array( $xml)
{
if (empty($xml)) {
throw new \Exception('Error xml data!');
}
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $index);
xml_parser_free($p);
$result = [];
foreach ($values as $val) {
$result[strtolower($val['tag'])] = $val['value'];
}
return $result;
}
你好懒啊,楼主。就让我们写个完整的的demo给你。
其实有没有想过,微信退款请求可以用同步方法实现,就是你发送退款请求的时候,微信那边会直接返回给你状态(成功、失败),顺带着继续处理你的逻辑。
补充:不好意思,刚刚没看懂你的意思嘲讽了楼主。因为我做的退款是同步执行的,因此暂时没办法直接提供demo给你。
req_info 解密遇到了问题对吧?最好能把你现在的req_info字段处理方法贴出来,我们帮你一起看看是哪里出了问题。
微信官方给出的解密步骤是:
(1)对加密串A做base64解码,得到加密串B
(2)对商户key做md5,得到32位小写key* ( key设置路径:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置 )
(3)用key*对加密串B做AES-256-ECB解密