新版订单消耗系统

AliPayAopClient.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ws655
  5. * Date: 2020/7/23
  6. * Time: 16:14
  7. */
  8. namespace App\Support;
  9. use App\Models\SecretConfig;
  10. use Illuminate\Support\Arr;
  11. require(dirname(__DIR__) . '/libs/alipay/aop/AopClient.php');
  12. require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeAppPayRequest.php');
  13. require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeRefundRequest.php');
  14. require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeWapPayRequest.php');
  15. require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradeQueryRequest.php');
  16. require(dirname(__DIR__) . '/libs/alipay/aop/request/AlipayTradePagePayRequest.php');
  17. class AliPayAopClient
  18. {
  19. private static $_instance;
  20. /** @var \AopClient */
  21. private $_aopClient;
  22. private $_appId, $_sellerId;
  23. private $_aliPayPublicKey, $_rsaPrivateKey;
  24. public function __construct()
  25. {
  26. $config = SecretConfig::getConfig(SecretConfig::NAME_ALI_PAY);
  27. $this->_appId = Arr::get($config, 'appId', '');
  28. $this->_sellerId = Arr::get($config, 'sellerId', '');
  29. $this->_aliPayPublicKey = Arr::get($config, 'aliPayPublicKey', '');
  30. $this->_rsaPrivateKey = Arr::get($config, 'rsaPrivateKey', '');
  31. }
  32. public static function getInstance()
  33. {
  34. if (empty(self::$_instance)) {
  35. self::$_instance = new self();
  36. }
  37. return self::$_instance;
  38. }
  39. public function __call($name, $params)
  40. {
  41. $apoClient = $this->getAopClient();
  42. if (method_exists($apoClient, $name)) {
  43. return call_user_func_array([$apoClient, $name], $params);
  44. }
  45. }
  46. public function getAopClient()
  47. {
  48. if (! $this->_aopClient instanceof \AopClient) {
  49. $this->_aopClient = $this->initAopClient();
  50. }
  51. return $this->_aopClient;
  52. }
  53. private function initAopClient()
  54. {
  55. $aopClient = new \AopClient();
  56. $aopClient->appId = $this->_appId;
  57. $aopClient->rsaPrivateKey = $this->_rsaPrivateKey;
  58. $aopClient->alipayrsaPublicKey = $this->_aliPayPublicKey;
  59. // 沙箱环境
  60. // $aopClient->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
  61. return $aopClient;
  62. }
  63. public function newAppPayRequest()
  64. {
  65. return new \AlipayTradeAppPayRequest();
  66. }
  67. public function newPagePayRequest()
  68. {
  69. return new \AlipayTradePagePayRequest();
  70. }
  71. public function newWapPayRequest()
  72. {
  73. return new \AlipayTradeWapPayRequest();
  74. }
  75. public function newQueryRequest()
  76. {
  77. return new \AlipayTradeQueryRequest();
  78. }
  79. /**
  80. * @param $trade_no
  81. * @return bool|mixed|\SimpleXMLElement
  82. * @throws \Exception
  83. */
  84. /**
  85. * @param $trade_no
  86. * @param $refund_amount
  87. * @param $out_request_no
  88. * @return bool|mixed|\SimpleXMLElement
  89. * @throws \Exception
  90. *
  91. * {
  92. "alipay_trade_refund_response": {
  93. "code": "10000",
  94. "msg": "Success",
  95. "buyer_logon_id": "ker***@sandbox.com",
  96. "buyer_user_id": "2088102174703025",
  97. "fund_change": "Y",
  98. "gmt_refund_pay": "2018-05-09 17:33:02",
  99. "out_trade_no": "20185917311764",
  100. "refund_fee": "100.00",
  101. "send_back_fee": "0.00",
  102. "trade_no": "2018050921001004020200310722"
  103. },
  104. "sign": "mm3hdIQkp4h9NYRYdekGl1vohsAoT5QtfebdvqyRFngxvPMZpxGPTar1Ml9N5MhmNsgZ4xRsmNLfrlqxgPLgkjfDey60iqXVnLG1UZI1qyvjJVVUKFXGADnhscZRXIPOPuEeyvQBracA/4NSG5VjUGoWZw1LZ8yBpcOJjPujPyu6FUw3duWQbeBxY5cGoZvgm5QSNbWydlwV1wfaFPSAbLXTcZf1mVv8KQIgyxjy9wgW4Nmu6RFUaGjMpFHFOZdHXfXgZ4DQuxcvg+48n1i0RZ8FB/BnvsiA5qdSTVdvj8vecB0en0/UO9mSlIszyDnYpDBk+h34umMrEfDpVn7X5Q=="
  105. }
  106. *
  107. * {
  108. "alipay_trade_refund_response": {
  109. "code": "40004",
  110. "msg": "Business Failed",
  111. "sub_code": "ACQ.TRADE_STATUS_ERROR",
  112. "sub_msg": "交易状态不合法",
  113. "refund_fee": "0.00",
  114. "send_back_fee": "0.00"
  115. },
  116. "sign": "IRVt/HKTDoI+yLvaQFLIQZpt7KNGsi2qhYZK7Syaf06duSaeQKNQ4Ky5AKNTT5HPRiPc7/uxTrldfFkqMwLIRbahvh3UyHGZgJJ6cOp6DYt536qYE9Z/rvOt0gSk8Aiwza3aSrsmHmCw2TESLh5Cf3p/sJh7fq0Mk84xN5wGDUnHci9bbT+mduZWkyTD9vebf4NVJwKDvNLEwBKdzXAfdGPNxfCTndt4Si0nMfYqIA5eG2aAmjS90SDsgrL0FOSi1JQhVGzLulHMEf63ysFYYP1W+qBOXC09xmj8KdFAAS3Dw2DZB0c0D1bXC8Z3dXgcLCDSg/jG8zLQ1Kax9Pz7BQ=="
  117. }
  118. */
  119. public function refund($trade_no, $refund_amount, $out_request_no)
  120. {
  121. $bizContent = json_encode(compact('trade_no', 'refund_amount', 'out_request_no'),JSON_UNESCAPED_UNICODE);
  122. $request = new \AlipayTradeRefundRequest();
  123. $request->setBizContent($bizContent);
  124. $aop = self::getAopClient();
  125. return $aop->execute($request);
  126. }
  127. public function checkOwner($appId, $sellerId)
  128. {
  129. return $this->_appId == $appId && (empty($this->_sellerId) || ($this->_sellerId == $sellerId));
  130. }
  131. }