$res['out_trade_no'], ]); if ($order_union->is_pay == 1) { echo "订单已支付"; return; } $order_id_list = json_decode($order_union->order_id_list, true); if (!$order_id_list) { echo "订单数据错误"; return; } foreach ($order_id_list as $order_id) { $order = Order::findOne([ 'id' => $order_id, 'is_pay' => 0, ]); if (!$order) { continue; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; $order->trade_status = Order::ORDER_FLOW_NO_SEND; $order->is_delete = 0; $order->alipay_trade_no = $res['transaction_id']; if ($order->save()) { try { $this->addReceiver($order); //支付完成之后,相关的操作 $form = new OrderComplete(); $form->order_id = $order->id; $form->order_type = 0; $form->notify(); } catch (\Exception $e) { \Yii::warning(['UNION ORDER EXCEPTION <======> ', $e->getMessage()]); } } } $order_union->is_pay = 1; $order_union->save(); echo 'success'; return; } /** * 商盟订单回调 * @param $res */ public function BusinessNotify($res) { $order = Order::findOne([ 'order_no' => $res['out_trade_no'], ]); if ($order->is_pay == 1) { echo "订单已支付"; return; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; $order->trade_status = Order::ORDER_FLOW_NO_SEND; $order->is_delete = 0; $order->alipay_trade_no = $res['trade_no']; if ($order->save()) { // 同城配送订单下单 // 预约商品库存操作 BookOrderNum::bookNumReduce($order); $this->addReceiver($order); //商盟订单 try { // 支付完成之后,相关的操作 $form = new AllianceOrderComplete(); $form->store_id = $order->store_id; $form->order_id = $order->id; $form->order_type = 0; $form->notify(); } catch (\Exception $e) { \Yii::warning(['UNION ORDER EXCEPTION <======> ', $e->getMessage()]); } echo 'success'; return; } else { echo "支付失败"; return; } } /** * 充值回调 */ public function RechargeOrderNotify($res) { $order = ReOrder::findOne(['order_no' => $res['out_trade_no']]); if (!$order) { return; } if ($order->is_pay == 1) { echo "订单已支付"; return; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; $order->alipay_trade_no = $res['trade_no']; if ($order->save()) { //金额充值 $user = User::findOne($order->user_id); $before = $user->money; $user->money += floatval($order->pay_price) + floatval($order->send_price); if ($order->send_integral > 0) { $old_integral = $user->integral; $user->integral += $order->send_integral; $user->total_integral += $order->send_integral; $log = new AccountLog(); $log->store_id = get_store_id(); $log->user_id = $user->id; $log->type = AccountLog::TYPE_INTEGRAL; $log->log_type = AccountLog::LOG_TYPE_INCOME; $log->amount = $order->send_integral; $log->desc = "充值赠送, 订单号:{$order->order_no}"; $log->before = $old_integral; $log->after = $user->integral; $log->operator = 'system'; $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_PLATFORM_ORDER; $log->save(); } $user->save(); $log = new AccountLog(); $log->store_id = get_store_id(); $log->user_id = $user->id; $log->type = AccountLog::TYPE_BALANCE; $log->log_type = AccountLog::LOG_TYPE_INCOME; $log->amount = floatval($order->pay_price) + floatval($order->send_price); $log->desc = "充值余额支付宝在线支付,付款金额:{$order->pay_price}元,赠送金额:{$order->send_price}元。"; $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_RECHARGE_ORDER; $log->save(); NoticeSend::UserChang($order->user_id, $user->binding, $order->pay_price, '充值到账'); echo 'success'; $this->ReOrderAfter($order); $this->addReceiver($order); sleep(10); $event = new OrderEvent(); $event->alipayProfitSharing($order); // 赠送优惠券 AutoSendCoupon::send($user->id, CouponAutoSend::EVENT_RECHARGE, $user->store_id); echo "success"; return; } else { echo "支付失败"; return; } } /** * @param $order * 充值订单回调 */ public function ReOrderAfter($order) { if ($order) { $total_price = 0; $orderAfter = null; if ((int)$order->type === 0 && $order->order_id) { $orderAfter = \app\plugins\scanCodePay\models\Order::findOne($order->order_id); if (!$orderAfter) { return; } $total_price = $orderAfter->total_price; } if ($total_price) { $user = User::findOne($order->user_id); if ($user) { $before = $user->money; $money = $user->money - $total_price; if ($money >= 0) { $user->money = $money; if ($user->save()) { $orderAfter->is_pay = 1; $orderAfter->pay_time = time(); if ($orderAfter->save()) { if (intval($orderAfter->user_coupon_id) > 0) { UserCoupon::updateAll(['is_use' => 1], ['id' => $orderAfter->user_coupon_id]); } $form = new NewMerchantForm(); $form->store_id = $orderAfter->store_id; $form->saasGiveIntegral($orderAfter->pay_price, $orderAfter->user_id, $orderAfter->order_no); $form_ = new \app\modules\client\models\v1\NewMerchantForm(); $form_->order_no = $orderAfter->order_no; $form->payResult(); $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($orderAfter->total_price); $log->desc = "当面付支付宝在线支付,订单编号为" . $orderAfter->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; $log->save(); }; }; } } } } } /** * 会员购买回调 */ public function LevelOrderNotify($res) { $order = LevelOrder::findOne([ 'order_no' => $res['out_trade_no'], ]); if (!$order) { return; } if ($order->is_pay == 1) { echo "订单已支付"; return; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; if ($order->save()) { //会员升级 $user = User::findOne($order->user_id); $user->level = $order->after_level; $user->save(); $level = Level::findOne(['level' => $user->level, 'store_id' => $order->store_id, 'is_delete' => 0]); if ($level) { $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]); $user_name = $user->nickname; if ($saas_user) { $user_name = $saas_user->name; } if ($user->old_parent_id) { $parent = User::findOne($user->old_parent_id);//上级 //检测一级是否存在且佣金是否设置 if ($parent && $level->firstProfit) { //发放一级佣金 //... $parent->total_price += $level->firstProfit; $parent->price += $level->firstProfit; $parent->save(); UserShareMoney::set($level->firstProfit, $user->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放一级佣金'); if ($parent->old_parent_id) { $parent_1 = User::findOne($parent->old_parent_id);//上级 //检测二级是否存在且佣金是否设置 if ($parent_1 && $level->secondProfit) { //发放二级佣金 $parent_1->total_price += $level->secondProfit; $parent_1->price += $level->secondProfit; $parent_1->save(); UserShareMoney::set($level->secondProfit, $parent->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放二级佣金'); //检测三级是否存在且佣金是否设置 $parent_2 = User::findOne($parent_1->old_parent_id);//上级 if ($parent_2 && $level->thirdProfit) { //发放三级佣金 $parent_2->total_price += $level->thirdProfit; $parent_2->price += $level->thirdProfit; $parent_2->save(); UserShareMoney::set($level->thirdProfit, $parent_1->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放三级佣金'); } } } } } } echo 'success'; return; } else { echo "支付失败"; return; } } /** * saas联盟佣金提现回调 */ public function saasProfitCash($res) { $order = SaasProfitCash::findOne([ 'order_no' => $res['out_trade_no'], ]); if (!$order) { return; } $order->is_pay = 1; $order->pay_time = time(); if ($order->save()) { echo 'success'; return; } else { echo "支付失败"; return; } } /** * 商城订单回调 * @param $res */ public function MallOrderNotify($res) { $order = Order::findOne([ 'order_no' => $res['out_trade_no'], ]); $orderPayType = $order->pay_type; if ($order->is_pay == 1) { echo "订单已支付"; return; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; // 如果是货到付款的支付 支付成功后修改订单状态为已完成 if($orderPayType == 2){ $order->trade_status = Order::ORDER_FLOW_CONFIRM; }else{ $order->trade_status = Order::ORDER_FLOW_NO_SEND; } $order->is_delete = 0; $order->alipay_trade_no = $res['trade_no']; if ($order->save()) { if ($order->level_order_id) { $levelOrder = LevelOrder::findOne($order->level_order_id); $this->LevelOrderNotify(['out_trade_no' => $levelOrder->order_no]); } $this->addReceiver($order); $this->OrderVoucherUse($order); // 支付完成之后,相关的操作 $form = new OrderComplete(); $form->order_id = $order->id; $form->order_type = 0; $form->notify(); echo 'success'; return; } else { echo "支付失败"; return; } } /** * 入驻商提现支付宝回调 * @param $res */ public function batchTransNotify($res) { $cash = MchCash::findOne(['order_no' => $res['out_batch_no']]); $cash->updated_at = time(); $cash->virtual_type = 2; if ($res['batch_status'] == 'SUCCESS' && $res['fail_count'] == '0') { $cash->status = 1; $cash->save(false); echo 'success'; return; } else if ($res['batch_status'] == 'FAIL' && $res['fail_count'] == '1'){ $cash->status = 2; $cash->save(); echo 'success'; return; } else { echo 'success'; return; } } /** * @param $res * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public function ScanOrderNotify($res) { $order = \app\plugins\scanCodePay\models\Order::findOne([ 'order_no' => $res['out_trade_no'], ]); $form = new AlipayThirdOrderForm(); $form->updateOrderInfo($order->id, \app\plugins\scanCodePay\models\Order::find()); if ($order->is_pay == 1) { echo "订单已支付"; return; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; $order->trade_status = Order::ORDER_FLOW_CONFIRM; $order->is_delete = 0; $order->alipay_trade_no = $res['trade_no']; if ($order->save()) { if (intval($order->user_coupon_id) > 0) { UserCoupon::updateAll(['is_use' => 1], ['id' => $order->user_coupon_id]); } if (intval($order->is_platform) === 1) { $form = new \app\modules\alliance\models\NewMerchantForm(); } else { $form = new \app\modules\client\models\v1\NewMerchantForm(); } $form->order_no = $res['out_trade_no']; $form->payResult(); //计算余额抵扣的钱 if ($order->is_combine_pay && $order->combine_money > 0 && (int)$order->is_reduce_combine === 1) { $user = User::findOne($order->user_id); $user->money -= $order->combine_money; $user->save(); $order->is_reduce_combine = 1; $order->save(); } $this->is_scan = 1; $this->addReceiver($order); sleep(5); $event = new OrderEvent(); $event->alipayProfitSharing($order); // 支付完成之后,相关的操作 $form = new OrderComplete(); $form->order_id = $order->id; $form->order_type = 6; $form->notify(); $this->ScanOrderAfter($order); echo 'success'; return; } else { echo "支付失败"; return; } } public function ScanOrderAfter($order) { $file_name = \Yii::$app->runtimePath . '/logs/scan_pay.log'; try { if ($order->store_id) { //获取规则 $rule = GiveIntegralRules::find()->where(['and', ['<', 'start_date', time()], ['>', 'end_date', time()], ['status' => 1, 'is_delete' => 0]])->select('id, rate')->asArray()->all(); $rule_ids = array_column($rule, 'id'); foreach ($rule_ids as $item) { $rule_id = RulesFaceStore::find()->where(['rule_id' => $item, 'store_id' => $order->store_id])->select('rule_id')->column(); foreach ($rule as $rule_item) { //$rule_ids = RulesFaceStore::find()->where(['rule_id' => $rule_ids, 'store_id' => $order->store_id])->select('rule_id')->column(); if ($rule_id) { if (in_array($rule_item['id'], $rule_id)) { $store_ids = RulesGiveIntegralStore::find()->where(['rule_id' => $rule_id])->select('store_id')->column(); $user = User::findOne($order->user_id); if (!$order->user_id) { $saas_user = SaasUser::findOne($order->saas_id); $user = User::findOne(['binding' => $saas_user->mobile]); if (empty($user)) { $user = User::findOne(['alipay_open_id' => $saas_user->ali_user_id]); } } foreach ($store_ids as $item) { $other_user = User::findOne(['store_id' => $item, 'binding' => $user->binding]); if (!$other_user) { $other_user = new User(); $other_user->access_token = \Yii::$app->security->generateRandomString(); $other_user->binding = $user->binding; $other_user->type = User::USER_TYPE_NORMAL; $other_user->nickname = substr_replace($user->binding, '******', 3, 6); $other_user->avatar_url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png'; $other_user->username = \Yii::$app->security->generateRandomString(); $other_user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5); $other_user->auth_key = \Yii::$app->security->generateRandomString(); $other_user->is_delete = User::USER_NOT_DELETE; $other_user->store_id = $item; $other_user->parent_id = $user->id; $other_user->platform = User::USER_FROM_WECHAT; $other_user->alipay_open_id = $user->alipay_open_id; $other_user->save(); if ($other_user->save()) { $share_log = new UserShareLog(); $share_log->parent_id = $user->id; $share_log->user_id = $other_user->id; $share_log->created_at = time(); $share_log->save(); } } $integral = (int)($order->total_price * ($rule_item['rate'] / 100)); $log = new AccountLog(); $log->store_id = $item; $log->user_id = $other_user->id; $log->type = AccountLog::TYPE_INTEGRAL; $log->log_type = AccountLog::LOG_TYPE_INCOME; $log->amount = $integral; $log->desc = '订单'.$order->order_no.'赠送积分'; $log->before = $other_user->integral * 1; $log->after = (int)($other_user->integral * 1 + $integral); $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()) { file_put_contents($file_name, "\r\n" . '[当面付][' . date('Y-m-d H:i:s') . ']' . json_encode($log->errors), FILE_APPEND); } else { $other_user->integral = (int)($other_user->integral * 1 + $integral); $other_user->total_integral = (int)($other_user->total_integral * 1 + $integral); if (!$other_user->save()) { file_put_contents($file_name, "\r\n" . '[用户积分][' . date('Y-m-d H:i:s') . ']' . json_encode($other_user->errors), FILE_APPEND); \Yii::error("[用户积分]" . json_encode($other_user->errors)); } } } } } } } } } catch (\Exception $e) { file_put_contents($file_name, "\r\n" . '[用户积分发放失败][' . date('Y-m-d H:i:s') . ']' . json_encode($e->getMessage() . $e->getFile() . $e->getLine()), FILE_APPEND); echo "支付失败"; return; } } /** * 支付宝分账支付回调时添加分账接收方并计算相关分账数额 */ public function addReceiver($order) { // saas平台分销以及推荐关系分利 try { $this->newSaasShareReceiver($order); } catch (\Exception $e) { debug_log(['1' => $e->getMessage()], 'ShareReceiver.log'); } // 商城级分销 try { $this->shareStoreToStore($order); } catch (\Exception $e) { debug_log(['2' => $e->getMessage()], 'ShareReceiver.log'); } // 平台推荐 try { $this->newSaasRecommendReceiver($order); } catch (\Exception $e) { debug_log(['3' => $e->getMessage()], 'ShareReceiver.log'); } //自购返利 try { $this->newSelfReceiver($order); } catch (\Exception $e) { debug_log(['4' => $e->getMessage()], 'ShareReceiver.log'); } // 服务商分成 try { $this->newSaasPlatformReceiver($order); } catch (\Exception $e) { debug_log(['5' => $e->getMessage()], 'ShareReceiver.log'); } return; } public function newSelfReceiver($order) { // saas平台让利比例 $store = Store::findOne($order->store_id); $self_rebate_switch = $store->self_rebate_switch; //如果开启自购返利 if (!empty($self_rebate_switch)) { $self_rebate_value = $store->self_rebate_value; if($self_rebate_value == 0){ $defaultSet = Option::get(OptionSetting::SHARE_SELF_REBATE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}'); $defaultSet = json_decode($defaultSet['value'], true); if(!empty($defaultSet['self_rebate_switch']) && !empty($defaultSet['self_rebate_value'])){ $self_rebate_value = $defaultSet['self_rebate_value']; } } $saas_transfer_profit = $store->transfer_profit; //当面付 if ($this->is_scan) { $saas_transfer_profit = $store->scan_transfer_profit; } // 点餐判断 判断订单中是否设置food_flag_id if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) { $saas_transfer_profit = $store->food_transfer_profit; } // 收银台判断 判断收银订单中是否存在当前订单 if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) { $saas_transfer_profit = $store->cashier_transfer_profit; } //0.1 * 0.7 = 0.07 $saas_transfer_profit = round($order->pay_price * $saas_transfer_profit / 100, 2); $saas_share_money = round($saas_transfer_profit * ($self_rebate_value / 100), 2); if (!empty($order->saas_id)) { $self_user = SaasUser::findOne($order->saas_id); } else { $binding = User::findOne(['id' => $order->user_id, 'store_id' => $order->store_id])->binding; $self_user = SaasUser::findOne(['mobile' => $binding]); if (!$self_user) { return; } } $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$order->saas_id,'is_delete'=>0])->asArray()->one(); if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) { return; } //新分销 $account = $distributionInfo['ali_name']; $name = $distributionInfo['name']; $user = User::findOne(['binding' => $self_user->mobile, 'store_id' => $order->store_id]); $remark = '平台一级分销(自购返利)'; $share_num = 1; $saas_share_money = ($saas_share_money * $share_num); if ($saas_share_money > 0.00 && !empty($account) && !empty($user->real_name ?: $user->nickname)) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_LOGIN_NAME, 'account' => $account, 'name' => $name, 'memo' => '分账给平台一级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app); \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销一级接收方添加结果 =====>, USER_ID:' . $user->id, $add_res_1]); $this->saas_user_1_money = $saas_share_money; $sharing = new SharingReceiver(); $sharing->transaction_id = $order->alipay_trade_no; $sharing->store_id = $order->store_id; $sharing->rate = $self_rebate_value; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME; $sharing->from = SharingReceiver::FROM_SELF; $sharing->account = $account; $sharing->name = !empty($user->nickname) ? $user->nickname : ''; $sharing->user_id = !empty($user->id) ? $user->id : 0; $sharing->saas_id = $self_user->id; $sharing->description = '平台分账到个人'; $sharing->amount = $saas_share_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->remark = $remark; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台一级分账详情, 平台user_id:' . $user->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]); } } } } /** * saas平台分销分利逻辑 * @param Order $order */ private function newSaasShareReceiver($order) { // saas平台让利比例 $store = Store::findOne($order->store_id); $saas_transfer_profit = $store->transfer_profit; //当面付 if ($this->is_scan) { $saas_transfer_profit = $store->scan_transfer_profit; } // 点餐判断 判断订单中是否设置food_flag_id if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) { $saas_transfer_profit = $store->food_transfer_profit; } // 收银台判断 判断收银订单中是否存在当前订单 if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) { $saas_transfer_profit = $store->cashier_transfer_profit; } if ($saas_transfer_profit <= 0) { return; } $store_share_setting = Json::decode($store->share_setting); if(!empty($store_share_setting['level']) && $store_share_setting['level_one'] == 0 && $store_share_setting['level_two'] == 0 && $store_share_setting['level_three'] == 0){ $defaultSet = Option::get(OptionSetting::SHARE_SAAS_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}'); $defaultSet = json_decode($defaultSet['value'], true); if(!empty($defaultSet['level']) && (!empty($defaultSet['level_one']) || !empty($defaultSet['level_two']) || !empty($defaultSet['level_three']))){ $store_share_setting = $defaultSet; } } //0.2 * 0.1 = 0.02 $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2); /***** 分销三级关系开始 *****/ // 一级 $file_name = \Yii::$app->runtimePath . '/logs/app_alipay.log'; $saas_user_1_money = 0; if ($store_share_setting['level'] >= 1) { $account_1 = null; if (!empty($order->saas_id)) { $share_user_1 = SaasUser::findOne($order->saas_id); } else { $binding = User::findOne(['id' => $order->user_id, 'store_id' => $order->store_id])->binding; $share_user_1 = SaasUser::findOne(['mobile' => $binding]); if (!$share_user_1) { return; } } $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$share_user_1->id,'is_delete'=>0])->asArray()->one(); if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) { return; } $account_1 = $distributionInfo['ali_name']; $name = $distributionInfo['name']; $user_1 = User::findOne(['binding' => $share_user_1->mobile, 'store_id' => $order->store_id]); $remark = '平台一级分销'; $saas_user_1_money = round($saas_share_money * ($store_share_setting['level_one'] / 100), 2); $share_num = 1; $saas_user_1_money = round(($saas_user_1_money * $share_num), 2); if ($saas_user_1_money > 0.00 ) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_LOGIN_NAME, 'account' => $account_1, 'name' => $name, 'memo' => '分账给平台一级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res_1 = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销一级接收方添加结果 =====>, USER_ID:' . $user_1->id, $add_res_1]); $this->saas_user_1_money = $saas_user_1_money; $sharing = new SharingReceiver(); $sharing->transaction_id = $order->alipay_trade_no; $sharing->store_id = $order->store_id; $sharing->rate = $store_share_setting['level_one']; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME; $sharing->from = SharingReceiver::FROM_PLATFORM; $sharing->account = $account_1; $sharing->name = $name; $sharing->user_id = !empty($user_1->id) ? $user_1->id : 0; $sharing->saas_id = $share_user_1->id; $sharing->description = '平台分账到个人'; $sharing->amount = $saas_user_1_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->remark = $remark; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台一级分账详情, 平台user_id:' . $user_1->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]); } } } // 二级 $saas_user_2_money = 0; if ($store_share_setting['level'] >= 2) { // 判断是否认证分账账户 $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$share_user_1->parent_id,'is_delete'=>0])->asArray()->one(); if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) { return; } $account_2 = $distributionInfo['ali_name']; $name2 = $distributionInfo['name']; $share_user_2 = SaasUser::findOne(['id' => $share_user_1->parent_id]); if (!$share_user_2) { return; } $user_2 = User::findOne(['binding' => $share_user_2->mobile, 'store_id' => $order->store_id]); // 计算佣金 $saas_user_2_money = round($saas_share_money * ($store_share_setting['level_two'] / 100), 2); $share_num = 1; $saas_user_2_money = round(($saas_user_2_money * $share_num), 2); if ($saas_user_2_money > 0.00) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_LOGIN_NAME, 'account' => $account_2, 'name' => $name2, 'memo' => '分账给平台二级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res_2 = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_2 = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销二级接收方添加结果 =====>, USER_ID:' . $user_2->id, $add_res_2]); $this->saas_user_2_money = $saas_user_2_money; $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $store_share_setting['level_two']; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME; $sharing->from = SharingReceiver::FROM_PLATFORM; $sharing->account = $account_2; $sharing->user_id = !empty($user_2->id) ? $user_2->id : 0; $sharing->saas_id = $share_user_2->id; $sharing->name = !empty($user_2->nickname) ? $user_2->nickname : ''; $sharing->description = '平台分账到个人'; $sharing->amount = $saas_user_2_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->transaction_id = $order->alipay_trade_no; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->remark = '平台二级分销'; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台二级分账详情, 平台user_id:' . $user_2->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]); } } } // 三级 $saas_user_3_money = 0; if ($store_share_setting['level'] >= 3) { $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$share_user_2->parent_id,'is_delete'=>0])->asArray()->one(); if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) { return; } $account_3 = $distributionInfo['ali_name']; $name3 = $distributionInfo['name']; $share_user_3 = SaasUser::findOne(['id' => $share_user_2->parent_id]); if (!$share_user_3) { return; } $user_3 = User::findOne(['binding' => $share_user_3->mobile, 'store_id' => $order->store_id]); $saas_user_3_money = round($saas_share_money * ($store_share_setting['level_three'] / 100), 2); $share_num = 1; $saas_user_3_money = round(($saas_user_3_money * $share_num), 2); if ($saas_user_3_money > 0.00 && !empty($account_3) && !empty($user_3->real_name ?: $user_3->nickname)) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_LOGIN_NAME, 'account' => $account_3, 'name'=> $name3, //'name' => ($user_3->real_name ?: $user_3->nickname), 'memo' => '分账给平台三级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res_3 = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_3 = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } file_put_contents($file_name, "\r\n" . '[分账进行中][' . date('Y-m-d H:i:s') . ']' . '<====== 订单号:' . $order->order_no .' 平台分销三级接收方添加结果 =====>, USER_ID:' . $user_3->id . $add_res_3 , FILE_APPEND); \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销三级接收方添加结果 =====>, USER_ID:' . $user_3->id, $add_res_3]); $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $store_share_setting['level_three']; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME; $sharing->from = SharingReceiver::FROM_PLATFORM; $sharing->account = $account_3; $sharing->user_id = !empty($user_3->id) ? $user_3->id : 0; $sharing->saas_id = $share_user_3->id; $sharing->name = !empty($user_3->nickname) ? $user_3->nickname : ''; $sharing->description = '平台分账到个人'; $sharing->amount = $saas_user_3_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->remark = '平台三级分销'; $sharing->transaction_id = $order->alipay_trade_no; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台三级分账详情, 平台user_id:' . $user_3->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]); } } } /***** 分销三级关系结束 *****/ } /** * saas平台商城间的分销(merchant) * @param Order $order */ private function shareStoreToStore($order) { // saas平台让利比例 $store = Store::findOne($order->store_id); $saas_transfer_profit = $store->transfer_profit; //当面付 if ($this->is_scan) { $saas_transfer_profit = $store->scan_transfer_profit; } // 点餐判断 判断订单中是否设置food_flag_id if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) { $saas_transfer_profit = $store->food_transfer_profit; } // 收银台判断 判断收银订单中是否存在当前订单 if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) { $saas_transfer_profit = $store->cashier_transfer_profit; } if ($saas_transfer_profit <= 0) { return; } $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2); if(empty($store->store_share_switch)){ return; } if(empty($store->store_share_value)){ $defaultSet = Option::get(OptionSetting::SHARE_STORE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}'); $defaultSet = json_decode($defaultSet['value'], true); if(!empty($defaultSet['store_share_switch']) && !empty($defaultSet['store_share_value'])){ $store->store_share_value = $defaultSet['store_share_value']; } if(empty($store->store_share_value)){ return; } } $saas_store_1_money = round($saas_share_money * ($store->store_share_value / 100), 2); if(!isset($order->user_id)){ return; } $user_id = $order->user_id; $user = User::findOne($user_id); $saasUser = SaasUser::findOne(['mobile' => $user->binding]); if(!$saasUser || $saasUser->store_id <= 0){ return; } $store_parent_id = $saasUser->store_id; if ($store_parent_id < 1) { return; } $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value']; if (empty($alipay_value)) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']); return; } $alipay_value = Json::decode($alipay_value); if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']); return; } $execute_flag = SharingReceiver::EXECUTE_TYPE_ALIPAY; $remark = '店铺分销佣金';//'商城级一级分销'; if ($saas_store_1_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_USER_ID, 'account' => $alipay_value['user_id'], 'name' => $alipay_value['name'], 'memo' => '分账给店铺分销'//商城级一级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res_1 = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_1]); $this->saas_store_1_money = $saas_store_1_money; $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $store->store_share_value; $sharing->order_no = $order->order_no; $sharing->transaction_id = $order->alipay_trade_no; $sharing->type = SharingReceiver::RECEIVE_USER_ID; $sharing->from = SharingReceiver::FROM_STORE_TO_STORE; $sharing->account = $alipay_value['user_id']; $sharing->execute_type = $execute_flag; $sharing->name = $alipay_value['name']; $sharing->user_id = 0; $sharing->description = '分账到商户'; $sharing->amount = $saas_store_1_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->remark = $remark; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级一级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]); } } } /** * saas平台商城间的分销(merchant)(有bug已废弃) * @param Order $order */ private function shareStoreToStoreOld($order) { // saas平台让利比例 $store = Store::findOne($order->store_id); $saas_transfer_profit = $store->transfer_profit; //当面付 if ($this->is_scan) { $saas_transfer_profit = $store->scan_transfer_profit; } // 点餐判断 判断订单中是否设置food_flag_id if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) { $saas_transfer_profit = $store->food_transfer_profit; } // 收银台判断 判断收银订单中是否存在当前订单 if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) { $saas_transfer_profit = $store->cashier_transfer_profit; } if ($saas_transfer_profit <= 0) { return; } // 分销详细配置 $store_share = Option::getStoreShare(); if (empty($store_share)) { return; } $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2); /***** 分销三级关系开始 *****/ // 一级 $parent_id = 0; $saas_store_1_money = 0; if ($store_share['level'] >= 1) { $store_parent_id = Store::findOne($order->store_id)->parent_id; $parent_id = $store_parent_id; if ($store_parent_id < 1) { return; } $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value']; if (empty($alipay_value)) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']); return; } $alipay_value = Json::decode($alipay_value); $remark = '店铺分销佣金';//'商城级一级分销'; if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']); return; } $saas_store_1_money = round($saas_share_money * ($store_share['level_one'] / 100), 2); if ($saas_store_1_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_USER_ID, 'account' => $alipay_value['user_id'], 'name' => $alipay_value['name'], 'memo' => '分账给店铺分销'//商城级一级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res_1 = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_1]); $this->saas_store_1_money = $saas_store_1_money; $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $store_share['level_one']; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_USER_ID; $sharing->from = SharingReceiver::FROM_STORE_TO_STORE; $sharing->account = $alipay_value['user_id']; $sharing->name = $alipay_value['name']; $sharing->user_id = 0; $sharing->description = '分账到商户'; $sharing->amount = $saas_store_1_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->remark = $remark; $sharing->transaction_id = $order->alipay_trade_no; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级一级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]); } } } // 二级 $saas_store_2_money = 0; if ($store_share['level'] >= 2) { $store_parent_id = Store::findOne($parent_id)->parent_id; if ($store_parent_id < 1) { return; } $parent_id = $store_parent_id; $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value']; if (empty($alipay_value)) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']); } $alipay_value = Json::decode($alipay_value); if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']); } $saas_store_2_money = round($saas_share_money * ($store_share['level_two'] / 100), 2); if ($saas_store_2_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_USER_ID, 'account' => $alipay_value['user_id'], 'name' => $alipay_value['name'], 'memo' => '分账给商城级二级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res_2 = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_2 = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_2]); $this->saas_store_2_money = $saas_store_2_money; $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $store_share['level_two']; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_USER_ID; $sharing->from = SharingReceiver::FROM_STORE_TO_STORE; $sharing->account = $alipay_value['user_id']; $sharing->user_id = 0; $sharing->name = $alipay_value['name']; $sharing->description = '分账到商户'; $sharing->amount = $saas_store_2_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->transaction_id = $order->alipay_trade_no; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->remark = '商城级二级分销'; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级二级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]); } } } // 三级 $saas_store_3_money = 0; if ($store_share['level'] >= 3) { $store_parent_id = Store::findOne($parent_id)->parent_id; if ($store_parent_id < 1) { return; } $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value']; if (empty($alipay_value)) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销三级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']); } $alipay_value = Json::decode($alipay_value); if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) { \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销三级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']); } $saas_store_3_money = round($saas_share_money * ($store_share['level_three'] / 100), 2); if ($saas_store_3_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) { $receiver = [ [ 'type' => SharingReceiver::RECEIVE_USER_ID, 'account' => $alipay_value['user_id'], 'name' => $alipay_value['name'], 'memo' => '分账给商城级三级分销' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res_3 = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res_3 = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销三级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_3]); $this->saas_store_3_money = $saas_store_3_money; $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $store_share['level_three']; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_USER_ID; $sharing->from = SharingReceiver::FROM_STORE_TO_STORE; $sharing->account = $alipay_value['user_id']; $sharing->user_id = 0; $sharing->name = $alipay_value['name']; $sharing->description = '分账到商户'; $sharing->amount = $saas_store_3_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->transaction_id = $order->alipay_trade_no; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->remark = '商城级三级分销'; $sharing->created_at = time(); $sharing->updated_at = time(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级三级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]); } } } /***** 分销三级关系结束 *****/ } /** * 推荐关系 * @param Order $order * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ private function newSaasRecommendReceiver($order) { // saas平台让利比例 $store = Store::findOne($order->store_id); $saas_transfer_profit = $store->transfer_profit; //当面付 if ($this->is_scan) { $saas_transfer_profit = $store->scan_transfer_profit; } // 点餐判断 判断订单中是否设置food_flag_id if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) { $saas_transfer_profit = $store->food_transfer_profit; } // 收银台判断 判断收银订单中是否存在当前订单 if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) { $saas_transfer_profit = $store->cashier_transfer_profit; } if ($saas_transfer_profit <= 0) { return; } //0.2 * 20 $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2); /***** 推荐关系开始 *****/ $result = SaasStoreReferral::find()->where(['store_id' => $order->store_id])->select(['referral_id', 'referral_rebate'])->asArray()->all(); $user_recommend_money = 0; $defaultSet = Option::get(OptionSetting::SHARE_SAAS_REFERRAL_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}'); $defaultSet = json_decode($defaultSet['value'], true); if (!empty($result) && is_array($result)) { foreach ($result as $value) { if(($value['referral_rebate'] == 0) && (!empty($defaultSet['is_enable']) && !empty($defaultSet['referral_rebate']))){ $value['referral_rebate'] = $defaultSet['referral_rebate']; } $user_money = round($saas_share_money * ($value['referral_rebate'] / 100), 2); $user_recommend_money += $user_money; if ($user_money > 0.00) { $user = User::findOne($value['referral_id']); $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]); if (!$user) { return; } //新分账逻辑 $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$saas_user->id,'is_delete'=>0])->asArray()->one(); if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) { return; } // if (empty($user->real_name ?: $user->nickname)) { // \Yii::error(['<====== 订单号:' . $order->order_no . ' 平台推荐方未获得接收方实名信息 =====>, USER_ID:' . $user->id]); // continue; // } $receiver = [ [ 'type' => SharingReceiver::RECEIVE_LOGIN_NAME, 'account' => $distributionInfo['ali_name'], 'name' => $distributionInfo['name'], 'memo' => '分账给平台推荐' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 推荐接收方添加结果 =====>, USER_ID:' . $user->id, $add_res]); if ((isset($add_res['code']) && $add_res['code'] == 0)) { $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $value['referral_rebate']; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME; $sharing->from = SharingReceiver::FROM_RECOMMEND; $sharing->account = ($user->alipay_open_id ?: $saas_user->ali_user_id); $sharing->name = !empty($user->nickname) ? $user->nickname : ''; $sharing->user_id = $user->id; $sharing->saas_id = $saas_user->id; $sharing->description = '推荐分账到个人'; $sharing->amount = $user_money; $sharing->remark = '平台推荐'; $sharing->transaction_id = $order->alipay_trade_no; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->created_at = time(); $sharing->updated_at = time(); $sharing->save(); if (!$sharing->save()) { \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 推荐详情, 平台USER_ID:' . $user->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]); } } } } $this->saas_recommend_money = $user_recommend_money; } /***** 推荐关系结束 *****/ } /** * @param Order $order * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ private function newSaasPlatformReceiver($order) { // saas平台让利比例 $store = Store::findOne($order->store_id); $saas_transfer_profit = $store->transfer_profit; //当面付 if ($this->is_scan) { $saas_transfer_profit = $store->scan_transfer_profit; } // 点餐判断 判断订单中是否设置food_flag_id if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) { $saas_transfer_profit = $store->food_transfer_profit; } // 收银台判断 判断收银订单中是否存在当前订单 if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) { $saas_transfer_profit = $store->cashier_transfer_profit; } if ($saas_transfer_profit <= 0) { return; } $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2); if ($saas_share_money <= 0.00) { return; } $platform_money = round($saas_share_money - $this->saas_recommend_money - $this->saas_user_1_money - $this->saas_user_2_money - $this->saas_user_3_money - $this->saas_store_1_money - $this->saas_store_2_money - $this->saas_store_3_money, 2); debug_log(['platform_money' => $platform_money], 'shareReceiver.log'); if($platform_money <= 0){ return; } /***** 服务商分账开始 *****/ $alipay_config = Option::getSaasAlipay(); $receiver = [ [ 'type' => SharingReceiver::RECEIVE_USER_ID, 'account' => $alipay_config['user_id'], 'name' => $alipay_config['name'], 'memo' => '分账给服务商' ] ]; $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one(); // if ($storeInfo['business_model'] != 1) { // $add_res = AlipayUnion::bind($order->order_no, $receiver); // } else { $is_app = false; if ($order->order_origin === Order::ORDER_SOURCE_APP) { $is_app = true; } $add_res = AlipayProfit::bind($order->order_no, $receiver, $is_app); // } \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 服务商接收方添加结果 =====>', $add_res]); if ((isset($add_res['code']) && $add_res['code'] == 0)) { $sharing = new SharingReceiver(); $sharing->store_id = $order->store_id; $sharing->rate = $saas_transfer_profit; $sharing->order_no = $order->order_no; $sharing->type = SharingReceiver::RECEIVE_USER_ID; $sharing->from = SharingReceiver::FROM_SERVICE_PROVIDER; $sharing->account = $alipay_config['user_id']; $sharing->name = $alipay_config['name']; $sharing->user_id = 0; $sharing->description = '分账到服务商'; $sharing->remark = '服务商'; $sharing->transaction_id = $order->alipay_trade_no; $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY; $sharing->amount = $platform_money; $sharing->is_pay = SharingReceiver::PAY_WAIT; $sharing->is_delete = SharingReceiver::IS_DELETE_NO; $sharing->created_at = time(); $sharing->updated_at = time(); $sharing->save(); if (!$sharing->save()) { \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 服务商分账详情,服务商id: ' . $alipay_config['user_id'] . ',入库失败 =====>', $sharing->errors[0]]); } } /***** 服务商分账结束 *****/ } /** * 点餐回调 */ public function FoodNotify($res) { $order = FoodOrder::findOne([ 'order_no' => $res['out_trade_no'], ]); if (!$order) { return; } if ($order->is_pay == 1) { echo "订单已支付"; return; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; $order->alipay_trade_no = $res['trade_no']; if ($order->save()) { $this->addReceiver($order); sleep(5); $event = new OrderEvent(); $event->alipayProfitSharing($order); // 同步订单 $this->syncAlipayOrder($order); echo 'success'; return; } else { \Yii::error($order->errors); echo "支付失败"; return; } } /** * @param FoodOrder $order */ private function syncAlipayOrder($order) { $params = [ 'out_biz_no' => $order->order_no, 'buyer_id' => User::findOne($order->user_id)->alipay_open_id, 'amount' => $order->pay_price, 'trade_no' => $order->alipay_trade_no, 'shop_info' => [ 'merchant_shop_id' => Store::findOne($order->store_id)->shop_id, ], 'ext_info' => [ [ 'ext_key' => 'merchant_biz_type', 'ext_value' => 'qr_food_order' ], [ 'ext_key' => 'business_info', 'ext_value' => Json::encode(['rebate_pid' => Option::getSaasAlipay()['user_id']]) ], [ 'ext_key' => 'merchant_order_link_page', 'ext_value' => 'alipay-order/order/order' ], ], ]; $goods_arr = FoodOrderDetail::findAll(['order_id' => $order->id]); foreach ($goods_arr as $goods) { $food_goods = FoodGoods::findOne($goods['goods_id']); $params['item_order_list'][] = [ 'sku_id' => $goods['goods_id'], 'item_id' => $goods['goods_id'], 'item_name' => $goods['goods_name'], 'unit_price' => $food_goods->price, 'quantity' => $goods->num, 'ext_info' => [ [ 'ext_key' => 'image_material_id', 'ext_value' => $food_goods->material_id ], [ 'ext_key' => 'merchant_item_link_page', 'ext_value' => 'alipay-order/goods/goods' ], [ 'ext_key' => 'flavor', 'ext_value' => 'other' ], [ 'ext_key' => 'dish_type', 'ext_value' => 'other' ], [ 'ext_key' => 'cooking_method', 'ext_value' => 'other' ] ] ]; } $res = AlipayUnion::order_sync($params, $order->store_id); \Yii::warning(['<========= 同步订单数据 ==========> 订单order_no: ' . $order->order_no, $params, $res]); } public function AdoptCostOrderNotify($res) { $order = AdoptCostOrder::findOne([ 'order_no' => $res['out_trade_no'], ]); if ($order->is_pay == 1) { echo "订单已支付"; return; } $order->is_pay = 1; $order->pay_time = time(); $order->pay_type = 4; if ($order->order_type == 1) { $order->trade_status = AdoptCostOrder::ORDER_FLOW_NO_SEND; } else{ $order->trade_status = AdoptCostOrder::ORDER_FLOW_CONFIRM; } $order->is_delete = 0; $order->alipay_trade_no = $res['trade_no']; if ($order->save()) { // $this->addReceiver($order); // // 支付完成之后,相关的操作 $form = new OrderComplete(); $form->order_id = $order->id; $form->order_type = 0; $form->adoptOrderCostNotify(); echo 'success'; return; } else { echo "支付失败"; return; } } /** * @param Order $order * 微信分账支付回调时添加分账接收方并计算相关分账数额 */ private function getBusiness($saas_id = 0) { $time = time(); // 判断是否有三级分销权限 $share = BusinessRightDuration::find()->where([ 'is_delete' => 0, 'saas_id' => $saas_id, 'right_id' => [1, 2, 3] ])->andWhere([ 'or', [ '>', 'expire_time', $time ], ['expire_time' => 0] ])->asArray()->all(); $share_right = array_unique(array_column($share, 'right_id')); // $self_num = 0; // if (in_array(1, $share_right)) { // $memberInfo = BusinessMemberOrder::find()->where(['saas_id'=>$saas_id, 'is_pay' => 1])->andWhere(["<=","begin_time", $time])->andWhere([">","end_time",$time])->asArray()->all(); // if(!$memberInfo) { // $self_num = 0; // } else { // $memberIds = array_unique(array_column($memberInfo, 'member_id')); // $memberInfo = BusinessMember::find()->where(['id'=>$memberIds, 'is_delete'=>0, 'give_self' => 1])->asArray()->all(); // //开启自购返利 // if($memberInfo){ // //获取返利比例 // $self_num = max(array_unique(array_column($memberInfo, 'self_info'))); // } // } // } return [ 'is_share' => in_array(2, $share_right) ? 1 : 0, 'self_num' => in_array(1, $share_right) ? 100 : 0, 'shop_referrer' => in_array(3, $share_right) ? 1 : 0, ]; } //https://opendocs.alipay.com/open/033p25 //同步券核销状态 public function OrderVoucherUse($model) { try { $coupon = UserCoupon::findOne($model->use_coupon_id); if (empty($coupon['voucher_code_id'])) { return true; } $alipay_code = AlipayVoucherCode::findOne($coupon['voucher_code_id']); $order_voucher = ActivityOrdervoucher::findOne($alipay_code->activity_ordervoucher_id); if (empty($alipay_code) || empty($order_voucher)) { return true; } $order_no = OrderNo::ALIPAY_VOUCHAR_CODE; $data = [ 'activity_id' => $order_voucher->alipay_activity_id, 'biz_dt' => date('Y-m-d H:i:s'), 'voucher_code' => $alipay_code->voucher_code, 'trade_channel' => 'ZHIFUBAO_TRADE_CHANNEL', 'trade_no' => $model->alipay_trade_no, 'total_fee' => $model->total_price, 'order_no' => OrderNo::ALIPAY_VOUCHAR_CODE ]; $form = new AlipayThirdForm(); $result = $form->AlipayMarketingActivityOrdervoucherUse($data); $file_name = \Yii::$app->runtimePath . '/logs/app_order_voucherUse.log'; file_put_contents($file_name, "\r\n" . '[核销结果推送][' . date('Y-m-d H:i:s') . ']' . json_encode($result), FILE_APPEND); if ($result['code'] === 0) { $alipay_code->status = 1; $alipay_code->order_no = $order_no; $alipay_code->save(); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }