|
@@ -0,0 +1,104 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+declare(strict_types=1);
|
|
|
|
|
+
|
|
|
|
|
+namespace SixShop\Balpay;
|
|
|
|
|
+
|
|
|
|
|
+use app\model\User;
|
|
|
|
|
+use SixShop\Core\Helper;
|
|
|
|
|
+use SixShop\Balpay\Entity\ExtensionBalpayLogEntity;
|
|
|
|
|
+use SixShop\Balpay\Enum\BalpayLogTypeEnum;
|
|
|
|
|
+use SixShop\Payment\Contracts\PaymentNotifyResult;
|
|
|
|
|
+use SixShop\Payment\Contracts\PaymentProviderInterface;
|
|
|
|
|
+use SixShop\Payment\Contracts\PaymentQueryResult;
|
|
|
|
|
+use SixShop\Payment\Contracts\PaymentRefundQueryResult;
|
|
|
|
|
+use SixShop\Payment\Contracts\PaymentRefundResult;
|
|
|
|
|
+use SixShop\Payment\Contracts\PaymentResponse;
|
|
|
|
|
+use SixShop\Payment\Entity\ExtensionPaymentEntity;
|
|
|
|
|
+use SixShop\Payment\Enum\PaymentBizEnum;
|
|
|
|
|
+use SixShop\Payment\Enum\PaymentStatusEnum;
|
|
|
|
|
+use SixShop\Payment\Event\PaymentSuccessEvent;
|
|
|
|
|
+use think\facade\Event;
|
|
|
|
|
+
|
|
|
|
|
+class PaymentProvider implements PaymentProviderInterface
|
|
|
|
|
+{
|
|
|
|
|
+ const string PAYMENT_TYPE = 'balpay';
|
|
|
|
|
+
|
|
|
|
|
+ public function __construct(
|
|
|
|
|
+ private readonly ExtensionBalpayLogEntity $logEntity,
|
|
|
|
|
+ private readonly ExtensionPaymentEntity $extensionPaymentEntity,
|
|
|
|
|
+ )
|
|
|
|
|
+ {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function create(array $order, PaymentBizEnum $bizType): PaymentResponse
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->checkPayPasswordAndBalance($order, $order['params']);
|
|
|
|
|
+ $payment = $this->extensionPaymentEntity->where([
|
|
|
|
|
+ 'order_id' => $order['id'],
|
|
|
|
|
+ 'pay_type' => self::PAYMENT_TYPE,
|
|
|
|
|
+ 'biz_type' => $bizType,
|
|
|
|
|
+ ])->findOrEmpty();
|
|
|
|
|
+ if (!$payment->isEmpty()) {
|
|
|
|
|
+ Helper::throw_logic_exception('订单已支付', status: 'balpay.order_already_paid');
|
|
|
|
|
+ }
|
|
|
|
|
+ $payment->transaction(function () use ($bizType, $order, $payment) {
|
|
|
|
|
+ $payment->save([
|
|
|
|
|
+ 'user_id' => $order['user_id'],
|
|
|
|
|
+ 'order_id' => $order['id'],
|
|
|
|
|
+ 'order_sn' => $order['order_sn'],
|
|
|
|
|
+ 'biz_type' => $bizType,
|
|
|
|
|
+ 'pay_type' => self::PAYMENT_TYPE,
|
|
|
|
|
+ 'amount' => $order['pay_amount'],
|
|
|
|
|
+ 'status' => PaymentStatusEnum::SUCCESS,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ $this->logEntity->add(
|
|
|
|
|
+ $order['user_id'],
|
|
|
|
|
+ (float)$order['pay_amount'],
|
|
|
|
|
+ BalpayLogTypeEnum::CONSUMTION,
|
|
|
|
|
+ '支付订单:' . $order['order_sn'],
|
|
|
|
|
+ $order['id']
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
+ // 余额支付直接成功
|
|
|
|
|
+ Event::trigger(new PaymentSuccessEvent($order['order_sn'], self::PAYMENT_TYPE, $payment->toArray(), $bizType));
|
|
|
|
|
+ return new PaymentResponse($order['order_sn'], self::PAYMENT_TYPE, $payment->toArray());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function notify(array $request): PaymentNotifyResult
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new \Exception('未实现notify方法');
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function query(string $orderNo): PaymentQueryResult
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new \Exception('未实现query方法');
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function refund(array $refund): PaymentRefundResult
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new \Exception('未实现refund方法');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function refundQuery(string $refundNo): PaymentRefundQueryResult
|
|
|
|
|
+ {
|
|
|
|
|
+ throw new \Exception('未实现refundQuery方法');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function checkPayPasswordAndBalance(array $order, array $params): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $password = $params['pay_password'] ?? '';
|
|
|
|
|
+ if (empty($password)) {
|
|
|
|
|
+ Helper::throw_logic_exception('请输入支付密码', status: 'balpay.pay_password_empty');
|
|
|
|
|
+ }
|
|
|
|
|
+ $user = User::find($order['user_id']);
|
|
|
|
|
+ if (empty($user->pay_password)) {
|
|
|
|
|
+ Helper::throw_logic_exception('请先设置支付密码', status: 'balpay.pay_password_not_set');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!password_verify($password, $user->pay_password)) {
|
|
|
|
|
+ Helper::throw_logic_exception('支付密码错误', status: 'balpay.pay_password_error');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|