封装的工具栏代码如下:
<?php
require_once '../extend/alipay/aop/AopClient.php';
require_once '../extend/alipay/aop/AopCertClient.php';
require_once '../extend/alipay/aop/AopCertification.php';
require_once '../extend/alipay/aop/AlipayConfig.php';
require_once '../extend/alipay/aop/request/AlipayTradeRefundRequest.php';
/**
* 支付宝服务类
* 封装支付宝相关功能,包含完整默认配置,适合单一支付宝账号的项目
*/
class Alipay
{
private $aop;
public function __construct() {
$config = get_addon_config('epay');//这里我用的fasadmin的插件配置,可自己修改为普通数据存储配置
$this->aop = new AopClient();
$this->aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$this->aop->appId = $config['alipay']['app_id'];
$this->aop->rsaPrivateKey = $config['alipay']['private_key'];
$this->aop->alipayrsaPublicKey = $config['alipay']['ali_public_key'];
$this->aop->signType = 'RSA2';
$this->aop->postCharset = 'UTF-8';
$this->aop->format = 'json';
}
/**
* 支付宝退货处理
* 退货前提是必须支付成功
* @param type $orderId
* @return bool
*/
public function refund($params) {
$request = new AlipayTradeRefundRequest ();
$bizcontent = json_encode([
//'out_trade_no'=>$params['out_trade_no'],
'trade_no'=>$params['trade_no'],
'refund_amount'=>$params['refund_amount'],
'refund_reason'=>'正常退款'
]);
$request->setBizContent($bizcontent);
$result = $this->aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
//var_dump($result->$responseNode);
if(!empty($resultCode)&&$resultCode == 10000){//退款成功
$result=[
'trade_no'=>$result->$responseNode->trade_no,
'out_trade_no' =>$result->$responseNode->out_trade_no,
];
return $result;
} else {//退款失败
return false;
}
}
}
?>
调用示例:
//测试支付宝退款
public function test_alipay_refund(){
$order_id=$this->request->param('order_id');//订单id
$option['id']=$order_id;
$order=Db::name('hospital_order')->where($option)->find();
if(!$order){
$this->error('该订单不存在');
}
$alipay = new \Alipay();
$out_refund_no='tk_'.create_order_sn();
$params=[
//'out_trade_no' => '商户订单号(与trade_no二选一)',
'trade_no' => $order['trade_no'],
'refund_amount' => $order['pay_amount'],
'refund_reason' => '正常退款',
'out_request_no' => $out_refund_no,
//'refund_goods_detail' =>[],
//'refund_royalty_parameters' => [],退分账明细信息
//'query_options' => '查询选项',
//'related_settle_confirm_no' => '针对账期交易'
];
$result=$alipay->refund($params);
var_dump($result);die;
}
(adsbygoogle = window.adsbygoogle || []).push({});