Просмотр исходного кода

add - 独立工单支付接口

liugc 8 месяцев назад
Родитель
Сommit
5b48cd7d30

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

@@ -136,4 +136,28 @@ class PayController extends BaseApiController
         }
     }
 
+    /**
+     * @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::userSharePay($params['pay_way'], $params['from'], $order, ['openid'=>$params['openid']??''], $redirectUrl);
+        if (false === $result) {
+            return $this->fail(PaymentLogic::getError(), $params);
+        }
+        $result['sn'] = $order['sn'];
+        return $this->success('', $result);
+    }
+
+
+
 }

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

@@ -462,7 +462,48 @@ class PaymentLogic extends BaseLogic
         $result['need_pay'] = 1;
         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 userSharePay($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];
+        }
 
+        $payService = null;
+        switch ($payWay) {
+            case PayEnum::WECHAT_PAY:
+                $payService = (new WeChatPayService($terminal, null));
+                $order['pay_sn'] = $paySn;
+                $order['redirect_url'] = $redirectUrl;
+                $result = $payService->pay($from, $order,$userInfo['openid']??'');
+                break;
+            default:
+                self::$error = '订单异常';
+                $result = false;
+        }
+
+        if (false === $result && !self::hasError()) {
+            self::setError($payService->getError());
+        }
+        $result['need_pay'] = 1;
+        return $result;
+    }
 
     /**
      * @notes 设置订单号 支付回调时截取前面的单号 18个

+ 8 - 6
app/common/service/pay/WeChatPayService.php

@@ -89,18 +89,18 @@ class WeChatPayService 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 = WeChatConfigService::getMnpConfig();
-                    $result = $this->jsapiPay($from, $order, $config['app_id']);
+                    $result = $this->jsapiPay($from, $order, $config['app_id'], $openid??'');
                     break;
                 case UserTerminalEnum::WECHAT_OA:
                     $config = WeChatConfigService::getOaConfig();
-                    $result = $this->jsapiPay($from, $order, $config['app_id']);
+                    $result = $this->jsapiPay($from, $order, $config['app_id'], $openid??'');
                     break;
                 case UserTerminalEnum::IOS:
                 case UserTerminalEnum::ANDROID:
@@ -141,7 +141,7 @@ class WeChatPayService 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,
@@ -153,7 +153,7 @@ class WeChatPayService extends BasePayService
                 "total" => intval($order['order_amount'] * 100),
             ],
             "payer" => [
-                "openid" => $this->auth['openid']
+                "openid" => $openid?:($this->auth['openid']??'')
             ],
             'attach' => $from
         ]);
@@ -380,7 +380,9 @@ class WeChatPayService extends BasePayService
                 $extra['transaction_id'] = $message['transaction_id'];
                 $attach = $message['attach'];
                 $message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
-                
+                $openid = '';
+                isset($message['payer']) && $message['payer'] && $openid = $message['payer']['openid']??'';
+                $extra['openid'] = $openid;
                 switch ($attach) {
                     case 'recharge':
                         $order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty();