541469799@qq.com преди 1 година
родител
ревизия
a13c60f2e4
променени са 5 файла, в които са добавени 40 реда и са изтрити 16 реда
  1. 2 0
      app/common/enum/order/PayType.php
  2. 6 3
      app/index/controller/Checkout.php
  3. 2 9
      app/index/model/Order.php
  4. 29 1
      app/index/service/Payment.php
  5. 1 3
      app/index/service/User.php

+ 2 - 0
app/common/enum/order/PayType.php

@@ -31,6 +31,8 @@ class PayType extends EnumBasics
     const PAYPAL = 30;
     // stripe支付
     const STRIPE = 40;
+    //积分支付
+    const POINTS = 50;
 
     /**
      * 获取枚举数据

+ 6 - 3
app/index/controller/Checkout.php

@@ -12,7 +12,6 @@ declare (strict_types=1);
 
 namespace app\index\controller;
 
-use app\common\library\Log;
 use app\index\model\Order as OrderModel;
 use app\index\service\order\PaySuccess;
 use app\index\service\User as UserService;
@@ -25,7 +24,6 @@ use cores\exception\BaseException;
 use think\facade\Cache;
 use think\facade\Session;
 use think\response\Json;
-use think\View;
 
 /**
  * 订单结算控制器
@@ -151,7 +149,6 @@ class Checkout extends Controller
         //$cartIds = $this->getCartIds();//不需要接口传过来
         $CartModel = new CartService;
         $cartIds = $CartModel->getCartIds();
-        \think\facade\Log::info('cartsId:'.json_encode($cartIds));
         // 商品结算信息
         // 购物车商品列表
         $goodsList = $CartModel->getOrderGoodsList($cartIds);
@@ -179,6 +176,12 @@ class Checkout extends Controller
         $CartModel->clear($cartIds);
         // 构建微信支付请求
         $payment = $Checkout->onOrderPayment();
+        if ($params['payType'] == OrderPayTypeEnum::POINTS){
+            if (!$payment['flag']){
+                return $this->renderError($payment['message'] ?? '兑换失败');
+            }
+        }
+
         // 返回状态,如果是paypal支付,需要给用户跳转页面之外,还需要轮训查询订单的支付情况,如果支付成功,则弹窗提示,超过一分钟的话,提示支付超时,请重试
         return $this->renderSuccess([
             'orderId' => $Checkout->model['order_id'],   // 订单id

+ 2 - 9
app/index/model/Order.php

@@ -125,18 +125,11 @@ class Order extends OrderModel
         $conf = [
             'client_id' => 'AS0FH780ZGtSAdpT1NTjwkFzryCPf69rZb_FR9Rt_rZdasB80cmjqTQ6CQELWiFVh_MU9e31CSnyz7Ai',     // ClientID
             'secret' => 'EDqRQhgLNHCb5bxld98T8-JJJZKvMIeqxudO7lMwDFOxBfy138PjM5A21FnDNyb3q4yYUh8r7Qr2BnVi',      // ClientSecret
-        'web_hook_id'=>'3NP026061E6858914',
+            'web_hook_id' => '3NP026061E6858914',
         ];
         $pp = new PayPal($conf);
 
-        $pp->unify($order['order_no'],$order['pay_price']);
-
-        return PaymentService::wechat(
-            $order['order_id'],
-            $order['order_no'],
-            $order['pay_price'],
-            OrderTypeEnum::ORDER
-        );
+        return $pp->unify($order['order_no'], $order['pay_price']);
     }
 
     /**

+ 29 - 1
app/index/service/Payment.php

@@ -12,6 +12,8 @@ declare (strict_types=1);
 
 namespace app\index\service;
 
+use app\index\model\User as UserModel;
+use app\index\service\order\PaySuccess;
 use app\index\service\User as UserService;
 use app\index\model\wxapp\Setting as WxappSettingModel;
 use app\common\enum\OrderType as OrderTypeEnum;
@@ -20,6 +22,7 @@ use app\common\library\paypal\PayPal;
 use app\common\service\BaseService;
 use app\common\library\wechat\WxPay;
 use app\common\exception\BaseException;
+use think\facade\Log;
 
 /**
  * 订单支付服务类
@@ -48,12 +51,37 @@ class Payment extends BaseService
                 OrderTypeEnum::ORDER
             );
         }
-
+        //paypal支付
         if ($payType == OrderPayTypeEnum::PAYPAL) {
             $conf = config('paypal');
             $pp = new PayPal($conf);
             return $pp->unify($order['order_no'], $order['pay_price']);
         }
+
+        //积分兑换
+        if ($payType == OrderPayTypeEnum::POINTS) {
+            $userInfo = User::getCurrentLoginUser();
+            $points = $userInfo['points'];
+
+            $payPoints = intval(bcmul(strval($order['pay_price']), '100', 0));//订单所需积分
+            if (intval($points) < $payPoints) {
+                return ['flag' => false, 'message' => '积分不够'];
+            } else {
+                $orderModel = new PaySuccess($order['order_no']);
+                $transId = 'VP' . date('YmdHis') . $userInfo['user_id'];
+
+                $describe = "用户消费:{$order['order_no']}";
+                UserModel::setIncPoints($userInfo['user_id'], -$payPoints, $describe);
+                $status = $orderModel->onPaySuccess(OrderPayTypeEnum::POINTS, ['transaction_id' => $transId]);
+                if ($status) {
+                    return ['flag' => true, 'message' => 'success'];
+                } else {
+                    Log::error('orderPayment:积分兑换error,userId:' . $userInfo['user_id'] . ',orderNo:' . $order['order_no']);
+                    return ['flag' => false, 'message' => 'error'];
+                }
+            }
+        }
+
         //todo stripe
         return [];
     }

+ 1 - 3
app/index/service/User.php

@@ -76,11 +76,9 @@ class User extends BaseService
 
     /**
      * 验证是否已登录
-     * @param bool $isForce 是否强制验证登录, 如果未登录将抛错
      * @return bool
-     * @throws BaseException
      */
-    public static function isLogin()
+    public static function isLogin(): bool
     {
         return !empty(Session::get(self::USER_ID));
     }