/** * * 申请退款,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"; $url = WxPayConfig::REFUND_URL; // 检测必填参数 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->SetAppid(WxPayConfig::$appId); $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; }
/** * 退款 * @throws WxPayException */ public function actionRefund() { $result = null; // 微信订单号 优先级别高 if (isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != "") { try { $transaction_id = $_REQUEST["transaction_id"]; $total_fee = $_REQUEST["total_fee"]; $refund_fee = $_REQUEST["refund_fee"]; $input = new WxPayRefund(); $input->SetTransaction_id($transaction_id); $input->SetTotal_fee($total_fee); $input->SetRefund_fee($refund_fee); $input->SetOut_refund_no(WxPayConfig::$mchId . date("YmdHis")); $input->SetOp_user_id(WxPayConfig::$mchId); $result = WxPayApi::refund($input); } catch (WxPayException $ex) { echo $ex->getMessage(); } } // 商户订单 优先级别低于微信订单 if (isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != "") { try { $out_trade_no = $_REQUEST["out_trade_no"]; $total_fee = $_REQUEST["total_fee"]; $refund_fee = $_REQUEST["refund_fee"]; $input = new WxPayRefund(); $input->SetOut_trade_no($out_trade_no); $input->SetTotal_fee($total_fee); $input->SetRefund_fee($refund_fee); $input->SetOut_refund_no(WxPayConfig::$mchId . date("YmdHis")); $input->SetOp_user_id(WxPayConfig::$mchId); $result = WxPayApi::refund($input); } catch (WxPayException $ex) { echo $ex->getMessage(); } } }