<?php
namespace alipay;
//APP支付
use think\Loader;
class Alipay
{
protected $gatewayUrl = "https://openapi.alipay.com/gateway.do";
protected $appId = "";
protected $rsaPrivateKey = "";
public $alipayrsaPublicKey = "";
protected $format = "json";
protected $charset = "UTF-8";
protected $signType = "RSA2";
public function __construct()
{
Loader::import('alipay.aop.AopClient');
Loader::import('alipay.aop.request.AlipayTradeAppPayRequest');
Loader::import('alipay.aop.request.AlipayTradeRefundRequest');
}
/**
* APP-支付
* @param array $param
* $param['body'] 对一笔交易的具体描述信息
* $param['subject'] 商品的标题/交易标题/订单标题/订单关键字等
* $param['total_amount'] 订单总金额,单位为元,精确到小数点后两位
* $param['out_trade_no'] 商户网站唯一订单号
* @return string
*/
public function pay(array $param)
{
$aop = new \AopClient();
$aop->gatewayUrl = $this->gatewayUrl;
$aop->appId = $this->appId;
$aop->rsaPrivateKey = $this->rsaPrivateKey;
$aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
$aop->format = $this->format;
$aop->charset = $this->charset;
$aop->signType = $this->signType;
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new \AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数
$data = [
"body" => $param['body'],
"subject" => $param['subject'],
"timeout_express" => "30m",
"total_amount" => $param['total_amount'],
"out_trade_no" => $param['out_trade_no']
];
$request->setNotifyUrl('http://'.$_SERVER['HTTP_HOST'].'/user/Allot/appNotifys');
$request->setBizContent(json_encode($data));
//这里和普通的接口调用不同,使用的是sdkExecute
$result = $aop->sdkExecute($request);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
//就是orderString 可以直接给客户端请求,无需再做处理。
return $result;
}
/**
* APP-退款
* @param $out_trade_no 订单支付时传入的商户订单号,不能和 trade_no同时为空
* @param $refund_amount 订单支付总金额
* @param $trade_no 支付宝交易号
* @return bool|mixed|\SimpleXMLElement
* @throws \Exception
*/
public function refund($out_trade_no, $refund_amount, $trade_no = "")
{
$aop = new \AopClient();
$aop->gatewayUrl = $this->gatewayUrl;
$aop->appId = $this->appId;
$aop->rsaPrivateKey = $this->rsaPrivateKey;
$aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
$aop->apiVersion = '1.0';
$aop->postCharset = $this->charset;
$aop->format = $this->format;
$aop->signType = $this->signType;
$request = new \AlipayTradeRefundRequest();
$data = [
'out_trade_no' => $out_trade_no,
'trade_no' => $trade_no,
'refund_amount' => $refund_amount,
];
$request->setBizContent(json_encode($data));
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$resultCode = $result->$responseNode->code;
if (!empty($resultCode) && $resultCode == 10000) {
$res = [
'msg' => $result->$responseNode->msg,
'buyer_logon_id' => $result->$responseNode->buyer_logon_id, //用户的登录id
'buyer_user_id' => $result->$responseNode->buyer_user_id, //买家在支付宝的用户id
'fund_change' => $result->$responseNode->fund_change, //本次退款是否发生了资金变化
'gmt_refund_pay' => $result->$responseNode->gmt_refund_pay, //退款支付时间
'out_trade_no' => $result->$responseNode->out_trade_no, //商户订单号
'refund_fee' => $result->$responseNode->refund_fee, //退款总金额
'send_back_fee' => $result->$responseNode->send_back_fee, //(文档未找到)
'trade_no' => $result->$responseNode->trade_no //支付宝交易号
];
return $res;
} else {
return $result;
}
}
}
网友评论