| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Payment;
- use SixShop\Payment\Contracts\PaymentProviderInterface;
- use SixShop\Payment\Contracts\PaymentQueryResult;
- use SixShop\Payment\Contracts\PaymentRefundRequest;
- use SixShop\Payment\Contracts\PaymentRefundResult;
- use SixShop\Payment\Contracts\PaymentResponse;
- use SixShop\Payment\Entity\ExtensionPaymentEntity;
- use SixShop\Payment\Entity\ExtensionRefundEntity;
- use SixShop\Payment\Enum\PaymentBizEnum;
- use SixShop\Payment\Event\BeforePayEvent;
- use SixShop\Payment\Event\GatheringPaymentEvent;
- use SixShop\System\ExtensionManager;
- use SixShop\System\Model\ExtensionModel;
- use think\facade\Event;
- readonly class PaymentManager
- {
- public function __construct(
- private ExtensionManager $extensionManager,
- )
- {
- }
- /**
- * 获取所有支付方式
- *
- * @return PaymentInfo[]
- */
- public function getAllPayment():array
- {
- /* @var PaymentInfo[] $paymentList */
- $paymentList = Event::trigger(GatheringPaymentEvent::class);
- $paymentIDs = array_column($paymentList, 'id');
- $statusMap = ExtensionModel::whereIn('id', $paymentIDs)->column(['status','id'], 'id', true);
- $payTypeList = extension_config('payment', 'pay_type')??[];
- foreach ($paymentList as $payment) {
- $payment->status = $statusMap[$payment->id]['status'];
- $payment->enabled = in_array($payment->id, $payTypeList, true);
- }
- return $paymentList;
- }
- /**
- * 创建支付订单
- */
- public function create(string $paymentID, array $order, PaymentBizEnum $bizType = PaymentBizEnum::ORDER_PAY): PaymentResponse
- {
- Event::trigger(new BeforePayEvent($order, $paymentID, $bizType));
- return $this->getPaymentProvider($paymentID)->create($order, $bizType);
- }
-
- /**
- * 查询支付订单
- */
- public function query(string $paymentID, $recordID): PaymentQueryResult
- {
- return $this->getPaymentProvider($paymentID)->query($recordID);
- }
- /**
- * 退款
- */
- public function refund(string $paymentID, int $recordID, PaymentRefundRequest $param): PaymentRefundResult
- {
- return $this->getPaymentProvider($paymentID)->refund($recordID, $param);
- }
-
- /**
- * 查询退款
- */
- public function refundQuery(string $paymentID, int $refundID): PaymentRefundResult
- {
- return $this->getPaymentProvider($paymentID)->refundQuery($refundID);
- }
-
- /**
- * 获取指定支付方式
- */
- public function getPayment($paymentID):array
- {
- return [
- // todo
- ];
- }
- /**
- * 开启支付方式
- */
- public function enablePayment($paymentID):bool
- {
- // todo
- return true;
- }
- /**
- * 关闭支付方式
- */
- public function disablePayment($paymentID):bool
- {
- // todo
- return true;
- }
- public function getPaymentProvider(string $paymentID): PaymentProviderInterface
- {
- $extension = $this->extensionManager->getExtension($paymentID);
- return $extension->getPaymentProvider();
- }
- }
|