shopAdminId; $addData['shopAdminId'] = $eId; $shopAdmin = $this->shopAdmin->attributes; $adminName = $shopAdmin['name'] ?? ''; $sjId = $shopAdmin['sjId'] ?? 0; $addData['shopAdminName'] = $adminName; $set = SetClass::getByCondition(['style' => SetClass::SET_STYLE_GHS], true, 'id asc'); if (empty($set)) { util::fail('没有找到套餐'); } $price = $set->price; $prePrice = $price; $actPrice = $price; $setId = $set->id; $addData['actPrice'] = $actPrice; $addData['prePrice'] = $prePrice; $addData['sjId'] = $this->sjId; $now = time(); $expireTime = $now + 300;//订单5分钟后过期 $addData['deadline'] = date("Y-m-d H:i:s", $expireTime); $order = RenewService::addOrder($addData); $orderId = $order['id']; $orderSn = $order['orderSn']; $couponId = 0; $name = $set->name . '开通及续费'; $totalFee = $actPrice; $admin = $this->admin->attributes; //小程序使用miniOpenId $openId = $admin['miniOpenId']; $capitalType = dict::getDict('capitalType', 'xhRenew', 'id'); //将流水类型、优惠卷传过去 $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&setId=' . $setId . '&sjId=' . $sjId; $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));//设置订单有效期5分钟 $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/'); $input->SetTrade_type("JSAPI"); //花卉宝代为申请的微信支付 $merchantExtend = WxOpenClass::getWxInfo(); $input->SetOpenid($openId); //小程序使用miniAppId $merchantExtend['wxAppId'] = $merchantExtend['miniAppId']; Yii::info('renew:' . json_encode($merchantExtend)); $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend); $newParams = json_decode($jsApiParameters, true); $newParams['orderId'] = $orderId; $newParams['sjId'] = $this->sjId; util::success($newParams); } /** * 花店邀请注册:创建年度会员 xhRenew 订单并唤起微信支付(注册成功后调用,applyId 可选) */ public function actionCreate() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $applyId = (int) ($post['applyId'] ?? 0); $registerConfig = dict::getDict('hdRegisterConfig'); $prePrice = round((float) ($registerConfig['price'] ?? 0), 2); $hdDiscountAmount = 0; if ($applyId > 0) { $apply = ApplyClass::getById($applyId); if (empty($apply)) { util::fail('申请不存在'); } if ((int) ($apply['adminId'] ?? 0) !== (int) $this->adminId) { util::fail('申请与当前账号不匹配'); } $hdDiscountAmount = round((float) ($apply['hdDiscountAmount'] ?? 0), 2); } $actPrice = round(max(0, $prePrice - $hdDiscountAmount), 2); $shopAdmin = $this->shopAdmin->attributes; $adminName = $shopAdmin['name'] ?? ''; $shop = $this->shop; $shopName = $shop['shopName'] ?? ($shop['name'] ?? ''); $sjName = is_array($this->sj) ? ($this->sj['name'] ?? '') : ($this->sj->name ?? ''); $now = time(); $expireTime = $now + 300; $addData = [ 'shopAdminId' => $this->shopAdminId, 'shopAdminName' => $adminName, 'prePrice' => $prePrice, 'actPrice' => $actPrice, 'sjId' => $this->sjId, 'sjName' => $sjName, 'mainId' => $this->mainId, 'shopId' => $this->shopId, 'shopName' => $shopName, 'deadline' => date('Y-m-d H:i:s', $expireTime), 'status' => 0, 'type' => 1, 'ptStyle' => dict::getDict('ptStyle', 'hd'), 'remark' => $applyId > 0 ? 'applyId=' . $applyId : '', 'beforeDeadline'=>$shop->beforeDeadline ?? '', ]; $order = RenewService::addOrder($addData); $orderId = (int) ($order['id'] ?? 0); $orderSn = (string) ($order['orderSn'] ?? ''); if ($orderId <= 0 || $orderSn === '') { util::fail('创建订单失败'); } if ($actPrice <= 0) { RenewService::completeRegisterRenewPay( dict::getDict('payWay', 'wxPay'), $orderSn, $actPrice, 'capitalType=' . dict::getDict('capitalType', 'xhRenew', 'id') . '&applyId=' . $applyId, '' ); util::success([ 'needPay' => 0, 'paid' => 1, 'applyId' => $applyId, 'orderId' => $orderId, 'actPrice' => $actPrice, ]); } $admin = $this->admin->attributes; $openId = trim((string) ($post['miniOpenId'] ?? ($admin['miniOpenId'] ?? ''))); if ($openId === '') { util::fail('请先授权微信登录'); } $capitalType = dict::getDict('capitalType', 'xhRenew', 'id'); $attach = 'capitalType=' . $capitalType . '&applyId=' . $applyId . '&sjId=' . $this->sjId; $wx = Yii::getAlias('@vendor/weixin'); require_once $wx . '/lib/WxPay.Api.php'; require_once $wx . '/example/WxPay.JsApiPay.php'; $input = new \WxPayUnifiedOrder(); $input->SetBody('花店年度会员'); $input->SetOut_trade_no($orderSn); $input->SetTotal_fee(round($actPrice * 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(); $input->SetOpenid($openId); $merchantExtend['wxAppId'] = $merchantExtend['miniAppId']; $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend); $newParams = json_decode($jsApiParameters, true) ?: []; $newParams['orderId'] = $orderId; $newParams['applyId'] = $applyId; $newParams['needPay'] = 1; $newParams['paid'] = 0; $newParams['actPrice'] = $actPrice; util::success($newParams); } }