后期预备:
当然是搞定了微信领取,否则怎样退款,此次仍是应用民间的demo。当然网上可能也有不少年夜神本人重写以及封装了demo,或者愈加好用简约,然而我仍是没有倡导用,缘由以下:
(1)可能性能没有全,或者他只是完成了微信领取,然而另有请求退款、查问退款、定单查问、打消定单等营业性能多是你后续需求的,假如你依赖于年夜神的SDK的便捷,假如有新的营业需要,你就懵逼了;
(2)平安思考,触及到领取触及到款项,必需要十分平安。民间SDK尽管我也吐槽,但至多会绝对比拟平安,再次重写,尽管临时没看出成绩,然而万一有破绽就欠好了。
本篇仍是应用到民间提供的SDK中的最首要的一个类文件WxPay.Api.php中提供的refund()办法来完成,此办法正在WxPay.Api.php文件的第141行,代码以下:
/** * * 请求退款,WxPayRefund中out_trade_no、transaction_id至多填一个且 * out_refund_no、total_fee、refund_fee、op_user_id为必填参数 * appid、mchid、spbill_create_ip、nonce_str没有需求填入 * @param WxPayRefund $inputObj * @param int $timeOut * @throws WxPayException * @return 胜利时前往,其余抛异样 */ public static function refund($inputObj, $timeOut = 6){ $url = "https://api.mch.weixin.qq.com/secapi/pay/refund"; //检测必填参数 if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) { throw new WxPayException("退款请求接口中,out_trade_no、transaction_id至多填一个!"); }else if(!$inputObj->IsOut_refund_noSet()){ throw new WxPayException("退款请求接口中,短少必填参数out_refund_no!"); }else if(!$inputObj->IsTotal_feeSet()){ throw new WxPayException("退款请求接口中,短少必填参数total_fee!"); }else if(!$inputObj->IsRefund_feeSet()){ throw new WxPayException("退款请求接口中,短少必填参数refund_fee!"); }else if(!$inputObj->IsOp_user_idSet()){ throw new WxPayException("退款请求接口中,短少必填参数op_user_id!"); } $inputObj->SetAppid(WxPayConfig::APPID);//大众账号ID $inputObj->SetMch_id(WxPayConfig::MCHID);//商户号 $inputObj->SetNonce_str(self::getNonceStr());//随机字符串 $inputObj->SetSign();//署名 $xml = $inputObj->ToXml(); $startTimeStamp = self::getMillisecond();//申请开端工夫 $response = self::postXmlCurl($xml, $url, true, $timeOut); $result = WxPayResults::Init($response); self::reportCostTime($url, $startTimeStamp, $result);//上报申请破费工夫 return $result; }
民间的办法,写的很分明需求哪些参数,另有一些必需参数SDK曾经帮咱们补齐了,我将这个办法从新封装一下,便于正在名目中挪用:
/** * 微信退款 * @param string $order_id 定单ID * @return 胜利时前往(array类型),其余抛异样 */ function wxRefund($order_id){ //我的SDK放正在名目根目次下的Api目次下 require_once APP_ROOT."/Api/wxpay/lib/WxPay.Api.php"; //查问定单,依据定单里边的数据进行退款 $order = M('order')->where(array('id'=>$order_id,'is_refund'=>2,'order_status'=>1))->find(); $merchid = WxPayConfig::MCHID; if(!$order) return false; $input = new WxPayRefund(); $input->SetOut_trade_no($order['order_sn']);//本人的定单号 $input->SetTransaction_id($order['transaction_id']); //微信民间天生的定单流水号,正在领取胜利中有前往 $input->SetOut_refund_no(getrand_num(true));//退款单号 $input->SetTotal_fee($order['total_price']);//定单标价金额,单元为分 $input->SetRefund_fee($order['total_price']);//退款总金额,定单总金额,单元为分,只能为整数 $input->SetOp_user_id($merchid); $result = WxPayApi::refund($input);//退款操作 // 这句file_put_contents是用来查看效劳器前往的退款后果 测试完能够删除了了 //file_put_contents(APP_ROOT.'/Api/wxpay/logs/log3.txt',arrayToXml($result),FILE_APPEND); return $result; }
这里需求吐槽一下,居然没有说前往值的类型,正在领取的时分前往的是XML数据,这里居然前往的是数组,让我措手不迭,哈哈不外仍是前往数组比拟好,能够间接判别解决。
办法挪用就愈加简略了:
//微信退款 $result = wxRefund($order_id); // 这句file_put_contents是用来查看效劳器前往的退款后果 测试完能够删除了了 //file_put_contents(APP_ROOT.'/Api/wxpay/logs/log4.txt',arrayToXml($result),FILE_APPEND); if(($result['return_code']=='SUCCESS') && ($result['result_code']=='SUCCESS')){ //退款胜利 }else if(($result['return_code']=='FAIL') || ($result['result_code']=='FAIL')){ //退款失败 //缘由 $reason = (empty($result['err_code_des'])?$result['return_msg']:$result['err_code_des']); }else{ //失败 }
退款胜利前往以下:
亲测无误:这是集成为了民间的SDK完成的,假如没有应用SDK,能够应用更简略的办法,见:PHP完成微信领取(jsapi领取)以及退款(无需集成领取SDK)
更多PHP相干常识,请拜访PHP教程!
以上就是PHP怎样完成微信请求退款的具体内容,更多请存眷资源魔其它相干文章!
标签: php开发教程 php开发资料 php开发自学 微信
抱歉,评论功能暂时关闭!