request->post(); $current = time(); //查找还有没待清算的订单 $list = PurchaseClearClass::getAllByCondition(['shopId' => $this->shopId, 'status' => PurchaseClearClass::STATUS_AWAIT_PAY], null, '*', null, true); if (!empty($list)) { foreach ($list as $item) { $deadline = $item->deadline; $deadTime = strtotime($deadline); if ($current > $deadTime) { $item->status = PurchaseClearClass::STATUS_EXPIRE; $item->save(); } else { util::fail('您还有结算订单没有付款'); } } } $post['sjId'] = $this->sjId; $post['shopId'] = $this->shopId; $post['type'] = 1; $respond = PurchaseClearClass::addOrder($post); util::success($respond); } 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], true); if (empty($order)) { util::fail('订单号无效'); } $current = time(); $id = $order->id; $deadline = $order->deadline; if ($current > strtotime($deadline)) { $order->status = PurchaseClearClass::STATUS_EXPIRE; $order->save(); util::fail('订单已经失效,请重新发起结算'); } $name = '采购单结算'; $totalFee = $order->actPrice; //强制使用小程序的miniOpenId $openId = $this->admin['miniOpenId']; $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id'); //小程序支付 $wxPayType = 1; $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType; //订单30分钟后过期 $expireTime = $current + 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", $current)); $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); $newParams['id'] = $id; $newParams['orderSn'] = $orderSn; util::success($newParams); } }