需要支付宝sdk 在支付宝下载
引入AopClient ,AlipayTradeRefundRequest以后配置自己的config文件
如果没有配置可以看我上一篇博客支付宝支付中有教程
代码如下:
/**
* 支付宝退货处理
* 退货前提是必须支付成功
* @param type $orderId
* @return bool
* $out_trade_no@订单号
* $refund_amount@退款金额
*/
public function refundAlyTest($out_trade_no,$out_request_no,$refund_amount,$trade_no)
{
$aop = new \AopClient();
$aop->gatewayUrl = config('config.alipayApp.gatewayUrl'); // 支付宝网关
$aop->appId = config('config.alipayApp.app_id'); // app_id
$aop->rsaPrivateKey = config('config.alipayApp.merchant_private_key'); // 私钥
$aop->alipayrsaPublicKey= config('config.alipayApp.alipay_public_key'); // 公钥
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset= config('config.alipayApp.charset'); // 编码
$aop->format='json';
$request = new \AlipayTradeRefundRequest();
$request->setBizContent("{" .
// "\"trade_no\":\"2017112821001004030523090753\"," .
"\"out_trade_no\":\"$out_trade_no\"," .
"\"refund_amount\":$refund_amount," .
"\"refund_reason\":\"正常退款\"," .
"\"out_request_no\":\"$out_request_no\"," .
"\"operator_id\":\"OP001\"," .
"\"store_id\":\"NJ_S_001\"," .
"\"terminal_id\":\"NJ_T_001\"" .
" }");
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
return $result;
if(!empty($resultCode) && $resultCode == 10000){
//退款成功,处理订单状态以及更新退款等信息
return ['code'=>200,'msg'=>'退款成功'];
} else {
return ['code'=>400,'msg'=>'退款失败'];
}
}