BasePayService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\common\service\pay\base;
  3. use app\common\enum\AfterSaleLogEnum;
  4. use app\common\enum\OrderEnum;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\UserTerminalEnum;
  7. use app\common\logic\BaseLogic;
  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 Psr\Http\Message\MessageInterface;
  14. use think\facade\Log;
  15. use Yansongda\Artful\Exception\InvalidResponseException;
  16. use Yansongda\Supports\Collection;
  17. /**
  18. * @notes notes
  19. * author lbzy
  20. * @datetime 2024-11-11 14:15:49
  21. * @class BasePayService
  22. * @package app\common\service\pay\base
  23. */
  24. abstract class BasePayService
  25. {
  26. use StatusTrait;
  27. protected array $params = [];
  28. protected int|null $user_id = null;
  29. protected int|null $terminal;
  30. protected array|null $config;
  31. protected int|null $pay_way;
  32. /**
  33. * @var
  34. */
  35. protected $payOrder;
  36. protected $payAttach;
  37. function __construct(array $params)
  38. {
  39. $this->params = $params;
  40. if (isset($params['terminal'])) {
  41. $this->terminal = $params['terminal'];
  42. }
  43. if (isset($params['user_id'])) {
  44. $this->user_id = $params['user_id'];
  45. }
  46. $this->pay_way = $params['pay_way'];
  47. $this->config = $params['config'] ?? [];
  48. }
  49. function set_params(array $params): void
  50. {
  51. $this->params = array_merge($this->params, $params);
  52. }
  53. function pay($from, $order) : bool|array
  54. {
  55. $this->payOrder = $order;
  56. $this->payAttach = $from;
  57. try {
  58. switch ($this->params['terminal']) {
  59. case UserTerminalEnum::PC:
  60. $result = $this->payPc();
  61. break;
  62. case UserTerminalEnum::IOS:
  63. $result = $this->payIos();
  64. break;
  65. case UserTerminalEnum::ANDROID:
  66. $result = $this->payAndroid();
  67. break;
  68. case UserTerminalEnum::H5:
  69. $result = $this->payH5();
  70. break;
  71. case UserTerminalEnum::WECHAT_MMP:
  72. $result = $this->payMnp();
  73. break;
  74. case UserTerminalEnum::WECHAT_OA:
  75. $result = $this->payOa();
  76. break;
  77. default:
  78. throw new \Exception('支付方式错误');
  79. }
  80. return [
  81. 'config' => $result,
  82. 'pay_way' => $this->pay_way,
  83. ];
  84. } catch (\Throwable $e) {
  85. if ($e instanceof InvalidResponseException) {
  86. if($e->response instanceof Collection || $e->response instanceof MessageInterface) {
  87. $this->setStatus(false, $e->response->get('message') ? : $e->getMessage());
  88. return false;
  89. }
  90. }
  91. Log::write($e->__toString(), 'payError');
  92. $this->setStatus(false, $e->getMessage());
  93. return false;
  94. }
  95. }
  96. protected function paySuccess(array $params)
  97. {
  98. $from = $params['from'] ?? '';
  99. $out_trade_no = $params['out_trade_no'] ?? '';
  100. $transaction_id = $params['transaction_id'] ?? '';
  101. $extra = $params['extra'] ?? [];
  102. switch ($from) {
  103. case 'order':
  104. $order = Order::where([ 'sn' => $out_trade_no ])->findOrEmpty();
  105. if (!$order || $order['pay_status'] >= PayEnum::ISPAID) {
  106. return true;
  107. }
  108. //特殊情况:用户在前端支付成功的情况下,调用回调接口之前,订单被关闭
  109. if ($order['order_status'] == OrderEnum::STATUS_CLOSE) {
  110. //更新订单支付状态为已支付
  111. Order::update([ 'pay_status' => PayEnum::ISPAID, 'transaction_id' => $transaction_id ],[
  112. 'id' => $order['id']
  113. ]);
  114. //发起售后
  115. AfterSaleService::orderRefund([
  116. 'order_id' => $order['id'],
  117. 'scene' => AfterSaleLogEnum::ORDER_CLOSE
  118. ]);
  119. return true;
  120. }
  121. PayNotifyLogic::handle('order', $out_trade_no, $extra);
  122. break;
  123. case 'recharge':
  124. $order = RechargeOrder::where(['sn' => $out_trade_no ])->findOrEmpty();
  125. if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
  126. return true;
  127. }
  128. PayNotifyLogic::handle('recharge', $out_trade_no, $extra);
  129. break;
  130. case 'integral':
  131. $order = IntegralOrder::where(['sn' => $out_trade_no ])->findOrEmpty();
  132. if($order->isEmpty() || $order['pay_status'] != PayEnum::UNPAID) {
  133. return true;
  134. }
  135. PayNotifyLogic::handle('integral', $out_trade_no, $extra);
  136. break;
  137. }
  138. }
  139. abstract public function payMnp();
  140. abstract public function payOa();
  141. abstract public function payIos();
  142. abstract public function payAndroid();
  143. abstract public function payH5();
  144. abstract public function payPc();
  145. abstract public function payNotify();
  146. abstract public function queryPay();
  147. abstract public function refund();
  148. abstract public function queryRefund();
  149. }