| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\common\models;
- use app\events\OrderEvent;
- use app\models\AccountLog;
- use app\models\ActivityOrdervoucher;
- use app\models\Admin;
- use app\models\AlipayVoucherCode;
- use app\models\BusinessMember;
- use app\models\BusinessMemberOrder;
- use app\models\BusinessRightDuration;
- use app\models\BusinessRightInfo;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\GiveIntegralRules;
- use app\models\Level;
- use app\models\LevelOrder;
- use app\models\MchCash;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\RulesFaceStore;
- use app\models\RulesGiveIntegralStore;
- use app\models\SaasProfitCash;
- use app\models\SaasUser;
- use app\models\Order;
- use app\models\OrderUnion;
- use app\models\ReOrder;
- use app\models\SaasShareRelation;
- use app\models\SaasStoreReferral;
- use app\models\Salesman;
- use app\models\SharingReceiver;
- use app\models\Store;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\UserShareLog;
- use app\models\UserShareMoney;
- use app\models\WechatConfig;
- use app\modules\admin\models\AlipayThirdAppForm;
- use app\modules\admin\models\AlipayThirdForm;
- use app\modules\alliance\models\AllianceOrderComplete;
- use app\modules\alliance\models\NewMerchantForm;
- use app\modules\client\models\OrderComplete;
- use app\modules\client\models\v1\AlipayThirdOrderForm;
- use app\plugins\adopt\models\AdoptCostOrder;
- use app\plugins\food\models\FoodGoods;
- use app\plugins\food\models\FoodOrder;
- use app\plugins\food\models\FoodOrderDetail;
- use app\utils\Alipay\AlipayProfit;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayTradeRoyaltyRelationBindRequest;
- use app\utils\Alipay\AlipayUnion;
- use app\utils\AutoSendCoupon;
- use app\utils\Notice\NoticeSend;
- use app\utils\OrderNo;
- use app\utils\Wechat\Wechat;
- use app\utils\Wechat\WechatShare;
- use Yansongda\Supports\Log;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\utils\BookOrderNum;
- use app\models\SaasDistribution;
- /**
- * 支付宝回调成功逻辑处理类
- * Class AlipayNotifyForm
- * @package app\modules\common\models
- */
- class AlipayNotifyForm extends Model
- {
- public $saas_user_1_money = 0;
- public $saas_user_2_money = 0;
- public $saas_user_3_money = 0;
- public $saas_recommend_money = 0;
- public $saas_store_1_money = 0;
- public $saas_store_2_money = 0;
- public $saas_store_3_money = 0;
- public $is_scan = 0;
- /**
- * 合并订单支付回调
- */
- public function UnionOrderNotify($res)
- {
- $order_union = OrderUnion::findOne([
- 'order_no' => $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()
- ];
- }
- }
- }
|