瀏覽代碼

add - 工单分享支付

liugc 1 年之前
父節點
當前提交
fdc6b6eb35

+ 52 - 0
app/common/logic/PaymentLogic.php

@@ -419,6 +419,58 @@ class PaymentLogic extends BaseLogic
         return $result;
     }
 
+
+    /**
+     * @notes 工单分享支付
+     * @param $payWay
+     * @param $from
+     * @param $order
+     * @param $terminal
+     * @param $redirectUrl
+     * @return array|false|mixed|string|string[]
+     * @throws \Exception
+     * @author mjf
+     * @date 2024/3/18 16:49
+     */
+    public static function workerSharePay($payWay, $from, $order, $userInfo, $redirectUrl)
+    {
+        $terminal = 2;
+        $paySn = self::formatOrderSn($order['sn'], $terminal);
+        RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
+
+        if ($order['order_amount'] == 0) {
+            PayNotifyLogic::handle($from, $order['sn']);
+            return ['sn' => $order['sn'],'need_pay'=>0];
+        }
+
+        $mapSession = MappingMworkerSession::where([['type', '=', 1],['map_token', '=', $userInfo['token']], ['expire_time', '>', time()]])->findOrEmpty();
+        if(!$mapSession->isEmpty()) {
+            $token = '';
+            $mapSession->token && $token = $mapSession->token;
+            $token && $userInfo = (new MasterWokerTokenCache())->getUserInfo($token);
+        }
+
+        $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个
      * @param $orderSn

+ 4 - 4
app/common/service/pay/WorkerWeChatPayService.php

@@ -72,14 +72,14 @@ class WorkerWeChatPayService extends BasePayService
      * @author 段誉
      * @date 2021/8/4 15:05
      */
-    public function pay($from, $order)
+    public function pay($from, $order, $openid = '')
     {
         try {
 
             switch ($this->terminal) {
                 case UserTerminalEnum::WECHAT_MMP:
                     $config = WorkerWeChatConfigService::getMnpConfig();
-                    $result = $this->jsapiPay($from, $order, $config['app_id']);
+                    $result = $this->jsapiPay($from, $order, $config['app_id'], $openid??'');
                     break;
                 default:
                     throw new \Exception('支付方式错误');
@@ -107,7 +107,7 @@ class WorkerWeChatPayService extends BasePayService
      * @author 段誉
      * @date 2023/2/28 12:12
      */
-    public function jsapiPay($from, $order, $appId)
+    public function jsapiPay($from, $order, $appId, $openid = '')
     {
         $response = $this->app->getClient()->postJson("v3/pay/transactions/jsapi", [
             "appid" => $appId,
@@ -119,7 +119,7 @@ class WorkerWeChatPayService extends BasePayService
                 "total" => intval($order['order_amount'] * 100),
             ],
             "payer" => [
-                "openid" => $this->auth['openid']
+                "openid" => $openid?:($this->auth['openid']??'')
             ],
             'attach' => $from
         ]);

+ 24 - 0
app/workerapi/controller/PayController.php

@@ -67,4 +67,28 @@ class PayController extends \app\workerapi\controller\BaseApiController
     {
         return (new WorkerWeChatPayService(UserTerminalEnum::WECHAT_MMP))->notify();
     }
+
+    /**
+     * @notes 工单分享代付
+     * @return \think\response\Json
+     */
+    public function sharePrepay()
+    {
+        $params = (new PayValidate())->post()->goCheck();
+        //订单信息
+        $order = PaymentLogic::getPayOrderInfo($params);
+        if (false === $order) {
+            return $this->fail(PaymentLogic::getError(), $params);
+        }
+        //支付流程
+        $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
+        $result = PaymentLogic::workerSharePay($params['pay_way'], $params['from'], $order, [], $redirectUrl);
+        if (false === $result) {
+            return $this->fail(PaymentLogic::getError(), $params);
+        }
+        $result['sn'] = $order['sn'];
+        return $this->success('', $result);
+    }
+
+
 }