微信退款成功后,会向用户指定的地址发送一个请求,参数是微信退款的状态,成功,失败都有回复,但是数据是加密的
网上的很多说解密的,但是那些解密的函数已数被php弃用了
//可逆加密
function encrypt($data, $key) {
$prep_code = serialize($data);
$block = mcrypt_get_block_size('des', 'ecb');
if (($pad = $block - (strlen($prep_code) % $block)) < $block) {
$prep_code .= str_repeat(chr($pad), $pad);
}
$encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB);
return base64_encode($encrypt);
}
//可逆解密
function decrypt($str, $key) {
$str = base64_decode($str);
$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
$block = mcrypt_get_block_size('des', 'ecb');
$pad = ord($str[($len = strlen($str)) - 1]);
if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) {
$str = substr($str, 0, strlen($str) - $pad);
}
return unserialize($str);
}
上面的 mcrypt类的函数都被弃用了,
下面是微信退款时的请求
public function refund_result(){
$xml = file_get_contents('php://input');
$refundObj = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
//退款回调
if($refundObj->return_code == "SUCCESS"){
$req_info = $refundObj->req_info;
$req = RoutineRefund::decipheringReqInfo($req_info);
if($req["refund_status"] == "SUCCESS"){
//表示退款成功
$status = "succ";
//如果退款成功,给用户发送模板消息
$order_id = $req["out_trade_no"];
$order = StoreOrder::where("order_id",$order_id)->find();
//如果退款是普通订单,则发普通退款消息,如果是拼团的则发拼团退款
if($order->combination_id){
$formId = RoutineFormId::getFormIdOne($order->uid);
$assemble = Assemble::find($order->pink_id);
$assemble_product = AssembleProduct::find($assemble->ass_id);
$product = StoreProduct::find($assemble_product->product_id);
$store = Store::find($assemble_product->store_id);
$assemble_coming_count = AssembleComing::where("a_id",$order->pink_id)->where("is_refund",0)->count();
$data['keyword1']['value']=$product->getData("store_name");
$data['keyword2']['value']=$assemble->createtime;
$data['keyword3']['value']= $assemble_coming_count;
$data['keyword4']['value']="拼团超时";
$data['keyword5']['value']=$assemble_product->member_num;
$data['keyword6']['value']=$store->getData("name");
$data['keyword7']['value']=$order->getData("pay_price");
RoutineFormId::delFormIdOne($formId);
RoutineTemplateService::sendTemplate(WechatUser::getOpenId($order->uid),RoutineTemplateService::setTemplateId(RoutineTemplateService::ASSEMBLE_FAIL),'',$data,$formId);
}else{
//暂时没有
}
}else if($req["refund_status"] == "CHANGE"){
//表示退款异常
$status = "change";
}else if($req["refund_status"] == 'REFUNDCLOSE'){
//表示退款关闭
$status = "refundclose";
}
Db::name("order_refund")->insert([
"order_id"=>$req["out_trade_no"],
"refund_status"=>$status ?:"",
"createtime"=>date("Y-m-d H:i:s"),
]);
return ' ';
//最终都要给微个返回一个成功的通知
}
}
这个里面就是官方推荐的解密方法
//解密退款成功时,微信回调返回的信息
public static function decipheringReqInfo($str){
//微信商户key
$key = self::options()['pay_routine_key'];
$str = base64_decode($str);
$xml = openssl_decrypt($str,'aes-256-ecb',md5($key),OPENSSL_RAW_DATA);
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}