Jelajahi Sumber

做到微信支付检测订单有效性

shish 5 tahun lalu
induk
melakukan
7f689684e0

+ 73 - 0
app/ghs/controllers/OrderController.php

@@ -82,4 +82,77 @@ class OrderController extends BaseController
         util::complete();
     }
 
+    //获取商城下单要用的微信支付和小程序支付参数 shish 2019.21.3
+    public function actionWxPay()
+    {
+        ini_set('date.timezone', 'Asia/Shanghai');
+        $post = Yii::$app->request->post();
+        $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
+        $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
+        $order = OrderClass::getByOrderSn($orderSn);
+        $orderId = $order['id'];
+
+        //支付前验证订单有效性
+        OrderClass::valid($order,$this->adminId);
+
+        $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
+        $totalFee = $order['actPrice'];
+
+        //小程序使用miniOpenId
+        if (httpUtil::isMiniProgram()) {
+            $openId = $this->user['miniOpenId'];
+        } else {
+            $openId = $this->user['openId'];
+        }
+        $typeList = configDict::getConfig('capitalType');
+        $capitalType = $typeList['xhOrder']['id'];
+        //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
+        $wxPayType = 0;
+        if (httpUtil::isMiniProgram()) {
+            $wxPayType = 1;
+        }
+        $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
+        //订单30分钟后过期
+        $now = time();
+        $expireTime = $now + 1800;
+
+        $wx = Yii::getAlias("@vendor/weixin");
+        require_once($wx . '/lib/WxPay.Api.php');
+        require_once($wx . '/example/WxPay.JsApiPay.php');
+        $input = new \WxPayUnifiedOrder();
+        $input->SetBody($name);
+        $input->SetOut_trade_no($orderSn);
+        $input->SetTotal_fee($totalFee * 100);
+        $input->SetTime_start(date("YmdHis", $now));
+        $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
+        $input->SetTime_expire(date("YmdHis", $expireTime));
+        $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
+        $input->SetTrade_type("JSAPI");
+
+        //花卉宝代为申请的微信支付
+        $merchantExtend = $this->merchantExtend;
+        if ($merchantExtend['wxPayApply'] == 1) {
+            $input->SetSub_openid($openId);
+        } else {
+            $input->SetOpenid($openId);
+        }
+        //小程序使用miniAppId
+        if (httpUtil::isMiniProgram()) {
+            $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
+        }
+
+        $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
+        $tools = new \JsApiPay();
+        $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
+        $newParams = json_decode($jsApiParameters, true);
+        //已请求微信不能修改价格
+        $updateData = [];
+        $updateData['modPrice'] = 0;
+        if (empty($order['deadline'])) {
+            $updateData['deadline'] = $expireTime;
+        }
+        OrderService::updateById($orderId, $updateData);
+        util::success($newParams);
+    }
+
 }

+ 2 - 2
biz-ghs/admin/classes/AdminClass.php

@@ -42,7 +42,7 @@ class AdminClass extends BaseClass
         return $list;
     }
 
-    //取员工详情 shish 2019.12.9
+    //取单个员工详情 shish 2019.12.9
     public static function getAdminById($id)
     {
         $detail = AdminClass::getById($id);
@@ -53,7 +53,7 @@ class AdminClass extends BaseClass
         return current($list);
     }
 
-    //取员工信息 shish 2021.1.19
+    //取多个员工信息 shish 2021.1.19
     public static function getAdminByIds($ids)
     {
         $adminList = self::getByIds($ids, null, 'id');

+ 6 - 0
biz-ghs/order/classes/OrderClass.php

@@ -73,4 +73,10 @@ class OrderClass extends BaseClass
         return $return;
     }
 
+    //根据订单编号查询 shish 2021.19
+    public static function getByOrderSn($orderSn)
+    {
+        return self::getByCondition(['orderSn' => $orderSn]);
+    }
+
 }

+ 0 - 6
biz-ghs/order/services/OrderService.php

@@ -104,12 +104,6 @@ class OrderService extends BaseService
         }
     }
 
-    //根据编号查询 shish 2019.12.14
-    public static function getByOrderSn($orderSn)
-    {
-        return OrderClass::getByOrderSn($orderSn);
-    }
-
     //点评 shish 2019.12.16
     public static function comment($data)
     {