PaymentManager.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Payment;
  4. use SixShop\Payment\Contracts\PaymentProviderInterface;
  5. use SixShop\Payment\Contracts\PaymentQueryResult;
  6. use SixShop\Payment\Contracts\PaymentRefundRequest;
  7. use SixShop\Payment\Contracts\PaymentRefundResult;
  8. use SixShop\Payment\Contracts\PaymentResponse;
  9. use SixShop\Payment\Entity\ExtensionPaymentEntity;
  10. use SixShop\Payment\Entity\ExtensionRefundEntity;
  11. use SixShop\Payment\Enum\PaymentBizEnum;
  12. use SixShop\Payment\Event\BeforePayEvent;
  13. use SixShop\Payment\Event\GatheringPaymentEvent;
  14. use SixShop\System\ExtensionManager;
  15. use SixShop\System\Model\ExtensionModel;
  16. use think\facade\Event;
  17. readonly class PaymentManager
  18. {
  19. public function __construct(
  20. private ExtensionManager $extensionManager,
  21. )
  22. {
  23. }
  24. /**
  25. * 获取所有支付方式
  26. *
  27. * @return PaymentInfo[]
  28. */
  29. public function getAllPayment():array
  30. {
  31. /* @var PaymentInfo[] $paymentList */
  32. $paymentList = Event::trigger(GatheringPaymentEvent::class);
  33. $paymentIDs = array_column($paymentList, 'id');
  34. $statusMap = ExtensionModel::whereIn('id', $paymentIDs)->column(['status','id'], 'id', true);
  35. $payTypeList = extension_config('payment', 'pay_type')??[];
  36. foreach ($paymentList as $payment) {
  37. $payment->status = $statusMap[$payment->id]['status'];
  38. $payment->enabled = in_array($payment->id, $payTypeList, true);
  39. }
  40. return $paymentList;
  41. }
  42. /**
  43. * 创建支付订单
  44. */
  45. public function create(string $paymentID, array $order, PaymentBizEnum $bizType = PaymentBizEnum::ORDER_PAY): PaymentResponse
  46. {
  47. Event::trigger(new BeforePayEvent($order, $paymentID, $bizType));
  48. return $this->getPaymentProvider($paymentID)->create($order, $bizType);
  49. }
  50. /**
  51. * 查询支付订单
  52. */
  53. public function query(string $paymentID, $recordID): PaymentQueryResult
  54. {
  55. return $this->getPaymentProvider($paymentID)->query($recordID);
  56. }
  57. /**
  58. * 退款
  59. */
  60. public function refund(string $paymentID, int $recordID, PaymentRefundRequest $param): PaymentRefundResult
  61. {
  62. return $this->getPaymentProvider($paymentID)->refund($recordID, $param);
  63. }
  64. /**
  65. * 查询退款
  66. */
  67. public function refundQuery(string $paymentID, int $refundID): PaymentRefundResult
  68. {
  69. return $this->getPaymentProvider($paymentID)->refundQuery($refundID);
  70. }
  71. /**
  72. * 获取指定支付方式
  73. */
  74. public function getPayment($paymentID):array
  75. {
  76. return [
  77. // todo
  78. ];
  79. }
  80. /**
  81. * 开启支付方式
  82. */
  83. public function enablePayment($paymentID):bool
  84. {
  85. // todo
  86. return true;
  87. }
  88. /**
  89. * 关闭支付方式
  90. */
  91. public function disablePayment($paymentID):bool
  92. {
  93. // todo
  94. return true;
  95. }
  96. public function getPaymentProvider(string $paymentID): PaymentProviderInterface
  97. {
  98. $extension = $this->extensionManager->getExtension($paymentID);
  99. return $extension->getPaymentProvider();
  100. }
  101. }