AlipayV2Default.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\common\service\pay\alipay;
  3. use Alipay\EasySDK\Kernel\Config;
  4. use Alipay\EasySDK\Kernel\Factory;
  5. use app\common\enum\AfterSaleLogEnum;
  6. use app\common\enum\OrderEnum;
  7. use app\common\enum\PayEnum;
  8. use app\common\logic\PayNotifyLogic;
  9. use app\common\model\IntegralOrder;
  10. use app\common\model\Order;
  11. use app\common\model\RechargeOrder;
  12. use app\common\service\after_sale\AfterSaleService;
  13. use app\common\service\pay\base\BasePayService;
  14. use think\facade\Log;
  15. /**
  16. * @notes 支付宝v2 默认
  17. * author lbzy
  18. * @datetime 2024-11-11 13:47:50
  19. * @class AlipayV2
  20. * @package app\common\service\pay\alipay
  21. */
  22. class AlipayV2Default extends BasePayService
  23. {
  24. function __construct($params)
  25. {
  26. parent::__construct($params);
  27. //初始化支付配置
  28. Factory::setOptions($this->getOptions());
  29. $this->pay = Factory::payment();
  30. }
  31. function getOptions()
  32. {
  33. $config = $this->config;
  34. $options = new Config();
  35. $options->protocol = 'https';
  36. $options->gatewayHost = 'openapi.alipay.com';
  37. // $options->gatewayHost = 'openapi.alipaydev.com'; //测试沙箱地址
  38. $options->signType = 'RSA2';
  39. $options->appId = $config['config']['app_id'] ?? '';
  40. // 应用私钥
  41. $options->merchantPrivateKey = $config['config']['private_key'] ?? '';
  42. //支付宝公钥
  43. $options->alipayPublicKey = $config['config']['ali_public_key'] ?? '';
  44. //回调地址
  45. $options->notifyUrl = (string)url('pay/aliNotify', [], false, true);
  46. return $options;
  47. }
  48. function payMnp(): string
  49. {
  50. return $this->payH5();
  51. }
  52. function payOa(): string
  53. {
  54. return $this->payH5();
  55. }
  56. function payIos(): string
  57. {
  58. $order = $this->payOrder;
  59. $result = $this->pay->app()->optional('passback_params', $this->payAttach)->pay(
  60. $order['sn'],
  61. $order['sn'],
  62. $order['order_amount']
  63. );
  64. return $result->body;
  65. }
  66. function payAndroid(): string
  67. {
  68. return $this->payIos();
  69. }
  70. function payH5(): string
  71. {
  72. $order = $this->payOrder;
  73. $attach = $this->payAttach;
  74. $domain = request()->domain();
  75. $url = $domain . '/mobile/pages/index/index';
  76. if ($attach == 'order') {
  77. $url = $domain . '/mobile/pages/order_list/order_list';
  78. }
  79. if ($attach == 'recharge') {
  80. $url = $domain . '/mobile/bundle/pages/user_recharge/user_recharge';
  81. }
  82. $result = $this->pay->wap()->optional('passback_params', $attach)->pay(
  83. '订单:' . $order['sn'],
  84. $order['sn'],
  85. $order['order_amount'],
  86. $url,
  87. $url
  88. );
  89. return $result->body;
  90. }
  91. function payPc(): string
  92. {
  93. $domain = request()->domain();
  94. $order = $this->params['payData']['order'];
  95. $attach = $this->params['payData']['attach'];
  96. $result = $this->pay->page()->optional('passback_params', $attach)->pay(
  97. '订单:' . $order['sn'],
  98. $order['sn'],
  99. $order['order_amount'],
  100. $domain . '/pc/user/order'
  101. );
  102. return $result->body;
  103. }
  104. function payNotify(): bool
  105. {
  106. $data = $this->params['notify_data'];
  107. try {
  108. $verify = $this->pay->common()->verifyNotify($data);
  109. if (false === $verify) {
  110. throw new \Exception('异步通知验签失败');
  111. }
  112. if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
  113. return true;
  114. }
  115. $extra['transaction_id'] = $data['trade_no'];
  116. //验证订单是否已支付
  117. $this->paySuccess([
  118. 'extra' => $extra,
  119. 'from' => $data['passback_params'],
  120. 'out_trade_no' => $data['out_trade_no'],
  121. 'transaction_id' => $data['trade_no'],
  122. ]);
  123. return true;
  124. } catch (\Exception $e) {
  125. $record = [
  126. __CLASS__,
  127. __FUNCTION__,
  128. $e->getFile(),
  129. $e->getLine(),
  130. $e->getMessage()
  131. ];
  132. Log::write(implode('-', $record));
  133. $this->setStatus(false, $e->getMessage());
  134. return false;
  135. }
  136. }
  137. function queryPay()
  138. {
  139. return $this->pay->common()->query($this->params['query_pay_data']['order_sn']);
  140. }
  141. function refund(): \Alipay\EasySDK\Payment\Common\Models\AlipayTradeRefundResponse
  142. {
  143. return $this->pay->common()
  144. ->optional('out_request_no', $this->params['refund_data']['out_request_no'])
  145. ->refund($this->params['refund_data']['order_sn'], $this->params['refund_data']['order_amount']);
  146. }
  147. function queryRefund()
  148. {
  149. return $this->pay->common()
  150. ->queryRefund($this->params['query_refund_data']['order_sn'], $this->params['query_refund_data']['refund_sn']);
  151. }
  152. }