Browse Source

add: 支持微信 APP 支付

joe 4 năm trước cách đây
mục cha
commit
cdabbadc91

+ 44 - 18
app/api/controller/order/StoreOrderController.php

@@ -232,6 +232,9 @@ class StoreOrderController
             $isChannel = 0;
         elseif ($from == 'weixinh5')
             $isChannel = 2;
+        elseif ($from == 'app') {
+            $isChannel = 3;
+        }
         $order = StoreOrder::cacheKeyCreateOrder($request->uid(), $key, $addressId, $payType, (int)$useIntegral, 
             $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, false, $isChannel, 
             $shipping_type, $real_name, $phone, $storeId);
@@ -263,6 +266,8 @@ class StoreOrderController
                                 $jsConfig = OrderRepository::jsPay($orderId); //创建订单jspay
                             } else if ($from == 'weixinh5') {
                                 $jsConfig = OrderRepository::h5Pay($orderId);
+                            } else if ($from == 'app') {
+                                $jsConfig = OrderRepository::wxAppPay($orderId);
                             } else {
                                 $jsConfig = OrderRepository::wxPay($orderId);
                             }
@@ -270,11 +275,14 @@ class StoreOrderController
                             return app('json')->status('pay_error', $e->getMessage(), $info);
                         }
                         $info['jsConfig'] = $jsConfig;
-                        if ($from == 'weixinh5') {
-                            return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
-                        } else {
-                            return app('json')->status('wechat_pay', '订单创建成功', $info);
-                        }
+                        return app('json')->status($payType . '_' . $from, '订单创建成功', $info);
+                        // if ($from == 'weixinh5') {
+                        //     return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
+                        // } elseif ($from == 'app') {
+                        //     return app('json')->status('wechat_app_pay', '订单创建成功', $info);
+                        // } else {
+                        //     return app('json')->status('wechat_pay', '订单创建成功', $info);
+                        // }
                     }
                     break;
                 case 'yue':
@@ -416,6 +424,9 @@ class StoreOrderController
             $isChannel = 0;
         elseif ($from == 'weixinh5')
             $isChannel = 2;
+        elseif ($from == 'app') {
+            $isChannel = 3;
+        }
         $porderId = StoreOrderBatch::cacheKeyCreateOrderBatch($request->uid(), $key, $addressId, $payType, (int)$useIntegral, 
             $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, false, $isChannel, 
             $shipping_type, $real_name, $phone, $storeId);
@@ -449,6 +460,8 @@ class StoreOrderController
                                 $jsConfig = OrderRepository::jsPayBatch($porderId); //创建订单jspay
                             } else if ($from == 'weixinh5') {
                                 $jsConfig = OrderRepository::h5PayBatch($porderId);
+                            } elseif ($from == 'app') {
+                                $jsConfig = OrderRepository::wxAppPayBatch($porderId);
                             } else {
                                 $jsConfig = OrderRepository::wxPayBatch($porderId);
                             }
@@ -456,11 +469,12 @@ class StoreOrderController
                             return app('json')->status('pay_error', $e->getMessage(), $info);
                         }
                         $info['jsConfig'] = $jsConfig;
-                        if ($from == 'weixinh5') {
-                            return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
-                        } else {
-                            return app('json')->status('wechat_pay', '订单创建成功', $info);
-                        }
+                        return app('json')->status($payType . '_' . $from, '订单创建成功', $info);
+                        // if ($from == 'weixinh5') {
+                        //     return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
+                        // } else {
+                        //     return app('json')->status('wechat_pay', '订单创建成功', $info);
+                        // }
                     }
                     break;
                 case 'yue':
@@ -539,17 +553,22 @@ class StoreOrderController
             }
         }
         if ($from == 'weixin') {//0
-            if (in_array($order->is_channel, [1, 2]))
+            if (in_array($order->is_channel, [1, 2, 3]))
                 $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
         }
         if ($from == 'weixinh5') {//2
-            if (in_array($order->is_channel, [0, 1]))
+            if (in_array($order->is_channel, [0, 1, 3]))
                 $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
         }
         if ($from == 'routine') {//1
-            if (in_array($order->is_channel, [0, 2]))
+            if (in_array($order->is_channel, [0, 2, 3]))
                 $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
         }
+        if ($from == 'app') {
+            if (in_array($order->is_channel, [0, 1, 2])) {
+                $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
+            }
+        }
 
         $order['pay_type'] = $paytype; //重新支付选择支付方式
         switch ($order['pay_type']) {
@@ -559,17 +578,22 @@ class StoreOrderController
                         $jsConfig = OrderRepository::jsPay($order); //订单列表发起支付
                     } else if ($from == 'weixinh5') {
                         $jsConfig = OrderRepository::h5Pay($order);
+                    } else if ($from == 'app') {
+                        $jsConfig = OrderRepository::wxAppPay($order);
                     } else {
                         $jsConfig = OrderRepository::wxPay($order);
                     }
                 } catch (\Exception $e) {
                     return app('json')->fail($e->getMessage());
                 }
-                if ($from == 'weixinh5') {
-                    return app('json')->status('wechat_h5_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
-                } else {
-                    return app('json')->status('wechat_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
-                }
+
+                return app('json')->status($paytype . '_' . $from, ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
+
+                // if ($from == 'weixinh5') {
+                //     return app('json')->status('wechat_h5_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
+                // } else {
+                //     return app('json')->status('wechat_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
+                // }
                 break;
             case 'yue':
                 if (StoreOrder::yuePay($order['order_id'], $request->uid()))
@@ -586,6 +610,8 @@ class StoreOrderController
                 else
                     return app('json')->status('success', '支付失败');
                 break;
+            case 'alipay':
+                break;
         }
         return app('json')->fail('支付方式错误');
     }

+ 5 - 1
app/api/controller/user/UserRechargeController.php

@@ -97,7 +97,11 @@ class UserRechargeController
                 try {
                     if ($from == 'weixinh5') {
                         $recharge = UserRecharge::wxH5Pay($rechargeOrder);
-                    } else {
+                    } elseif ($from == 'weixinapp') {
+                        $recharge = UserRecharge::wxAppPay($rechargeOrder);
+                    } elseif ($from == 'alipayapp') {
+                        throw new \Exception('unsupported');
+                    } else {    // weixin routine
                         $recharge = UserRecharge::wxPay($rechargeOrder);
                     }
                 } catch (\Exception $e) {

+ 11 - 0
app/models/user/UserRecharge.php

@@ -89,6 +89,17 @@ class UserRecharge extends BaseModel
         return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
     }
 
+    /**
+     * weixin app 支付
+     * @param $orderInfo
+     * @return array|string
+     * @throws \Exception
+     */
+    public static function wxAppPay($orderInfo)
+    {
+        return WechatService::appPay(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
+    }
+
     /**
      * 公众号支付
      * @param $orderInfo

+ 55 - 2
crmeb/repositories/OrderRepository.php

@@ -143,7 +143,7 @@ class OrderRepository
         if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
         $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
         $site_name = sys_config('site_name');
-        if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
+        if (!$bodyContent && !$site_name) exception('缺少支付参数');
         return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
     }
 
@@ -169,9 +169,62 @@ class OrderRepository
         if ($fieldValues['pay_price'] <= 0) exception('该支付无需支付!');
         $bodyContent = StoreOrderBatch::getProductTitleBatch($subOrders);
         $site_name = sys_config('site_name');
-        if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
+        if (!$bodyContent && !$site_name) exception('缺少支付参数');
         return WechatService::paymentPrepare(null, $porderInfo['porder_id'], $porderInfo['total_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
     }
+
+    /**
+     * 微信 app 支付
+     * @param $orderId
+     * @param string $field
+     * @return array|string
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public static function wxAppPay($orderId, $field = 'order_id')
+    {
+        if (is_string($orderId))
+            $orderInfo = StoreOrder::where($field, $orderId)->find();
+        else
+            $orderInfo = $orderId;
+        if (!$orderInfo || !isset($orderInfo['paid'])) {
+            exception('支付订单不存在!');
+        }
+        if ($orderInfo['paid']) exception('订单已支付');
+        if ($orderInfo['pay_price'] <= 0) exception('该订单无需支付');
+        $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
+        $site_name = sys_config('site_name');
+        if (!$bodyContent && !$site_name) exception('缺少支付参数');
+        return WechatService::appPay(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
+    }    
+
+    /**
+     * 微信 app 支付
+     * @param $orderId
+     * @param string $field
+     * @return array|string
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public static function wxAppPayBatch($porderId, $field = 'order_id')
+    {
+        if (is_string($porderId))
+            $porderInfo = StoreOrderBatch::where($field, $porderId)->find();
+        else
+            $porderInfo = $porderId;
+        $subOrders = StoreOrderBatch::getAllSubOrders($porderId);
+        if (!$subOrders) exception('支付订单不存在!');
+        $fieldValues = StoreOrderBatch::sumFields($subOrders, ['paid', 'pay_price']);
+        if ($fieldValues['paid']) exception('支付已支付!');
+        if ($fieldValues['pay_price'] <= 0) exception('该支付无需支付!');
+        $bodyContent = StoreOrderBatch::getProductTitleBatch($subOrders);
+        $site_name = sys_config('site_name');
+        if (!$bodyContent && !$site_name) exception('缺少支付参数');
+        return WechatService::appPay(null, $porderInfo['porder_id'], $porderInfo['total_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
+    }
+
     /**
      * 用户确认收货
      * @param $order

+ 18 - 0
crmeb/services/WechatService.php

@@ -405,6 +405,24 @@ class WechatService
         return self::paymentService()->configForJSSDKPayment($paymentPrepare->prepay_id);
     }
 
+    /**
+     * 获得AppSdk支付参数
+     * @param $openid
+     * @param $out_trade_no
+     * @param $total_fee
+     * @param $attach
+     * @param $body
+     * @param string $detail
+     * @param string $trade_type
+     * @param array $options
+     * @return array|string
+     */
+    public static function appPay($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'APP', $options = [])
+    {
+        $paymentPrepare = self::paymentPrepare($openid, $out_trade_no, $total_fee, $attach, $body, $detail, $trade_type, $options);
+        return self::paymentService()->configForAppPayment($paymentPrepare->prepay_id);
+    }
+
     /**
      * 使用商户订单号退款
      * @param $orderNo