whitefang пре 1 година
родитељ
комит
58a5b56016

+ 12 - 0
app/common/logic/PayNotifyLogic.php

@@ -18,6 +18,7 @@ use app\common\enum\PayEnum;
 use app\common\enum\user\AccountLogEnum;
 use app\common\enum\user\AccountLogEnum;
 use app\common\enum\WorkEnum;
 use app\common\enum\WorkEnum;
 use app\common\model\recharge\RechargeOrder;
 use app\common\model\recharge\RechargeOrder;
+use app\common\model\shops\ShopOrders;
 use app\common\model\user\User;
 use app\common\model\user\User;
 use app\common\model\works\ServiceWork;
 use app\common\model\works\ServiceWork;
 use think\facade\Db;
 use think\facade\Db;
@@ -127,5 +128,16 @@ class PayNotifyLogic extends BaseLogic
         }
         }
     }
     }
 
 
+    public static function shop_goods($orderSn, array $extra = [])
+    {
+        $order = ShopOrders::where('sn', $orderSn)->findOrEmpty();
+        // 更新充值订单状态
+        $order->transaction_id = $extra['transaction_id'] ?? '';
+        $order->pay_status = PayEnum::ISPAID;
+        $order->paid_amount = $order->order_amount;
+        $order->pay_time = time();
+        $order->save();
+    }
+
 
 
 }
 }

+ 74 - 2
app/common/logic/PaymentLogic.php

@@ -20,6 +20,7 @@ use app\common\enum\YesNoEnum;
 use app\common\model\effective\EffectiveCategory;
 use app\common\model\effective\EffectiveCategory;
 use app\common\model\pay\PayWay;
 use app\common\model\pay\PayWay;
 use app\common\model\recharge\RechargeOrder;
 use app\common\model\recharge\RechargeOrder;
+use app\common\model\shops\ShopOrders;
 use app\common\model\user\User;
 use app\common\model\user\User;
 use app\common\service\pay\AliPayService;
 use app\common\service\pay\AliPayService;
 use app\common\service\pay\WeChatPayService;
 use app\common\service\pay\WeChatPayService;
@@ -138,8 +139,6 @@ class PaymentLogic extends BaseLogic
      * @notes 获取预支付订单信息
      * @notes 获取预支付订单信息
      * @param $params
      * @param $params
      * @return RechargeOrder|array|false|\think\Model
      * @return RechargeOrder|array|false|\think\Model
-     * @author 段誉
-     * @date 2023/2/27 15:19
      */
      */
     public static function getPayOrderInfo($params)
     public static function getPayOrderInfo($params)
     {
     {
@@ -167,6 +166,30 @@ class PaymentLogic extends BaseLogic
         }
         }
     }
     }
 
 
+    /**
+     * @notes 获取电子商城预支付订单信息
+     * @param $params
+     * @return RechargeOrder|array|false|\think\Model
+     */
+    public static function getPayShopOrderInfo($params)
+    {
+        try {
+            $order = ShopOrders::findOrEmpty($params['order_id']);
+            if ($order->isEmpty()) {
+                throw new Exception('订单不存在');
+            }
+            if ($order['pay_status'] == PayEnum::ISPAID) {
+                throw new Exception('订单已支付');
+            }
+            return $order;
+        } catch (\Exception $e) {
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+
     /**
     /**
      * @notes 支付
      * @notes 支付
      * @param $payWay
      * @param $payWay
@@ -288,6 +311,55 @@ class PaymentLogic extends BaseLogic
         return $result;
         return $result;
     }
     }
 
 
+    /**
+     * @notes 电子商城支付
+     * @param $payWay
+     * @param $from
+     * @param $order
+     * @param $terminal
+     * @param $redirectUrl
+     * @return array|false|mixed|string|string[]
+     * @throws \Exception
+     */
+    public static function shopPay($payWay, $from, $order, $terminal, $redirectUrl)
+    {
+        // 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
+        $paySn = $order['sn'];
+        if ($payWay == PayEnum::WECHAT_PAY) {
+            $paySn = self::formatOrderSn($order['sn'], $terminal);
+        }
+        //更新支付方式
+        switch ($from) {
+            case 'shop_goods':
+                ShopOrders::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
+                break;
+        }
+
+        if ($order['order_amount'] == 0) {
+            PayNotifyLogic::handle($from, $order['sn']);
+            return ['sn' => $order['sn'],'need_pay'=>0];
+        }
+
+        $payService = null;
+        switch ($payWay) {
+            case PayEnum::WECHAT_PAY:
+                $payService = (new WorkerWeChatPayService($terminal, $userInfo['user_id'] ?? null));
+                $order['pay_sn'] = $paySn;
+                $order['redirect_url'] = $redirectUrl;
+                $result = $payService->pay($from, $order);
+                break;
+            default:
+                self::$error = '订单异常';
+                $result = false;
+        }
+
+        if (false === $result && !self::hasError()) {
+            self::setError($payService->getError());
+        }
+//        $result['need_pay'] = 1;
+        return $result;
+    }
+
     /**
     /**
      * @notes 设置订单号 支付回调时截取前面的单号 18个
      * @notes 设置订单号 支付回调时截取前面的单号 18个
      * @param $orderSn
      * @param $orderSn

+ 6 - 0
app/common/service/pay/WorkerWeChatPayService.php

@@ -74,6 +74,7 @@ class WorkerWeChatPayService extends BasePayService
     public function pay($from, $order)
     public function pay($from, $order)
     {
     {
         try {
         try {
+
             switch ($this->terminal) {
             switch ($this->terminal) {
                 case UserTerminalEnum::WECHAT_MMP:
                 case UserTerminalEnum::WECHAT_MMP:
                     $config = WorkerWeChatConfigService::getMnpConfig();
                     $config = WorkerWeChatConfigService::getMnpConfig();
@@ -107,6 +108,7 @@ class WorkerWeChatPayService extends BasePayService
      */
      */
     public function jsapiPay($from, $order, $appId)
     public function jsapiPay($from, $order, $appId)
     {
     {
+
         $response = $this->app->getClient()->postJson("v3/pay/transactions/jsapi", [
         $response = $this->app->getClient()->postJson("v3/pay/transactions/jsapi", [
             "appid" => $appId,
             "appid" => $appId,
             "mchid" => $this->config['mch_id'],
             "mchid" => $this->config['mch_id'],
@@ -122,6 +124,7 @@ class WorkerWeChatPayService extends BasePayService
             'attach' => $from
             'attach' => $from
         ]);
         ]);
         $result = $response->toArray(false);
         $result = $response->toArray(false);
+
         $this->checkResultFail($result);
         $this->checkResultFail($result);
         return $this->getPrepayConfig($result['prepay_id'], $appId);
         return $this->getPrepayConfig($result['prepay_id'], $appId);
     }
     }
@@ -204,6 +207,9 @@ class WorkerWeChatPayService extends BasePayService
                     case 'goods':
                     case 'goods':
                         PayNotifyLogic::handle('goods', $message['out_trade_no'], $extra);
                         PayNotifyLogic::handle('goods', $message['out_trade_no'], $extra);
                         break;
                         break;
+                    case 'shop_goods':
+                        PayNotifyLogic::handle('shop_goods', $message['out_trade_no'], $extra);
+                        break;
                 }
                 }
             }
             }
             return true;
             return true;

+ 24 - 0
app/workerapi/controller/shops/OrderController.php

@@ -1,10 +1,12 @@
 <?php
 <?php
 namespace app\workerapi\controller\shops;
 namespace app\workerapi\controller\shops;
 
 
+use app\common\logic\PaymentLogic;
 use app\workerapi\controller\BaseApiController;
 use app\workerapi\controller\BaseApiController;
 use app\workerapi\lists\shops\ShopOrderLists;
 use app\workerapi\lists\shops\ShopOrderLists;
 use app\workerapi\logic\shops\ShopOrderLogic;
 use app\workerapi\logic\shops\ShopOrderLogic;
 use app\workerapi\validate\shops\ShopOrderValidate;
 use app\workerapi\validate\shops\ShopOrderValidate;
+use app\workerapi\validate\shops\ShopPayValidate;
 
 
 /**
 /**
  * 商城订单
  * 商城订单
@@ -57,4 +59,26 @@ class OrderController extends BaseApiController
         }
         }
         return $this->success('取消成功', [], 1, 1);
         return $this->success('取消成功', [], 1, 1);
     }
     }
+
+    /**
+     * @notes 预支付
+     * @return \think\response\Json
+     */
+    public function prepay()
+    {
+        $params = (new ShopPayValidate())->post()->goCheck('pay');
+        //订单信息
+        $order = PaymentLogic::getPayShopOrderInfo($params);
+        if (false === $order) {
+            return $this->fail(PaymentLogic::getError(), $params);
+        }
+        //支付流程
+        $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
+        $result = PaymentLogic::shopPay($params['pay_way'], 'shop_goods', $order, $this->userInfo['terminal'], $redirectUrl);
+        if (false === $result) {
+            return $this->fail(PaymentLogic::getError(), $params);
+        }
+        $result['sn'] = $order['sn'];
+        return $this->success('', $result);
+    }
 }
 }

+ 34 - 0
app/workerapi/validate/shops/ShopPayValidate.php

@@ -0,0 +1,34 @@
+<?php
+namespace app\workerapi\validate\shops;
+
+use app\common\enum\PayEnum;
+use app\common\validate\BaseValidate;
+
+/**
+ * 支付验证
+ * Class ShopPayValidate
+ */
+class ShopPayValidate extends BaseValidate
+{
+    protected $rule = [
+        'pay_way'   => 'require|in:' . PayEnum::BALANCE_PAY . ',' . PayEnum::WECHAT_PAY . ',' . PayEnum::ALI_PAY,
+        'order_id'  => 'require'
+    ];
+
+
+    protected $message = [
+        'pay_way.require'   => '支付方式参数缺失',
+        'pay_way.in'        => '支付方式参数错误',
+        'order_id.require'  => '订单参数缺失'
+    ];
+
+    /**
+     * @notes 支付方式场景
+     * @return ShopPayValidate
+     */
+    public function scenePay()
+    {
+        return $this->only(['pay_way' ,'order_id']);
+    }
+
+}