AlipayNotifyForm.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\models;
  8. use app\events\OrderEvent;
  9. use app\models\AccountLog;
  10. use app\models\ActivityOrdervoucher;
  11. use app\models\Admin;
  12. use app\models\AlipayVoucherCode;
  13. use app\models\BusinessMember;
  14. use app\models\BusinessMemberOrder;
  15. use app\models\BusinessRightDuration;
  16. use app\models\BusinessRightInfo;
  17. use app\models\Coupon;
  18. use app\models\CouponAutoSend;
  19. use app\models\GiveIntegralRules;
  20. use app\models\Level;
  21. use app\models\LevelOrder;
  22. use app\models\MchCash;
  23. use app\models\Option;
  24. use app\constants\OptionSetting;
  25. use app\models\RulesFaceStore;
  26. use app\models\RulesGiveIntegralStore;
  27. use app\models\SaasProfitCash;
  28. use app\models\SaasUser;
  29. use app\models\Order;
  30. use app\models\OrderUnion;
  31. use app\models\ReOrder;
  32. use app\models\SaasShareRelation;
  33. use app\models\SaasStoreReferral;
  34. use app\models\Salesman;
  35. use app\models\SharingReceiver;
  36. use app\models\Store;
  37. use app\models\User;
  38. use app\models\UserCoupon;
  39. use app\models\UserShareLog;
  40. use app\models\UserShareMoney;
  41. use app\models\WechatConfig;
  42. use app\modules\admin\models\AlipayThirdAppForm;
  43. use app\modules\admin\models\AlipayThirdForm;
  44. use app\modules\alliance\models\AllianceOrderComplete;
  45. use app\modules\alliance\models\NewMerchantForm;
  46. use app\modules\client\models\OrderComplete;
  47. use app\modules\client\models\v1\AlipayThirdOrderForm;
  48. use app\plugins\adopt\models\AdoptCostOrder;
  49. use app\plugins\food\models\FoodGoods;
  50. use app\plugins\food\models\FoodOrder;
  51. use app\plugins\food\models\FoodOrderDetail;
  52. use app\utils\Alipay\AlipayProfit;
  53. use app\utils\Alipay\alipaySdk\aop\request\AlipayTradeRoyaltyRelationBindRequest;
  54. use app\utils\Alipay\AlipayUnion;
  55. use app\utils\AutoSendCoupon;
  56. use app\utils\Notice\NoticeSend;
  57. use app\utils\OrderNo;
  58. use app\utils\Wechat\Wechat;
  59. use app\utils\Wechat\WechatShare;
  60. use Yansongda\Supports\Log;
  61. use yii\base\BaseObject;
  62. use yii\base\Model;
  63. use yii\helpers\Json;
  64. use app\utils\BookOrderNum;
  65. use app\models\SaasDistribution;
  66. /**
  67. * 支付宝回调成功逻辑处理类
  68. * Class AlipayNotifyForm
  69. * @package app\modules\common\models
  70. */
  71. class AlipayNotifyForm extends Model
  72. {
  73. public $saas_user_1_money = 0;
  74. public $saas_user_2_money = 0;
  75. public $saas_user_3_money = 0;
  76. public $saas_recommend_money = 0;
  77. public $saas_store_1_money = 0;
  78. public $saas_store_2_money = 0;
  79. public $saas_store_3_money = 0;
  80. public $is_scan = 0;
  81. /**
  82. * 合并订单支付回调
  83. */
  84. public function UnionOrderNotify($res)
  85. {
  86. $order_union = OrderUnion::findOne([
  87. 'order_no' => $res['out_trade_no'],
  88. ]);
  89. if ($order_union->is_pay == 1) {
  90. echo "订单已支付";
  91. return;
  92. }
  93. $order_id_list = json_decode($order_union->order_id_list, true);
  94. if (!$order_id_list) {
  95. echo "订单数据错误";
  96. return;
  97. }
  98. foreach ($order_id_list as $order_id) {
  99. $order = Order::findOne([
  100. 'id' => $order_id,
  101. 'is_pay' => 0,
  102. ]);
  103. if (!$order) {
  104. continue;
  105. }
  106. $order->is_pay = 1;
  107. $order->pay_time = time();
  108. $order->pay_type = 4;
  109. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  110. $order->is_delete = 0;
  111. $order->alipay_trade_no = $res['transaction_id'];
  112. if ($order->save()) {
  113. try {
  114. $this->addReceiver($order);
  115. //支付完成之后,相关的操作
  116. $form = new OrderComplete();
  117. $form->order_id = $order->id;
  118. $form->order_type = 0;
  119. $form->notify();
  120. } catch (\Exception $e) {
  121. \Yii::warning(['UNION ORDER EXCEPTION <======> ', $e->getMessage()]);
  122. }
  123. }
  124. }
  125. $order_union->is_pay = 1;
  126. $order_union->save();
  127. echo 'success';
  128. return;
  129. }
  130. /**
  131. * 商盟订单回调
  132. * @param $res
  133. */
  134. public function BusinessNotify($res) {
  135. $order = Order::findOne([
  136. 'order_no' => $res['out_trade_no'],
  137. ]);
  138. if ($order->is_pay == 1) {
  139. echo "订单已支付";
  140. return;
  141. }
  142. $order->is_pay = 1;
  143. $order->pay_time = time();
  144. $order->pay_type = 4;
  145. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  146. $order->is_delete = 0;
  147. $order->alipay_trade_no = $res['trade_no'];
  148. if ($order->save()) {
  149. // 同城配送订单下单
  150. // 预约商品库存操作
  151. BookOrderNum::bookNumReduce($order);
  152. $this->addReceiver($order);
  153. //商盟订单
  154. try {
  155. // 支付完成之后,相关的操作
  156. $form = new AllianceOrderComplete();
  157. $form->store_id = $order->store_id;
  158. $form->order_id = $order->id;
  159. $form->order_type = 0;
  160. $form->notify();
  161. } catch (\Exception $e) {
  162. \Yii::warning(['UNION ORDER EXCEPTION <======> ', $e->getMessage()]);
  163. }
  164. echo 'success';
  165. return;
  166. } else {
  167. echo "支付失败";
  168. return;
  169. }
  170. }
  171. /**
  172. * 充值回调
  173. */
  174. public function RechargeOrderNotify($res) {
  175. $order = ReOrder::findOne(['order_no' => $res['out_trade_no']]);
  176. if (!$order) {
  177. return;
  178. }
  179. if ($order->is_pay == 1) {
  180. echo "订单已支付";
  181. return;
  182. }
  183. $order->is_pay = 1;
  184. $order->pay_time = time();
  185. $order->pay_type = 4;
  186. $order->alipay_trade_no = $res['trade_no'];
  187. if ($order->save()) {
  188. //金额充值
  189. $user = User::findOne($order->user_id);
  190. $before = $user->money;
  191. $user->money += floatval($order->pay_price) + floatval($order->send_price);
  192. if ($order->send_integral > 0) {
  193. $old_integral = $user->integral;
  194. $user->integral += $order->send_integral;
  195. $user->total_integral += $order->send_integral;
  196. $log = new AccountLog();
  197. $log->store_id = get_store_id();
  198. $log->user_id = $user->id;
  199. $log->type = AccountLog::TYPE_INTEGRAL;
  200. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  201. $log->amount = $order->send_integral;
  202. $log->desc = "充值赠送, 订单号:{$order->order_no}";
  203. $log->before = $old_integral;
  204. $log->after = $user->integral;
  205. $log->operator = 'system';
  206. $log->operator_id = 0;
  207. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  208. $log->created_at = time();
  209. $log->order_id = $order->id;
  210. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  211. $log->save();
  212. }
  213. $user->save();
  214. $log = new AccountLog();
  215. $log->store_id = get_store_id();
  216. $log->user_id = $user->id;
  217. $log->type = AccountLog::TYPE_BALANCE;
  218. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  219. $log->amount = floatval($order->pay_price) + floatval($order->send_price);
  220. $log->desc = "充值余额支付宝在线支付,付款金额:{$order->pay_price}元,赠送金额:{$order->send_price}元。";
  221. $log->before = $before;
  222. $log->after = $user->money;
  223. $log->operator = '';
  224. $log->operator_id = 0;
  225. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  226. $log->created_at = time();
  227. $log->order_id = $order->id;
  228. $log->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  229. $log->save();
  230. NoticeSend::UserChang($order->user_id, $user->binding, $order->pay_price, '充值到账');
  231. echo 'success';
  232. $this->ReOrderAfter($order);
  233. $this->addReceiver($order);
  234. sleep(10);
  235. $event = new OrderEvent();
  236. $event->alipayProfitSharing($order);
  237. // 赠送优惠券
  238. AutoSendCoupon::send($user->id, CouponAutoSend::EVENT_RECHARGE, $user->store_id);
  239. echo "success";
  240. return;
  241. } else {
  242. echo "支付失败";
  243. return;
  244. }
  245. }
  246. /**
  247. * @param $order
  248. * 充值订单回调
  249. */
  250. public function ReOrderAfter($order) {
  251. if ($order) {
  252. $total_price = 0;
  253. $orderAfter = null;
  254. if ((int)$order->type === 0 && $order->order_id) {
  255. $orderAfter = \app\plugins\scanCodePay\models\Order::findOne($order->order_id);
  256. if (!$orderAfter) {
  257. return;
  258. }
  259. $total_price = $orderAfter->total_price;
  260. }
  261. if ($total_price) {
  262. $user = User::findOne($order->user_id);
  263. if ($user) {
  264. $before = $user->money;
  265. $money = $user->money - $total_price;
  266. if ($money >= 0) {
  267. $user->money = $money;
  268. if ($user->save()) {
  269. $orderAfter->is_pay = 1;
  270. $orderAfter->pay_time = time();
  271. if ($orderAfter->save()) {
  272. if (intval($orderAfter->user_coupon_id) > 0) {
  273. UserCoupon::updateAll(['is_use' => 1], ['id' => $orderAfter->user_coupon_id]);
  274. }
  275. $form = new NewMerchantForm();
  276. $form->store_id = $orderAfter->store_id;
  277. $form->saasGiveIntegral($orderAfter->pay_price, $orderAfter->user_id, $orderAfter->order_no);
  278. $form_ = new \app\modules\client\models\v1\NewMerchantForm();
  279. $form_->order_no = $orderAfter->order_no;
  280. $form->payResult();
  281. $log = new AccountLog();
  282. $log->store_id = $order->store_id;
  283. $log->user_id = $user->id;
  284. $log->type = AccountLog::TYPE_BALANCE;
  285. $log->log_type = AccountLog::LOG_TYPE_EXPEND;
  286. $log->amount = floatval($orderAfter->total_price);
  287. $log->desc = "当面付支付宝在线支付,订单编号为" . $orderAfter->order_no;
  288. $log->before = $before;
  289. $log->after = $user->money;
  290. $log->operator = '';
  291. $log->operator_id = 0;
  292. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  293. $log->created_at = time();
  294. $log->order_id = $order->id;
  295. $log->order_type = AccountLog::TYPE_SCAN_CODE_PAY;
  296. $log->save();
  297. };
  298. };
  299. }
  300. }
  301. }
  302. }
  303. }
  304. /**
  305. * 会员购买回调
  306. */
  307. public function LevelOrderNotify($res) {
  308. $order = LevelOrder::findOne([
  309. 'order_no' => $res['out_trade_no'],
  310. ]);
  311. if (!$order) {
  312. return;
  313. }
  314. if ($order->is_pay == 1) {
  315. echo "订单已支付";
  316. return;
  317. }
  318. $order->is_pay = 1;
  319. $order->pay_time = time();
  320. $order->pay_type = 4;
  321. if ($order->save()) {
  322. //会员升级
  323. $user = User::findOne($order->user_id);
  324. $user->level = $order->after_level;
  325. $user->save();
  326. $level = Level::findOne(['level' => $user->level, 'store_id' => $order->store_id, 'is_delete' => 0]);
  327. if ($level) {
  328. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  329. $user_name = $user->nickname;
  330. if ($saas_user) {
  331. $user_name = $saas_user->name;
  332. }
  333. if ($user->old_parent_id) {
  334. $parent = User::findOne($user->old_parent_id);//上级
  335. //检测一级是否存在且佣金是否设置
  336. if ($parent && $level->firstProfit) {
  337. //发放一级佣金
  338. //...
  339. $parent->total_price += $level->firstProfit;
  340. $parent->price += $level->firstProfit;
  341. $parent->save();
  342. UserShareMoney::set($level->firstProfit, $user->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放一级佣金');
  343. if ($parent->old_parent_id) {
  344. $parent_1 = User::findOne($parent->old_parent_id);//上级
  345. //检测二级是否存在且佣金是否设置
  346. if ($parent_1 && $level->secondProfit) {
  347. //发放二级佣金
  348. $parent_1->total_price += $level->secondProfit;
  349. $parent_1->price += $level->secondProfit;
  350. $parent_1->save();
  351. UserShareMoney::set($level->secondProfit, $parent->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放二级佣金');
  352. //检测三级是否存在且佣金是否设置
  353. $parent_2 = User::findOne($parent_1->old_parent_id);//上级
  354. if ($parent_2 && $level->thirdProfit) {
  355. //发放三级佣金
  356. $parent_2->total_price += $level->thirdProfit;
  357. $parent_2->price += $level->thirdProfit;
  358. $parent_2->save();
  359. UserShareMoney::set($level->thirdProfit, $parent_1->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放三级佣金');
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366. echo 'success';
  367. return;
  368. } else {
  369. echo "支付失败";
  370. return;
  371. }
  372. }
  373. /**
  374. * saas联盟佣金提现回调
  375. */
  376. public function saasProfitCash($res) {
  377. $order = SaasProfitCash::findOne([
  378. 'order_no' => $res['out_trade_no'],
  379. ]);
  380. if (!$order) {
  381. return;
  382. }
  383. $order->is_pay = 1;
  384. $order->pay_time = time();
  385. if ($order->save()) {
  386. echo 'success';
  387. return;
  388. } else {
  389. echo "支付失败";
  390. return;
  391. }
  392. }
  393. /**
  394. * 商城订单回调
  395. * @param $res
  396. */
  397. public function MallOrderNotify($res) {
  398. $order = Order::findOne([
  399. 'order_no' => $res['out_trade_no'],
  400. ]);
  401. $orderPayType = $order->pay_type;
  402. if ($order->is_pay == 1) {
  403. echo "订单已支付";
  404. return;
  405. }
  406. $order->is_pay = 1;
  407. $order->pay_time = time();
  408. $order->pay_type = 4;
  409. // 如果是货到付款的支付 支付成功后修改订单状态为已完成
  410. if($orderPayType == 2){
  411. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  412. }else{
  413. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  414. }
  415. $order->is_delete = 0;
  416. $order->alipay_trade_no = $res['trade_no'];
  417. if ($order->save()) {
  418. if ($order->level_order_id) {
  419. $levelOrder = LevelOrder::findOne($order->level_order_id);
  420. $this->LevelOrderNotify(['out_trade_no' => $levelOrder->order_no]);
  421. }
  422. $this->addReceiver($order);
  423. $this->OrderVoucherUse($order);
  424. // 支付完成之后,相关的操作
  425. $form = new OrderComplete();
  426. $form->order_id = $order->id;
  427. $form->order_type = 0;
  428. $form->notify();
  429. echo 'success';
  430. return;
  431. } else {
  432. echo "支付失败";
  433. return;
  434. }
  435. }
  436. /**
  437. * 入驻商提现支付宝回调
  438. * @param $res
  439. */
  440. public function batchTransNotify($res)
  441. {
  442. $cash = MchCash::findOne(['order_no' => $res['out_batch_no']]);
  443. $cash->updated_at = time();
  444. $cash->virtual_type = 2;
  445. if ($res['batch_status'] == 'SUCCESS' && $res['fail_count'] == '0') {
  446. $cash->status = 1;
  447. $cash->save(false);
  448. echo 'success';
  449. return;
  450. } else if ($res['batch_status'] == 'FAIL' && $res['fail_count'] == '1'){
  451. $cash->status = 2;
  452. $cash->save();
  453. echo 'success';
  454. return;
  455. } else {
  456. echo 'success';
  457. return;
  458. }
  459. }
  460. /**
  461. * @param $res
  462. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  463. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  464. * @throws \GuzzleHttp\Exception\GuzzleException
  465. */
  466. public function ScanOrderNotify($res) {
  467. $order = \app\plugins\scanCodePay\models\Order::findOne([
  468. 'order_no' => $res['out_trade_no'],
  469. ]);
  470. $form = new AlipayThirdOrderForm();
  471. $form->updateOrderInfo($order->id, \app\plugins\scanCodePay\models\Order::find());
  472. if ($order->is_pay == 1) {
  473. echo "订单已支付";
  474. return;
  475. }
  476. $order->is_pay = 1;
  477. $order->pay_time = time();
  478. $order->pay_type = 4;
  479. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  480. $order->is_delete = 0;
  481. $order->alipay_trade_no = $res['trade_no'];
  482. if ($order->save()) {
  483. if (intval($order->user_coupon_id) > 0) {
  484. UserCoupon::updateAll(['is_use' => 1], ['id' => $order->user_coupon_id]);
  485. }
  486. if (intval($order->is_platform) === 1) {
  487. $form = new \app\modules\alliance\models\NewMerchantForm();
  488. } else {
  489. $form = new \app\modules\client\models\v1\NewMerchantForm();
  490. }
  491. $form->order_no = $res['out_trade_no'];
  492. $form->payResult();
  493. //计算余额抵扣的钱
  494. if ($order->is_combine_pay && $order->combine_money > 0 && (int)$order->is_reduce_combine === 1) {
  495. $user = User::findOne($order->user_id);
  496. $user->money -= $order->combine_money;
  497. $user->save();
  498. $order->is_reduce_combine = 1;
  499. $order->save();
  500. }
  501. $this->is_scan = 1;
  502. $this->addReceiver($order);
  503. sleep(5);
  504. $event = new OrderEvent();
  505. $event->alipayProfitSharing($order);
  506. // 支付完成之后,相关的操作
  507. $form = new OrderComplete();
  508. $form->order_id = $order->id;
  509. $form->order_type = 6;
  510. $form->notify();
  511. $this->ScanOrderAfter($order);
  512. echo 'success';
  513. return;
  514. } else {
  515. echo "支付失败";
  516. return;
  517. }
  518. }
  519. public function ScanOrderAfter($order)
  520. {
  521. $file_name = \Yii::$app->runtimePath . '/logs/scan_pay.log';
  522. try {
  523. if ($order->store_id) {
  524. //获取规则
  525. $rule = GiveIntegralRules::find()->where(['and', ['<', 'start_date', time()], ['>', 'end_date', time()], ['status' => 1, 'is_delete' => 0]])->select('id, rate')->asArray()->all();
  526. $rule_ids = array_column($rule, 'id');
  527. foreach ($rule_ids as $item) {
  528. $rule_id = RulesFaceStore::find()->where(['rule_id' => $item, 'store_id' => $order->store_id])->select('rule_id')->column();
  529. foreach ($rule as $rule_item) {
  530. //$rule_ids = RulesFaceStore::find()->where(['rule_id' => $rule_ids, 'store_id' => $order->store_id])->select('rule_id')->column();
  531. if ($rule_id) {
  532. if (in_array($rule_item['id'], $rule_id)) {
  533. $store_ids = RulesGiveIntegralStore::find()->where(['rule_id' => $rule_id])->select('store_id')->column();
  534. $user = User::findOne($order->user_id);
  535. if (!$order->user_id) {
  536. $saas_user = SaasUser::findOne($order->saas_id);
  537. $user = User::findOne(['binding' => $saas_user->mobile]);
  538. if (empty($user)) {
  539. $user = User::findOne(['alipay_open_id' => $saas_user->ali_user_id]);
  540. }
  541. }
  542. foreach ($store_ids as $item) {
  543. $other_user = User::findOne(['store_id' => $item, 'binding' => $user->binding]);
  544. if (!$other_user) {
  545. $other_user = new User();
  546. $other_user->access_token = \Yii::$app->security->generateRandomString();
  547. $other_user->binding = $user->binding;
  548. $other_user->type = User::USER_TYPE_NORMAL;
  549. $other_user->nickname = substr_replace($user->binding, '******', 3, 6);
  550. $other_user->avatar_url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  551. $other_user->username = \Yii::$app->security->generateRandomString();
  552. $other_user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  553. $other_user->auth_key = \Yii::$app->security->generateRandomString();
  554. $other_user->is_delete = User::USER_NOT_DELETE;
  555. $other_user->store_id = $item;
  556. $other_user->parent_id = $user->id;
  557. $other_user->platform = User::USER_FROM_WECHAT;
  558. $other_user->alipay_open_id = $user->alipay_open_id;
  559. $other_user->save();
  560. if ($other_user->save()) {
  561. $share_log = new UserShareLog();
  562. $share_log->parent_id = $user->id;
  563. $share_log->user_id = $other_user->id;
  564. $share_log->created_at = time();
  565. $share_log->save();
  566. }
  567. }
  568. $integral = (int)($order->total_price * ($rule_item['rate'] / 100));
  569. $log = new AccountLog();
  570. $log->store_id = $item;
  571. $log->user_id = $other_user->id;
  572. $log->type = AccountLog::TYPE_INTEGRAL;
  573. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  574. $log->amount = $integral;
  575. $log->desc = '订单'.$order->order_no.'赠送积分';
  576. $log->before = $other_user->integral * 1;
  577. $log->after = (int)($other_user->integral * 1 + $integral);
  578. $log->operator = '';
  579. $log->operator_id = 0;
  580. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  581. $log->created_at = time();
  582. $log->order_id = $order->id;
  583. $log->order_type = AccountLog::TYPE_SCAN_CODE_PAY;
  584. if (!$log->save()) {
  585. file_put_contents($file_name, "\r\n" . '[当面付][' . date('Y-m-d H:i:s') . ']' . json_encode($log->errors), FILE_APPEND);
  586. } else {
  587. $other_user->integral = (int)($other_user->integral * 1 + $integral);
  588. $other_user->total_integral = (int)($other_user->total_integral * 1 + $integral);
  589. if (!$other_user->save()) {
  590. file_put_contents($file_name, "\r\n" . '[用户积分][' . date('Y-m-d H:i:s') . ']' . json_encode($other_user->errors), FILE_APPEND);
  591. \Yii::error("[用户积分]" . json_encode($other_user->errors));
  592. }
  593. }
  594. }
  595. }
  596. }
  597. }
  598. }
  599. }
  600. } catch (\Exception $e) {
  601. file_put_contents($file_name, "\r\n" . '[用户积分发放失败][' . date('Y-m-d H:i:s') . ']' . json_encode($e->getMessage() . $e->getFile() . $e->getLine()), FILE_APPEND);
  602. echo "支付失败";
  603. return;
  604. }
  605. }
  606. /**
  607. * 支付宝分账支付回调时添加分账接收方并计算相关分账数额
  608. */
  609. public function addReceiver($order) {
  610. // saas平台分销以及推荐关系分利
  611. try {
  612. $this->newSaasShareReceiver($order);
  613. } catch (\Exception $e) {
  614. debug_log(['1' => $e->getMessage()], 'ShareReceiver.log');
  615. }
  616. // 商城级分销
  617. try {
  618. $this->shareStoreToStore($order);
  619. } catch (\Exception $e) {
  620. debug_log(['2' => $e->getMessage()], 'ShareReceiver.log');
  621. }
  622. // 平台推荐
  623. try {
  624. $this->newSaasRecommendReceiver($order);
  625. } catch (\Exception $e) {
  626. debug_log(['3' => $e->getMessage()], 'ShareReceiver.log');
  627. }
  628. //自购返利
  629. try {
  630. $this->newSelfReceiver($order);
  631. } catch (\Exception $e) {
  632. debug_log(['4' => $e->getMessage()], 'ShareReceiver.log');
  633. }
  634. // 服务商分成
  635. try {
  636. $this->newSaasPlatformReceiver($order);
  637. } catch (\Exception $e) {
  638. debug_log(['5' => $e->getMessage()], 'ShareReceiver.log');
  639. }
  640. return;
  641. }
  642. public function newSelfReceiver($order)
  643. {
  644. // saas平台让利比例
  645. $store = Store::findOne($order->store_id);
  646. $self_rebate_switch = $store->self_rebate_switch;
  647. //如果开启自购返利
  648. if (!empty($self_rebate_switch)) {
  649. $self_rebate_value = $store->self_rebate_value;
  650. if($self_rebate_value == 0){
  651. $defaultSet = Option::get(OptionSetting::SHARE_SELF_REBATE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  652. $defaultSet = json_decode($defaultSet['value'], true);
  653. if(!empty($defaultSet['self_rebate_switch']) && !empty($defaultSet['self_rebate_value'])){
  654. $self_rebate_value = $defaultSet['self_rebate_value'];
  655. }
  656. }
  657. $saas_transfer_profit = $store->transfer_profit;
  658. //当面付
  659. if ($this->is_scan) {
  660. $saas_transfer_profit = $store->scan_transfer_profit;
  661. }
  662. // 点餐判断 判断订单中是否设置food_flag_id
  663. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  664. $saas_transfer_profit = $store->food_transfer_profit;
  665. }
  666. // 收银台判断 判断收银订单中是否存在当前订单
  667. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  668. $saas_transfer_profit = $store->cashier_transfer_profit;
  669. }
  670. //0.1 * 0.7 = 0.07
  671. $saas_transfer_profit = round($order->pay_price * $saas_transfer_profit / 100, 2);
  672. $saas_share_money = round($saas_transfer_profit * ($self_rebate_value / 100), 2);
  673. if (!empty($order->saas_id)) {
  674. $self_user = SaasUser::findOne($order->saas_id);
  675. } else {
  676. $binding = User::findOne(['id' => $order->user_id, 'store_id' => $order->store_id])->binding;
  677. $self_user = SaasUser::findOne(['mobile' => $binding]);
  678. if (!$self_user) {
  679. return;
  680. }
  681. }
  682. $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$order->saas_id,'is_delete'=>0])->asArray()->one();
  683. if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) {
  684. return;
  685. }
  686. //新分销
  687. $account = $distributionInfo['ali_name'];
  688. $name = $distributionInfo['name'];
  689. $user = User::findOne(['binding' => $self_user->mobile, 'store_id' => $order->store_id]);
  690. $remark = '平台一级分销(自购返利)';
  691. $share_num = 1;
  692. $saas_share_money = ($saas_share_money * $share_num);
  693. if ($saas_share_money > 0.00 && !empty($account) && !empty($user->real_name ?: $user->nickname)) {
  694. $receiver = [
  695. [
  696. 'type' => SharingReceiver::RECEIVE_LOGIN_NAME,
  697. 'account' => $account,
  698. 'name' => $name,
  699. 'memo' => '分账给平台一级分销'
  700. ]
  701. ];
  702. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  703. $is_app = false;
  704. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  705. $is_app = true;
  706. }
  707. $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  708. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销一级接收方添加结果 =====>, USER_ID:' . $user->id, $add_res_1]);
  709. $this->saas_user_1_money = $saas_share_money;
  710. $sharing = new SharingReceiver();
  711. $sharing->transaction_id = $order->alipay_trade_no;
  712. $sharing->store_id = $order->store_id;
  713. $sharing->rate = $self_rebate_value;
  714. $sharing->order_no = $order->order_no;
  715. $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME;
  716. $sharing->from = SharingReceiver::FROM_SELF;
  717. $sharing->account = $account;
  718. $sharing->name = !empty($user->nickname) ? $user->nickname : '';
  719. $sharing->user_id = !empty($user->id) ? $user->id : 0;
  720. $sharing->saas_id = $self_user->id;
  721. $sharing->description = '平台分账到个人';
  722. $sharing->amount = $saas_share_money;
  723. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  724. $sharing->remark = $remark;
  725. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  726. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  727. $sharing->created_at = time();
  728. $sharing->updated_at = time();
  729. if (!$sharing->save()) {
  730. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台一级分账详情, 平台user_id:' . $user->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]);
  731. }
  732. }
  733. }
  734. }
  735. /**
  736. * saas平台分销分利逻辑
  737. * @param Order $order
  738. */
  739. private function newSaasShareReceiver($order) {
  740. // saas平台让利比例
  741. $store = Store::findOne($order->store_id);
  742. $saas_transfer_profit = $store->transfer_profit;
  743. //当面付
  744. if ($this->is_scan) {
  745. $saas_transfer_profit = $store->scan_transfer_profit;
  746. }
  747. // 点餐判断 判断订单中是否设置food_flag_id
  748. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  749. $saas_transfer_profit = $store->food_transfer_profit;
  750. }
  751. // 收银台判断 判断收银订单中是否存在当前订单
  752. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  753. $saas_transfer_profit = $store->cashier_transfer_profit;
  754. }
  755. if ($saas_transfer_profit <= 0) {
  756. return;
  757. }
  758. $store_share_setting = Json::decode($store->share_setting);
  759. if(!empty($store_share_setting['level']) && $store_share_setting['level_one'] == 0 && $store_share_setting['level_two'] == 0 && $store_share_setting['level_three'] == 0){
  760. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  761. $defaultSet = json_decode($defaultSet['value'], true);
  762. if(!empty($defaultSet['level']) &&
  763. (!empty($defaultSet['level_one']) || !empty($defaultSet['level_two']) || !empty($defaultSet['level_three']))){
  764. $store_share_setting = $defaultSet;
  765. }
  766. }
  767. //0.2 * 0.1 = 0.02
  768. $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2);
  769. /***** 分销三级关系开始 *****/
  770. // 一级
  771. $file_name = \Yii::$app->runtimePath . '/logs/app_alipay.log';
  772. $saas_user_1_money = 0;
  773. if ($store_share_setting['level'] >= 1) {
  774. $account_1 = null;
  775. if (!empty($order->saas_id)) {
  776. $share_user_1 = SaasUser::findOne($order->saas_id);
  777. } else {
  778. $binding = User::findOne(['id' => $order->user_id, 'store_id' => $order->store_id])->binding;
  779. $share_user_1 = SaasUser::findOne(['mobile' => $binding]);
  780. if (!$share_user_1) {
  781. return;
  782. }
  783. }
  784. $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$share_user_1->id,'is_delete'=>0])->asArray()->one();
  785. if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) {
  786. return;
  787. }
  788. $account_1 = $distributionInfo['ali_name'];
  789. $name = $distributionInfo['name'];
  790. $user_1 = User::findOne(['binding' => $share_user_1->mobile, 'store_id' => $order->store_id]);
  791. $remark = '平台一级分销';
  792. $saas_user_1_money = round($saas_share_money * ($store_share_setting['level_one'] / 100), 2);
  793. $share_num = 1;
  794. $saas_user_1_money = round(($saas_user_1_money * $share_num), 2);
  795. if ($saas_user_1_money > 0.00 ) {
  796. $receiver = [
  797. [
  798. 'type' => SharingReceiver::RECEIVE_LOGIN_NAME,
  799. 'account' => $account_1,
  800. 'name' => $name,
  801. 'memo' => '分账给平台一级分销'
  802. ]
  803. ];
  804. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  805. // if ($storeInfo['business_model'] != 1) {
  806. // $add_res_1 = AlipayUnion::bind($order->order_no, $receiver);
  807. // } else {
  808. $is_app = false;
  809. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  810. $is_app = true;
  811. }
  812. $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  813. // }
  814. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销一级接收方添加结果 =====>, USER_ID:' . $user_1->id, $add_res_1]);
  815. $this->saas_user_1_money = $saas_user_1_money;
  816. $sharing = new SharingReceiver();
  817. $sharing->transaction_id = $order->alipay_trade_no;
  818. $sharing->store_id = $order->store_id;
  819. $sharing->rate = $store_share_setting['level_one'];
  820. $sharing->order_no = $order->order_no;
  821. $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME;
  822. $sharing->from = SharingReceiver::FROM_PLATFORM;
  823. $sharing->account = $account_1;
  824. $sharing->name = $name;
  825. $sharing->user_id = !empty($user_1->id) ? $user_1->id : 0;
  826. $sharing->saas_id = $share_user_1->id;
  827. $sharing->description = '平台分账到个人';
  828. $sharing->amount = $saas_user_1_money;
  829. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  830. $sharing->remark = $remark;
  831. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  832. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  833. $sharing->created_at = time();
  834. $sharing->updated_at = time();
  835. if (!$sharing->save()) {
  836. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台一级分账详情, 平台user_id:' . $user_1->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]);
  837. }
  838. }
  839. }
  840. // 二级
  841. $saas_user_2_money = 0;
  842. if ($store_share_setting['level'] >= 2) {
  843. // 判断是否认证分账账户
  844. $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$share_user_1->parent_id,'is_delete'=>0])->asArray()->one();
  845. if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) {
  846. return;
  847. }
  848. $account_2 = $distributionInfo['ali_name'];
  849. $name2 = $distributionInfo['name'];
  850. $share_user_2 = SaasUser::findOne(['id' => $share_user_1->parent_id]);
  851. if (!$share_user_2) {
  852. return;
  853. }
  854. $user_2 = User::findOne(['binding' => $share_user_2->mobile, 'store_id' => $order->store_id]);
  855. // 计算佣金
  856. $saas_user_2_money = round($saas_share_money * ($store_share_setting['level_two'] / 100), 2);
  857. $share_num = 1;
  858. $saas_user_2_money = round(($saas_user_2_money * $share_num), 2);
  859. if ($saas_user_2_money > 0.00) {
  860. $receiver = [
  861. [
  862. 'type' => SharingReceiver::RECEIVE_LOGIN_NAME,
  863. 'account' => $account_2,
  864. 'name' => $name2,
  865. 'memo' => '分账给平台二级分销'
  866. ]
  867. ];
  868. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  869. // if ($storeInfo['business_model'] != 1) {
  870. // $add_res_2 = AlipayUnion::bind($order->order_no, $receiver);
  871. // } else {
  872. $is_app = false;
  873. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  874. $is_app = true;
  875. }
  876. $add_res_2 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  877. // }
  878. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销二级接收方添加结果 =====>, USER_ID:' . $user_2->id, $add_res_2]);
  879. $this->saas_user_2_money = $saas_user_2_money;
  880. $sharing = new SharingReceiver();
  881. $sharing->store_id = $order->store_id;
  882. $sharing->rate = $store_share_setting['level_two'];
  883. $sharing->order_no = $order->order_no;
  884. $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME;
  885. $sharing->from = SharingReceiver::FROM_PLATFORM;
  886. $sharing->account = $account_2;
  887. $sharing->user_id = !empty($user_2->id) ? $user_2->id : 0;
  888. $sharing->saas_id = $share_user_2->id;
  889. $sharing->name = !empty($user_2->nickname) ? $user_2->nickname : '';
  890. $sharing->description = '平台分账到个人';
  891. $sharing->amount = $saas_user_2_money;
  892. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  893. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  894. $sharing->transaction_id = $order->alipay_trade_no;
  895. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  896. $sharing->remark = '平台二级分销';
  897. $sharing->created_at = time();
  898. $sharing->updated_at = time();
  899. if (!$sharing->save()) {
  900. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台二级分账详情, 平台user_id:' . $user_2->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]);
  901. }
  902. }
  903. }
  904. // 三级
  905. $saas_user_3_money = 0;
  906. if ($store_share_setting['level'] >= 3) {
  907. $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$share_user_2->parent_id,'is_delete'=>0])->asArray()->one();
  908. if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) {
  909. return;
  910. }
  911. $account_3 = $distributionInfo['ali_name'];
  912. $name3 = $distributionInfo['name'];
  913. $share_user_3 = SaasUser::findOne(['id' => $share_user_2->parent_id]);
  914. if (!$share_user_3) {
  915. return;
  916. }
  917. $user_3 = User::findOne(['binding' => $share_user_3->mobile, 'store_id' => $order->store_id]);
  918. $saas_user_3_money = round($saas_share_money * ($store_share_setting['level_three'] / 100), 2);
  919. $share_num = 1;
  920. $saas_user_3_money = round(($saas_user_3_money * $share_num), 2);
  921. if ($saas_user_3_money > 0.00 && !empty($account_3) && !empty($user_3->real_name ?: $user_3->nickname)) {
  922. $receiver = [
  923. [
  924. 'type' => SharingReceiver::RECEIVE_LOGIN_NAME,
  925. 'account' => $account_3,
  926. 'name'=> $name3,
  927. //'name' => ($user_3->real_name ?: $user_3->nickname),
  928. 'memo' => '分账给平台三级分销'
  929. ]
  930. ];
  931. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  932. // if ($storeInfo['business_model'] != 1) {
  933. // $add_res_3 = AlipayUnion::bind($order->order_no, $receiver);
  934. // } else {
  935. $is_app = false;
  936. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  937. $is_app = true;
  938. }
  939. $add_res_3 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  940. // }
  941. 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);
  942. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销三级接收方添加结果 =====>, USER_ID:' . $user_3->id, $add_res_3]);
  943. $sharing = new SharingReceiver();
  944. $sharing->store_id = $order->store_id;
  945. $sharing->rate = $store_share_setting['level_three'];
  946. $sharing->order_no = $order->order_no;
  947. $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME;
  948. $sharing->from = SharingReceiver::FROM_PLATFORM;
  949. $sharing->account = $account_3;
  950. $sharing->user_id = !empty($user_3->id) ? $user_3->id : 0;
  951. $sharing->saas_id = $share_user_3->id;
  952. $sharing->name = !empty($user_3->nickname) ? $user_3->nickname : '';
  953. $sharing->description = '平台分账到个人';
  954. $sharing->amount = $saas_user_3_money;
  955. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  956. $sharing->remark = '平台三级分销';
  957. $sharing->transaction_id = $order->alipay_trade_no;
  958. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  959. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  960. $sharing->created_at = time();
  961. $sharing->updated_at = time();
  962. if (!$sharing->save()) {
  963. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台三级分账详情, 平台user_id:' . $user_3->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]);
  964. }
  965. }
  966. }
  967. /***** 分销三级关系结束 *****/
  968. }
  969. /**
  970. * saas平台商城间的分销(merchant)
  971. * @param Order $order
  972. */
  973. private function shareStoreToStore($order) {
  974. // saas平台让利比例
  975. $store = Store::findOne($order->store_id);
  976. $saas_transfer_profit = $store->transfer_profit;
  977. //当面付
  978. if ($this->is_scan) {
  979. $saas_transfer_profit = $store->scan_transfer_profit;
  980. }
  981. // 点餐判断 判断订单中是否设置food_flag_id
  982. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  983. $saas_transfer_profit = $store->food_transfer_profit;
  984. }
  985. // 收银台判断 判断收银订单中是否存在当前订单
  986. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  987. $saas_transfer_profit = $store->cashier_transfer_profit;
  988. }
  989. if ($saas_transfer_profit <= 0) {
  990. return;
  991. }
  992. $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2);
  993. if(empty($store->store_share_switch)){
  994. return;
  995. }
  996. if(empty($store->store_share_value)){
  997. $defaultSet = Option::get(OptionSetting::SHARE_STORE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  998. $defaultSet = json_decode($defaultSet['value'], true);
  999. if(!empty($defaultSet['store_share_switch']) && !empty($defaultSet['store_share_value'])){
  1000. $store->store_share_value = $defaultSet['store_share_value'];
  1001. }
  1002. if(empty($store->store_share_value)){
  1003. return;
  1004. }
  1005. }
  1006. $saas_store_1_money = round($saas_share_money * ($store->store_share_value / 100), 2);
  1007. if(!isset($order->user_id)){
  1008. return;
  1009. }
  1010. $user_id = $order->user_id;
  1011. $user = User::findOne($user_id);
  1012. $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
  1013. if(!$saasUser || $saasUser->store_id <= 0){
  1014. return;
  1015. }
  1016. $store_parent_id = $saasUser->store_id;
  1017. if ($store_parent_id < 1) {
  1018. return;
  1019. }
  1020. $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value'];
  1021. if (empty($alipay_value)) {
  1022. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']);
  1023. return;
  1024. }
  1025. $alipay_value = Json::decode($alipay_value);
  1026. if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) {
  1027. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']);
  1028. return;
  1029. }
  1030. $execute_flag = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  1031. $remark = '店铺分销佣金';//'商城级一级分销';
  1032. if ($saas_store_1_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) {
  1033. $receiver = [
  1034. [
  1035. 'type' => SharingReceiver::RECEIVE_USER_ID,
  1036. 'account' => $alipay_value['user_id'],
  1037. 'name' => $alipay_value['name'],
  1038. 'memo' => '分账给店铺分销'//商城级一级分销'
  1039. ]
  1040. ];
  1041. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  1042. // if ($storeInfo['business_model'] != 1) {
  1043. // $add_res_1 = AlipayUnion::bind($order->order_no, $receiver);
  1044. // } else {
  1045. $is_app = false;
  1046. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  1047. $is_app = true;
  1048. }
  1049. $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  1050. // }
  1051. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_1]);
  1052. $this->saas_store_1_money = $saas_store_1_money;
  1053. $sharing = new SharingReceiver();
  1054. $sharing->store_id = $order->store_id;
  1055. $sharing->rate = $store->store_share_value;
  1056. $sharing->order_no = $order->order_no;
  1057. $sharing->transaction_id = $order->alipay_trade_no;
  1058. $sharing->type = SharingReceiver::RECEIVE_USER_ID;
  1059. $sharing->from = SharingReceiver::FROM_STORE_TO_STORE;
  1060. $sharing->account = $alipay_value['user_id'];
  1061. $sharing->execute_type = $execute_flag;
  1062. $sharing->name = $alipay_value['name'];
  1063. $sharing->user_id = 0;
  1064. $sharing->description = '分账到商户';
  1065. $sharing->amount = $saas_store_1_money;
  1066. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1067. $sharing->remark = $remark;
  1068. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1069. $sharing->created_at = time();
  1070. $sharing->updated_at = time();
  1071. if (!$sharing->save()) {
  1072. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级一级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]);
  1073. }
  1074. }
  1075. }
  1076. /**
  1077. * saas平台商城间的分销(merchant)(有bug已废弃)
  1078. * @param Order $order
  1079. */
  1080. private function shareStoreToStoreOld($order) {
  1081. // saas平台让利比例
  1082. $store = Store::findOne($order->store_id);
  1083. $saas_transfer_profit = $store->transfer_profit;
  1084. //当面付
  1085. if ($this->is_scan) {
  1086. $saas_transfer_profit = $store->scan_transfer_profit;
  1087. }
  1088. // 点餐判断 判断订单中是否设置food_flag_id
  1089. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  1090. $saas_transfer_profit = $store->food_transfer_profit;
  1091. }
  1092. // 收银台判断 判断收银订单中是否存在当前订单
  1093. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  1094. $saas_transfer_profit = $store->cashier_transfer_profit;
  1095. }
  1096. if ($saas_transfer_profit <= 0) {
  1097. return;
  1098. }
  1099. // 分销详细配置
  1100. $store_share = Option::getStoreShare();
  1101. if (empty($store_share)) {
  1102. return;
  1103. }
  1104. $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2);
  1105. /***** 分销三级关系开始 *****/
  1106. // 一级
  1107. $parent_id = 0;
  1108. $saas_store_1_money = 0;
  1109. if ($store_share['level'] >= 1) {
  1110. $store_parent_id = Store::findOne($order->store_id)->parent_id;
  1111. $parent_id = $store_parent_id;
  1112. if ($store_parent_id < 1) {
  1113. return;
  1114. }
  1115. $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value'];
  1116. if (empty($alipay_value)) {
  1117. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']);
  1118. return;
  1119. }
  1120. $alipay_value = Json::decode($alipay_value);
  1121. $remark = '店铺分销佣金';//'商城级一级分销';
  1122. if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) {
  1123. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']);
  1124. return;
  1125. }
  1126. $saas_store_1_money = round($saas_share_money * ($store_share['level_one'] / 100), 2);
  1127. if ($saas_store_1_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) {
  1128. $receiver = [
  1129. [
  1130. 'type' => SharingReceiver::RECEIVE_USER_ID,
  1131. 'account' => $alipay_value['user_id'],
  1132. 'name' => $alipay_value['name'],
  1133. 'memo' => '分账给店铺分销'//商城级一级分销'
  1134. ]
  1135. ];
  1136. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  1137. // if ($storeInfo['business_model'] != 1) {
  1138. // $add_res_1 = AlipayUnion::bind($order->order_no, $receiver);
  1139. // } else {
  1140. $is_app = false;
  1141. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  1142. $is_app = true;
  1143. }
  1144. $add_res_1 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  1145. // }
  1146. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_1]);
  1147. $this->saas_store_1_money = $saas_store_1_money;
  1148. $sharing = new SharingReceiver();
  1149. $sharing->store_id = $order->store_id;
  1150. $sharing->rate = $store_share['level_one'];
  1151. $sharing->order_no = $order->order_no;
  1152. $sharing->type = SharingReceiver::RECEIVE_USER_ID;
  1153. $sharing->from = SharingReceiver::FROM_STORE_TO_STORE;
  1154. $sharing->account = $alipay_value['user_id'];
  1155. $sharing->name = $alipay_value['name'];
  1156. $sharing->user_id = 0;
  1157. $sharing->description = '分账到商户';
  1158. $sharing->amount = $saas_store_1_money;
  1159. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1160. $sharing->remark = $remark;
  1161. $sharing->transaction_id = $order->alipay_trade_no;
  1162. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  1163. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1164. $sharing->created_at = time();
  1165. $sharing->updated_at = time();
  1166. if (!$sharing->save()) {
  1167. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级一级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]);
  1168. }
  1169. }
  1170. }
  1171. // 二级
  1172. $saas_store_2_money = 0;
  1173. if ($store_share['level'] >= 2) {
  1174. $store_parent_id = Store::findOne($parent_id)->parent_id;
  1175. if ($store_parent_id < 1) {
  1176. return;
  1177. }
  1178. $parent_id = $store_parent_id;
  1179. $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value'];
  1180. if (empty($alipay_value)) {
  1181. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']);
  1182. }
  1183. $alipay_value = Json::decode($alipay_value);
  1184. if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) {
  1185. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']);
  1186. }
  1187. $saas_store_2_money = round($saas_share_money * ($store_share['level_two'] / 100), 2);
  1188. if ($saas_store_2_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) {
  1189. $receiver = [
  1190. [
  1191. 'type' => SharingReceiver::RECEIVE_USER_ID,
  1192. 'account' => $alipay_value['user_id'],
  1193. 'name' => $alipay_value['name'],
  1194. 'memo' => '分账给商城级二级分销'
  1195. ]
  1196. ];
  1197. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  1198. // if ($storeInfo['business_model'] != 1) {
  1199. // $add_res_2 = AlipayUnion::bind($order->order_no, $receiver);
  1200. // } else {
  1201. $is_app = false;
  1202. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  1203. $is_app = true;
  1204. }
  1205. $add_res_2 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  1206. // }
  1207. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销二级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_2]);
  1208. $this->saas_store_2_money = $saas_store_2_money;
  1209. $sharing = new SharingReceiver();
  1210. $sharing->store_id = $order->store_id;
  1211. $sharing->rate = $store_share['level_two'];
  1212. $sharing->order_no = $order->order_no;
  1213. $sharing->type = SharingReceiver::RECEIVE_USER_ID;
  1214. $sharing->from = SharingReceiver::FROM_STORE_TO_STORE;
  1215. $sharing->account = $alipay_value['user_id'];
  1216. $sharing->user_id = 0;
  1217. $sharing->name = $alipay_value['name'];
  1218. $sharing->description = '分账到商户';
  1219. $sharing->amount = $saas_store_2_money;
  1220. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1221. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1222. $sharing->transaction_id = $order->alipay_trade_no;
  1223. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  1224. $sharing->remark = '商城级二级分销';
  1225. $sharing->created_at = time();
  1226. $sharing->updated_at = time();
  1227. if (!$sharing->save()) {
  1228. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级二级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]);
  1229. }
  1230. }
  1231. }
  1232. // 三级
  1233. $saas_store_3_money = 0;
  1234. if ($store_share['level'] >= 3) {
  1235. $store_parent_id = Store::findOne($parent_id)->parent_id;
  1236. if ($store_parent_id < 1) {
  1237. return;
  1238. }
  1239. $alipay_value = Option::get('alipay_config', $store_parent_id, 'alipay')['value'];
  1240. if (empty($alipay_value)) {
  1241. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销三级,STORE_ID:' . $store_parent_id . '未找到支付宝配置 =====>']);
  1242. }
  1243. $alipay_value = Json::decode($alipay_value);
  1244. if (empty($alipay_value['user_id']) || empty($alipay_value['name'])) {
  1245. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销三级,STORE_ID:' . $store_parent_id . 'USER_ID 或 NAME 为空 =====>']);
  1246. }
  1247. $saas_store_3_money = round($saas_share_money * ($store_share['level_three'] / 100), 2);
  1248. if ($saas_store_3_money > 0.00 && !empty($alipay_value['user_id']) && !empty($alipay_value['name'])) {
  1249. $receiver = [
  1250. [
  1251. 'type' => SharingReceiver::RECEIVE_USER_ID,
  1252. 'account' => $alipay_value['user_id'],
  1253. 'name' => $alipay_value['name'],
  1254. 'memo' => '分账给商城级三级分销'
  1255. ]
  1256. ];
  1257. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  1258. // if ($storeInfo['business_model'] != 1) {
  1259. // $add_res_3 = AlipayUnion::bind($order->order_no, $receiver);
  1260. // } else {
  1261. $is_app = false;
  1262. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  1263. $is_app = true;
  1264. }
  1265. $add_res_3 = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  1266. // }
  1267. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销三级接收方添加结果 =====> STORE_ID:' . $store_parent_id, $add_res_3]);
  1268. $this->saas_store_3_money = $saas_store_3_money;
  1269. $sharing = new SharingReceiver();
  1270. $sharing->store_id = $order->store_id;
  1271. $sharing->rate = $store_share['level_three'];
  1272. $sharing->order_no = $order->order_no;
  1273. $sharing->type = SharingReceiver::RECEIVE_USER_ID;
  1274. $sharing->from = SharingReceiver::FROM_STORE_TO_STORE;
  1275. $sharing->account = $alipay_value['user_id'];
  1276. $sharing->user_id = 0;
  1277. $sharing->name = $alipay_value['name'];
  1278. $sharing->description = '分账到商户';
  1279. $sharing->amount = $saas_store_3_money;
  1280. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1281. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1282. $sharing->transaction_id = $order->alipay_trade_no;
  1283. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  1284. $sharing->remark = '商城级三级分销';
  1285. $sharing->created_at = time();
  1286. $sharing->updated_at = time();
  1287. if (!$sharing->save()) {
  1288. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级三级分账详情, 商城user_id:' . $alipay_value['user_id'] . ',入库失败 =====>', $sharing->errors[0]]);
  1289. }
  1290. }
  1291. }
  1292. /***** 分销三级关系结束 *****/
  1293. }
  1294. /**
  1295. * 推荐关系
  1296. * @param Order $order
  1297. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  1298. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  1299. * @throws \GuzzleHttp\Exception\GuzzleException
  1300. */
  1301. private function newSaasRecommendReceiver($order) {
  1302. // saas平台让利比例
  1303. $store = Store::findOne($order->store_id);
  1304. $saas_transfer_profit = $store->transfer_profit;
  1305. //当面付
  1306. if ($this->is_scan) {
  1307. $saas_transfer_profit = $store->scan_transfer_profit;
  1308. }
  1309. // 点餐判断 判断订单中是否设置food_flag_id
  1310. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  1311. $saas_transfer_profit = $store->food_transfer_profit;
  1312. }
  1313. // 收银台判断 判断收银订单中是否存在当前订单
  1314. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  1315. $saas_transfer_profit = $store->cashier_transfer_profit;
  1316. }
  1317. if ($saas_transfer_profit <= 0) {
  1318. return;
  1319. }
  1320. //0.2 * 20
  1321. $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2);
  1322. /***** 推荐关系开始 *****/
  1323. $result = SaasStoreReferral::find()->where(['store_id' => $order->store_id])->select(['referral_id', 'referral_rebate'])->asArray()->all();
  1324. $user_recommend_money = 0;
  1325. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_REFERRAL_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1326. $defaultSet = json_decode($defaultSet['value'], true);
  1327. if (!empty($result) && is_array($result)) {
  1328. foreach ($result as $value) {
  1329. if(($value['referral_rebate'] == 0) &&
  1330. (!empty($defaultSet['is_enable']) && !empty($defaultSet['referral_rebate']))){
  1331. $value['referral_rebate'] = $defaultSet['referral_rebate'];
  1332. }
  1333. $user_money = round($saas_share_money * ($value['referral_rebate'] / 100), 2);
  1334. $user_recommend_money += $user_money;
  1335. if ($user_money > 0.00) {
  1336. $user = User::findOne($value['referral_id']);
  1337. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  1338. if (!$user) {
  1339. return;
  1340. }
  1341. //新分账逻辑
  1342. $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$saas_user->id,'is_delete'=>0])->asArray()->one();
  1343. if(!isset($distributionInfo['ali_name']) || !isset($distributionInfo['name']) || !$distributionInfo['ali_name'] || !$distributionInfo['name']) {
  1344. return;
  1345. }
  1346. // if (empty($user->real_name ?: $user->nickname)) {
  1347. // \Yii::error(['<====== 订单号:' . $order->order_no . ' 平台推荐方未获得接收方实名信息 =====>, USER_ID:' . $user->id]);
  1348. // continue;
  1349. // }
  1350. $receiver = [
  1351. [
  1352. 'type' => SharingReceiver::RECEIVE_LOGIN_NAME,
  1353. 'account' => $distributionInfo['ali_name'],
  1354. 'name' => $distributionInfo['name'],
  1355. 'memo' => '分账给平台推荐'
  1356. ]
  1357. ];
  1358. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  1359. // if ($storeInfo['business_model'] != 1) {
  1360. // $add_res = AlipayUnion::bind($order->order_no, $receiver);
  1361. // } else {
  1362. $is_app = false;
  1363. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  1364. $is_app = true;
  1365. }
  1366. $add_res = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  1367. // }
  1368. \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 推荐接收方添加结果 =====>, USER_ID:' . $user->id, $add_res]);
  1369. if ((isset($add_res['code']) && $add_res['code'] == 0)) {
  1370. $sharing = new SharingReceiver();
  1371. $sharing->store_id = $order->store_id;
  1372. $sharing->rate = $value['referral_rebate'];
  1373. $sharing->order_no = $order->order_no;
  1374. $sharing->type = SharingReceiver::RECEIVE_LOGIN_NAME;
  1375. $sharing->from = SharingReceiver::FROM_RECOMMEND;
  1376. $sharing->account = ($user->alipay_open_id ?: $saas_user->ali_user_id);
  1377. $sharing->name = !empty($user->nickname) ? $user->nickname : '';
  1378. $sharing->user_id = $user->id;
  1379. $sharing->saas_id = $saas_user->id;
  1380. $sharing->description = '推荐分账到个人';
  1381. $sharing->amount = $user_money;
  1382. $sharing->remark = '平台推荐';
  1383. $sharing->transaction_id = $order->alipay_trade_no;
  1384. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  1385. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1386. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1387. $sharing->created_at = time();
  1388. $sharing->updated_at = time();
  1389. $sharing->save();
  1390. if (!$sharing->save()) {
  1391. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 推荐详情, 平台USER_ID:' . $user->alipay_open_id . ',入库失败 =====>', $sharing->errors[0]]);
  1392. }
  1393. }
  1394. }
  1395. }
  1396. $this->saas_recommend_money = $user_recommend_money;
  1397. }
  1398. /***** 推荐关系结束 *****/
  1399. }
  1400. /**
  1401. * @param Order $order
  1402. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  1403. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  1404. * @throws \GuzzleHttp\Exception\GuzzleException
  1405. */
  1406. private function newSaasPlatformReceiver($order) {
  1407. // saas平台让利比例
  1408. $store = Store::findOne($order->store_id);
  1409. $saas_transfer_profit = $store->transfer_profit;
  1410. //当面付
  1411. if ($this->is_scan) {
  1412. $saas_transfer_profit = $store->scan_transfer_profit;
  1413. }
  1414. // 点餐判断 判断订单中是否设置food_flag_id
  1415. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  1416. $saas_transfer_profit = $store->food_transfer_profit;
  1417. }
  1418. // 收银台判断 判断收银订单中是否存在当前订单
  1419. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  1420. $saas_transfer_profit = $store->cashier_transfer_profit;
  1421. }
  1422. if ($saas_transfer_profit <= 0) {
  1423. return;
  1424. }
  1425. $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2);
  1426. if ($saas_share_money <= 0.00) {
  1427. return;
  1428. }
  1429. $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);
  1430. debug_log(['platform_money' => $platform_money], 'shareReceiver.log');
  1431. if($platform_money <= 0){
  1432. return;
  1433. }
  1434. /***** 服务商分账开始 *****/
  1435. $alipay_config = Option::getSaasAlipay();
  1436. $receiver = [
  1437. [
  1438. 'type' => SharingReceiver::RECEIVE_USER_ID,
  1439. 'account' => $alipay_config['user_id'],
  1440. 'name' => $alipay_config['name'],
  1441. 'memo' => '分账给服务商'
  1442. ]
  1443. ];
  1444. $storeInfo = Store::find()->where(['id'=>$order->store_id,'is_delete'=>0])->asArray()->one();
  1445. // if ($storeInfo['business_model'] != 1) {
  1446. // $add_res = AlipayUnion::bind($order->order_no, $receiver);
  1447. // } else {
  1448. $is_app = false;
  1449. if ($order->order_origin === Order::ORDER_SOURCE_APP) {
  1450. $is_app = true;
  1451. }
  1452. $add_res = AlipayProfit::bind($order->order_no, $receiver, $is_app);
  1453. // }
  1454. \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 服务商接收方添加结果 =====>', $add_res]);
  1455. if ((isset($add_res['code']) && $add_res['code'] == 0)) {
  1456. $sharing = new SharingReceiver();
  1457. $sharing->store_id = $order->store_id;
  1458. $sharing->rate = $saas_transfer_profit;
  1459. $sharing->order_no = $order->order_no;
  1460. $sharing->type = SharingReceiver::RECEIVE_USER_ID;
  1461. $sharing->from = SharingReceiver::FROM_SERVICE_PROVIDER;
  1462. $sharing->account = $alipay_config['user_id'];
  1463. $sharing->name = $alipay_config['name'];
  1464. $sharing->user_id = 0;
  1465. $sharing->description = '分账到服务商';
  1466. $sharing->remark = '服务商';
  1467. $sharing->transaction_id = $order->alipay_trade_no;
  1468. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_ALIPAY;
  1469. $sharing->amount = $platform_money;
  1470. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1471. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1472. $sharing->created_at = time();
  1473. $sharing->updated_at = time();
  1474. $sharing->save();
  1475. if (!$sharing->save()) {
  1476. \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 服务商分账详情,服务商id: ' . $alipay_config['user_id'] . ',入库失败 =====>', $sharing->errors[0]]);
  1477. }
  1478. }
  1479. /***** 服务商分账结束 *****/
  1480. }
  1481. /**
  1482. * 点餐回调
  1483. */
  1484. public function FoodNotify($res) {
  1485. $order = FoodOrder::findOne([
  1486. 'order_no' => $res['out_trade_no'],
  1487. ]);
  1488. if (!$order) {
  1489. return;
  1490. }
  1491. if ($order->is_pay == 1) {
  1492. echo "订单已支付";
  1493. return;
  1494. }
  1495. $order->is_pay = 1;
  1496. $order->pay_time = time();
  1497. $order->pay_type = 4;
  1498. $order->alipay_trade_no = $res['trade_no'];
  1499. if ($order->save()) {
  1500. $this->addReceiver($order);
  1501. sleep(5);
  1502. $event = new OrderEvent();
  1503. $event->alipayProfitSharing($order);
  1504. // 同步订单
  1505. $this->syncAlipayOrder($order);
  1506. echo 'success';
  1507. return;
  1508. } else {
  1509. \Yii::error($order->errors);
  1510. echo "支付失败";
  1511. return;
  1512. }
  1513. }
  1514. /**
  1515. * @param FoodOrder $order
  1516. */
  1517. private function syncAlipayOrder($order) {
  1518. $params = [
  1519. 'out_biz_no' => $order->order_no,
  1520. 'buyer_id' => User::findOne($order->user_id)->alipay_open_id,
  1521. 'amount' => $order->pay_price,
  1522. 'trade_no' => $order->alipay_trade_no,
  1523. 'shop_info' => [
  1524. 'merchant_shop_id' => Store::findOne($order->store_id)->shop_id,
  1525. ],
  1526. 'ext_info' => [
  1527. [
  1528. 'ext_key' => 'merchant_biz_type',
  1529. 'ext_value' => 'qr_food_order'
  1530. ],
  1531. [
  1532. 'ext_key' => 'business_info',
  1533. 'ext_value' => Json::encode(['rebate_pid' => Option::getSaasAlipay()['user_id']])
  1534. ],
  1535. [
  1536. 'ext_key' => 'merchant_order_link_page',
  1537. 'ext_value' => 'alipay-order/order/order'
  1538. ],
  1539. ],
  1540. ];
  1541. $goods_arr = FoodOrderDetail::findAll(['order_id' => $order->id]);
  1542. foreach ($goods_arr as $goods) {
  1543. $food_goods = FoodGoods::findOne($goods['goods_id']);
  1544. $params['item_order_list'][] = [
  1545. 'sku_id' => $goods['goods_id'],
  1546. 'item_id' => $goods['goods_id'],
  1547. 'item_name' => $goods['goods_name'],
  1548. 'unit_price' => $food_goods->price,
  1549. 'quantity' => $goods->num,
  1550. 'ext_info' => [
  1551. [
  1552. 'ext_key' => 'image_material_id',
  1553. 'ext_value' => $food_goods->material_id
  1554. ],
  1555. [
  1556. 'ext_key' => 'merchant_item_link_page',
  1557. 'ext_value' => 'alipay-order/goods/goods'
  1558. ],
  1559. [
  1560. 'ext_key' => 'flavor',
  1561. 'ext_value' => 'other'
  1562. ],
  1563. [
  1564. 'ext_key' => 'dish_type',
  1565. 'ext_value' => 'other'
  1566. ],
  1567. [
  1568. 'ext_key' => 'cooking_method',
  1569. 'ext_value' => 'other'
  1570. ]
  1571. ]
  1572. ];
  1573. }
  1574. $res = AlipayUnion::order_sync($params, $order->store_id);
  1575. \Yii::warning(['<========= 同步订单数据 ==========> 订单order_no: ' . $order->order_no, $params, $res]);
  1576. }
  1577. public function AdoptCostOrderNotify($res) {
  1578. $order = AdoptCostOrder::findOne([
  1579. 'order_no' => $res['out_trade_no'],
  1580. ]);
  1581. if ($order->is_pay == 1) {
  1582. echo "订单已支付";
  1583. return;
  1584. }
  1585. $order->is_pay = 1;
  1586. $order->pay_time = time();
  1587. $order->pay_type = 4;
  1588. if ($order->order_type == 1) {
  1589. $order->trade_status = AdoptCostOrder::ORDER_FLOW_NO_SEND;
  1590. } else{
  1591. $order->trade_status = AdoptCostOrder::ORDER_FLOW_CONFIRM;
  1592. }
  1593. $order->is_delete = 0;
  1594. $order->alipay_trade_no = $res['trade_no'];
  1595. if ($order->save()) {
  1596. // $this->addReceiver($order);
  1597. // // 支付完成之后,相关的操作
  1598. $form = new OrderComplete();
  1599. $form->order_id = $order->id;
  1600. $form->order_type = 0;
  1601. $form->adoptOrderCostNotify();
  1602. echo 'success';
  1603. return;
  1604. } else {
  1605. echo "支付失败";
  1606. return;
  1607. }
  1608. }
  1609. /**
  1610. * @param Order $order
  1611. * 微信分账支付回调时添加分账接收方并计算相关分账数额
  1612. */
  1613. private function getBusiness($saas_id = 0) {
  1614. $time = time();
  1615. // 判断是否有三级分销权限
  1616. $share = BusinessRightDuration::find()->where([
  1617. 'is_delete' => 0,
  1618. 'saas_id' => $saas_id,
  1619. 'right_id' => [1, 2, 3]
  1620. ])->andWhere([
  1621. 'or',
  1622. [
  1623. '>',
  1624. 'expire_time',
  1625. $time
  1626. ],
  1627. ['expire_time' => 0]
  1628. ])->asArray()->all();
  1629. $share_right = array_unique(array_column($share, 'right_id'));
  1630. // $self_num = 0;
  1631. // if (in_array(1, $share_right)) {
  1632. // $memberInfo = BusinessMemberOrder::find()->where(['saas_id'=>$saas_id, 'is_pay' => 1])->andWhere(["<=","begin_time", $time])->andWhere([">","end_time",$time])->asArray()->all();
  1633. // if(!$memberInfo) {
  1634. // $self_num = 0;
  1635. // } else {
  1636. // $memberIds = array_unique(array_column($memberInfo, 'member_id'));
  1637. // $memberInfo = BusinessMember::find()->where(['id'=>$memberIds, 'is_delete'=>0, 'give_self' => 1])->asArray()->all();
  1638. // //开启自购返利
  1639. // if($memberInfo){
  1640. // //获取返利比例
  1641. // $self_num = max(array_unique(array_column($memberInfo, 'self_info')));
  1642. // }
  1643. // }
  1644. // }
  1645. return [
  1646. 'is_share' => in_array(2, $share_right) ? 1 : 0,
  1647. 'self_num' => in_array(1, $share_right) ? 100 : 0,
  1648. 'shop_referrer' => in_array(3, $share_right) ? 1 : 0,
  1649. ];
  1650. }
  1651. //https://opendocs.alipay.com/open/033p25
  1652. //同步券核销状态
  1653. public function OrderVoucherUse($model) {
  1654. try {
  1655. $coupon = UserCoupon::findOne($model->use_coupon_id);
  1656. if (empty($coupon['voucher_code_id'])) {
  1657. return true;
  1658. }
  1659. $alipay_code = AlipayVoucherCode::findOne($coupon['voucher_code_id']);
  1660. $order_voucher = ActivityOrdervoucher::findOne($alipay_code->activity_ordervoucher_id);
  1661. if (empty($alipay_code) || empty($order_voucher)) {
  1662. return true;
  1663. }
  1664. $order_no = OrderNo::ALIPAY_VOUCHAR_CODE;
  1665. $data = [
  1666. 'activity_id' => $order_voucher->alipay_activity_id,
  1667. 'biz_dt' => date('Y-m-d H:i:s'),
  1668. 'voucher_code' => $alipay_code->voucher_code,
  1669. 'trade_channel' => 'ZHIFUBAO_TRADE_CHANNEL',
  1670. 'trade_no' => $model->alipay_trade_no,
  1671. 'total_fee' => $model->total_price,
  1672. 'order_no' => OrderNo::ALIPAY_VOUCHAR_CODE
  1673. ];
  1674. $form = new AlipayThirdForm();
  1675. $result = $form->AlipayMarketingActivityOrdervoucherUse($data);
  1676. $file_name = \Yii::$app->runtimePath . '/logs/app_order_voucherUse.log';
  1677. file_put_contents($file_name, "\r\n" . '[核销结果推送][' . date('Y-m-d H:i:s') . ']' . json_encode($result), FILE_APPEND);
  1678. if ($result['code'] === 0) {
  1679. $alipay_code->status = 1;
  1680. $alipay_code->order_no = $order_no;
  1681. $alipay_code->save();
  1682. }
  1683. } catch (\Exception $e) {
  1684. return [
  1685. 'code' => 1,
  1686. 'msg' => $e->getMessage()
  1687. ];
  1688. }
  1689. }
  1690. }