shish 5 năm trước cách đây
mục cha
commit
ca8b98a735

+ 77 - 27
app-hd/controllers/PurchaseClearController.php

@@ -3,38 +3,88 @@
 namespace hd\controllers;
 
 use bizHd\purchase\classes\PurchaseClearClass;
+use bizHd\wx\classes\WxOpenClass;
+use common\components\dict;
+use common\components\httpUtil;
 use Yii;
 use common\components\util;
 
 class PurchaseClearController extends BaseController
 {
 
-	//生成结算订单 ssh 2021.1.26
-	public function actionCreateOrder()
-	{
-		$post = Yii::$app->request->post();
-		$ids = $post['id'];
-		$data['merchantId'] = $this->sjId;
-		$data['id'] = $ids;
-		$respond = PurchaseClearClass::addClear($data);
-		util::success($respond);
-	}
-
-	public function actionWxPay()
-	{
-		$string = '{
-    "code": 1,
-    "msg": "操作成功",
-    "data": {
-        "appId": "wx21b7c3ef12082099",
-        "nonceStr": "i7f1e9z6ls42f3342m4j2bngxj6rq08e",
-        "package": "prepay_id=wx042304351179687ee46ee32d8ecf420000",
-        "signType": "MD5",
-        "timeStamp": "1612451097",
-        "paySign": "39FD249CD03B24FDA1755493F09D7C2E"
+    //生成结算订单 ssh 2021.1.26
+    public function actionCreateOrder()
+    {
+        $post = Yii::$app->request->post();
+        $ids = $post['id'];
+        $data['merchantId'] = $this->sjId;
+        $data['id'] = $ids;
+        $respond = PurchaseClearClass::addClear($data);
+        util::success($respond);
     }
-}';
-		echo $string;
-	}
-	
+
+    public function actionWxPay()
+    {
+        ini_set('date.timezone', 'Asia/Shanghai');
+        $post = Yii::$app->request->post();
+        $couponId = $post['couponId'] ?? 0;
+        $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
+        $order = PurchaseClearClass::getByCondition(['orderSn' => $orderSn]);
+        if (empty($order)) {
+            util::fail('订单号无效');
+        }
+        $id = $order['id'];
+
+        $name = $order['orderName'] ?? '购买商品';
+        $totalFee = $order['actPrice'];
+
+        //强制使用小程序的miniOpenId
+        $openId = $this->admin['miniOpenId'];
+
+        $typeList = dict::getConfig('capitalType');
+        $capitalType = $typeList['xhGhsOrder']['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['hdHost'] . '/notice/wx-callback/');
+        $input->SetTrade_type("JSAPI");
+
+        //花卉宝代为申请的微信支付
+        $merchantExtend = WxOpenClass::getWxInfo();
+        if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
+            $input->SetSub_openid($openId);
+        } else {
+            $input->SetOpenid($openId);
+        }
+
+        //强制使用小程序的miniAppId
+        $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
+
+        $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
+        $tools = new \JsApiPay();
+        $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
+        $newParams = json_decode($jsApiParameters, true);
+        $current = date("Y-m-d H:i:s");
+        $updateData = ['deadline' => $current, 'changePrice' => 2];
+        PurchaseClearClass::updateById($id, $updateData);
+        util::success($newParams);
+    }
+
 }

+ 14 - 8
biz-hd/purchase/classes/PurchaseClearClass.php

@@ -3,15 +3,21 @@
 namespace bizHd\purchase\classes;
 
 use bizHd\base\classes\BaseClass;
+use common\components\orderSn;
 
 class PurchaseClearClass extends BaseClass
 {
-	
-	public static $baseFile = '\bizHd\purchase\models\PurchaseClear';
-	
-	public static function addClear($data)
-	{
-		return ['orderSn' => 'PC_CS154836', 'actPrice' => 0.01];
-	}
-	
+
+    public static $baseFile = '\bizHd\purchase\models\PurchaseClear';
+
+    public static function addClear($data)
+    {
+        $orderSn = orderSn::getPurchaseClearSn();
+        $data['actPrice'] = 0.01;
+        $data['orderSn'] = $orderSn;
+        $data['purchaseIds'] = $data['id'];
+        unset($data['id']);
+        return self::add($data);
+    }
+
 }

+ 2 - 0
sql.sql

@@ -2016,6 +2016,8 @@ ADD COLUMN `smallUnit`  smallint(6) NOT NULL DEFAULT 0 COMMENT '小单位' AFTER
 ====== shish 2021-4-9
 ALTER TABLE xhCustom DROP COLUMN `totalBuyNum`;
 ALTER TABLE `xhUser` ADD recentShop VARCHAR(800) NOT NULL DEFAULT '' COMMENT '最近访问的店铺' AFTER `visitTime`;
+ALTER TABLE xhPurchaseClear ADD `deadline` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '订单失效时间' AFTER `purchaseIds`;
+ALTER TABLE xhPurchaseClear ADD changePrice TINYINT NOT NULL DEFAULT 1 COMMENT '1可以修改价格 2不能修改价格,微信发起支付后不能修改' AFTER `deadline`;
 
 ========
 ==linqh 2021-4-9