Browse Source

预下单

wufeng 2 years ago
parent
commit
61c20bc390

+ 1 - 1
app/mobile/controller/Common.php

@@ -52,7 +52,7 @@ class Common extends BaseController
         ]);
 
         foreach ($param['list'] as $list){
-            if($val2->check($list)) throw new ValidateException($val2->getError());
+            if(!$val2->check($list)) throw new ValidateException($val2->getError());
         }
 
         return CommonLogic::getPrepayId($param);

+ 46 - 18
app/mobile/logic/CommonLogic.php

@@ -4,7 +4,9 @@ namespace app\mobile\logic;
 
 use app\model\AccountModel;
 use app\model\CommonModel;
+use app\model\GoodModel;
 use app\model\GroupModel;
+use app\model\PayInfoModel;
 use app\model\ServiceModel;
 use app\model\ThemeModel;
 use app\model\VideoModel;
@@ -71,7 +73,6 @@ class CommonLogic extends BaseLogic
     //微信支付预下单
     public static function getPrepayId(array $data = []): json
     {
-
         $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
             ->value('wx_openId', '');
 
@@ -81,33 +82,60 @@ class CommonLogic extends BaseLogic
 
         try {
 
-
             $amount = '0';//支付金额
-            if($data['type'] == CommonModel::$pay_type_service){
+            $num = array_column($data['list'], 'num', 'id');//
+            $date = date('Y-m-d H:i:s');
+
+            if ($data['type'] == CommonModel::$pay_type_service) {
                 //购买服务
-                $rs=ServiceModel::field('id,original_price,activity_price,title,activity_status')
-                    ->whereIn('id',array_column($data['list'],'id'))
-                    ->where(['is_del'=>CommonModel::$del_normal,'status'=>CommonModel::$status_normal])
+                $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
+                    ->whereIn('id', array_column($data['list'], 'id'))
+                    ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
                     ->select()
                     ->toArray();
-                if(empty($rs)) throw new Exception('购买的服务不存在或已下架');
-
-                foreach ($rs as $item){
-
-                    if($item['activity_status'] == ServiceModel::$activity_status_ing) $amount = bcadd($amount,bcmul($rs['activity_price'],$),2);
-
+                if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
 
+                foreach ($rs as $item) {
+                    if ($item['activity_status'] == ServiceModel::$activity_status_ing) $amount = bcadd($amount, bcmul($rs['activity_price'], $num[$item['id']] ?? 0), 2);
+                    else $amount = bcadd($amount, bcmul($rs['original_price'], $num[$item['id']] ?? 0), 2);
                 }
+            } else {
+                //购买商城商品
+                $rs = GoodModel::field('id,price,good_name')
+                    ->whereIn('id', array_column($data['list'], 'id'))
+                    ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
+                    ->select()
+                    ->toArray();
+                if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
 
-
-
+                foreach ($rs as $item) {
+                    $amount = bcadd($amount, bcmul($rs['price'], $num[$item['id']] ?? 0), 2);
+                }
 
             }
 
-
-
-
-
+            $code = make_no('ZF');
+
+            //获取预支付信息
+            $result = WechatLogic::getPrepayId($openId, '', $code, $amount);
+
+            PayInfoModel::create([
+                'code' => $code,
+                'wx_openId' => $openId,
+                'type' => $data['type'],
+                'ids' => json_encode($data['list']),
+                'prepay_info' => $result,
+                'amount' => $amount,
+                '' => '',//流水号
+                'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
+                'status' => PayInfoModel::$pay_wait,
+                'createrid' => self::$aid,
+                'creater' => self::$aname,
+                'addtime' => $date,
+                'updaterid' => self::$aid,
+                'updater' => self::$aname,
+                'updatetime' => $date,
+            ])->save();
 
             Db::commit();
             return json_show(CommonModel::$success, '微信支付预下单成功', $res);

+ 15 - 0
app/mobile/logic/WechatLogic.php

@@ -5,6 +5,7 @@ namespace app\mobile\logic;
 
 //关于微信相关操作的工具类
 use think\exception\ValidateException;
+use think\facade\Config;
 use think\facade\Validate;
 
 class WechatLogic
@@ -46,4 +47,18 @@ class WechatLogic
     }
 
 
+    //获取预支付信息
+    public static function getPrepayId(string $openid = '', string $body = '', string $out_trade_no = '', float $total_fee = 0, string $notify_url = '', string $trade_type = "JSAPI")
+    {
+
+        if($notify_url == '') $notify_url=env('notify_url');
+
+        $pay = &load_wechat('Pay');
+
+        $rs = $pay->getPrepayId($openid, $body, $out_trade_no, bcmul($total_fee, '100'), $notify_url, $trade_type);
+
+        halt('预支付结果:', $rs);
+    }
+
+
 }

+ 4 - 0
app/mobile/route/app.php

@@ -8,6 +8,7 @@ Route::rule('logout', 'Account/logout');//登出
 Route::rule('info', 'Account/info');//账户详情
 Route::rule('updatePassword', 'Account/updatePassword');//修改密码
 Route::rule('bindAccountByCode', 'Account/bindAccountByCode');//通过微信端code绑定账户
+Route::rule('getPrepayId', 'Common/getPrepayId');//微信支付预下单
 
 //【公共】
 Route::rule('video', 'Common/getVideoList');//视频列表
@@ -36,3 +37,6 @@ Route::rule('exchangeOrderInfo', 'Exchange/orderInfo');//兑换订单详情
 Route::rule('shoppingGoodList', 'Shopping/goodList');//商城商品列表
 Route::rule('shoppingGoodInfo', 'Shopping/goodInfo');//商城商品详情
 
+
+
+

+ 19 - 0
app/model/PayInfoModel.php

@@ -0,0 +1,19 @@
+<?php
+
+
+namespace app\model;
+
+
+use think\Model;
+
+class PayInfoModel extends Model
+{
+    protected $table='fc_pay_info';
+    protected $pk='id';
+
+    //支付状态,0待支付,1支付成功,2支付失败
+    public static $pay_wait=0;
+    public static $pay_success=1;
+    public static $pay_fail=2;
+
+}