123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /**
- * Created by PhpStorm.
- * User: ws655
- * Date: 2020/7/23
- * Time: 16:14
- */
- namespace App\Support;
- use App\Models\SecretConfig;
- use Illuminate\Support\Arr;
- require(dirname(__DIR__) . '/libs/alipay/aop/AopClient.php');
- require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeAppPayRequest.php');
- require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeRefundRequest.php');
- require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeWapPayRequest.php');
- require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeQueryRequest.php');
- require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradePagePayRequest.php');
- class AliPayAopClient
- {
- private static $_instance;
- /** @var \AopClient */
- private $_aopClient;
- private $_appId, $_sellerId;
- private $_aliPayPublicKey, $_rsaPrivateKey;
- public function __construct()
- {
- $config = SecretConfig::getConfig(SecretConfig::NAME_ALI_PAY);
- $this->_appId = Arr::get($config, 'appId', '');
- $this->_sellerId = Arr::get($config, 'sellerId', '');
- $this->_aliPayPublicKey = Arr::get($config, 'aliPayPublicKey', '');
- $this->_rsaPrivateKey = Arr::get($config, 'rsaPrivateKey', '');
- }
- public static function getInstance()
- {
- if (empty(self::$_instance)) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- public function __call($name, $params)
- {
- $apoClient = $this->getAopClient();
- if (method_exists($apoClient, $name)) {
- return call_user_func_array([$apoClient, $name], $params);
- }
- }
- public function getAopClient()
- {
- if (! $this->_aopClient instanceof \AopClient) {
- $this->_aopClient = $this->initAopClient();
- }
- return $this->_aopClient;
- }
- private function initAopClient()
- {
- $aopClient = new \AopClient();
- $aopClient->appId = $this->_appId;
- $aopClient->rsaPrivateKey = $this->_rsaPrivateKey;
- $aopClient->alipayrsaPublicKey = $this->_aliPayPublicKey;
- // 沙箱环境
- // $aopClient->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
- return $aopClient;
- }
- public function newAppPayRequest()
- {
- return new \AlipayTradeAppPayRequest();
- }
- public function newPagePayRequest()
- {
- return new \AlipayTradePagePayRequest();
- }
- public function newWapPayRequest()
- {
- return new \AlipayTradeWapPayRequest();
- }
- public function newQueryRequest()
- {
- return new \AlipayTradeQueryRequest();
- }
- /**
- * @param $trade_no
- * @return bool|mixed|\SimpleXMLElement
- * @throws \Exception
- */
- /**
- * @param $trade_no
- * @param $refund_amount
- * @param $out_request_no
- * @return bool|mixed|\SimpleXMLElement
- * @throws \Exception
- *
- * {
- "alipay_trade_refund_response": {
- "code": "10000",
- "msg": "Success",
- "buyer_logon_id": "ker***@sandbox.com",
- "buyer_user_id": "2088102174703025",
- "fund_change": "Y",
- "gmt_refund_pay": "2018-05-09 17:33:02",
- "out_trade_no": "20185917311764",
- "refund_fee": "100.00",
- "send_back_fee": "0.00",
- "trade_no": "2018050921001004020200310722"
- },
- "sign": "mm3hdIQkp4h9NYRYdekGl1vohsAoT5QtfebdvqyRFngxvPMZpxGPTar1Ml9N5MhmNsgZ4xRsmNLfrlqxgPLgkjfDey60iqXVnLG1UZI1qyvjJVVUKFXGADnhscZRXIPOPuEeyvQBracA/4NSG5VjUGoWZw1LZ8yBpcOJjPujPyu6FUw3duWQbeBxY5cGoZvgm5QSNbWydlwV1wfaFPSAbLXTcZf1mVv8KQIgyxjy9wgW4Nmu6RFUaGjMpFHFOZdHXfXgZ4DQuxcvg+48n1i0RZ8FB/BnvsiA5qdSTVdvj8vecB0en0/UO9mSlIszyDnYpDBk+h34umMrEfDpVn7X5Q=="
- }
- *
- * {
- "alipay_trade_refund_response": {
- "code": "40004",
- "msg": "Business Failed",
- "sub_code": "ACQ.TRADE_STATUS_ERROR",
- "sub_msg": "交易状态不合法",
- "refund_fee": "0.00",
- "send_back_fee": "0.00"
- },
- "sign": "IRVt/HKTDoI+yLvaQFLIQZpt7KNGsi2qhYZK7Syaf06duSaeQKNQ4Ky5AKNTT5HPRiPc7/uxTrldfFkqMwLIRbahvh3UyHGZgJJ6cOp6DYt536qYE9Z/rvOt0gSk8Aiwza3aSrsmHmCw2TESLh5Cf3p/sJh7fq0Mk84xN5wGDUnHci9bbT+mduZWkyTD9vebf4NVJwKDvNLEwBKdzXAfdGPNxfCTndt4Si0nMfYqIA5eG2aAmjS90SDsgrL0FOSi1JQhVGzLulHMEf63ysFYYP1W+qBOXC09xmj8KdFAAS3Dw2DZB0c0D1bXC8Z3dXgcLCDSg/jG8zLQ1Kax9Pz7BQ=="
- }
- */
- public function refund($trade_no, $refund_amount, $out_request_no)
- {
- $bizContent = json_encode(compact('trade_no', 'refund_amount', 'out_request_no'),JSON_UNESCAPED_UNICODE);
- $request = new \AlipayTradeRefundRequest();
- $request->setBizContent($bizContent);
- $aop = self::getAopClient();
- return $aop->execute($request);
- }
- public function checkOwner($appId, $sellerId)
- {
- return $this->_appId == $appId && (empty($this->_sellerId) || ($this->_sellerId == $sellerId));
- }
- }
|