[1, 2, 3, 4, COrder::PAY_TYPE_ADAPAY_WX]], // 'WECHAT_PAY', 'HUODAO_PAY', 'BALANCE_PAY' 'ALIPAY', ]; } public function __construct($config = []) { parent::__construct($config); $this->config = [ 'app_id' => Option::get("platform_third_appid",0,'saas')['value'], 'secret' => Option::get("platform_third_secret",0,'saas')['value'], 'token' =>Option::get("platform_token",0,'saas')['value'], 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value'] ]; $this->openPlatform = Factory::openPlatform($this->config); } /** * mini业务 */ public function miniProgram($appid = ""){ $store = StoreMini::find()->where(['or',['id' => $this->id],['appid'=>$appid,'store_id'=>get_store_id()]])->one(); return $this->openPlatform->miniProgram($store->appid,$store->authorizer_refresh_token); } //商户逻辑 end //TODO 当面付逻辑 start public function facePayConfig($total_price = 0){ try { // HKG 新增判断商城打烊状态 开始 if(Option::get(OptionSetting::MALL_STATUS,get_store_id(),'store',0)['value'] == 1) return [ 'code' => 1, 'msg' => '商城已打烊~' ]; // HKG 新增判断商城打烊状态 结束 $info = Store::find()->where(['id' => get_store_id()])->select("id,name,logo")->asArray()->one(); $rule_ids = GiveIntegralRules::find()->where(['and', ['<', 'start_date', time()], ['>', 'end_date', time()], ['status' => 1, 'is_delete' => 0]])->select('id')->column(); $rule_ids = RulesFaceStore::find()->where(['rule_id' => $rule_ids, 'store_id' => get_store_id()])->select('rule_id')->column(); $mini_info = StoreAliMini::find()->where(['store_id' => get_store_id()])->andWhere(['is not', 'license_pic', null]) ->select('license_no, license_pic, app_name, app_slogan, app_desc, service_phone, contact_name, category_name')->asArray()->one(); if (empty($mini_info)) { $mini_info = [ 'license_no' => md5(123), 'app_name' => Option::get('sp_name', '0', 'saas')['value'], 'app_slogan' => "小程序新版本更新", 'app_desc' => "小程序新版本更新,优化用户操作体验", 'service_phone' => "暂无", 'contact_name' => "暂无", 'category_name' => "购物_零售电商_综合零售商品;" ]; } $combine_pay = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge', 0)['value']; if ($rule_ids || $combine_pay) { if ((int)get_user_id() === 0) { return [ 'code' => 0, 'msg' => "请重新登陆", 'data' => [ 'reLogin' => 1, 'store_info' => $info, 'mini_info' => $mini_info ] ]; } } $user = null; if (get_user()) { $user = []; $user['id'] = get_user()->id; $user['money'] = get_user()->money; } $query = Recharge::find(); $recharge = $query->where(['is_delete' => Recharge::IS_DELETE_NO, 'store_id' => get_store_id()]) ->orderBy("sort desc,id desc")->asArray()->all(); $store = Store::findOne($info['id']); // $send_profit_model = Option::findOne(['name' => 'store_send_profit']);//平台让利比例 $send_model= Option::findOne(['name' => 'goods_send_profit','store_id' => get_store_id()]); $take_model= Option::findOne(['name' => 'goods_take_price','store_id' => get_store_id()]); // $this_store_send_profit_model= Option::findOne(['name' => 'store_this_profit','store_id' => get_store_id()]); // if($this_store_send_profit_model->value > 0){ // $basic_send_profit = $this_store_send_profit_model->value; // }else{ // $basic_send_profit = $send_profit_model->value; // } $new_send_profit = floatval($send_model->value * 0.01); $new_take_profit = floatval($take_model->value * 0.01); $user = User::findOne($user['id']); $saas_user = SaasUser::findOne(['mobile' => $user->binding]); // $store_league_model = Store::findOne(get_store_id()); $show_saas_alliance_coupon = true; if (\Yii::$app->prod_is_dandianpu()) { $show_saas_alliance_coupon = false; } $coupon_list = $this->getCouponList($total_price); $use_balance = (int)Option::get('recharge_wallet_status', get_store_id(), 'recharge', 0)['value']; return [ 'code'=>0, 'msg'=>"商户信息获取成功", 'data'=>[ 'send_profit_model' => 100, 'send_model' =>$send_model->value, 'take_model' =>$take_model->value, 'user_price' => $saas_user->league_price, 'store_price' => $store->league_price, 'new_send_profit' => $new_send_profit, 'new_take_profit' => $new_take_profit, 'take_price' => $take_model->value, 'send_profit' => $send_model->value, 'store_info'=>$info, 'mini_info' => $mini_info, 'user' => $user, 'recharge' => $recharge, 'coupon_list' => $coupon_list, 'show_saas_alliance_coupon' => $show_saas_alliance_coupon, 'reLogin' => (int)((int)get_user_id() === 0), 'use_balance' => $use_balance, ] ]; }catch (\Exception $e){ return [ 'code'=>1, 'msg'=>$e->getMessage() ]; } } protected function getCouponList($total_price = 1100001) { // $total_price = 1100001 ;//$mch['total_price']; $list = UserCoupon::find()->alias('uc') ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id') ->where([ 'AND', ['uc.is_delete' => 0], ['uc.is_use' => 0], ['c.mch_id' => 0], ['uc.is_expire' => 0], ['uc.user_id' => get_user_id()], // ['<=', 'c.min_price', $total_price], ]) ->select('c.name, c.expire_day, c.id coupon_id, c.expire_type, c.discount_type, c.discount, c.discount_max_price, uc.id user_coupon_id,c.mch_id,c.rule,c.sub_price,c.min_price,uc.begin_time,uc.end_time,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list') ->asArray()->all(); $new_list = []; foreach ($list as $i => $item) { if ($item['expire_type'] == 1) { if ($item['end_time'] > 0) { if (time() > $item['end_time']) { continue; } } } if ($item['expire_type'] == 2) { if (time() > $item['end_time'] || time() < $item['begin_time']) { continue; } } if (isset($item['is_use']) && $item['is_use']) { continue; } if (isset($item['is_expire']) && $item['is_expire']) { continue; } if ($total_price < $item['min_price']) { continue; } $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用'; $list[$i]['begin_time'] = date('Y.m.d H:i', (int)$item['begin_time']); $list[$i]['end_time'] = date('Y.m.d H:i', (int)$item['end_time']); $list[$i]['discount_type'] = $item['discount_type']; $list[$i]['discount'] = $item['discount']; $list[$i]['min_price'] = doubleval($item['min_price']); if ($list[$i]['discount_type'] == 1) { $list[$i]['sub_price'] = round($total_price - doubleval($total_price * $list[$i]['discount'] / 10), 2); if($item['discount_max_price'] > 0 && $list[$i]['sub_price'] > $item['discount_max_price']){ $list[$i]['sub_price'] = $item['discount_max_price']; } } else { $list[$i]['sub_price'] = doubleval($item['sub_price']); } $new_list[] = $list[$i]; } return $new_list; } public function submitOrder($pay_type = 1){ $t = \Yii::$app->db->beginTransaction(); try { $pay_fee = $this->pay_fee; $combine_pay = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge', 0)['value']; $is_combine_pay = 0; $combine_money = 0; $user = User::findOne(get_user_id()); $recharge_id = $this->recharge_id; $is_balance = $this->is_balance; //使用余额抵扣 $is_finish = 0; $user_coupon_id = 0; $sub_price = 0; if ($this->coupon_id) { // $order->user_coupon_id = $this->coupon_id; $userCoupon = UserCoupon::findOne($this->coupon_id); $coupon = Coupon::findOne($userCoupon->coupon_id); if ($coupon) { if ($pay_fee > $coupon->min_price) { if ($coupon->discount_type == 1) { $sub_price = round($pay_fee - doubleval($pay_fee * $coupon->discount / 10), 2); if($coupon['discount_max_price'] > 0 && $sub_price > $coupon['discount_max_price']){ $sub_price = $coupon['discount_max_price']; } } else { $sub_price = doubleval($coupon->sub_price); } $user_coupon_id = $this->coupon_id; $pay_fee -= $sub_price; $this->is_use_coupon = 1; } } // $order->coupon_sub_price = $sub_price; // $order->pay_price -= $sub_price; // UserCoupon::updateAll(['is_use' => 1], ['id' => $this->coupon_id]); } //计算联盟券抵扣的钱 和赠送的联盟券 $send_model = Option::findOne(['name' => 'goods_send_profit', 'store_id' => get_store_id()]); $take_model = Option::findOne(['name' => 'goods_take_price', 'store_id' => get_store_id()]); $new_send_profit = bcmul($send_model->value, 0.01, 2); $new_take_profit = bcmul($take_model->value, 0.01, 2); $take_price = 0; $send_price = 0; //计算联盟券赠送的金额 $store = Store::findOne(get_store_id()); if ($store && $store->league_price) { // $pay_fee = bcmul($pay_fee, $new_send_profit, 2); $send_price = bcmul($pay_fee, $new_send_profit, 2); if ($send_price > $store->league_price) { $send_price = 0; } } //计算联盟券抵扣的金额 $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]); if ($saas_user) { $take_price = bcmul($pay_fee, $new_take_profit, 2); if ($take_price > $saas_user->league_price) { $take_price = 0; if ($saas_user->league_price > 0) { $take_price = $saas_user->league_price; } } $pay_fee = bcsub($pay_fee, $take_price, 2); } if ($combine_pay && $is_balance) { $is_combine_pay = 1; $money = $user->money; if (($money - $pay_fee) >= 0) { $combine_money = $pay_fee; $pay_fee = 0; } else { $pay_fee = number_format(($pay_fee - $money), 2, '.', ''); $combine_money = $money; $is_finish = 1; } } $md_id = $this->md_id; if ($md_id <= 0) { $md_id = 0; } $user_id = $this->user_id; $mch_id = $this->mch_id ?: 0; $order = new Order(); $order->store_id = get_store_id(); $order->md_id = $md_id; $order->saas_id = get_saas_user_id(); $order->user_id = ($user_id ?: get_user_id()); $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SCAN_CODE_PAY); $order->pay_price = $pay_fee ?: 0; $order->send_price = $send_price; $order->take_price = $take_price; $order->created_at = time(); $order->first_price = 0; $order->second_price = 0; $order->third_price = 0; $order->content = ""; $order->is_platform = 0; $order->is_offline = 1; $order->version = cyy_version(); $order->mch_id = $mch_id; $order->discount = 10; // 当面付订单不享受会员折扣 $order->remark = '' ; $order->pay_type = $pay_type; $order->is_pay = ($pay_fee == 0 ? 1 : 0); $order->total_price = $this->pay_fee ?: 0; $order->express_price = 0; $order->is_combine_pay = $is_combine_pay; $order->combine_money = $combine_money; if ($is_balance && $pay_fee == 0) { $order->trade_status = Order::ORDER_FLOW_CONFIRM; } if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($order->store_id)) { $order->is_use_platform_mch = 1; } if ($user_coupon_id > 0) { $order->user_coupon_id = $user_coupon_id; $order->coupon_sub_price = $sub_price; } $res = $order->save(); $this->order = $order; if($res){ //使用充值方案 if ($recharge_id) { $recharge = Recharge::findOne(['id' => $recharge_id, 'is_delete' => 0, 'store_id' => get_store_id()]); $pay_price = $recharge->pay_price; $all_price = $recharge->pay_price + $recharge->send_price; /** * 如果为余额支付 * 判断充值金额、赠送金额和用户余额是否大于支付金额 * 如果不为余额支付 * 判断充值金额、赠送金额是否大于支付金额 * 如果选择的充值金额依旧小于应支付的金额,则这时应该使用应支付的金额进行微信支付 * 充值后将选择的充值条件增加记录并增加支付记录 */ if ($is_balance) { $all_price += doubleval(sprintf('%.2f', $user->money)); } if ($all_price < $this->pay_fee) { return [ 'code' => 1, 'msg' => '当前所选充值方案不能满足支付需求' ]; $pay_price = $this->pay_fee; } if ($recharge) { $ReOrder = new ReOrder(); $ReOrder->store_id = get_store_id(); $ReOrder->user_id = ($user_id ?: get_user_id()); $ReOrder->pay_price = $recharge->pay_price; $ReOrder->send_price = $recharge->send_price; $ReOrder->send_integral = $recharge->send_integral; $ReOrder->order_no = OrderNo::getOrderNo(OrderNo::ORDER_RECHARGE); $ReOrder->is_pay = 0; $ReOrder->pay_type = $pay_type; $ReOrder->is_delete = 0; $ReOrder->created_at = time(); $ReOrder->type = 0; $ReOrder->order_id = $order->id; if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($ReOrder->store_id)) { $ReOrder->is_use_platform_mch = 1; } if ($ReOrder->save()) { $this->order = $ReOrder; $t->commit(); return [ 'code'=>0, 'data'=>[ 'order_id'=>$ReOrder->id, 'add_time'=>time(), 'order_no'=>$ReOrder->order_no, 'pay_fee' => $pay_price, 'is_recharge' => 1, 'is_finish' => $is_finish ] ]; } else { throw new \Exception("创建订单失败" . json_encode($ReOrder->errors)); } } else { throw new \Exception("充值方案查找失败"); } } else { if ($pay_fee == 0) { $before = $user->money; $user->money -= $combine_money; if ($user->save()) { $log = new AccountLog(); $log->store_id = $order->store_id; $log->user_id = $user->id; $log->type = AccountLog::TYPE_BALANCE; $log->log_type = AccountLog::LOG_TYPE_EXPEND; $log->amount = floatval($combine_money); $log->desc = "当面付在线支付,订单编号为" . $order->order_no; $log->before = $before; $log->after = $user->money; $log->operator = ''; $log->operator_id = 0; $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $log->created_at = time(); $log->order_id = $order->id; $log->order_type = AccountLog::TYPE_SCAN_CODE_PAY; if (!$log->save()) { throw new \Exception("创建余额记录失败" . json_encode($log->errors)); }; } } $t->commit(); return [ 'code'=>0, 'data'=>[ 'order_id'=>$order->id, 'add_time'=>time(), 'order_no'=>$order->order_no, 'pay_fee' => $pay_fee, 'is_recharge' => 0, 'is_finish' => $is_finish ] ]; } }else{ throw new \Exception("创建订单失败" . json_encode($order->errors)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function wx_b2b_pay($session_key, $pay_type, $config){ $result = \app\modules\admin\models\UserAuditForm::pay($session_key, $this->order, OrderNo::ORDER_SCAN_CODE_PAY, $config['body']); return $result; } public function adapay($pay_type, $config){ $result = Adapay::create($pay_type, $this->order, OrderNo::ORDER_SCAN_CODE_PAY, $config['body']); return $result; } public function facePay(){ if($this->type == "weixin"){ //获取商户配置信息 $wechat_config = WechatConfig::find()->where(['store_id' => get_store_id(), 'type' => 1])->select(['mch_id','app_id'])->asArray()->one(); $audit_status = Option::get('one_store_is_shenhe', 0, 'saas', 0)['value']; // if (\Yii::$app->prod_is_dandianpu() && $audit_status) { // $wechat_config = WechatConfig::find()->where(['type' => 1]) // ->andWhere(['AND', ['<>', 'mch_id', ''], ['<>', 'mch_id', 0], ['is not', 'mch_id', null]]) // ->select(['mch_id','app_id'])->asArray()->one(); // } $open_id = ''; $user = get_user(); if (!$user) { if (is_isv()) { //小程序登陆获取openid $miniProgram = $this->miniProgram($wechat_config['app_id']); $res = $miniProgram->auth->session($this->code); } else { $wechat = isset(\Yii::$app->controller->wechat) ? \Yii::$app->controller->wechat : null; if (!$wechat) { return [ 'code' => 1, 'msg' => '登录异常' ]; } $res = $wechat->auth->session($this->code); //小程序登陆获取openid // $res = \Yii::$app->controller->wechatMini->auth->session($this->code); } $open_id = $res['openid']; if (!isset($open_id)) { return [ 'code' => 1, 'msg' => 'openId错误' ]; } $user = User::find()->where(['wechat_open_id' => $open_id, 'store_id' => get_store_id()])->select('id')->one(); } if ($user) { $this->user_id = $user->id; $open_id = $user->wechat_open_id; if (is_h5()) { $open_id = $user->wechat_platform_open_id; } } $res = $this->submitOrder($this->pay_type); if($res['code'] === 0){ $data = $res['data']; $data['pay_fee'] = round($data['pay_fee'],2); if ($data['pay_fee'] == 0) { if (get_user_id() > 0) { $form = new \app\modules\alliance\models\NewMerchantForm(); $form->store_id = get_store_id(); $form->saasGiveIntegral($this->pay_fee, get_user_id(), $data['order_no']); if ($this->is_use_coupon) { UserCoupon::updateAll(['is_use' => 1], ['id' => $this->coupon_id]); } } $this->order_no = $data['order_no']; $this->payResult(); return [ 'code' => 0, 'data' => [ 'is_pay' => 1, 'order_no'=>$data['order_no'], ] ]; } }else{ return $res; } $config = [ 'body' => '当面付', 'out_trade_no' => $data['order_no'], 'total_fee' => $data['pay_fee'] * 100, 'notify_url' => pay_notify_url(self::$notify_url), 'trade_type' => 'JSAPI', ]; if (in_array($this->pay_type, [COrder::PAY_TYPE_WX_B2B])) { $res = $this->wx_b2b_pay($session_key, $this->pay_type, $config); debug_log('支付数据' . json_encode($res)); return $res; } if (in_array($this->pay_type, [COrder::PAY_TYPE_ADAPAY_WX, COrder::PAY_TYPE_ADAPAY_ALIPAY])) { $res = $this->adapay($this->pay_type, $config); if ($res['code'] != 0) { debug_log('支付数据' . json_encode($res)); return $res; } $res['data'] = array_merge($res['data'], [ 'order_no'=>$data['order_no'], 'is_pay' => 0, 'is_finish' => 1 ]); debug_log('支付数据' . json_encode($res)); return $res; } $is_h5 = false; if (is_h5()) { $is_h5 = true; } if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming(get_store_id()) && self_mini() === false) { $res = WechatPay::orderUnify($this->order,OrderNo::ORDER_SCAN_CODE_PAY, $config['body'], '0', false, '0', false, $is_h5, $openid ?? ''); if ($res['res']['return_code'] === 'SUCCESS' && $res['res']['result_code'] === 'SUCCESS' && isset($res['data'])) { $res['data'] = array_merge($res['data'], [ 'order_no'=>$data['order_no'], 'is_pay' => 0, 'is_finish' => 1 ]); } debug_log('支付数据' . json_encode($res)); return $res; } else { // 判断是否走服务商支付 if (is_profit_pay()) { $result = WechatNewPay::orderUnify($this->order,OrderNo::ORDER_SCAN_CODE_PAY, $config['body'], 0, false, 0, false, $is_h5, $open_id); } else { $result = WechatPay::orderUnify($this->order,OrderNo::ORDER_SCAN_CODE_PAY, $config['body'], 0, false, 0, false, false, $open_id); } if ($result['code']) { return $result; } $data = [ 'order_no'=>$data['order_no'], 'is_pay' => 0, 'is_finish' => 1 ]; $result['data'] = array_merge($result['data'], $data); return $result; } } elseif ($this->type == "yunst") { $res = $this->submitOrder(\app\models\Order::PAY_TYPE_YUNST_WECHAT_PAY); if($res['code'] === 0){ $data = $res['data']; $data['pay_fee'] = round($data['pay_fee'],2); if ($data['pay_fee'] == 0) { if (get_user_id() > 0) { $form = new \app\modules\alliance\models\NewMerchantForm(); $form->store_id = get_store_id(); $form->saasGiveIntegral($this->pay_fee, get_user_id(), $data['order_no']); if ($this->is_use_coupon) { UserCoupon::updateAll(['is_use' => 1], ['id' => $this->coupon_id]); } } $this->order_no = $data['order_no']; $this->payResult(); return [ 'code' => 0, 'data' => [ 'is_pay' => 1, 'order_no'=>$data['order_no'], ] ]; } }else{ return $res; } $order_no = $data['order_no']; $store = Store::findOne(['id' => get_store_id()]); // print_r($store);die; if ($store->cusid) { // print_r($store->cusid);die; $OrderYunst = new OrderYunst(); $result2 = $OrderYunst->payYunst(get_store_id(), $order_no, $store->cusid, $data['pay_fee'] * 100); if ($result2['subCode']== 'SUCCESS'){ if ($result2['data']['trxstatus'] == 3045){ return [ 'code' => 1, 'msg' => $result2['data']['errmsg'] ]; } $result2 = json_decode($result2['data']['payinfo'],true); $result2['order_no'] = $order_no; $result2['is_pay'] = 0; $result2['is_finish'] = 1; $res_data = [ 'code'=>0, 'data'=>$result2, 'msg'=>'success', 'res'=>$result2 ]; return $res_data; }else{ return [ 'code' => 1, 'msg' => '支付异常' ]; } } } elseif ($this->type == "alipay") { $id = $this->id ?: 0; if($id != 0 && is_isv()){ $query = StoreAliMini::find()->where(['id' => $this->id])->orderBy("id desc"); $mini_info = $query->select("app_name,app_logo,id")->asArray()->one(); if(empty($mini_info['id'])){ return [ 'code'=>1, 'msg'=>"获取数据失败", ]; } $id = $mini_info['id']; } $form = new AlipayThirdForm(); $form->method = "alipay.open.auth.token.app"; $form->bind_store_id = get_store_id(); if (is_isv()) { $request = new AlipaySystemOauthTokenRequest(); $request->setGrantType("authorization_code"); $request->setCode($this->code); $result = $form->miniCommon($request, 1, $id, 0, 1); $open_user_id = $result->user_id; } else { if (\Yii::$app->prod_is_dandianpu() && self_mini() === false) { $alipay_config = Json::decode(Option::get('one_store_alipay_config', 0, 'saas', '')['value']); } else { $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . get_store_id()); if ($config_cache) { $alipay_config = Json::decode($config_cache); } else { $alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value']); } } $config = [ 'alipay' => [ 'client_id' => $alipay_config['app_id'], 'rsa_private_key' => $alipay_config['app_private_key'], ] ]; $socialite = new SocialiteManager($config); $res_code = $socialite->create('alipay')->tokenFromCode($this->code); $open_user_id = $res_code['user_id']; } if (!$open_user_id) { return [ 'code' => 0, 'msg' => '参数获取错误' ]; } $user = User::findOne(['alipay_open_id' => $open_user_id, 'store_id' => get_store_id()]); if ($user) { $this->user_id = $user->id; } $OrderRes = $this->submitOrder(4); if($OrderRes['code'] == 1){ return $OrderRes; } if ($OrderRes['data']['pay_fee'] == 0) { if (get_user_id() > 0) { $form = new \app\modules\alliance\models\NewMerchantForm(); $form->store_id = get_store_id(); $form->saasGiveIntegral($this->pay_fee, get_user_id(), $OrderRes['data']['order_no']); if ($this->is_use_coupon) { UserCoupon::updateAll(['is_use' => 1], ['id' => $this->coupon_id]); } } $this->order_no = $OrderRes['data']['order_no']; debug_log($this->payResult(), 'scanCodePay.log'); return [ 'code' => 0, 'data' => [ 'is_pay' => 1, 'order_no'=>$OrderRes['data']['order_no'], ] ]; } if (is_profit_pay('ali')) { if(!empty($user)){ if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming(get_store_id()) && self_mini() === false) { $res = Alipay::mini($this->order, '当面付', $user, '', '', 0, 0); } else { if (is_profit_pay('ali')) { $res = AlipayProfit::mini($this->order, '当面付', $user, '', '', false, 0, 0); } else { $res = Alipay::mini($this->order, '当面付', $user, '', '', 0, 0); } } if (isset($res['code']) && $res['code'] == 1) { return $res; } if (isset($res['data'])) { $res['data'] = array_merge($res['data'], [ 'is_finish' => 1, 'order_no' => $res['data']['out_trade_no'] ]); } return $res; } return $result; } else { if ($OrderRes['data']['is_recharge'] === 1) { $order = ReOrder::findOne($OrderRes['data']['order_id']); $subject = '充值'; } else { $order = Order::findOne($OrderRes['data']['order_id']); $order->pay_price = $OrderRes['data']['pay_fee']; $subject = '当面付'; } $result = Alipay::scan($order, $subject, '', $open_user_id); $data = [ 'trade_no' => $result['data']['trade_no'], 'order_no' => $order->order_no, 'is_pay' => 0, 'is_finish' => 1 ]; return [ 'code' => 0, 'msg' => "成功", 'data' => $data ]; } // if (is_profit_pay('ali')) { // if(!empty($result->user_id)){ // $user_id = $result->user_id; // $res = $form->payconfig($id,$OrderRes['data']['order_no'],$user_id,$this->pay_fee); // \Yii::error($res); // return $res; // } // return $result; // } else { // $order = Order::findOne($res['data']['order_id']); // $result = Alipay::scan($order, '当面付'); // $data = [ // 'trade_no' => $result['data']['out_trade_no'], // 'order_no' => $order->order_no // ]; // return [ // 'code' => 0, // 'msg' => "成功", // 'data' => $data // ]; // // return $result; // } } } //返回前端展示支付结果 public function payResult(){ try { $reorder = ReOrder::findOne(['order_no' => $this->order_no]); if ($reorder) { $order_no = Order::findOne($reorder->order_id)->order_no; } else { $order_no = $this->order_no; } $order = Order::find()->alias('o')->where(['o.order_no'=>$order_no]) ->leftJoin(['s'=>Store::tableName()],'o.store_id = s.id') ->select("o.id,o.is_pay,o.pay_type,o.pay_time,o.order_no,o.store_id,s.name,s.logo,o.pay_price, o.is_combine_pay, o.combine_money, o.send_price, o.take_price")->asArray()->one(); if(empty($order)){ throw new \Exception("未查找到数据"); } $goods_list = Goods::find()->where(['store_id' => $order['store_id'], 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0, 'md_food_id' => 0, 'is_recommend' => 1]) ->andWhere(['!=', 'product_type',Goods::GOODS_TYPE_INTEGRAL]) ->orderBy('sort desc')->asArray()->all(); foreach ($goods_list as &$item) { $item['virtual_sales'] += OrderDetail::find()->where(['goods_id' => $item['id']])->select('id')->count() ?? 0; } $data['list'] = $goods_list; $data = array_merge($order,$data); if(!empty($order['is_pay']) && $order['is_pay'] == 1){ $this->order_event(); $id = \queue_push(new NoAfterSalesOrderJob(['order_id' => $order['id'], 'store_id' => $order['store_id'], 'is_scan' => 1]), 30); debug_log($id, '20250519.log'); return [ 'code'=>0, 'msg'=>"支付成功", 'data'=>$data ]; }else{ return [ 'code'=>0, 'msg'=>"订单尚未支付", 'data'=>$data ]; } }catch (\Exception $e){ return [ 'code'=>1, 'msg'=>$e->getMessage() ]; } } //TODO id 790 public function order_event() { $order = Order::findOne(['order_no' => $this->order_no]); $face_first_profit = Option::findOne(['name' => 'face_first_profit','store_id' => $order['store_id']]); $face_second_profit = Option::findOne(['name' => 'face_second_profit','store_id' =>$order['store_id']]); $face_third_profit = Option::findOne(['name' => 'face_third_profit','store_id' => $order['store_id']]); $order_all_price = $order->total_price + $order->take_price; $order_first_price = $face_first_profit->value * $order_all_price / 100; $order_second_price = $face_second_profit->value * $order_all_price / 100; $order_third_price = $face_third_profit->value * $order_all_price ; //奖金池补充奖金 \app\utils\Share\BonusPool::poolPush($order, 6); //处理联盟券 $order = Order::findOne($order->id); $store = Store::findOne($order->store_id); $user = User::findOne($order->user_id); //处理佣金 $user = User::findOne($order->user_id); $first_user = User::findOne($user->old_parent_id); //判断已经处理过就不再处理佣金 if ($order->is_share) { return; } if ($first_user && $order_first_price > 0) { if($order_first_price > 0){ $order->share_user_first_id = $first_user->id; $order->is_share = 1; $order->first_price = $order_first_price; $order->save(); $first_user->total_price += $order->first_price; $first_user->price += $order->first_price; $first_user->save(); UserShareMoney::set($order->first_price, $first_user->id, $order->id, 0, 3, $order->store_id, 0); } $second_user = User::findOne($first_user->old_parent_id); if ($second_user && $order_second_price > 0) { if($order_second_price > 0){ $order->share_user_second_id = $second_user->id; $order->is_share = 1; $order->second_price = $order_second_price; $order->save(); $second_user->total_price += $order->second_price; $second_user->price += $order->second_price; $second_user->save(); UserShareMoney::set($order->second_price, $second_user->id, $order->id, 0, 3, $order->store_id, 0); } $third_user = User::findOne($second_user->old_parent_id); if ($third_user && $order_third_price > 0) { if($order_third_price > 0){ $order->share_user_third_id = $third_user->id; $order->is_share = 1; $order->third_price = $order_third_price; $order->save(); $third_user->total_price += $order->third_price; $third_user->price += $order->third_price; $third_user->save(); UserShareMoney::set($order->third_price, $third_user->id, $order->id, 0, 3, $order->store_id, 0); } } } } // if ($order->send_price > 0 && $store->league_price > 0) { //给用户发放联盟券 if ($user->binding) { $saas_user = SaasUser::findOne(['mobile' => $user->binding]); } elseif ($user->alipay_open_id) { $saas_user = SaasUser::findOne(['ali_user_id' => $user->alipay_open_id]); } else { $saas_user = SaasUser::findOne(get_saas_user_id()); } if ($order->send_price > $store->league_price) { $send_price = 0; } else { $send_price = $order->send_price; } $saaSLeaguePriceLog = SaaSLeaguePriceLog::findOne(['order_id' => $order->id]); if ($saaSLeaguePriceLog) { return; } //赠送的联盟券 if ($send_price) { if ($saas_user) { $before = $saas_user->league_price; $saas_user->updateCounters(['league_price' => floatval($send_price)]); \app\models\SaaSLeaguePriceLog::setLeaguePriceLog( $order->store_id, $saas_user->id, $send_price, $before, \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER, \app\models\SaaSLeaguePriceLog::SEND_TYPE, \app\models\SaaSLeaguePriceLog::ROLE_USER, $order->id ); } //给商家扣减 (用户赠送的联盟券钱从商家从扣减) $before = $store->league_price; $store->updateCounters(['league_price' => -floatval($send_price)]); \app\models\SaaSLeaguePriceLog::setLeaguePriceLog( $order->store_id, $saas_user->id, $send_price, $before, \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER, \app\models\SaaSLeaguePriceLog::TAKE_TYPE, \app\models\SaaSLeaguePriceLog::ROLE_STORE, $order->id ); } // } //计算抵扣的联盟券 if ($order->take_price > 0){ if ($saas_user) { $before = $saas_user->league_price; $saas_user->updateCounters(['league_price' => -floatval($order->take_price)]); \app\models\SaaSLeaguePriceLog::setLeaguePriceLog( $order->store_id, $saas_user->id, $order->take_price, $before, \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER, \app\models\SaaSLeaguePriceLog::TAKE_TYPE, \app\models\SaaSLeaguePriceLog::ROLE_USER, $order->id ); } //给商家增加 (用户抵扣钱从赠送给商家) $before = $store->league_price; $store->updateCounters(['league_price' => floatval($order->take_price)]); \app\models\SaaSLeaguePriceLog::setLeaguePriceLog( $order->store_id, $saas_user->id, $send_price, $order->take_price, \app\models\SaaSLeaguePriceLog::TYPE_FACE_PAY_ORDER, \app\models\SaaSLeaguePriceLog::SEND_TYPE, \app\models\SaaSLeaguePriceLog::ROLE_STORE, $order->id ); } } // // } }