调用退款
public function testRefundApliy()
{
$restul = R('ApiAlipayclass/refundApliy',array('201811264810110084499168','',19.80,'正常退款','vip会员退款'));
var_dump($restul);
}
/**
* [refundApliy 支付宝-统一收单交易退款接口]
* @param [type] $out_trade_no [商户订单号]
* @param [type] $trade_no [支付宝交易号]
* @param [type] $refund_amount [需要退款的金额]
* @param string $refund_reason [退款的原因说明]
* @param string $targetType [退款的来源:leader]
* @return [type] [description]
*/
public function refundApliy($out_trade_no,$trade_no,$refund_amount,$refund_reason='正常退款',$targetType=''){
// 支付宝交易号和商户订单号不能同时为空,退款金额必须大于0
if((!$out_trade_no && !$trade_no) || $refund_amount <= 0){
return -1;
}
//实例化sdk
$aop = new \AopClient();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';//HTTPS请求地址
$aop->appId = C('app_alipay_config.app_id');//支付宝分配给开发者的应用ID
$aop->rsaPrivateKey = C('app_alipay_config.rsaPrivate');
$aop->alipayrsaPublicKey = C('app_alipay_config.alipy_rsaPublic');
$aop->apiVersion = '1.0';
$aop->signType = 'RSA';
$aop->postCharset='UTF-8';
$aop->format='json';
$request = new \AlipayTradeRefundRequest();
$bizcontent_json="{" .
"\"out_trade_no\":\"{$out_trade_no}\"," .//商户订单号,不能和 trade_no同时为空
"\"trade_no\":\"{$trade_no}\"," .//支付宝交易号
"\"refund_amount\":{$refund_amount}," .//需要退款的金额
"\"refund_reason\":\"{$refund_reason}\"" .//退款的原因说明
// "\"out_request_no\":\"HZ01RF001\"," .//如需部分退款,则此参数必传。
// "\"operator_id\":\"OP001\"," .//商户的操作员编号
// "\"store_id\":\"NJ_S_001\"," .//商户的门店编号
// "\"terminal_id\":\"NJ_T_001\"" .//商户的终端编号
" }";
$request->setBizContent($bizcontent_json);
log_into_txt($this->alipy_log,"【发起支付宝退款申请】:\n",'head');
log_into_txt($this->alipy_log,"【申请来源】:{$targetType}\n");
log_into_txt($this->alipy_log,"【支付宝退款请求参数】:".$bizcontent_json."\n");
//返回结果验签,格式化
$result = $aop->execute($request);
log_into_txt($this->alipy_log,"【支付宝返回结果】:".json_encode($result)."\n");
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
//返回结果
if(!empty($resultCode) && $resultCode == 10000){
log_into_txt($this->alipy_log,"【退款成功】:\n");
return ['checkCode'=>1,'data'=>$result,'keyName'=>$responseNode];
} else {
log_into_txt($this->alipy_log,"【退款失败】:\n");
return ['checkCode'=>0,'data'=>$result,'keyName'=>$responseNode];
}
}