NotifyForm.php 171 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703
  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\jobs\CancelPtOrderJob;
  10. use app\jobs\CreatedPtOrderJob;
  11. use app\jobs\order\NoAfterSalesOrderJob;
  12. use app\models\AccountLog;
  13. use app\models\ActivityOrdervoucher;
  14. use app\models\Admin;
  15. use app\models\AlipayVoucherCode;
  16. use app\models\CouponAutoSend;
  17. use app\models\DeliveryInfo;
  18. use app\models\District;
  19. use app\models\GiveIntegralRules;
  20. use app\models\GoodsBook;
  21. use app\models\IntegralRecharge;
  22. use app\models\IntegralRechargeOrder;
  23. use app\models\Level;
  24. use app\models\LevelOrder;
  25. use app\models\MchCash;
  26. use app\models\Option;
  27. use app\constants\OptionSetting;
  28. use app\models\OrderDetail;
  29. use app\models\PtActivity;
  30. use app\models\PtActivityGoods;
  31. use app\models\PtActivityOrderDetail;
  32. use app\models\RechargeReOrder;
  33. use app\models\PtActivityOrder;
  34. use app\models\RulesFaceStore;
  35. use app\models\RulesGiveIntegralStore;
  36. use app\models\SaaSLeaguePriceLog;
  37. use app\models\SaasProfitCash;
  38. use app\models\SaasUser;
  39. use app\models\Order;
  40. use app\models\OrderUnion;
  41. use app\models\ReOrder;
  42. use app\models\SaasShareRelation;
  43. use app\models\SaasStoreReferral;
  44. use app\models\Salesman;
  45. use app\models\SharingReceiver;
  46. use app\models\SharingReceiverCustom;
  47. use app\models\Store;
  48. use app\models\StoreReOrder;
  49. use app\models\StoreShareMoney;
  50. use app\models\User;
  51. use app\models\UserCoupon;
  52. use app\models\UserShareLog;
  53. use app\models\UserShareMoney;
  54. use app\models\VideoGoodsShare;
  55. use app\models\WechatConfig;
  56. use app\models\BusinessMemberOrder;
  57. use app\modules\admin\models\AlipayThirdForm;
  58. use app\modules\alliance\models\NewMerchantForm;
  59. use app\modules\client\models\OrderComplete;
  60. use app\modules\alliance\models\AllianceOrderComplete;
  61. use app\plugins\adopt\models\AdoptCostOrder;
  62. use app\plugins\adopt\models\AdoptOrderInfo;
  63. use app\plugins\food\models\FoodOrder;
  64. use app\utils\AutoSendCoupon;
  65. use app\utils\BookOrderNum;
  66. use app\utils\Delivery\Delivery;
  67. use app\utils\Delivery\Alipay\ADelivery;
  68. use app\utils\Notice\NoticeSend;
  69. use app\utils\OrderNo;
  70. use app\utils\Wechat\Wechat;
  71. use app\utils\Wechat\WechatShare;
  72. use app\utils\yunst\src\Merchant;
  73. use app\utils\yunst\src\OrderYunst;
  74. use yii\base\BaseObject;
  75. use yii\base\Model;
  76. use yii\helpers\Json;
  77. use app\models\SaasDistribution;
  78. use app\modules\admin\models\StoreForm;
  79. use app\models\BusinessRightInfo;
  80. use app\models\BusinessMember;
  81. use app\models\BusinessRightDuration;
  82. use app\models\MemberUpgradeRecord;
  83. use app\models\NewDiyTemplate;
  84. use app\models\SaasShareMoney;
  85. use app\models\PurchaseReOrder;
  86. use app\modules\alliance\models\purchase\BalanceForm;
  87. use app\models\PurchaseOrder;
  88. use app\models\RcCommissionLog;
  89. use app\models\Recharge;
  90. use app\models\Share;
  91. use app\modules\alliance\models\purchase\order\OrderPayDataForm;
  92. use app\utils\Share\BonusPool;
  93. use app\utils\Wechat\B2b\B2bPay;
  94. /**
  95. * 回调成功逻辑处理类
  96. * Class NotifyForm
  97. * @package app\modules\common\models
  98. */
  99. class NotifyForm extends Model
  100. {
  101. public $pay_type = 1;
  102. public $saas_self_money = 0;
  103. public $saas_user_1_money = 0;
  104. public $saas_user_2_money = 0;
  105. public $saas_user_3_money = 0;
  106. public $saas_recommend_money = 0;
  107. public $saas_store_1_money = 0;
  108. public $saas_store_2_money = 0;
  109. public $saas_store_3_money = 0;
  110. public $saas_custom_money = 0;//自定义分账金额
  111. public $saas_agent_money = 0;//代理分账金额
  112. public $business_saas_self_money = 0;
  113. public $business_saas_1_money = 0;
  114. public $business_saas_2_money = 0;
  115. public $business_saas_3_money = 0;
  116. public $is_scan = 0;//是当面付
  117. public $is_yunst = 0;
  118. public function adapay() {
  119. $payTypes = [
  120. Order::PAY_TYPE_ADAPAY_WX,
  121. Order::PAY_TYPE_ADAPAY_ALIPAY,
  122. ];
  123. if(in_array($this->pay_type, $payTypes)){
  124. return true;
  125. }
  126. return false;
  127. }
  128. public function is_b2b() {
  129. $payTypes = [
  130. Order::PAY_TYPE_WX_B2B,
  131. ];
  132. if(in_array($this->pay_type, $payTypes)){
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * 商城充值回调
  139. */
  140. public function StoreReOrderNotify($res) {
  141. $order = StoreReOrder::findOne([
  142. 'order_no' => $res['out_trade_no'],
  143. ]);
  144. if (!$order) {
  145. return;
  146. }
  147. $order->is_pay = 1;
  148. $order->pay_time = time();
  149. if ($order->save()) {
  150. $time = strtotime('+1 year');
  151. $store = Store::findOne($order->store_id);
  152. if($order->store_price_id){
  153. $store_price = \app\models\StorePrice::findOne($order->store_price_id);
  154. $itime = $store->end_time > time() ? $store->end_time : time();
  155. $time = $itime + 86400 * $store_price->days;
  156. }
  157. $store->end_time = $time;
  158. $store->save();
  159. //分佣逻辑(代理分润比例)start
  160. /**
  161. * 判断当前商城所属的代理人
  162. * 将佣金分发给代理人
  163. * 增加佣金明细记录
  164. */
  165. //计算分佣金额
  166. $profit = Option::get('admin_profit', 0, 'saas', 0)['value'];//$store->profit
  167. $share_profit = sprintf('%.2f',($order->total_price * ($profit / 100)));
  168. if ($store->salesman_id > 0) {
  169. $admin_id = Salesman::findOne($store->salesman_id)->admin_id;
  170. //判断当前商城所属的代理人并将佣金分发给代理人
  171. $saas_user_id = Admin::findOne($admin_id)->saas_user_id;
  172. if(!empty($saas_user_id)){
  173. $saas_user = SaasUser::findOne($saas_user_id);
  174. $saas_user->share_profit = $saas_user->share_profit*1 + $share_profit*1;
  175. if($saas_user->save()){
  176. //增加佣金明细记录
  177. $log = new StoreShareMoney();
  178. $log->profit = $profit;
  179. $log->user_id = $saas_user_id;
  180. $log->order_id = $order->id;
  181. $log->store_id = $store->id;
  182. $log->created_at = time();
  183. $log->total_price = $order->total_price;
  184. $log->commission = $share_profit*1;
  185. $log->desc = $order->desc;
  186. $log->is_send = 1;
  187. $log->status = StoreShareMoney::STATUS_STORE_SETTLED;
  188. $log->type = StoreShareMoney::TYPE_BD_AGENT;
  189. $log->save();
  190. }
  191. }
  192. }
  193. $option = Option::get('agency_price_config', 0, 'saas', [])['value'];
  194. if ($option) {
  195. $option = json_decode($option, true);
  196. }
  197. $province_percent = isset($option['province_percent']) ? $option['province_percent'] : 0;
  198. $city_percent = isset($option['city_percent']) ? $option['city_percent'] : 0;
  199. $district_percent = isset($option['district_percent']) ? $option['district_percent'] : 0;
  200. // $model_type = $option['model_type'] ?? 0;
  201. // $rate = 0;
  202. // if ((int)$model_type === 1) {
  203. // $check_ = Admin::findOne([
  204. // 'area_level' => 1,
  205. // 'province_id' => $store->province_id,
  206. // 'city_id' => $store->city_id,
  207. // 'district_id' => $store->district_id,
  208. // ]);
  209. // if ($check_) {
  210. //
  211. // $rate = $district_percent;
  212. //
  213. // }
  214. // } else {
  215. // $check_ = Admin::findOne([
  216. // 'area_level' => [2, 3],
  217. // 'province_id' => $store->province_id,
  218. // 'city_id' => $store->city_id,
  219. // ]);
  220. //
  221. // if ($check_) {
  222. // $rate = (int)$check_->area_level === 2 ? $city_percent : $province_percent;
  223. // }
  224. // }
  225. $province_id = $store->province_id;
  226. $city_id = $store->city_id;
  227. $district_id = $store->district_id;
  228. $admin_list = Admin::find()->where(['province_id' => $province_id, 'is_delete' => 0])->andWhere(['>', 'area_level', 0])->asArray()->all();
  229. foreach ($admin_list as $item) {
  230. $rate = 0;
  231. $desc = '';
  232. $saas_user_id = $item['saas_user_id'];
  233. $ag_rebate = 0;
  234. //省
  235. if ((int)$item['area_level'] === 3) {
  236. $rate = $province_percent;
  237. $ag_rebate = $order->total_price * ($rate / 100);
  238. $desc = '用户下单,订单' . $order->order_no . ',发放 ' . $ag_rebate . '元省代佣金';
  239. }
  240. //市
  241. if ((int)$item['area_level'] === 2 && (int)$item['city_id'] === (int)$city_id) {
  242. $rate = $city_percent;
  243. $ag_rebate = $order->total_price * ($rate / 100);
  244. $desc = '用户下单,订单' . $order->order_no . ',发放 ' . $ag_rebate . '元市代佣金';
  245. }
  246. //区
  247. if ((int)$item['area_level'] === 1 && (int)$item['city_id'] === (int)$city_id && (int)$item['district_id'] === (int)$district_id) {
  248. $rate = $district_percent;
  249. $ag_rebate = $order->total_price * ($rate / 100);
  250. $desc = '用户下单,订单' . $order->order_no . ',发放 ' . $ag_rebate . '元区代佣金';
  251. }
  252. if ($ag_rebate) {
  253. //给用户增加佣金
  254. $saasUser = SaasUser::findOne($saas_user_id);
  255. $saasUser->share_profit += $ag_rebate;
  256. //增加记录
  257. if ($saasUser->save()) {
  258. $form = new StoreShareMoney();
  259. $form->user_id = $saas_user_id;
  260. $form->store_id = $store->id;
  261. $form->profit = $rate;
  262. $form->total_price = $order->total_price;
  263. $form->desc = $desc;
  264. $form->order_id = $order->id;
  265. $form->created_at = time();
  266. $form->status = StoreShareMoney::STATUS_STORE_ORDER;
  267. $form->commission = $ag_rebate;
  268. $form->type = StoreShareMoney::TYPE_AREA_AGENT;
  269. $form->is_send = 1;
  270. $form->save();
  271. }
  272. }
  273. }
  274. // $new = $rate / 100;
  275. // $saas_user_id = $check_->saas_user_id;
  276. // //给用户增加佣金
  277. // $saasUser = SaasUser::findOne($saas_user_id);
  278. // if($saasUser){
  279. // $ag_rebate = $order->total_price * 1 * $new;
  280. // $saasUser->share_profit += $ag_rebate;
  281. // if ($ag_rebate < 0.01) {
  282. // return;
  283. // }
  284. // //增加记录
  285. // if ($saasUser->save()) {
  286. // $form = new StoreShareMoney();
  287. // $form->user_id = $saas_user_id;
  288. // $form->store_id = $store->id;
  289. // $form->profit = $rate;
  290. // $form->total_price = $order->total_price;
  291. // $form->desc = "用户下单" . $order->order_no;
  292. // $form->order_id = $order->id;
  293. // $form->created_at = time();
  294. // $form->type = 1;
  295. // $form->commission = $ag_rebate;
  296. // $form->status = 1;
  297. // $form->is_send = 1;
  298. // $form->save();
  299. // }
  300. // }
  301. //end
  302. echo 'success';
  303. return;
  304. } else {
  305. echo "支付失败";
  306. return;
  307. }
  308. }
  309. /**
  310. * 合并订单支付回调
  311. */
  312. public function UnionOrderNotify($res)
  313. {
  314. $order_union = OrderUnion::findOne([
  315. 'order_no' => $res['out_trade_no'],
  316. ]);
  317. if ($order_union->is_pay == 1) {
  318. echo "订单已支付";
  319. return;
  320. }
  321. $order_id_list = json_decode($order_union->order_id_list, true);
  322. if (!$order_id_list) {
  323. echo "订单数据错误";
  324. return;
  325. }
  326. if($order_union->type == \app\utils\OrderNo::ORDER_ALLIANCE_PURCHASE){
  327. return $this->APUnionOrderNotify($res);
  328. }
  329. foreach ($order_id_list as $order_id) {
  330. $order = Order::findOne([
  331. 'id' => $order_id,
  332. 'is_pay' => 0,
  333. ]);
  334. if (!$order) {
  335. continue;
  336. }
  337. $order->is_pay = 1;
  338. $order->pay_time = time();
  339. $order->pay_type = $this->pay_type;
  340. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  341. $order->is_delete = 0;
  342. $order->transaction_id = $res['transaction_id'];
  343. if ($order->save()) {
  344. try {
  345. $this->addReceiver($order);
  346. //支付完成之后,相关的操作
  347. $form = new OrderComplete();
  348. $form->store_id = $order->store_id;
  349. $form->order_id = $order->id;
  350. $form->order_type = 0;
  351. $form->notify();
  352. } catch (\Exception $e) {
  353. \Yii::warning(['UNION ORDER EXCEPTION <======> ', $e->getMessage()]);
  354. }
  355. }
  356. }
  357. $order_union->is_pay = 1;
  358. $order_union->save();
  359. echo 'success';
  360. return;
  361. }
  362. /**
  363. * 充值回调
  364. */
  365. public function RechargeOrderNotify($res) {
  366. $order = ReOrder::findOne(['order_no' => $res['out_trade_no']]);
  367. if (!$order) {
  368. return;
  369. }
  370. if ($order->is_pay == 1) {
  371. echo "订单已支付";
  372. return;
  373. }
  374. // 此处取回调过来的金额作为订单金额,防止用户充值时抓包修改充值金额
  375. $order->pay_price = $res['total_fee'] / 100;
  376. $order->save();
  377. $order->is_pay = 1;
  378. $order->pay_time = time();
  379. $order->pay_type = $this->pay_type;
  380. $order->transaction_id = $res['transaction_id'];
  381. if ($order->save()) {
  382. try {
  383. $this->updateMembershipAndCommission($order);
  384. if ($order->is_use_platform_mch == 1) {
  385. $store = Store::findOne($order->store_id);
  386. $profit = $order->pay_price * $store->transfer_profit / 100;
  387. $price = $order->pay_price - $profit;
  388. Store::addMoney($store, $price, '用户充值', $order->id, $order->user_id);
  389. }
  390. } catch (\Exception $e) {
  391. // Todo
  392. }
  393. try {
  394. // 用户充值成为股东
  395. BonusPool::generalUserRecharge($order->user_id, $order->store_id);
  396. } catch (\Exception $e) {}
  397. //金额充值
  398. $user = User::findOne($order->user_id);
  399. $before = $user->money;
  400. $user->money += floatval($order->pay_price) + floatval($order->send_price);
  401. if ($order->send_integral > 0) {
  402. $old_integral = $user->integral;
  403. $user->integral += $order->send_integral;
  404. $user->total_integral += $order->send_integral;
  405. $log = new AccountLog();
  406. $log->store_id = get_store_id();
  407. $log->user_id = $user->id;
  408. $log->type = AccountLog::TYPE_INTEGRAL;
  409. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  410. $log->amount = $order->send_integral;
  411. $log->desc = "充值赠送, 订单号:{$order->order_no}";
  412. $log->before = $old_integral;
  413. $log->after = $user->integral;
  414. $log->operator = 'system';
  415. $log->operator_id = 0;
  416. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  417. $log->created_at = time();
  418. $log->order_id = $order->id;
  419. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  420. $log->save();
  421. }
  422. $user->save();
  423. $log = new AccountLog();
  424. $log->store_id = $order->store_id;
  425. $log->user_id = $user->id;
  426. $log->type = AccountLog::TYPE_BALANCE;
  427. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  428. $log->amount = floatval($order->pay_price) + floatval($order->send_price);
  429. $log->desc = "充值余额微信在线支付,付款金额:{$order->pay_price}元,赠送金额:{$order->send_price}元。";
  430. $log->before = $before;
  431. $log->after = $user->money;
  432. $log->operator = '';
  433. $log->operator_id = 0;
  434. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  435. $log->created_at = time();
  436. $log->order_id = $order->id;
  437. $log->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  438. $log->save();
  439. NoticeSend::UserChang($order->user_id, $user->binding, $order->pay_price, '充值到账');
  440. echo 'success';
  441. try {
  442. $this->ReOrderAfter($order);
  443. $this->addRechargeReceiver($order);
  444. sleep(10);
  445. $event = new OrderEvent();
  446. $event->wechatProfitSharing($order);
  447. } catch (\Exception $e) {
  448. }
  449. // 赠送优惠券
  450. AutoSendCoupon::send($user->id, CouponAutoSend::EVENT_RECHARGE, $user->store_id, $order->pay_price);
  451. } else {
  452. echo "支付失败";
  453. }
  454. return;
  455. }
  456. /**
  457. * 更新用户会员等级和佣金信息
  458. * @param $order 订单对象,用于获取订单相关信息
  459. */
  460. private function updateMembershipAndCommission($order)
  461. {
  462. $t = \Yii::$app->db->beginTransaction();
  463. try {
  464. $user = User::findOne($order->user_id);
  465. $old_level_id = $user->level;
  466. if ($user->level < $order->level_up) {
  467. $user->level = $order->level_up;
  468. }
  469. // 如果用户不是分销商,并且对应的会员等级允许推广
  470. $level = Level::findOne(['level' => $order->level_up]);
  471. if ($user->is_distributor == 0 && $level->promotion_status == 1) {
  472. $user->is_distributor = 1; // 更新用户为分销商
  473. $share = Share::findOne(['user_id' => $user->id, 'store_id' => $user->store_id, 'is_delete' => 0]);
  474. if (!$share) {
  475. $share = new Share();
  476. $share->user_id = $user->id;
  477. $share->mobile = $user->binding;
  478. $share->name = $user->nickname;
  479. $share->is_delete = 0;
  480. $share->store_id = $user->store_id;
  481. $share->status = 1;
  482. $share->created_at = time();
  483. if (!$share->save()) {
  484. debug_log('添加用户分销商信息失败' . $share->getErrors(), 'wx.log');
  485. }
  486. }
  487. }
  488. if (!$user->save()) {
  489. debug_log('更新用户会员等级失败' . $user->getErrors(), 'wx.log');
  490. }
  491. $upgradeRecord = new MemberUpgradeRecord([
  492. 'user_id' => $order->user_id,
  493. 'store_id' => $order->store_id,
  494. 'old_level_id' => $old_level_id,
  495. 'new_level_id' => $order->level_up,
  496. 'recharge_id' => $order->recharge_id,
  497. ]);
  498. if (!$upgradeRecord->save()) {
  499. debug_log('记录会员等级变更失败' . $upgradeRecord->getErrors(), 'wx.log');
  500. }
  501. if ($user->parent_id != 0) {
  502. $recharge = Recharge::findOne($order->recharge_id);
  503. // 计算应发放给上级的佣金金额,根据被推荐人的数量计算
  504. $referrals_count = RcCommissionLog::find()->where(['parent_id' => $user->parent_id, 'type' => 21])->count();
  505. $referrer = User::findOne($user->parent_id);
  506. // 上级是代理才发放佣金
  507. if ($referrer->is_distributor == 1) {
  508. // 如果用户成为代理,发放推广佣金
  509. $commission = json_decode($recharge->referral_commission, true);
  510. $commission_amount = 0;
  511. if ($user->is_distributor == 1 && !empty($commission)) {
  512. // 发放推广佣金的逻辑...
  513. $length = count($commission);
  514. $index = ($referrals_count) % $length;
  515. $value = $commission[$index];
  516. $commission_amount = $value['value'];
  517. if ($commission_amount > 0) {
  518. $log = new RcCommissionLog();
  519. $log->user_id = $user->id;
  520. $log->store_id = get_store_id();
  521. $log->amount = $commission_amount;
  522. $log->desc = "充值余额" . $recharge->name . "套餐赠送推广佣金, 订单号:{$order->order_no}";
  523. $log->recharge_id = 1;
  524. $log->commission_type = 1;
  525. $log->created_at = time();
  526. $log->order_id = $order->id;
  527. $log->parent_id = $user->parent_id;
  528. $log->recharge_id = $order->recharge_id;
  529. $log->bind_type = 1;
  530. $log->operator_type = RcCommissionLog::TYPE_OPERATOR_NORMAL;
  531. $log->order_type = RcCommissionLog::TYPE_RECHARGE_ORDER;
  532. $log->log_type = RcCommissionLog::LOG_TYPE_INCOME;
  533. $log->type = RcCommissionLog::TYPE_REFERRAL_COMMISSION;
  534. if (!$log->save()) {
  535. throw new \Exception('充值余额赠送佣金失败' . $log->getErrors());
  536. }
  537. $referrer->total_price += $commission_amount;
  538. $referrer->price += $commission_amount;
  539. if (!$referrer->save()) {
  540. throw new \Exception('增加上级推广佣金失败' . $referrer->getErrors());
  541. }
  542. $UserShareMoney = new UserShareMoney();
  543. $UserShareMoney->store_id = get_store_id();
  544. $UserShareMoney->order_id = $order->id;
  545. $UserShareMoney->user_id = $user->parent_id;
  546. $UserShareMoney->type = 0;
  547. $UserShareMoney->source = 7; //推广佣金
  548. $UserShareMoney->money = $commission_amount;
  549. $UserShareMoney->created_at = time();
  550. $UserShareMoney->order_type = 5; //充值订单
  551. $UserShareMoney->is_delete = 0;
  552. $UserShareMoney->desc = "充值余额" . $recharge->name . "套餐赠送推广佣金, 订单号:{$order->order_no}";
  553. $UserShareMoney->version = cyy_version();
  554. if (!$UserShareMoney->save()) {
  555. throw new \Exception('用户推广佣金记录失败' . $referrer->getErrors());
  556. }
  557. } else {
  558. $commission_amount_one = $recharge->level_one_bonus;
  559. $commission_amount_two = $recharge->level_two_bonus;
  560. if ($commission_amount_one > 0 && $user->parent_id > 0) {
  561. $log = new RcCommissionLog();
  562. $log->user_id = $user->id;
  563. $log->store_id = get_store_id();
  564. $log->amount = $commission_amount_one;
  565. $log->desc = "充值余额" . $recharge->name . "套餐赠送一级返现奖励, 订单号:{$order->order_no}";
  566. $log->commission_type = 1;
  567. $log->created_at = time();
  568. $log->order_id = $order->id;
  569. $log->parent_id = $user->parent_id;
  570. $log->recharge_id = $order->recharge_id;
  571. $log->bind_type = 1;
  572. $log->operator_type = RcCommissionLog::TYPE_OPERATOR_NORMAL;
  573. $log->order_type = RcCommissionLog::TYPE_RECHARGE_ORDER;
  574. $log->log_type = RcCommissionLog::LOG_TYPE_INCOME;
  575. $log->type = RcCommissionLog::TYPE_LEVEL_ONE_BONUS;
  576. if (!$log->save()) {
  577. throw new \Exception('充值余额赠送一级返现奖励失败' . $log->getErrors());
  578. }
  579. $referrer->total_price += $commission_amount_one;
  580. $referrer->price += $commission_amount_one;
  581. if (!$referrer->save()) {
  582. throw new \Exception('增加上级返现奖励失败' . $referrer->getErrors());
  583. }
  584. $UserShareMoney = new UserShareMoney();
  585. $UserShareMoney->store_id = get_store_id();
  586. $UserShareMoney->order_id = $order->id;
  587. $UserShareMoney->user_id = $user->parent_id;
  588. $UserShareMoney->type = 0;
  589. $UserShareMoney->source = 8; // 一级返现奖励
  590. $UserShareMoney->money = $commission_amount_one;
  591. $UserShareMoney->created_at = time();
  592. $UserShareMoney->order_type = 5; //充值订单
  593. $UserShareMoney->is_delete = 0;
  594. $UserShareMoney->version = cyy_version();
  595. $UserShareMoney->desc = "充值余额" . $recharge->name . "套餐赠送一级返现奖励, 订单号:{$order->order_no}";
  596. if (!$UserShareMoney->save()) {
  597. throw new \Exception('用户一级返现奖励记录失败' . $referrer->getErrors());
  598. }
  599. }
  600. if ($commission_amount_two > 0 && $referrer->parent_id > 0) {
  601. $log = new RcCommissionLog();
  602. $log->user_id = $user->id;
  603. $log->store_id = get_store_id();
  604. $log->amount = $commission_amount_two;
  605. $log->desc = "充值余额" . $recharge->name . "套餐赠送二级返现奖励, 订单号:{$order->order_no}";
  606. $log->commission_type = 1;
  607. $log->created_at = time();
  608. $log->order_id = $order->id;
  609. $log->parent_id = $referrer->parent_id;
  610. $log->recharge_id = $order->recharge_id;
  611. $log->bind_type = 2;
  612. $log->operator_type = RcCommissionLog::TYPE_OPERATOR_NORMAL;
  613. $log->order_type = RcCommissionLog::TYPE_RECHARGE_ORDER;
  614. $log->log_type = RcCommissionLog::LOG_TYPE_INCOME;
  615. $log->type = RcCommissionLog::TYPE_LEVEL_TWO_BONUS;
  616. if (!$log->save()) {
  617. throw new \Exception('充值余额赠送二级返现奖励失败' . $log->getErrors());
  618. }
  619. $referrer = User::findOne($referrer->parent_id);
  620. $referrer->total_price += $commission_amount_two;
  621. $referrer->price += $commission_amount_two;
  622. if (!$referrer->save()) {
  623. throw new \Exception('增加上级返现奖励失败' . $referrer->getErrors());
  624. }
  625. $UserShareMoney = new UserShareMoney();
  626. $UserShareMoney->store_id = get_store_id();
  627. $UserShareMoney->order_id = $order->id;
  628. $UserShareMoney->user_id = $referrer->parent_id;
  629. $UserShareMoney->type = 0;
  630. $UserShareMoney->source = 9; //二级返现奖励
  631. $UserShareMoney->money = $commission_amount_two;
  632. $UserShareMoney->created_at = time();
  633. $UserShareMoney->order_type = 5; // 充值订单
  634. $UserShareMoney->is_delete = 0;
  635. $UserShareMoney->version = cyy_version();
  636. $UserShareMoney->desc = "充值余额" . $recharge->name . "套餐赠送二级返现奖励, 订单号:{$order->order_no}";
  637. if (!$UserShareMoney->save()) {
  638. throw new \Exception('用户二级返现奖励记录失败' . $referrer->getErrors());
  639. }
  640. }
  641. }
  642. } else { // 如果用户没有成为代理,发放一二级佣金
  643. $commission_amount_one = $recharge->level_one_bonus;
  644. $commission_amount_two = $recharge->level_two_bonus;
  645. if ($commission_amount_one > 0 && $user->parent_id > 0) {
  646. $log = new RcCommissionLog();
  647. $log->user_id = $user->id;
  648. $log->store_id = get_store_id();
  649. $log->amount = $commission_amount_one;
  650. $log->desc = "充值余额" . $recharge->name . "套餐赠送一级返现奖励, 订单号:{$order->order_no}";
  651. $log->commission_type = 1;
  652. $log->created_at = time();
  653. $log->order_id = $order->id;
  654. $log->parent_id = $user->parent_id;
  655. $log->recharge_id = $order->recharge_id;
  656. $log->bind_type = 1;
  657. $log->operator_type = RcCommissionLog::TYPE_OPERATOR_NORMAL;
  658. $log->order_type = RcCommissionLog::TYPE_RECHARGE_ORDER;
  659. $log->log_type = RcCommissionLog::LOG_TYPE_INCOME;
  660. $log->type = RcCommissionLog::TYPE_LEVEL_ONE_BONUS;
  661. if (!$log->save()) {
  662. throw new \Exception('充值余额赠送一级返现奖励失败' . $log->getErrors());
  663. }
  664. $referrer->total_price += $commission_amount_one;
  665. $referrer->price += $commission_amount_one;
  666. if (!$referrer->save()) {
  667. throw new \Exception('增加上级返现奖励失败' . $referrer->getErrors());
  668. }
  669. $UserShareMoney = new UserShareMoney();
  670. $UserShareMoney->store_id = get_store_id();
  671. $UserShareMoney->order_id = $order->id;
  672. $UserShareMoney->user_id = $user->parent_id;
  673. $UserShareMoney->type = 0;
  674. $UserShareMoney->source = 8; // 一级返现奖励
  675. $UserShareMoney->money = $commission_amount_one;
  676. $UserShareMoney->created_at = time();
  677. $UserShareMoney->order_type = 5; //充值订单
  678. $UserShareMoney->is_delete = 0;
  679. $UserShareMoney->version = cyy_version();
  680. $UserShareMoney->desc = "充值余额" . $recharge->name . "套餐赠送一级返现奖励, 订单号:{$order->order_no}";
  681. if (!$UserShareMoney->save()) {
  682. throw new \Exception('用户一级返现奖励记录失败' . $referrer->getErrors());
  683. }
  684. }
  685. if ($commission_amount_two > 0 && $referrer->parent_id > 0) {
  686. $log = new RcCommissionLog();
  687. $log->user_id = $user->id;
  688. $log->store_id = get_store_id();
  689. $log->amount = $commission_amount_two;
  690. $log->desc = "充值余额" . $recharge->name . "套餐赠送二级返现奖励, 订单号:{$order->order_no}";
  691. $log->commission_type = 1;
  692. $log->created_at = time();
  693. $log->order_id = $order->id;
  694. $log->parent_id = $referrer->parent_id;
  695. $log->recharge_id = $order->recharge_id;
  696. $log->bind_type = 2;
  697. $log->operator_type = RcCommissionLog::TYPE_OPERATOR_NORMAL;
  698. $log->order_type = RcCommissionLog::TYPE_RECHARGE_ORDER;
  699. $log->log_type = RcCommissionLog::LOG_TYPE_INCOME;
  700. $log->type = RcCommissionLog::TYPE_LEVEL_TWO_BONUS;
  701. if (!$log->save()) {
  702. throw new \Exception('充值余额赠送二级返现奖励失败' . $log->getErrors());
  703. }
  704. $referrer = User::findOne($referrer->parent_id);
  705. $referrer->total_price += $commission_amount_two;
  706. $referrer->price += $commission_amount_two;
  707. if (!$referrer->save()) {
  708. throw new \Exception('增加上级返现奖励失败' . $referrer->getErrors());
  709. }
  710. $UserShareMoney = new UserShareMoney();
  711. $UserShareMoney->store_id = get_store_id();
  712. $UserShareMoney->order_id = $order->id;
  713. $UserShareMoney->user_id = $referrer->parent_id;
  714. $UserShareMoney->type = 0;
  715. $UserShareMoney->source = 9; //二级返现奖励
  716. $UserShareMoney->money = $commission_amount_two;
  717. $UserShareMoney->created_at = time();
  718. $UserShareMoney->order_type = 5; // 充值订单
  719. $UserShareMoney->is_delete = 0;
  720. $UserShareMoney->version = cyy_version();
  721. $UserShareMoney->desc = "充值余额" . $recharge->name . "套餐赠送二级返现奖励, 订单号:{$order->order_no}";
  722. if (!$UserShareMoney->save()) {
  723. throw new \Exception('用户二级返现奖励记录失败' . $referrer->getErrors());
  724. }
  725. }
  726. }
  727. }
  728. }
  729. $t->commit();
  730. } catch (\Exception $e) {
  731. $t->rollBack();
  732. throw $e;
  733. }
  734. }
  735. /**
  736. * 积分充值回调
  737. */
  738. public function IntegralRechargeOrderNotify($res) {
  739. $order = IntegralRechargeOrder::findOne(['order_no' => $res['out_trade_no']]);
  740. if (!$order) {
  741. return;
  742. }
  743. if ($order->is_pay == 1) {
  744. echo "订单已支付";
  745. return;
  746. }
  747. $order->is_pay = 1;
  748. $order->pay_time = time();
  749. $order->pay_type = $this->pay_type;
  750. $order->transaction_id = $res['transaction_id'];
  751. if ($order->save()) {
  752. //金额充值
  753. $user = User::findOne($order->user_id);
  754. if ($order->send_integral > 0) {
  755. $old_integral = $user->integral;
  756. $user->integral += $order->send_integral;
  757. $user->total_integral += $order->send_integral;
  758. $log = new AccountLog();
  759. $log->store_id = get_store_id();
  760. $log->user_id = $user->id;
  761. $log->type = AccountLog::TYPE_INTEGRAL;
  762. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  763. $log->amount = $order->send_integral;
  764. $log->desc = "充值积分, 订单号:{$order->order_no}";
  765. $log->before = $old_integral;
  766. $log->after = $user->integral;
  767. $log->operator = 'system';
  768. $log->operator_id = 0;
  769. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  770. $log->created_at = time();
  771. $log->order_id = $order->id;
  772. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  773. $log->save();
  774. }
  775. $user->save();
  776. NoticeSend::UserChang($order->user_id, $user->binding, $order->send_integral, '充值积分到账');
  777. echo 'success';
  778. $this->addIntegralRechargeReceiver($order);
  779. sleep(10);
  780. $event = new OrderEvent();
  781. $event->wechatProfitSharing($order);
  782. } else {
  783. echo "支付失败";
  784. }
  785. return;
  786. }
  787. private function addIntegralRechargeReceiver($order) {
  788. if (!\Yii::$app->isSaas()) {
  789. return;
  790. }
  791. $exists = IntegralRecharge::findOne($order->recharge_id);
  792. // 一级佣金发放
  793. $user_1 = User::findOne($order->user_id);
  794. if ($user_1->parent_id <= 0) {
  795. \Yii::warning('<============= 订单:' . $order->order_no . '不存在店铺分销关系');
  796. return;
  797. }
  798. $user_1 = User::findOne($user_1->parent_id);
  799. if (!$user_1) {
  800. return;
  801. }
  802. $user_1_money = round($order->pay_price * ($exists->first_commission / 100), 2);
  803. if ($user_1_money > 0.00) {
  804. $old_integral = $user_1->integral;
  805. $user_1->integral += $user_1_money;
  806. $user_1->total_integral += $user_1_money;
  807. $log = new AccountLog();
  808. $log->store_id = get_store_id();
  809. $log->user_id = $user_1->id;
  810. $log->type = AccountLog::TYPE_INTEGRAL;
  811. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  812. $log->amount = $order->send_integral;
  813. $log->desc = "充值积分分销, 订单号:{$order->order_no}";
  814. $log->before = $old_integral;
  815. $log->after = $user_1->integral;
  816. $log->operator = 'system';
  817. $log->operator_id = 0;
  818. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  819. $log->created_at = time();
  820. $log->order_id = $order->id;
  821. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  822. $log->save();
  823. }
  824. //二级佣金发放
  825. $user_2 = User::findOne($user_1->parent_id);
  826. if (!$user_2) {
  827. return;
  828. }
  829. $user_2_money = round($order->pay_price * ($exists->second_commission / 100), 2);
  830. if ($user_2_money > 0.00) {
  831. $old_integral = $user_2->integral;
  832. $user_2->integral += $user_2_money;
  833. $user_2->total_integral += $user_2_money;
  834. $log = new AccountLog();
  835. $log->store_id = get_store_id();
  836. $log->user_id = $user_2->id;
  837. $log->type = AccountLog::TYPE_INTEGRAL;
  838. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  839. $log->amount = $order->send_integral;
  840. $log->desc = "充值积分分销, 订单号:{$order->order_no}";
  841. $log->before = $old_integral;
  842. $log->after = $user_2->integral;
  843. $log->operator = 'system';
  844. $log->operator_id = 0;
  845. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  846. $log->created_at = time();
  847. $log->order_id = $order->id;
  848. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  849. $log->save();
  850. }
  851. //三级佣金发放
  852. $user_3 = User::findOne($user_2->parent_id);
  853. if (!$user_3) {
  854. return;
  855. }
  856. $user_3_money = round($order->pay_price * ($exists->third_commission / 100), 2);
  857. if ($user_3_money > 0.00) {
  858. $old_integral = $user_3->integral;
  859. $user_3->integral += $user_3_money;
  860. $user_3->total_integral += $user_3_money;
  861. $log = new AccountLog();
  862. $log->store_id = get_store_id();
  863. $log->user_id = $user_3->id;
  864. $log->type = AccountLog::TYPE_INTEGRAL;
  865. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  866. $log->amount = $order->send_integral;
  867. $log->desc = "充值积分分销, 订单号:{$order->order_no}";
  868. $log->before = $old_integral;
  869. $log->after = $user_3->integral;
  870. $log->operator = 'system';
  871. $log->operator_id = 0;
  872. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  873. $log->created_at = time();
  874. $log->order_id = $order->id;
  875. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  876. $log->save();
  877. }
  878. }
  879. /**
  880. * @param $order
  881. * 充值订单回调
  882. */
  883. public function ReOrderAfter($order) {
  884. if ($order) {
  885. $total_price = 0;
  886. $orderAfter = null;
  887. if ((int)$order->type === 0 && $order->order_id) {
  888. $orderAfter = \app\plugins\scanCodePay\models\Order::findOne($order->order_id);
  889. if (!$orderAfter) {
  890. return;
  891. }
  892. $total_price = $orderAfter->total_price;
  893. }
  894. if ($total_price) {
  895. $user = User::findOne($order->user_id);
  896. if ($user) {
  897. $before = $user->money;
  898. $money = $user->money - $total_price;
  899. if ($money >= 0) {
  900. $user->money = $money;
  901. if ($user->save()) {
  902. $orderAfter->is_pay = 1;
  903. $orderAfter->pay_time = time();
  904. if ($orderAfter->save()) {
  905. if (intval($orderAfter->user_coupon_id) > 0) {
  906. UserCoupon::updateAll(['is_use' => 1], ['id' => $orderAfter->user_coupon_id]);
  907. }
  908. $form = new NewMerchantForm();
  909. $form->store_id = $orderAfter->store_id;
  910. $form->saasGiveIntegral($orderAfter->pay_price, $orderAfter->user_id, $orderAfter->order_no);
  911. $form_ = new \app\modules\client\models\v1\NewMerchantForm();
  912. $form_->order_no = $orderAfter->order_no;
  913. $form->payResult();
  914. $log = new AccountLog();
  915. $log->store_id = $order->store_id;
  916. $log->user_id = $user->id;
  917. $log->type = AccountLog::TYPE_BALANCE;
  918. $log->log_type = AccountLog::LOG_TYPE_EXPEND;
  919. $log->amount = floatval($orderAfter->total_price);
  920. $log->desc = "当面付微信在线支付,订单编号为" . $orderAfter->order_no;
  921. $log->before = $before;
  922. $log->after = $user->money;
  923. $log->operator = '';
  924. $log->operator_id = 0;
  925. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  926. $log->created_at = time();
  927. $log->order_id = $order->id;
  928. $log->order_type = AccountLog::TYPE_SCAN_CODE_PAY;
  929. $log->save();
  930. };
  931. };
  932. }
  933. }
  934. }
  935. }
  936. }
  937. public function LPorderNotify($res)
  938. {
  939. $order = RechargeReOrder::findOne(['order_no' => $res['out_trade_no']]);
  940. if (!$order) {
  941. return;
  942. }
  943. if ($order->is_pay == 1) {
  944. echo "订单已支付";
  945. return;
  946. }
  947. $order->is_pay = 1;
  948. $order->pay_time = time();
  949. $order->pay_type = $this->pay_type;
  950. $order->transaction_id = $res['transaction_id'];
  951. if ($order->save()) {
  952. //联盟券充值
  953. $store_profit_max_option_model = Option::findOne(['name' => 'store_profit_max','store_id' => 0]);
  954. $recharge_profit = $store_profit_max_option_model->value;
  955. $league_price = $order->league_price;
  956. $store = Store::findOne($order->store_id);
  957. //给商家返券
  958. $before = $store->league_price;
  959. $store->updateCounters(['league_price' => floatval($league_price)]);
  960. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  961. $order->store_id,
  962. 0,
  963. $league_price,
  964. $before,
  965. \app\models\SaaSLeaguePriceLog::TYPE_LEAGUE_RECHARGE,
  966. \app\models\SaaSLeaguePriceLog::SEND_TYPE,
  967. \app\models\SaaSLeaguePriceLog::ROLE_STORE,
  968. $order->id
  969. );
  970. echo 'success';
  971. return;
  972. } else {
  973. echo "支付失败";
  974. return;
  975. }
  976. }
  977. /**
  978. * 充值回调
  979. */
  980. public function PROrderNotify($res) {
  981. $order = PurchaseReOrder::findOne(['order_no' => $res['out_trade_no']]);
  982. if (!$order) {
  983. return;
  984. }
  985. if ($order->is_pay == 1) {
  986. echo "订单已支付";
  987. return;
  988. }
  989. $order->is_pay = 1;
  990. $order->pay_time = time();
  991. $order->pay_type = $this->pay_type;
  992. $order->transaction_id = $res['transaction_id'];
  993. if ($order->save()) {
  994. //金额充值
  995. $form = new BalanceForm();
  996. $form->store_id = $order->store_id;
  997. $form->saas_id = $order->saas_id;
  998. $form->order = $order;
  999. $result = $form->storeSubmitRecharge();
  1000. if($result['code'] != 0){
  1001. $order->err = '支付成功,但是余额数据修改失败,原因:' . $result['msg'];
  1002. $order->save();
  1003. \Yii::error([__METHOD__, $res, $result, $order]);
  1004. }
  1005. echo 'success';
  1006. return;
  1007. } else {
  1008. echo "支付失败";
  1009. return;
  1010. }
  1011. }
  1012. /**
  1013. * 会员购买回调
  1014. */
  1015. public function LevelOrderNotify($res) {
  1016. $order = LevelOrder::findOne([
  1017. 'order_no' => $res['out_trade_no'],
  1018. ]);
  1019. if (!$order) {
  1020. return;
  1021. }
  1022. if ($order->is_pay == 1) {
  1023. echo "订单已支付";
  1024. return;
  1025. }
  1026. $order->is_pay = 1;
  1027. $order->pay_time = time();
  1028. $order->pay_type = $this->pay_type;
  1029. if ($order->save()) {
  1030. //会员升级
  1031. $user = User::findOne($order->user_id);
  1032. $user->level = $order->after_level;
  1033. $user->save();
  1034. try {
  1035. if ($order->is_use_platform_mch == 1) {
  1036. $store = Store::findOne($order->store_id);
  1037. $profit = $order->pay_price * $store->transfer_profit / 100;
  1038. $price = $order->pay_price - $profit;
  1039. Store::addMoney($store, $price, '用户购买会员等级', $order->id, $order->user_id);
  1040. }
  1041. } catch (\Exception $e) {
  1042. // Todo
  1043. }
  1044. $level = Level::findOne(['level' => $user->level, 'store_id' => $order->store_id, 'is_delete' => 0]);
  1045. if ($level) {
  1046. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  1047. $user_name = $user->nickname;
  1048. if ($saas_user) {
  1049. $user_name = $saas_user->name;
  1050. }
  1051. if ($user->old_parent_id) {
  1052. $parent = User::findOne($user->old_parent_id);//上级
  1053. //检测一级是否存在且佣金是否设置
  1054. if ($parent && $level->firstProfit) {
  1055. //发放一级佣金
  1056. //...
  1057. $parent->total_price += $level->firstProfit;
  1058. $parent->price += $level->firstProfit;
  1059. $parent->save();
  1060. UserShareMoney::set($level->firstProfit, $user->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放一级佣金');
  1061. if ($parent->old_parent_id) {
  1062. $parent_1 = User::findOne($parent->old_parent_id);//上级
  1063. //检测二级是否存在且佣金是否设置
  1064. if ($parent_1 && $level->secondProfit) {
  1065. //发放二级佣金
  1066. $parent_1->total_price += $level->secondProfit;
  1067. $parent_1->price += $level->secondProfit;
  1068. $parent_1->save();
  1069. UserShareMoney::set($level->secondProfit, $parent->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放二级佣金');
  1070. //检测三级是否存在且佣金是否设置
  1071. $parent_2 = User::findOne($parent_1->old_parent_id);//上级
  1072. if ($parent_2 && $level->thirdProfit) {
  1073. //发放三级佣金
  1074. $parent_2->total_price += $level->thirdProfit;
  1075. $parent_2->price += $level->thirdProfit;
  1076. $parent_2->save();
  1077. UserShareMoney::set($level->thirdProfit, $parent_1->old_parent_id, $order->id, 0, 1, $order->store_id, UserShareMoney::LEVEL_ORDER_TYPE, '用户' . $user_name . '购买会员发放三级佣金');
  1078. }
  1079. }
  1080. }
  1081. }
  1082. }
  1083. }
  1084. echo 'success';
  1085. return;
  1086. } else {
  1087. echo "支付失败";
  1088. return;
  1089. }
  1090. }
  1091. /**
  1092. * saas联盟佣金提现回调
  1093. */
  1094. public function saasProfitCash($res) {
  1095. $order = SaasProfitCash::findOne([
  1096. 'order_no' => $res['out_trade_no'],
  1097. ]);
  1098. if (!$order) {
  1099. return;
  1100. }
  1101. $order->is_pay = 1;
  1102. $order->pay_time = time();
  1103. if ($order->save()) {
  1104. echo 'success';
  1105. return;
  1106. } else {
  1107. echo "支付失败";
  1108. return;
  1109. }
  1110. }
  1111. /**
  1112. * 商城订单回调
  1113. * @param $res
  1114. */
  1115. public function MallOrderNotify($res) {
  1116. $order = Order::findOne([
  1117. 'order_no' => $res['out_trade_no'],
  1118. ]);
  1119. $orderPayType = $order->pay_type;
  1120. if ($order->is_pay == 1) {
  1121. echo "订单已支付";
  1122. return;
  1123. }
  1124. $order->is_pay = 1;
  1125. $order->pay_time = time();
  1126. $order->pay_type = $this->pay_type;
  1127. // 如果是货到付款的支付 支付成功后修改订单状态为已完成
  1128. if($orderPayType == 2){
  1129. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  1130. $order->send_time = time();
  1131. $order->confirm_time = time();
  1132. }else{
  1133. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  1134. }
  1135. $order->is_delete = 0;
  1136. $order->transaction_id = $res['transaction_id'];
  1137. $res['trxid'] && $order->allinpay_payment_id = $res['trxid'];
  1138. $res['trxcode'] && $order->allinpay_trxcode = $res['trxcode'];
  1139. if ($order->save()) {
  1140. // // 同城配送订单下单
  1141. // if ($order->is_delivery == 1) {
  1142. // if($order->alipay_trade_no){
  1143. // $delivery_info = DeliveryInfo::findOne(['store_id' => $order->store_id, 'order_no' => $order->order_no]);
  1144. // $detail_goods = OrderDetail::find()->where(['order_id' => $order->id])->select('goods_name as name, pic, num, total_price as price')->asArray()->all();
  1145. // $delivery_res = ADelivery::createOrder(0, $order->store_id, $delivery_info->mini_id,
  1146. // $order->user->alipay_open_id, $detail_goods, $order->order_no, $delivery_info->address_id);
  1147. // \Yii::error('-----------同城配送返回' . Json::encode($delivery_res));
  1148. // }else{
  1149. // $detail_goods = OrderDetail::find()->where(['order_id' => $order->id])->select('goods_name as name, pic, num, total_price as price')->asArray()->all();
  1150. // $delivery_res = Delivery::addOrder($detail_goods, $order->order_no);
  1151. // \Yii::error('-----------同城配送返回' . Json::encode($delivery_res));
  1152. // }
  1153. // }
  1154. if ($order->level_order_id) {
  1155. $levelOrder = LevelOrder::findOne($order->level_order_id);
  1156. $this->LevelOrderNotify(['out_trade_no' => $levelOrder->order_no]);
  1157. }
  1158. // 预约商品库存操作
  1159. BookOrderNum::bookNumReduce($order);
  1160. $this->addReceiver($order);
  1161. $this->OrderVoucherUse($order);
  1162. //支付完成之后,相关的操作
  1163. //抖品佣金处理
  1164. $this->videoGoodsShare($order);
  1165. $form = new OrderComplete(['notify_res' => $res]);
  1166. $form->store_id = $order->store_id;
  1167. $form->order_id = $order->id;
  1168. $form->order_type = 0;
  1169. $form->notify();
  1170. echo 'success';
  1171. return;
  1172. } else {
  1173. echo "支付失败";
  1174. return;
  1175. }
  1176. }
  1177. /**
  1178. * 采购员单回调
  1179. * @param $res
  1180. */
  1181. public function APOrderNotify($res) {
  1182. $order = PurchaseOrder::findOne([
  1183. 'order_no' => $res['out_trade_no'],
  1184. ]);
  1185. if ($order->is_pay == 1) {
  1186. echo "订单已支付";
  1187. return;
  1188. }
  1189. $order->is_pay = 1;
  1190. $order->pay_time = time();
  1191. $order->pay_type = $this->pay_type;
  1192. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  1193. $order->is_delete = 0;
  1194. $order->transaction_id = $res['transaction_id'];
  1195. if ($order->save()) {
  1196. //支付完成之后,相关的操作
  1197. OrderPayDataForm::notify($order->id);
  1198. echo 'success';
  1199. return;
  1200. } else {
  1201. \Yii::error('-----------PurchaseOrder保存失败' . Json::encode($order->getErrors()));
  1202. echo "支付失败";
  1203. return;
  1204. }
  1205. }
  1206. /**
  1207. * 采购员单回调
  1208. * @param $res
  1209. */
  1210. public function APUnionOrderNotify($res)
  1211. {
  1212. $order_union = OrderUnion::findOne([
  1213. 'order_no' => $res['out_trade_no'],
  1214. ]);
  1215. if ($order_union->is_pay == 1) {
  1216. echo "订单已支付";
  1217. return;
  1218. }
  1219. $order_id_list = json_decode($order_union->order_id_list, true);
  1220. if (!$order_id_list) {
  1221. echo "订单数据错误";
  1222. return;
  1223. }
  1224. foreach ($order_id_list as $order_id) {
  1225. $order = PurchaseOrder::findOne([
  1226. 'id' => $order_id,
  1227. 'is_pay' => 0,
  1228. ]);
  1229. if (!$order) {
  1230. continue;
  1231. }
  1232. $order->is_pay = 1;
  1233. $order->pay_time = time();
  1234. $order->pay_type = $this->pay_type;
  1235. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  1236. $order->is_delete = 0;
  1237. $order->transaction_id = $res['transaction_id'];
  1238. $order->save();
  1239. }
  1240. try {
  1241. OrderPayDataForm::notify($order->id, $order_id_list);
  1242. } catch (\Exception $e) {
  1243. \Yii::warning(['UNION ORDER EXCEPTION <======> ', $e->getMessage()]);
  1244. }
  1245. $order_union->is_pay = 1;
  1246. $order_union->save();
  1247. echo 'success';
  1248. return;
  1249. }
  1250. /**
  1251. * 商盟订单回调
  1252. * @param $res
  1253. */
  1254. public function BusinessNotify($res) {
  1255. $order = Order::findOne([
  1256. 'order_no' => $res['out_trade_no'],
  1257. ]);
  1258. if ($order->is_pay == 1) {
  1259. echo "订单已支付";
  1260. return;
  1261. }
  1262. $order->is_pay = 1;
  1263. $order->pay_time = time();
  1264. $order->pay_type = $this->pay_type;
  1265. $order->trade_status = Order::ORDER_FLOW_NO_SEND;
  1266. $order->is_delete = 0;
  1267. $order->transaction_id = $res['transaction_id'];
  1268. if ($order->save()) {
  1269. // 同城配送订单下单
  1270. // 预约商品库存操作
  1271. BookOrderNum::bookNumReduce($order);
  1272. //商盟订单
  1273. $this->addReceiver($order);
  1274. // 支付完成之后,相关的操作
  1275. $form = new AllianceOrderComplete();
  1276. $form->store_id = $order->store_id;
  1277. $form->order_id = $order->id;
  1278. $form->order_type = 0;
  1279. $form->notify();
  1280. echo 'success';
  1281. return;
  1282. } else {
  1283. echo "支付失败";
  1284. return;
  1285. }
  1286. }
  1287. /**
  1288. * 入驻商提现支付宝回调
  1289. * @param $res
  1290. */
  1291. public function batchTransNotify($res)
  1292. {
  1293. $cash = MchCash::findOne(['order_no' => $res['out_batch_no']]);
  1294. $cash->updated_at = time();
  1295. $cash->virtual_type = 2;
  1296. if ($res['batch_status'] == 'SUCCESS' && $res['fail_count'] == '0') {
  1297. $cash->status = 1;
  1298. $cash->save(false);
  1299. echo 'success';
  1300. return;
  1301. } else if ($res['batch_status'] == 'FAIL' && $res['fail_count'] == '1'){
  1302. $cash->status = 2;
  1303. $cash->save();
  1304. echo 'success';
  1305. return;
  1306. } else {
  1307. echo 'success';
  1308. return;
  1309. }
  1310. }
  1311. /**
  1312. * @param $res
  1313. */
  1314. public function ScanOrderNotify($res) {
  1315. $order = \app\plugins\scanCodePay\models\Order::findOne([
  1316. 'order_no' => $res['out_trade_no'],
  1317. ]);
  1318. $this->is_scan = 1;
  1319. if ($order->is_pay == 1) {
  1320. echo "订单已支付";
  1321. return;
  1322. }
  1323. $order->is_pay = 1;
  1324. $order->pay_time = time();
  1325. $order->pay_type = $this->pay_type;
  1326. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  1327. $order->is_delete = 0;
  1328. $order->transaction_id = $res['transaction_id'];
  1329. if ($order->save()) {
  1330. if (intval($order->user_coupon_id) > 0) {
  1331. UserCoupon::updateAll(['is_use' => 1], ['id' => $order->user_coupon_id]);
  1332. }
  1333. //
  1334. if (intval($order->is_platform) === 1) {
  1335. $form = new \app\modules\alliance\models\NewMerchantForm();
  1336. $form->store_id = $order->store_id;
  1337. $result = $form->saasGiveIntegral($order->pay_price, $order->user_id, $order->order_no);
  1338. } else {
  1339. $form = new \app\modules\client\models\v1\NewMerchantForm();
  1340. }
  1341. $form->order_no = $res['out_trade_no'];
  1342. $form->payResult();
  1343. //计算余额抵扣的钱
  1344. if ($order->is_combine_pay && $order->combine_money > 0 && (int)$order->is_reduce_combine === 1) {
  1345. $user = User::findOne($order->user_id);
  1346. $user->money -= $order->combine_money;
  1347. $user->save();
  1348. $order->is_reduce_combine = 1;
  1349. $order->save();
  1350. }
  1351. try {
  1352. if ($order->is_use_platform_mch == 1) {
  1353. $store = Store::findOne($order->store_id);
  1354. $profit = $order->pay_price * $store->transfer_profit / 100;
  1355. $price = $order->pay_price - $profit;
  1356. Store::addMoney($store, $price, '用户当面付', $order->id, $order->user_id);
  1357. }
  1358. } catch (\Exception $e) {
  1359. // Todo
  1360. }
  1361. //商盟订单
  1362. if (Store::hasIncoming($order->store_id) || intval($order->pay_type) === Order::PAY_TYPE_YUNST_WECHAT_PAY) {
  1363. $this->addReceiver($order);
  1364. }
  1365. // sleep(10);
  1366. // $event = new OrderEvent();
  1367. // $event->wechatProfitSharing($order);
  1368. if ($order->store_id > 0) {
  1369. $delay = 86400;
  1370. if (intval($order->pay_type) === Order::PAY_TYPE_YUNST_WECHAT_PAY) {
  1371. $tomoTime = strtotime(date('Y-m-d', strtotime("+1 day"))) + 39600;
  1372. $delay = $tomoTime - time();
  1373. debug_log(['当面付添加延时队列时间' => $delay]);
  1374. }
  1375. \queue_push(new NoAfterSalesOrderJob(['order_id' => $order->id, 'store_id' => $order->store_id, 'is_scan' => 1]), $delay);
  1376. }
  1377. // 支付完成之后,相关的操作
  1378. $form = new OrderComplete();
  1379. $form->order_id = $order->id;
  1380. $form->order_type = 6;
  1381. $form->notify();
  1382. $this->ScanOrderAfter($order);
  1383. echo 'success';
  1384. return;
  1385. } else {
  1386. echo "支付失败";
  1387. return;
  1388. }
  1389. }
  1390. //自购返利
  1391. private function buseinessSaasSelf($order){
  1392. $store = Store::findOne($order->store_id);
  1393. $saas_transfer_profit = $store->transfer_profit; //判断商城让利比例
  1394. //当面付
  1395. if ($this->is_scan) {
  1396. $saas_transfer_profit = $store->scan_transfer_profit;
  1397. }
  1398. // 点餐判断 判断订单中是否设置food_flag_id
  1399. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  1400. $saas_transfer_profit = $store->food_transfer_profit;
  1401. }
  1402. // 收银台判断 判断收银订单中是否存在当前订单
  1403. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  1404. $saas_transfer_profit = $store->cashier_transfer_profit;
  1405. }
  1406. // if (isset($order->pay_type) && intval($order->pay_type) === Order::PAY_TYPE_YUNST_WECHAT_PAY) {
  1407. // $saas_transfer_profit = $store->ratio ?: 0;
  1408. // }
  1409. if ($saas_transfer_profit <= 0) {
  1410. return;
  1411. }
  1412. if($store->self_rebate_switch != 0 && $store->self_rebate_value == 0){
  1413. $defaultSet = Option::get(OptionSetting::SHARE_SELF_REBATE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1414. $defaultSet = json_decode($defaultSet['value'], true);
  1415. if(!empty($defaultSet['self_rebate_switch']) && !empty($defaultSet['self_rebate_value'])){
  1416. $store->self_rebate_value = $defaultSet['self_rebate_value'];
  1417. }
  1418. }
  1419. // 自购返利
  1420. if ($store->self_rebate_switch != 0 && $store->self_rebate_value > 0) { //判断是否开启自购返利 且 自购返利比例大于0
  1421. $saas_share_money = floor_num($order->pay_price * $saas_transfer_profit / 100, 2); //计算初始总返利金额
  1422. $self_money = (($saas_share_money * ($store->self_rebate_value / 100)));
  1423. $user = User::findOne(['id' => $order->user_id, 'store_id' => $order->store_id]);
  1424. if ($order->saas_id) {
  1425. //普通分账
  1426. $self_user = SaasUser::findOne(['id' => $order->saas_id]);
  1427. } else {
  1428. $binding = $user->binding;
  1429. $self_user = SaasUser::findOne(['mobile' => $binding]);
  1430. }
  1431. $share_num = 1;
  1432. $self_money = round(($self_money * $share_num), 2);
  1433. // if ($order->is_platform) {
  1434. // $execute_flag = SharingReceiver::EXECUTE_TYPE_TRANSFER;
  1435. // }
  1436. // 判断是否为联盟支付
  1437. if ($order->is_platform) {
  1438. // //没有分销账户信息的不分账
  1439. // $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$self_user->id,'is_delete'=>0])->asArray()->one();
  1440. // if(empty($distributionInfo) || !isset($distributionInfo['platform_open_id']) || empty($distributionInfo['platform_open_id'])){
  1441. // return;
  1442. // }
  1443. //新逻辑
  1444. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  1445. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  1446. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1447. $account = $self_user->platform_open_id;
  1448. } else {
  1449. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  1450. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  1451. if ($user) {//如果用户存在,则走分账
  1452. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1453. $account = $user->wechat_open_id;
  1454. }
  1455. // else {//不存在走联盟转账
  1456. // $execute_flag = SharingReceiver::EXECUTE_TYPE_TRANSFER;
  1457. // $account = $self_user->platform_open_id;
  1458. // }
  1459. }
  1460. if ($account) {
  1461. $sharing = SharingReceiver::findOne([
  1462. 'store_id' => $order->store_id,
  1463. 'account' => $account,
  1464. 'order_no' => $order->order_no,
  1465. 'from' => SharingReceiver::FROM_SELF,
  1466. 'is_delete' => 0
  1467. ]);
  1468. if ($self_money > 0) {
  1469. if ($execute_flag == SharingReceiver::EXECUTE_TYPE_NORMAL) { //如果走分账
  1470. if (!$sharing) {
  1471. $add_self_res = WechatShare::addReceivers($account, WechatShare::RELATION_USER, $add_type, null, false, $order->store_id, $order->is_platform, $order);
  1472. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销一级接收方添加结果 =====>', $add_self_res]);
  1473. }
  1474. $is_pay = SharingReceiver::PAY_WAIT;
  1475. } else {
  1476. $self_user->share_profit += $self_money;
  1477. $self_user->save();
  1478. $is_pay = SharingReceiver::PAY_SUCCESS;
  1479. }
  1480. $this->business_saas_self_money = $self_money;
  1481. if (!$sharing) {
  1482. $sharing = new SharingReceiver();
  1483. $sharing->store_id = $order->store_id;
  1484. $sharing->rate = $store->self_rebate_value;
  1485. $sharing->order_no = $order->order_no;
  1486. $sharing->transaction_id = $order->transaction_id;
  1487. $sharing->type = $add_type_num;
  1488. $sharing->from = SharingReceiver::FROM_SELF;
  1489. $sharing->account = $account;
  1490. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1491. $sharing->name = $self_user->name;
  1492. $sharing->saas_id = $self_user->id;
  1493. $sharing->user_id = 0;
  1494. $sharing->description = '平台分账到个人';
  1495. $sharing->is_pay = $is_pay;
  1496. $sharing->remark = '平台(自购返利)';
  1497. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1498. $sharing->created_at = time();
  1499. $sharing->updated_at = time();
  1500. $sharing->is_platform = $order->is_platform;
  1501. $this->saas_self_money = $sharing->amount;
  1502. }
  1503. $sharing->amount = $self_money;
  1504. if (!$sharing->save()) {
  1505. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台自购返利分账详情, 平台openid:' . $self_user->platform_open_id . ',入库失败 =====>', $sharing->errors[0]]);
  1506. }
  1507. } else {
  1508. SharingReceiver::updateAll([
  1509. 'is_delete' => 1
  1510. ], [
  1511. 'store_id' => $order->store_id,
  1512. 'account' => $account,
  1513. 'order_no' => $order->order_no,
  1514. 'from' => SharingReceiver::FROM_SELF,
  1515. 'is_delete' => 0
  1516. ]);
  1517. }
  1518. }
  1519. }
  1520. }
  1521. public function ScanOrderAfter($order)
  1522. {
  1523. $file_name = \Yii::$app->runtimePath . '/logs/scan_pay.log';
  1524. try {
  1525. if ($order->store_id) {
  1526. //获取规则
  1527. $rule = GiveIntegralRules::find()->where(['and', ['<', 'start_date', time()], ['>', 'end_date', time()], ['status' => 1, 'is_delete' => 0]])->select('id, rate')->asArray()->all();
  1528. $rule_ids = array_column($rule, 'id');
  1529. foreach ($rule_ids as $item) {
  1530. $rule_id = RulesFaceStore::find()->where(['rule_id' => $item, 'store_id' => $order->store_id])->select('rule_id')->column();
  1531. foreach ($rule as $rule_item) {
  1532. //$rule_ids = RulesFaceStore::find()->where(['rule_id' => $rule_ids, 'store_id' => $order->store_id])->select('rule_id')->column();
  1533. if ($rule_id) {
  1534. if (in_array($rule_item['id'], $rule_id)) {
  1535. $store_ids = RulesGiveIntegralStore::find()->where(['rule_id' => $rule_id])->select('store_id')->column();
  1536. $user = User::findOne($order->user_id);
  1537. if (!$order->user_id) {
  1538. $saas_user = SaasUser::findOne($order->saas_id);
  1539. $user = User::findOne(['binding' => $saas_user->mobile]);
  1540. if (empty($user)) {
  1541. $user = User::findOne(['alipay_open_id' => $saas_user->ali_user_id]);
  1542. }
  1543. }
  1544. foreach ($store_ids as $item) {
  1545. $other_user = User::findOne(['store_id' => $item, 'binding' => $user->binding]);
  1546. if (!$other_user) {
  1547. $other_user = new User();
  1548. $other_user->access_token = \Yii::$app->security->generateRandomString();
  1549. $other_user->binding = $user->binding;
  1550. $other_user->type = User::USER_TYPE_NORMAL;
  1551. $other_user->nickname = substr_replace($user->binding, '******', 3, 6);
  1552. $other_user->avatar_url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  1553. $other_user->username = \Yii::$app->security->generateRandomString();
  1554. $other_user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  1555. $other_user->auth_key = \Yii::$app->security->generateRandomString();
  1556. $other_user->is_delete = User::USER_NOT_DELETE;
  1557. $other_user->store_id = $item;
  1558. $other_user->parent_id = $user->id;
  1559. $other_user->platform = User::USER_FROM_WECHAT;
  1560. $other_user->alipay_open_id = $user->alipay_open_id;
  1561. $other_user->save();
  1562. if ($other_user->save()) {
  1563. $share_log = new UserShareLog();
  1564. $share_log->parent_id = $user->id;
  1565. $share_log->user_id = $other_user->id;
  1566. $share_log->created_at = time();
  1567. $share_log->save();
  1568. }
  1569. }
  1570. $integral = (int)($order->total_price * ($rule_item['rate'] / 100));
  1571. $log = new AccountLog();
  1572. $log->store_id = $item;
  1573. $log->user_id = $other_user->id;
  1574. $log->type = AccountLog::TYPE_INTEGRAL;
  1575. $log->log_type = AccountLog::LOG_TYPE_INCOME;
  1576. $log->amount = $integral;
  1577. $log->desc = '订单'.$order->order_no.'赠送积分';
  1578. $log->before = $other_user->integral * 1;
  1579. $log->after = (int)($other_user->integral * 1 + $integral);
  1580. $log->operator = '';
  1581. $log->operator_id = 0;
  1582. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  1583. $log->created_at = time();
  1584. $log->order_id = $order->id;
  1585. $log->order_type = AccountLog::TYPE_SCAN_CODE_PAY;
  1586. if (!$log->save()) {
  1587. file_put_contents($file_name, "\r\n" . '[当面付][' . date('Y-m-d H:i:s') . ']' . json_encode($log->errors), FILE_APPEND);
  1588. } else {
  1589. $other_user->integral = (int)($other_user->integral * 1 + $integral);
  1590. $other_user->total_integral = (int)($other_user->total_integral * 1 + $integral);
  1591. if (!$other_user->save()) {
  1592. file_put_contents($file_name, "\r\n" . '[用户积分][' . date('Y-m-d H:i:s') . ']' . json_encode($other_user->errors), FILE_APPEND);
  1593. \Yii::error("[用户积分]" . json_encode($other_user->errors));
  1594. }
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. }
  1601. }
  1602. } catch (\Exception $e) {
  1603. file_put_contents($file_name, "\r\n" . '[用户积分发放失败][' . date('Y-m-d H:i:s') . ']' . json_encode($e->getMessage() . $e->getFile() . $e->getLine()), FILE_APPEND);
  1604. echo "支付失败";
  1605. return;
  1606. }
  1607. }
  1608. /**
  1609. * @param Order $order
  1610. * 微信分账支付回调时添加分账接收方并计算相关分账数额
  1611. */
  1612. public function addReceiver($order) {
  1613. if (!\Yii::$app->isSaas()) {
  1614. return;
  1615. }
  1616. debug_log(['type' => '分账'], 'orderDetail.log');
  1617. // TODO: 目前平台分销部分因openid问题,走提现逻辑
  1618. //联盟分销
  1619. $this->newSaasShareReceiver($order);
  1620. // 商城级分销
  1621. $this->shareStoreToStore($order);
  1622. // 平台推荐
  1623. $this->newSaasRecommendReceiver($order);
  1624. //自购返利
  1625. $this->buseinessSaasSelf($order);
  1626. //自定义分账接收方
  1627. $this->addCustomReceiver($order);
  1628. //区域代理 / 推广代理分账接收方
  1629. $this->addAgentReceiver($order);
  1630. // 服务商分成
  1631. $this->newSaasPlatformReceiver($order);
  1632. // 以下为商城分销
  1633. return;
  1634. }
  1635. /**
  1636. * @param ReOrder $order
  1637. * 微信分账支付回调时添加分账接收方并计算相关分账数额
  1638. */
  1639. public function addRechargeReceiver($order) {
  1640. if (!\Yii::$app->isSaas()) {
  1641. return;
  1642. }
  1643. // TODO: 目前平台推荐和分销不走分账,走提现逻辑
  1644. $this->newSaasShareReceiver($order);
  1645. $this->shareStoreToStore($order);
  1646. // $this->newSaasRecommendReceiver($order);
  1647. $this->newSaasPlatformReceiver($order);
  1648. $share_setting = Option::get('share_basic_setting', $order->store_id);
  1649. $share_setting = $share_setting ? Json::decode($share_setting['value']) : [];
  1650. if (!isset($share_setting['level'])) {
  1651. return;
  1652. }
  1653. if ($share_setting['level']['value'] == 0) {
  1654. return;
  1655. }
  1656. $share_money_setting = Option::get('share_money_setting', $order->store_id);
  1657. $share_money_setting = $share_money_setting ? Json::decode($share_money_setting['value']) : [];
  1658. // 一级佣金发放
  1659. if ($share_setting['level']['value'] >= 1) {
  1660. if ($share_setting['is_rebate']['value'] > 0) {
  1661. $user_1 = User::findOne($order->user_id);
  1662. $remark = '店铺一级分销(自购返利)';
  1663. } else {
  1664. $user_1 = User::findOne($order->user_id);
  1665. if ($user_1->parent_id <= 0) {
  1666. \Yii::warning('<============= 订单:' . $order->order_no . '不存在店铺分销关系');
  1667. return;
  1668. }
  1669. $remark = '店铺一级分销';
  1670. $user_1 = User::findOne($user_1->parent_id);
  1671. }
  1672. if (!$user_1) {
  1673. return;
  1674. }
  1675. // 如果是平台用户,获取平台openid
  1676. if ($order->is_platform) {
  1677. $saas_user_1 = SaasUser::findOne(['mobile' => $user_1->binding]);
  1678. if (!$saas_user_1) {
  1679. return;
  1680. }
  1681. $wechat_openid_1 = $saas_user_1->platform_open_id;
  1682. $user_name_1 = $saas_user_1->name;
  1683. $user_id_1 = $saas_user_1->id;
  1684. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  1685. $add_type_number = SharingReceiver::RECEIVE_SUB_OPENID;
  1686. } else {
  1687. $wechat_openid_1 = $user_1->wechat_open_id;
  1688. $user_name_1 = $user_1->nickname;
  1689. $user_id_1 = $user_1->id;
  1690. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  1691. $add_type_number = SharingReceiver::RECEIVE_SUB_OPENID;
  1692. }
  1693. $user_1_money = round($order->pay_price * ($share_money_setting['level_one']['value'] / 100), 2);
  1694. if ($user_1_money > 0.00) {
  1695. $add_res_1 = [
  1696. 'code' => 1
  1697. ];
  1698. $share_receiver = SharingReceiver::findOne(['store_id' => $order->store_id, 'account' => $wechat_openid_1, 'order_no' => $order->order_no]);
  1699. if (!$share_receiver) {
  1700. $add_res_1 = WechatShare::addReceivers($wechat_openid_1, WechatShare::RELATION_USER, $add_type, null, false, $order->store_id, $order->is_platform, $order);
  1701. \Yii::error(['<====== 充值订单号:' . $order->order_no .' 一级接收方添加结果 =====>', $add_res_1]);
  1702. }
  1703. if ($this->adapay() || $add_res_1['code'] == 0 || $share_receiver) {
  1704. $sharing = new SharingReceiver();
  1705. $sharing->store_id = $order->store_id;
  1706. $sharing->rate = $share_money_setting['level_one']['value'];
  1707. $sharing->order_no = $order->order_no;
  1708. $sharing->transaction_id = $order->transaction_id;
  1709. $sharing->type = $add_type_number;
  1710. $sharing->from = SharingReceiver::FROM_STORE;
  1711. $sharing->account = $wechat_openid_1;
  1712. $sharing->user_id = $user_id_1;
  1713. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1714. $sharing->name = $user_name_1;
  1715. $sharing->description = '店铺分账到个人';
  1716. $sharing->remark = $remark;
  1717. $sharing->amount = $user_1_money;
  1718. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1719. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1720. $sharing->created_at = time();
  1721. $sharing->updated_at = time();
  1722. if (!$sharing->save()) {
  1723. \Yii::error(['<====== 订单号:' . $order->order_no .' 添加接收方openid ' . $wechat_openid_1 . '入库失败 =====>', $sharing->errors[0]]);
  1724. }
  1725. }
  1726. }
  1727. }
  1728. //二级佣金发放
  1729. if ($share_setting['level']['value'] >= 2) {
  1730. $user_2 = User::findOne($user_1->parent_id);
  1731. if (!$user_2) {
  1732. return;
  1733. }
  1734. // 如果是平台用户,获取平台openid
  1735. if ($order->is_platform) {
  1736. $saas_user_2 = SaasUser::findOne(['mobile' => $user_2->binding]);
  1737. if (!$saas_user_2) {
  1738. return;
  1739. }
  1740. $wechat_openid_2 = $saas_user_2->platform_open_id;
  1741. $user_name_2 = $saas_user_2->name;
  1742. $user_id_2 = $saas_user_2->id;
  1743. } else {
  1744. $wechat_openid_2 = $user_2->wechat_open_id;
  1745. $user_name_2 = $user_2->nickname;
  1746. $user_id_2 = $user_2->id;
  1747. }
  1748. $user_2_money = round($order->pay_price * ($share_money_setting['level_two']['value'] / 100), 2);
  1749. if ($user_2_money > 0.00) {
  1750. $add_res_2 = [
  1751. 'code' => 1
  1752. ];
  1753. $share_receiver = SharingReceiver::findOne(['store_id' => $order->store_id, 'account' => $wechat_openid_2, 'order_no' => $order->order_no]);
  1754. if (!$share_receiver) {
  1755. $add_res_2 = WechatShare::addReceivers($wechat_openid_2, WechatShare::RELATION_USER, $add_type, null, false, $order->store_id, $order->is_platform, $order);
  1756. \Yii::error(['<====== 订单号:' . $order->order_no . ' 二级接收方添加结果 =====>', $add_res_2]);
  1757. }
  1758. if ($this->adapay() || $add_res_2['code'] == 0 || $share_receiver) {
  1759. $sharing = new SharingReceiver();
  1760. $sharing->store_id = $order->store_id;
  1761. $sharing->rate = $share_money_setting['level_two']['value'];
  1762. $sharing->order_no = $order->order_no;
  1763. $sharing->transaction_id = $order->transaction_id;
  1764. $sharing->type = $add_type_number;
  1765. $sharing->from = SharingReceiver::FROM_STORE;
  1766. $sharing->account = $wechat_openid_2;
  1767. $sharing->user_id = $user_id_2;
  1768. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1769. $sharing->name = $user_name_2;
  1770. $sharing->description = '店铺分账到个人';
  1771. $sharing->remark = '店铺二级分销';
  1772. $sharing->amount = $user_2_money;
  1773. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1774. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1775. $sharing->created_at = time();
  1776. $sharing->updated_at = time();
  1777. if (!$sharing->save()) {
  1778. \Yii::error(['<====== 订单号:' . $order->order_no .' 添加接收方openid ' . $wechat_openid_2 . '入库失败 =====>', $sharing->errors[0]]);
  1779. }
  1780. }
  1781. }
  1782. }
  1783. //三级佣金发放
  1784. if ($share_setting['level']['value'] >= 3) {
  1785. $user_3 = User::findOne($user_2->parent_id);
  1786. if (!$user_3) {
  1787. return;
  1788. }
  1789. // 如果是平台用户,获取平台openid
  1790. if ($order->is_platform) {
  1791. $saas_user_3 = SaasUser::findOne(['mobile' => $user_3->binding]);
  1792. if (!$saas_user_3) {
  1793. return;
  1794. }
  1795. $wechat_openid_3 = $saas_user_3->platform_open_id;
  1796. $user_name_3 = $saas_user_3->name;
  1797. $user_id_3 = $saas_user_3->id;
  1798. } else {
  1799. $wechat_openid_3 = $user_3->wechat_open_id;
  1800. $user_name_3 = $user_3->nickname;
  1801. $user_id_3 = $user_3->id;
  1802. }
  1803. $user_3_money = round($order->pay_price * ($share_money_setting['level_three']['value'] / 100), 2);
  1804. if ($user_3_money > 0.00) {
  1805. $add_res_3 = [
  1806. 'code' => 1
  1807. ];
  1808. $share_receiver = SharingReceiver::findOne(['store_id' => $order->store_id, 'account' => $wechat_openid_3, 'order_no' => $order->order_no]);
  1809. if (!$share_receiver) {
  1810. $add_res_3 = WechatShare::addReceivers($wechat_openid_3, WechatShare::RELATION_USER, $add_type, null, false, $order->store_id, $order->is_platform, $order);
  1811. \Yii::error(['<====== 订单号:' . $order->order_no .' 三级接收方添加结果 =====>', $add_res_3]);
  1812. }
  1813. if ($this->adapay() || $add_res_3 == 0 || $share_receiver) {
  1814. $sharing = new SharingReceiver();
  1815. $sharing->store_id = $order->store_id;
  1816. $sharing->rate = $share_money_setting['level_three']['value'];
  1817. $sharing->order_no = $order->order_no;
  1818. $sharing->transaction_id = $order->transaction_id;
  1819. $sharing->type = $add_type_number;
  1820. $sharing->from = SharingReceiver::FROM_STORE;
  1821. $sharing->account = $wechat_openid_3;
  1822. $sharing->user_id = $user_id_3;
  1823. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1824. $sharing->name = $user_name_3;
  1825. $sharing->description = '店铺分账到个人';
  1826. $sharing->remark = '店铺三级分销';
  1827. $sharing->amount = $user_3_money;
  1828. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  1829. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1830. $sharing->created_at = time();
  1831. $sharing->updated_at = time();
  1832. if (!$sharing->save()) {
  1833. \Yii::error(['<====== 订单号:' . $order->order_no .' 添加接收方openid ' . $wechat_openid_3 . '入库失败 =====>', $sharing->errors[0]]);
  1834. }
  1835. }
  1836. }
  1837. }
  1838. }
  1839. private function newSaasShareReceiver($order)
  1840. {
  1841. debug_log(['type' => 'newSaasShareReceiver'], 'orderDetail.log');
  1842. $store = Store::findOne($order->store_id);
  1843. $saas_transfer_profit = $store->transfer_profit;
  1844. if ($this->is_scan) {
  1845. $saas_transfer_profit = $store->scan_transfer_profit;
  1846. }
  1847. // 点餐判断 判断订单中是否设置food_flag_id
  1848. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  1849. $saas_transfer_profit = $store->food_transfer_profit;
  1850. }
  1851. // 收银台判断 判断收银订单中是否存在当前订单
  1852. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  1853. $saas_transfer_profit = $store->cashier_transfer_profit;
  1854. }
  1855. // if (isset($order->pay_type) && intval($order->pay_type) === Order::PAY_TYPE_YUNST_WECHAT_PAY) {
  1856. // $saas_transfer_profit = $store->ratio ?: 0;
  1857. // }
  1858. if ($saas_transfer_profit <= 0) {
  1859. return;
  1860. }
  1861. $saas_share_money = floor_num($order->pay_price * $saas_transfer_profit / 100, 2);//10 * 0.1 = 1
  1862. // 根据商城运营模式,判断分账权限
  1863. /**if ($store->business_model != 1) {
  1864. //分账接收方类型枚举:个人sub_openid,特约商户option_id
  1865. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  1866. // 接收方类型:sub_openid
  1867. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  1868. } else {
  1869. //分账接收方类型枚举:个人sub_openid,特约商户option_id
  1870. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  1871. // 接收方类型:sub_openid
  1872. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  1873. }*/
  1874. // 联盟三级分销
  1875. $store_share_setting = Json::decode($store->share_setting);
  1876. if(!empty($store_share_setting['level']) && $store_share_setting['level_one'] == 0 && $store_share_setting['level_two'] == 0 && $store_share_setting['level_three'] == 0){
  1877. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1878. $defaultSet = json_decode($defaultSet['value'], true);
  1879. if(!empty($defaultSet['level']) &&
  1880. (!empty($defaultSet['level_one']) || !empty($defaultSet['level_two']) || !empty($defaultSet['level_three']))){
  1881. $store_share_setting = $defaultSet;
  1882. }
  1883. }
  1884. if (is_array($store_share_setting)) {
  1885. if ($store_share_setting['level'] > 0) {
  1886. // 一级
  1887. if ($store_share_setting['level'] >= 1 && $store_share_setting['level_one'] > 0) {
  1888. $saas_user_1_money = floor_num($saas_share_money * ($store_share_setting['level_one'] / 100), 2);
  1889. if ($order->saas_id > 0) {
  1890. $share_user_1 = SaasUser::findOne($order->saas_id);
  1891. } else {
  1892. $binding = User::findOne(['id' => $order->user_id, 'store_id' => $order->store_id])->binding;
  1893. $share_user_1 = SaasUser::findOne(['mobile' => $binding]);
  1894. }
  1895. if (!$share_user_1) {
  1896. return;
  1897. }
  1898. $share_user_1 = SaasUser::findOne(['id' => $share_user_1->parent_id]);
  1899. if (!$share_user_1) {
  1900. return;
  1901. }
  1902. //没有分销账户信息的不分账
  1903. // $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$share_user_1->id,'is_delete'=>0])->asArray()->one();
  1904. // if(empty($distributionInfo) || !isset($distributionInfo['platform_open_id']) || empty($distributionInfo['platform_open_id']) ){
  1905. // return;
  1906. // }
  1907. $user_1 = User::findOne(['binding' => $share_user_1->mobile, 'store_id' => $order->store_id]);
  1908. $account_1 = null;
  1909. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1910. // 判断是否为联盟支付
  1911. if ($order->is_platform) {
  1912. //新逻辑
  1913. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  1914. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  1915. $account_1 = $share_user_1->platform_open_id;
  1916. } else {
  1917. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  1918. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  1919. if ($user_1) {//如果用户存在,则走分账
  1920. $account_1 = $user_1->wechat_open_id;
  1921. }
  1922. // else {//不存在走联盟转账
  1923. // $execute_flag = SharingReceiver::EXECUTE_TYPE_TRANSFER;
  1924. // $account_1 = $share_user_1->platform_open_id;
  1925. // }
  1926. }
  1927. if ($account_1) {
  1928. $sharing = SharingReceiver::findOne([
  1929. 'store_id' => $order->store_id,
  1930. 'account' => $account_1,
  1931. 'order_no' => $order->order_no,
  1932. 'from' => SharingReceiver::FROM_PLATFORM,
  1933. 'is_delete' => 0
  1934. ]);
  1935. if ($saas_user_1_money > 0) {
  1936. $this->saas_user_1_money = $saas_user_1_money;
  1937. $is_pay = SharingReceiver::PAY_WAIT;
  1938. if (!$sharing) {
  1939. $add_res_1 = WechatShare::addReceivers($account_1, WechatShare::RELATION_USER, $add_type, null, false, $order->store_id, $order->is_platform, $order);
  1940. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销一级接收方添加结果 =====>', $add_res_1]);
  1941. $sharing = new SharingReceiver();
  1942. $sharing->store_id = $order->store_id;
  1943. $sharing->rate = $store_share_setting['level_one'];
  1944. $sharing->order_no = $order->order_no;
  1945. $sharing->transaction_id = $order->transaction_id;
  1946. $sharing->type = $add_type_num;
  1947. $sharing->from = SharingReceiver::FROM_PLATFORM;
  1948. $sharing->account = $account_1;
  1949. $sharing->execute_type = $execute_flag;
  1950. $sharing->name = $order->is_platform ? $share_user_1->name : ($user_1->nickname ?? $share_user_1->name);
  1951. // $sharing->user_id = isset($user_1->id) && $user_1->id ? $user_1->id : 0;
  1952. $sharing->user_id = isset($user_1->id) && $user_1->id ? $user_1->id : 0;
  1953. $sharing->saas_id = $share_user_1->id ? $share_user_1->id : 0;
  1954. $sharing->description = '平台分账到个人';
  1955. $sharing->is_pay = $is_pay;
  1956. $sharing->remark = '平台一级分销';
  1957. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  1958. $sharing->created_at = time();
  1959. $sharing->updated_at = time();
  1960. }
  1961. $sharing->amount = $saas_user_1_money;
  1962. if (!$sharing->save()) {
  1963. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台一级分账详情, 平台openid:' . $account_1 . ',入库失败 =====>', $sharing->errors[0]]);
  1964. }
  1965. } else {
  1966. SharingReceiver::updateAll(['is_delete' => 1], [
  1967. 'store_id' => $order->store_id,
  1968. 'account' => $account_1,
  1969. 'order_no' => $order->order_no,
  1970. 'from' => SharingReceiver::FROM_PLATFORM,
  1971. 'is_delete' => 0
  1972. ]);
  1973. }
  1974. }
  1975. }
  1976. // 二级
  1977. if ($store_share_setting['level'] >= 2 && $store_share_setting['level_two'] > 0) {
  1978. $saas_user_2_money = floor_num($saas_share_money * ($store_share_setting['level_two'] / 100), 2);
  1979. if (empty($share_user_1->parent_id)) {
  1980. return;
  1981. }
  1982. $share_user_2 = SaasUser::findOne(['id' => $share_user_1->parent_id]);
  1983. if (!$share_user_2) {
  1984. return;
  1985. }
  1986. //没有分销账户信息的不分账
  1987. // $distributionInfo2 = SaasDistribution::find()->where(['saas_id'=>$share_user_2->id,'is_delete'=>0])->asArray()->one();
  1988. // if(empty($distributionInfo2) || !isset($distributionInfo2['platform_open_id']) || empty($distributionInfo2['platform_open_id']) ){
  1989. // return;
  1990. // }
  1991. // 判断上级是否有分账权限
  1992. $share_num_2 = 1; // 分账比例
  1993. if ($share_num_2 > 0) {
  1994. $user_2 = User::findOne(['binding' => $share_user_2->mobile, 'store_id' => $order->store_id]);
  1995. $account_2 = null;
  1996. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  1997. // 判断是否为联盟支付
  1998. if ($order->is_platform) {
  1999. //新逻辑
  2000. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  2001. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  2002. $account_2 = $share_user_2->platform_open_id;
  2003. } else {
  2004. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  2005. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  2006. if ($user_2) {//如果用户存在,则走分账
  2007. $account_2 = $user_2->wechat_open_id;
  2008. }
  2009. // else {//不存在走联盟转账
  2010. // $execute_flag = SharingReceiver::EXECUTE_TYPE_TRANSFER;
  2011. // $account_2 = $share_user_2->platform_open_id;
  2012. // }
  2013. }
  2014. if ($account_2) {
  2015. $sharing = SharingReceiver::findOne([
  2016. 'store_id' => $order->store_id,
  2017. 'account' => $account_2,
  2018. 'order_no' => $order->order_no,
  2019. 'from' => SharingReceiver::FROM_PLATFORM,
  2020. 'is_delete' => 0
  2021. ]);
  2022. if ($saas_user_2_money > 0) {
  2023. $this->saas_user_2_money = $saas_user_2_money;
  2024. $is_pay = SharingReceiver::PAY_WAIT;
  2025. if (!$sharing) {
  2026. $add_res_2 = WechatShare::addReceivers($account_2, WechatShare::RELATION_USER, $add_type, null, false, $order->store_id, $order->is_platform, $order);
  2027. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销二级接收方添加结果 =====>', $add_res_2]);
  2028. $sharing = new SharingReceiver();
  2029. $sharing->store_id = $order->store_id;
  2030. $sharing->rate = $store_share_setting['level_two'];
  2031. $sharing->order_no = $order->order_no;
  2032. $sharing->transaction_id = $order->transaction_id;
  2033. $sharing->type = $add_type_num;
  2034. $sharing->from = SharingReceiver::FROM_PLATFORM;
  2035. $sharing->account = $account_2;
  2036. $sharing->execute_type = $execute_flag;
  2037. $sharing->name = $order->is_platform ? $share_user_2->name : ($user_2->nickname ?? $share_user_2->name);
  2038. $sharing->user_id = isset($user_2->id) && $user_2->id ? $user_2->id : 0;
  2039. $sharing->saas_id = $share_user_2->id ? $share_user_2->id : 0;
  2040. $sharing->description = '平台分账到个人';
  2041. $sharing->is_pay = $is_pay;
  2042. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  2043. $sharing->remark = '平台二级分销';
  2044. $sharing->created_at = time();
  2045. $sharing->updated_at = time();
  2046. }
  2047. $sharing->amount = $saas_user_2_money;
  2048. if (!$sharing->save()) {
  2049. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台二级分账详情, 平台openid:' . $account_2 . ',入库失败 =====>', $sharing->errors[0]]);
  2050. }
  2051. } else {
  2052. SharingReceiver::updateAll([
  2053. 'is_delete' => 1
  2054. ], [
  2055. 'store_id' => $order->store_id,
  2056. 'account' => $account_2,
  2057. 'order_no' => $order->order_no,
  2058. 'from' => SharingReceiver::FROM_PLATFORM,
  2059. 'is_delete' => 0
  2060. ]);
  2061. }
  2062. }
  2063. }
  2064. }
  2065. // 三级
  2066. if ($store_share_setting['level'] >= 3 && $store_share_setting['level_three'] > 0) {
  2067. $saas_user_3_money = floor_num($saas_share_money * ($store_share_setting['level_three'] / 100), 2);
  2068. $share_user_3 = SaasUser::findOne(['id' => $share_user_2->parent_id]);
  2069. if (!$share_user_3) {
  2070. return;
  2071. }
  2072. //没有分销账户信息的不分账
  2073. // $distributionInfo3 = SaasDistribution::find()->where(['saas_id'=>$share_user_3->id,'is_delete'=>0])->asArray()->one();
  2074. // if(empty($distributionInfo3) || !isset($distributionInfo3['platform_open_id']) || empty($distributionInfo3['platform_open_id']) ){
  2075. // return;
  2076. // }
  2077. // 判断上级是否有分账权限
  2078. $user_3 = User::findOne(['binding' => $share_user_3->mobile, 'store_id' => $order->store_id]);
  2079. //旧分账逻辑
  2080. $account_3 = null;
  2081. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  2082. if ($order->is_platform) {
  2083. //新逻辑
  2084. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  2085. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  2086. $account_3 = $share_user_3->platform_open_id;
  2087. } else {
  2088. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  2089. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  2090. if ($user_3) {//如果用户存在,则走分账
  2091. $account_3 = $user_3->wechat_open_id;
  2092. }
  2093. // else {//不存在走联盟转账
  2094. // $execute_flag = SharingReceiver::EXECUTE_TYPE_TRANSFER;
  2095. // $account_3 = $share_user_3->platform_open_id;
  2096. // }
  2097. }
  2098. if ($account_3) {
  2099. $sharing = SharingReceiver::findOne([
  2100. 'store_id' => $order->store_id,
  2101. 'account' => $account_3,
  2102. 'order_no' => $order->order_no,
  2103. 'from' => SharingReceiver::FROM_PLATFORM,
  2104. 'is_delete' => 0
  2105. ]);
  2106. if ($saas_user_3_money > 0) {
  2107. $this->saas_user_3_money = $saas_user_3_money;
  2108. $is_pay = SharingReceiver::PAY_WAIT;
  2109. if (!$sharing) {
  2110. $add_res_3 = WechatShare::addReceivers($account_3, WechatShare::RELATION_USER, $add_type, null, false, $order->store_id, $order->is_platform, $order);
  2111. \Yii::error(['<====== 订单号:' . $order->order_no .' 平台分销三级接收方添加结果 =====>', $add_res_3]);
  2112. $sharing = new SharingReceiver();
  2113. $sharing->store_id = $order->store_id;
  2114. $sharing->rate = $store_share_setting['level_three'];
  2115. $sharing->order_no = $order->order_no;
  2116. $sharing->transaction_id = $order->transaction_id;
  2117. $sharing->type = $add_type_num;
  2118. $sharing->from = SharingReceiver::FROM_PLATFORM;
  2119. $sharing->account = $account_3;
  2120. $sharing->execute_type = $execute_flag;
  2121. $sharing->name = $order->is_platform ? $share_user_3->name : ($user_3->nickname ?? $share_user_3->name);
  2122. // $sharing->user_id = $order->is_platform ? $share_user_3->id : $user_3->id;
  2123. $sharing->user_id = isset($user_3->id) && $user_3->id ? $user_3->id : 0;
  2124. $sharing->saas_id = $share_user_3->id ? $share_user_3->id : 0;
  2125. $sharing->description = '平台分账到个人';
  2126. $sharing->is_pay = $is_pay;
  2127. $sharing->remark = '平台三级分销';
  2128. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  2129. $sharing->created_at = time();
  2130. $sharing->updated_at = time();
  2131. }
  2132. $sharing->amount = $saas_user_3_money;
  2133. if (!$sharing->save()) {
  2134. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台三级分账详情, 平台openid:' . $account_3 . ',入库失败 =====>', $sharing->errors[0]]);
  2135. }
  2136. } else {
  2137. SharingReceiver::updateAll([
  2138. 'is_delete' => 1
  2139. ], [
  2140. 'store_id' => $order->store_id,
  2141. 'account' => $account_3,
  2142. 'order_no' => $order->order_no,
  2143. 'from' => SharingReceiver::FROM_PLATFORM,
  2144. 'is_delete' => 0
  2145. ]);
  2146. }
  2147. }
  2148. }
  2149. }
  2150. }
  2151. }
  2152. /**
  2153. * saas平台商城间的分销(merchant)
  2154. * @param Order $order
  2155. */
  2156. private function shareStoreToStore($order) {
  2157. try {
  2158. debug_log(['type' => 'shareStoreToStore'], 'orderDetail.log');
  2159. // saas平台让利比例
  2160. $store = Store::findOne($order->store_id);
  2161. $saas_transfer_profit = $store->transfer_profit;
  2162. if ($this->is_scan) {
  2163. $saas_transfer_profit = $store->scan_transfer_profit;
  2164. }
  2165. // 点餐判断 判断订单中是否设置food_flag_id
  2166. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  2167. $saas_transfer_profit = $store->food_transfer_profit;
  2168. }
  2169. // 收银台判断 判断收银订单中是否存在当前订单
  2170. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  2171. $saas_transfer_profit = $store->cashier_transfer_profit;
  2172. }
  2173. // if (isset($order->pay_type) && intval($order->pay_type) === Order::PAY_TYPE_YUNST_WECHAT_PAY) {
  2174. // $saas_transfer_profit = $store->ratio ?: 0;
  2175. // }
  2176. if ($saas_transfer_profit <= 0) {
  2177. return;
  2178. }
  2179. $saas_share_money = round($order->pay_price * $saas_transfer_profit / 100, 2);
  2180. if(empty($store->store_share_switch)){
  2181. return;
  2182. }
  2183. if(empty($store->store_share_value)){
  2184. $defaultSet = Option::get(OptionSetting::SHARE_STORE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  2185. $defaultSet = json_decode($defaultSet['value'], true);
  2186. if(!empty($defaultSet['store_share_switch']) && !empty($defaultSet['store_share_value'])){
  2187. $store->store_share_value = $defaultSet['store_share_value'];
  2188. }
  2189. if(empty($store->store_share_value)){
  2190. return;
  2191. }
  2192. }
  2193. $saas_store_1_money = floor_num($saas_share_money * ($store->store_share_value / 100), 2);
  2194. if(!isset($order->user_id)){
  2195. return;
  2196. }
  2197. $user_id = $order->user_id;
  2198. $user = User::findOne($user_id);
  2199. $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
  2200. if(!$saasUser || $saasUser->store_id <= 0){
  2201. return;
  2202. }
  2203. $store_parent_id = $saasUser->store_id;
  2204. if ($store_parent_id < 1) {
  2205. return;
  2206. }
  2207. $mch_wechat = WechatConfig::findOne(['store_id' => $store_parent_id]);
  2208. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  2209. $remark = '店铺分销佣金';//'商城级一级分销';
  2210. $sharing = SharingReceiver::findOne([
  2211. 'store_id' => $order->store_id,
  2212. 'account' => $mch_wechat['mch_id'],
  2213. 'order_no' => $order->order_no,
  2214. 'from' => SharingReceiver::FROM_STORE_TO_STORE,
  2215. 'is_delete' => 0
  2216. ]);
  2217. if ($saas_store_1_money > 0.00) {
  2218. $this->saas_store_1_money = $saas_store_1_money;
  2219. if (!$sharing) {
  2220. $add_res_1 = WechatShare::addReceivers($mch_wechat['mch_id'], WechatShare::RELATION_STORE, WechatShare::RECEIVE_MERCHANT_ID, $mch_wechat['name'], false, $order->store_id, $order->is_platform, $order);
  2221. \Yii::error(['<====== 订单号:' . $order->order_no .' 商城级分销一级接收方添加结果 =====>', $add_res_1]);
  2222. $sharing = new SharingReceiver();
  2223. $sharing->store_id = $order->store_id;
  2224. $sharing->rate = $store->store_share_value;
  2225. $sharing->order_no = $order->order_no;
  2226. $sharing->transaction_id = $order->transaction_id;
  2227. $sharing->type = SharingReceiver::RECEIVE_MERCHANT_ID;
  2228. $sharing->from = SharingReceiver::FROM_STORE_TO_STORE;
  2229. $sharing->account = $mch_wechat['mch_id'];
  2230. $sharing->execute_type = $execute_flag;
  2231. $sharing->name = $mch_wechat['name'];
  2232. $sharing->user_id = 0;
  2233. $sharing->description = '分账到商户';
  2234. $sharing->to_store_id = $store_parent_id;
  2235. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  2236. $sharing->remark = $remark;
  2237. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  2238. $sharing->created_at = time();
  2239. $sharing->updated_at = time();
  2240. }
  2241. $sharing->amount = $saas_store_1_money;
  2242. if (!$sharing->save()) {
  2243. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城级一级分账详情, 商城mch_id:' . $mch_wechat['mch_id'] . ',入库失败 =====>', $sharing->errors[0]]);
  2244. }
  2245. } else {
  2246. SharingReceiver::updateAll([
  2247. 'is_delete' => 1
  2248. ], [
  2249. 'store_id' => $order->store_id,
  2250. 'account' => $mch_wechat['mch_id'],
  2251. 'order_no' => $order->order_no,
  2252. 'from' => SharingReceiver::FROM_STORE_TO_STORE,
  2253. 'is_delete' => 0
  2254. ]);
  2255. }
  2256. } catch (\Exception $e) {
  2257. \Yii::error(['店铺分销添加分账人失败' . $e->getMessage()]);
  2258. }
  2259. }
  2260. /**
  2261. * 添加自定义分账接收方
  2262. */
  2263. private function addCustomReceiver($order)
  2264. {
  2265. try {
  2266. $store = Store::findOne($order->store_id);
  2267. $saas_transfer_profit = $store->transfer_profit;
  2268. if ($this->is_scan) {
  2269. $saas_transfer_profit = $store->scan_transfer_profit;
  2270. }
  2271. // 点餐判断 判断订单中是否设置food_flag_id
  2272. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  2273. $saas_transfer_profit = $store->food_transfer_profit;
  2274. }
  2275. // 收银台判断 判断收银订单中是否存在当前订单
  2276. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  2277. $saas_transfer_profit = $store->cashier_transfer_profit;
  2278. }
  2279. if ($saas_transfer_profit <= 0) {
  2280. return;
  2281. }
  2282. $saas_share_money = floor_num($order->pay_price * $saas_transfer_profit / 100, 2);
  2283. //这里继续走 单个商品退款之后 需要重新计算分账金额 分账金额不足的需要删除
  2284. // if ($saas_share_money <= 0) {
  2285. // return;
  2286. // }
  2287. //让利比例金额 - 自购返利 - 联盟分销一级 - 联盟分销二级 - 联盟分销三级 - 商城级一级;
  2288. // $platform_money = floor_num($saas_share_money - $this->saas_self_money - $this->saas_user_1_money - $this->saas_user_2_money - $this->saas_user_3_money - $this->saas_store_1_money, 2);
  2289. // if($platform_money <= 0){
  2290. // return;
  2291. // }
  2292. $platform_money = $saas_share_money;
  2293. $saas_custom_money = 0;
  2294. $sharingReceiverCustomList = SharingReceiverCustom::find()
  2295. ->where(['is_delete' => SharingReceiverCustom::IS_DELETE_NO])
  2296. ->andWhere(['OR',
  2297. [
  2298. 'sharing_way' => SharingReceiverCustom::SHARING_WAY_STORE,
  2299. 'sharing_store_id' => $order->store_id
  2300. ], [
  2301. 'sharing_way' => SharingReceiverCustom::SHARING_WAY_PLATFORM,
  2302. ]
  2303. ])
  2304. ->andWhere(['>', 'sharing_profit', 0])
  2305. ->select('id, name, sharing_type, type_id, sharing_way, sharing_store_id, sharing_profit, extra_saas_user_id')
  2306. ->asArray()->all();
  2307. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  2308. foreach ($sharingReceiverCustomList as $item) {
  2309. $sharing_amount = bcdiv(bcmul($saas_share_money, $item['sharing_profit'], 2), 100 ,2);
  2310. $platform_money = bcsub($platform_money, $sharing_amount, 2);
  2311. $saas_id = 0;
  2312. $user_id = 0;
  2313. if (intval($item['sharing_type']) == SharingReceiverCustom::SHARING_TYPE_USER) {
  2314. $saasUser = SaasUser::findOne($item['type_id']);
  2315. $saas_id = $saasUser->id ?? 0;
  2316. $name = $saasUser->name;
  2317. if ($order->is_platform) {
  2318. //新逻辑
  2319. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  2320. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  2321. $account = $saasUser->platform_open_id;
  2322. } else {
  2323. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  2324. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  2325. $user = User::findOne(['binding' => $saasUser->mobile, 'store_id' => $order->store_id]);
  2326. $account = $user->wechat_open_id;
  2327. $user_id = $user->id ?? 0;
  2328. $name = $user->nickname;
  2329. }
  2330. $account_name = null;
  2331. $relation_type = WechatShare::RELATION_USER;
  2332. $description = '平台分账到个人:自定义分账接收方';
  2333. } else {
  2334. $account = $item['type_id'];
  2335. $add_type = WechatShare::RECEIVE_MERCHANT_ID;
  2336. $name = $account_name = $item['name'];
  2337. $relation_type = WechatShare::RELATION_STORE;
  2338. $add_type_num = SharingReceiver::RECEIVE_MERCHANT_ID;
  2339. $description = '平台分账到商户:自定义分账接收方';
  2340. $user_id = $item['extra_saas_user_id'];
  2341. }
  2342. $sharing = SharingReceiver::findOne([
  2343. 'store_id' => $order->store_id,
  2344. 'account' => $account,
  2345. 'order_no' => $order->order_no,
  2346. 'from' => SharingReceiver::FROM_CUSTOM,
  2347. 'is_delete' => 0
  2348. ]);
  2349. if ($sharing_amount > 0) {
  2350. if (!$sharing) {
  2351. $result = WechatShare::addReceivers($account, $relation_type, $add_type, $account_name, false, $order->store_id, $order->is_platform, $order);
  2352. \Yii::error(['<====== 订单号:' . $order->order_no .' 自定义分账接收方添加结果 =====>', $result]);
  2353. $sharing = new SharingReceiver();
  2354. $sharing->store_id = $order->store_id;
  2355. $sharing->rate = $item['sharing_profit'];
  2356. $sharing->order_no = $order->order_no;
  2357. $sharing->transaction_id = $order->transaction_id;
  2358. $sharing->type = $add_type_num;
  2359. $sharing->from = SharingReceiver::FROM_CUSTOM;
  2360. $sharing->account = $account;
  2361. $sharing->execute_type = $execute_flag;
  2362. $sharing->name = $name;
  2363. $sharing->user_id = $user_id;
  2364. $sharing->saas_id = $saas_id;
  2365. $sharing->description = $description;
  2366. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  2367. $sharing->remark = '平台自定义分销';
  2368. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  2369. $sharing->created_at = time();
  2370. $sharing->updated_at = time();
  2371. }
  2372. $sharing->amount = $sharing_amount;
  2373. if (!$sharing->save()) {
  2374. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台自定义分账详情, 分账账户:' . $account . ',添加分账失败 =====>', $sharing->errors]);
  2375. };
  2376. } else {
  2377. SharingReceiver::updateAll(['is_delete' => 1], [
  2378. 'store_id' => $order->store_id,
  2379. 'account' => $account,
  2380. 'order_no' => $order->order_no,
  2381. 'from' => SharingReceiver::FROM_CUSTOM,
  2382. 'is_delete' => 0
  2383. ]);
  2384. }
  2385. $saas_custom_money = bcadd($saas_custom_money, $sharing_amount, 2);
  2386. if ($platform_money < 0.01) {
  2387. return ;
  2388. }
  2389. }
  2390. $this->saas_custom_money = $saas_custom_money;
  2391. return [
  2392. 'code' => 0,
  2393. 'msg' => '处理完成'
  2394. ];
  2395. } catch (\Exception $e) {
  2396. debug_log(['message' => $e->getMessage()], '20250310.log');
  2397. return [
  2398. 'code' => 1,
  2399. 'msg' => $e->getMessage()
  2400. ];
  2401. }
  2402. }
  2403. private function addAgentReceiver($order) {
  2404. try {
  2405. debug_log(['start' => '开始添加代理分账'], '20250314.log');
  2406. $store = Store::findOne($order->store_id);
  2407. $saas_transfer_profit = $store->transfer_profit;
  2408. if ($this->is_scan) {
  2409. $saas_transfer_profit = $store->scan_transfer_profit;
  2410. }
  2411. // 点餐判断 判断订单中是否设置food_flag_id
  2412. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  2413. $saas_transfer_profit = $store->food_transfer_profit;
  2414. }
  2415. // 收银台判断 判断收银订单中是否存在当前订单
  2416. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  2417. $saas_transfer_profit = $store->cashier_transfer_profit;
  2418. }
  2419. if ($saas_transfer_profit <= 0) {
  2420. return;
  2421. }
  2422. $saas_share_money = floor_num($order->pay_price * $saas_transfer_profit / 100, 2);
  2423. debug_log(['saas_share_money' => $saas_share_money], '20250314.log');
  2424. //这里继续走 单个商品退款之后 需要重新计算分账金额 分账金额不足的需要删除
  2425. // if ($saas_share_money <= 0) {
  2426. // return;
  2427. // }
  2428. $saas_agent_money = 0;
  2429. $agency_price_config = Option::get('agency_price_config', 0, 'saas')['value'];
  2430. $agency_price_config = json_decode($agency_price_config, true) ?: [];
  2431. $province_percent = $agency_price_config['province_percent'] ?: 0;
  2432. $city_percent = $agency_price_config['city_percent'] ?: 0;
  2433. $district_percent = $agency_price_config['district_percent'] ?: 0;
  2434. $bd_agency_price_config = Option::get('bd_agency_price_config', 0, 'saas')['value'];
  2435. $bd_agency_price_config = json_decode($bd_agency_price_config, true) ?: [];
  2436. $bd_agent_percent = $bd_agency_price_config['bd_agent_percent'] ?: 0;
  2437. $admin_id = null;
  2438. //查询推广代理
  2439. if ($store->salesman_id) {
  2440. $salesman = Salesman::findOne($store->salesman_id);
  2441. $admin_id = $salesman->admin_id;
  2442. }
  2443. if (empty($admin_id) && $store->admin_id) {
  2444. $admin_id = $store->admin_id;
  2445. }
  2446. //查询当前区域下的代理 或者推广代理
  2447. $query = Admin::find()->where(['is_delete' => 0])->andWhere([
  2448. 'OR', [
  2449. 'AND', [
  2450. 'type' => Admin::ADMIN_TYPE_DEFAULT,
  2451. ], [
  2452. 'OR', [
  2453. 'area_level' => 3,
  2454. 'province_id' => $store->province_id,
  2455. ],
  2456. [
  2457. 'area_level' => 2,
  2458. 'city_id' => $store->city_id,
  2459. ],
  2460. [
  2461. 'area_level' => 1,
  2462. 'district_id' => $store->district_id,
  2463. ],
  2464. ]
  2465. ], [
  2466. 'id' => $admin_id, 'type' => Admin::ADMIN_TYPE_BD_AGENT
  2467. ]
  2468. ]);
  2469. debug_log(['sql' => $query->createCommand()->getRawSql()], '20250314.log');
  2470. $admin_list = $query->asArray()->all();
  2471. debug_log(['admin_list' => $admin_list], '20250314.log');
  2472. $execute_flag = SharingReceiver::EXECUTE_TYPE_NORMAL;
  2473. foreach ($admin_list as $item) {
  2474. $user_role_name = '推广代理';
  2475. $item['area_level'] = intval($item['area_level']);
  2476. //推广代理
  2477. $sharing_profit = $bd_agent_percent;
  2478. $from = SharingReceiver::FROM_BD_AGENT;
  2479. //省市县代理
  2480. if ($item['type'] === Admin::ADMIN_TYPE_DEFAULT) {
  2481. $sharing_profit = 0;
  2482. $from = SharingReceiver::FROM_AREA_AGENT_PROVIDER;
  2483. //区域代理
  2484. if ($item['area_level'] === 3) {
  2485. //省
  2486. $sharing_profit = $province_percent;
  2487. $user_role_name = '省代理';
  2488. }
  2489. if ($item['area_level'] === 2) {
  2490. //市
  2491. $from = SharingReceiver::FROM_AREA_AGENT_CITY;
  2492. $sharing_profit = $city_percent;
  2493. $user_role_name = '市代理';
  2494. }
  2495. if ($item['area_level'] === 1) {
  2496. //县
  2497. $from = SharingReceiver::FROM_AREA_AGENT_DISTRICT;
  2498. $sharing_profit = $district_percent;
  2499. $user_role_name = '区代理';
  2500. }
  2501. }
  2502. $sharing_amount = bcdiv(bcmul($saas_share_money, $sharing_profit, 2), 100 ,2);
  2503. debug_log(['saas_share_money' => $saas_share_money, 'sharing_profit' => $sharing_profit, 'sharing_amount' => $sharing_amount], '20250314.log');
  2504. $saasUser = SaasUser::findOne($item['saas_user_id']);
  2505. $saas_id = $saasUser->id ?? 0;
  2506. $name = $saasUser->name;
  2507. if ($order->is_platform) {
  2508. //新逻辑
  2509. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  2510. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  2511. $account = $saasUser->platform_open_id;
  2512. $user_id = 0;
  2513. } else {
  2514. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  2515. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  2516. $user = User::findOne(['binding' => $saasUser->mobile, 'store_id' => $order->store_id]);
  2517. $account = $user->wechat_open_id;
  2518. $user_id = $user->id ?? 0;
  2519. $name = $user->nickname;
  2520. }
  2521. $account_name = null;
  2522. $relation_type = WechatShare::RELATION_USER;
  2523. $description = '平台分账到代理_' . $user_role_name;
  2524. $sharing = SharingReceiver::findOne([
  2525. 'store_id' => $order->store_id,
  2526. 'account' => $account,
  2527. 'order_no' => $order->order_no,
  2528. 'from' => $from,
  2529. 'is_delete' => 0
  2530. ]);
  2531. if ($sharing_amount > 0) {
  2532. if (!$sharing) {
  2533. $result = WechatShare::addReceivers($account, $relation_type, $add_type, $account_name, false, $order->store_id, $order->is_platform, $order);
  2534. \Yii::error(['<====== 订单号:' . $order->order_no .' 代理分账接收方添加结果 =====>', $result]);
  2535. $sharing = new SharingReceiver();
  2536. $sharing->store_id = $order->store_id;
  2537. $sharing->rate = $sharing_profit;
  2538. $sharing->order_no = $order->order_no;
  2539. $sharing->transaction_id = $order->transaction_id;
  2540. $sharing->type = $add_type_num;
  2541. $sharing->from = $from;
  2542. $sharing->account = $account;
  2543. $sharing->execute_type = $execute_flag;
  2544. $sharing->name = $name;
  2545. $sharing->user_id = $user_id;
  2546. $sharing->saas_id = $saas_id;
  2547. $sharing->description = $description;
  2548. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  2549. $sharing->remark = '平台代理分销';
  2550. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  2551. $sharing->created_at = time();
  2552. $sharing->updated_at = time();
  2553. }
  2554. $sharing->amount = $sharing_amount;
  2555. if (!$sharing->save()) {
  2556. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 平台代理分账详情, 分账账户:' . $account . ',添加分账失败 =====>', $sharing->errors]);
  2557. }
  2558. } else {
  2559. SharingReceiver::updateAll(['is_delete' => 1], [
  2560. 'store_id' => $order->store_id,
  2561. 'account' => $account,
  2562. 'order_no' => $order->order_no,
  2563. 'from' => $from,
  2564. 'is_delete' => 0
  2565. ]);
  2566. }
  2567. $saas_agent_money = bcadd($saas_agent_money, $sharing_amount, 2);
  2568. }
  2569. $this->saas_agent_money = $saas_agent_money;
  2570. return [
  2571. 'code' => 0,
  2572. 'msg' => '处理完成'
  2573. ];
  2574. } catch (\Exception $e) {
  2575. debug_log(['message' => $e->getMessage()], '20250310.log');
  2576. return [
  2577. 'code' => 1,
  2578. 'msg' => $e->getMessage()
  2579. ];
  2580. }
  2581. }
  2582. /**
  2583. * 推荐关系
  2584. * @param Order $order
  2585. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  2586. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  2587. * @throws \GuzzleHttp\Exception\GuzzleException
  2588. */
  2589. public function newSaasRecommendReceiver($order) {
  2590. // saas平台让利比例
  2591. $store = Store::findOne($order->store_id);
  2592. $saas_transfer_profit = $store->transfer_profit;
  2593. if ($this->is_scan) {
  2594. $saas_transfer_profit = $store->scan_transfer_profit;
  2595. }
  2596. // 点餐判断 判断订单中是否设置food_flag_id
  2597. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  2598. $saas_transfer_profit = $store->food_transfer_profit;
  2599. }
  2600. // 收银台判断 判断收银订单中是否存在当前订单
  2601. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  2602. $saas_transfer_profit = $store->cashier_transfer_profit;
  2603. }
  2604. // if (isset($order->pay_type) && intval($order->pay_type) === Order::PAY_TYPE_YUNST_WECHAT_PAY) {
  2605. // $saas_transfer_profit = $store->ratio ?: 0;
  2606. // }
  2607. if ($saas_transfer_profit <= 0) {
  2608. return;
  2609. }
  2610. $saas_share_money = floor_num($order->pay_price * $saas_transfer_profit / 100, 2);
  2611. /***** 推荐关系开始 *****/
  2612. $result = SaasStoreReferral::find()->where(['store_id' => $order->store_id, 'is_enable' => 1, 'type' => 1])
  2613. ->select(['referral_id', 'referral_rebate'])->asArray()->all();
  2614. $user_recommend_money = 0;
  2615. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_REFERRAL_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  2616. $defaultSet = json_decode($defaultSet['value'], true);
  2617. if (!empty($result) && is_array($result)) {
  2618. \Yii::warning($result);
  2619. foreach ($result as $value) {
  2620. if(($value['referral_rebate'] == 0) &&
  2621. (!empty($defaultSet['is_enable']) && !empty($defaultSet['referral_rebate']))){
  2622. $value['referral_rebate'] = $defaultSet['referral_rebate'];
  2623. }
  2624. $user_money = floor_num($saas_share_money * ($value['referral_rebate'] / 100), 2);
  2625. $user_recommend_money += $user_money;
  2626. $saas_user = SaasUser::findOne($value['referral_id']);
  2627. if (!$saas_user) {
  2628. \Yii::error([$value, $value['referral_id'], '没有$saas_user']);
  2629. continue;
  2630. }
  2631. if ($order->is_platform) {
  2632. //新逻辑
  2633. $add_type = WechatShare::RECEIVE_PERSONAL_OPENID;
  2634. $add_type_num = SharingReceiver::RECEIVE_OPENID;
  2635. $account = $saas_user->platform_open_id;
  2636. $user_id = 0;
  2637. } else {
  2638. $add_type = WechatShare::RECEIVE_PERSONAL_SUB_OPENID;
  2639. $add_type_num = SharingReceiver::RECEIVE_SUB_OPENID;
  2640. $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => $order->store_id]);
  2641. $account = $user->wechat_open_id;
  2642. $user_id = $user->id ?? 0;
  2643. }
  2644. $account_name = null;
  2645. $relation_type = WechatShare::RELATION_USER;
  2646. $description = '平台分账到店铺推荐人';
  2647. if ($user_money > 0) {
  2648. $sharing = SharingReceiver::findOne([
  2649. 'store_id' => $order->store_id,
  2650. 'account' => $account,
  2651. 'order_no' => $order->order_no,
  2652. 'from' => SharingReceiver::FROM_NEW_STORE_RECOMMEND,
  2653. 'is_delete' => 0
  2654. ]);
  2655. if (!$sharing) {
  2656. $WechatShareResult = WechatShare::addReceivers($account, $relation_type, $add_type, $account_name, false, $order->store_id, $order->is_platform, $order);
  2657. \Yii::error(['<====== 订单号:' . $order->order_no .' 店铺推荐人分账接收方添加结果 =====>', $WechatShareResult]);
  2658. $sharing = new SharingReceiver();
  2659. $sharing->store_id = $order->store_id;
  2660. $sharing->rate = $value['referral_rebate'];
  2661. $sharing->order_no = $order->order_no;
  2662. $sharing->transaction_id = $order->transaction_id;
  2663. $sharing->type = $add_type_num;
  2664. $sharing->from = SharingReceiver::FROM_NEW_STORE_RECOMMEND;
  2665. $sharing->account = $account;
  2666. $sharing->saas_id = $saas_user->id;
  2667. $sharing->name = $order->is_platform ? $saas_user->name : $user->nickname;
  2668. $sharing->user_id = $user_id;
  2669. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_NORMAL;
  2670. $sharing->description = '推荐分账到个人';
  2671. $sharing->remark = '店铺推荐人返利';//'商城推荐';
  2672. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  2673. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  2674. $sharing->created_at = time();
  2675. $sharing->updated_at = time();
  2676. }
  2677. $sharing->amount = $user_money;
  2678. if (!$sharing->save()) {
  2679. \Yii::error(['联盟佣金 <====== 订单号:' . $order->order_no .' SAAS 商城推荐详情, 平台openid:' . $account . ',入库失败 =====>', $sharing->errors[0]]);
  2680. }
  2681. } else {
  2682. SharingReceiver::updateAll([
  2683. 'is_delete' => 1
  2684. ], [
  2685. 'store_id' => $order->store_id,
  2686. 'account' => $account,
  2687. 'order_no' => $order->order_no,
  2688. 'from' => SharingReceiver::FROM_NEW_STORE_RECOMMEND,
  2689. 'is_delete' => 0
  2690. ]);
  2691. }
  2692. }
  2693. $this->saas_recommend_money = $user_recommend_money;
  2694. }
  2695. /***** 推荐关系结束 *****/
  2696. }
  2697. /**
  2698. * @param Order $order
  2699. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  2700. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  2701. * @throws \GuzzleHttp\Exception\GuzzleException
  2702. */
  2703. private function newSaasPlatformReceiver($order) {
  2704. // saas平台让利比例
  2705. $store = Store::findOne($order->store_id);
  2706. $saas_transfer_profit = $store->transfer_profit;
  2707. if ($this->is_scan) {
  2708. $saas_transfer_profit = $store->scan_transfer_profit;
  2709. }
  2710. // 点餐判断 判断订单中是否设置food_flag_id
  2711. if (isset($order->food_flag_id) && intval($order->food_flag_id) > 0) {
  2712. $saas_transfer_profit = $store->food_transfer_profit;
  2713. }
  2714. // 收银台判断 判断收银订单中是否存在当前订单
  2715. if (isset($order->hanging_order_id) && intval($order->hanging_order_id) > 0) {
  2716. $saas_transfer_profit = $store->cashier_transfer_profit;
  2717. }
  2718. // if (isset($order->pay_type) && intval($order->pay_type) === Order::PAY_TYPE_YUNST_WECHAT_PAY) {
  2719. // $saas_transfer_profit = $store->ratio ?: 0;
  2720. // }
  2721. if ($saas_transfer_profit <= 0) {
  2722. //如果分账比例小于等于0,直接解冻商户资金,调用分账接口,上送抽用金额为0,不然自己一直冻结不释放
  2723. debug_log('分账给服务商1');
  2724. if(in_array($order->pay_type, [Order::PAY_TYPE_YUNST_WECHAT_PAY, Order::PAY_TYPE_YUNST_ALI_PAY])){
  2725. $res_yunst = $this->yunstReceiver($order);
  2726. $orderNoHead = substr($order->order_no, 0, 2);
  2727. if ($orderNoHead == 'SC'){
  2728. $order_info = \app\plugins\scanCodePay\models\Order::findOne($order->id);
  2729. }else{
  2730. $order_info = Order::findOne($order->id);
  2731. }
  2732. if ($res_yunst) {
  2733. debug_log('0佣金分账成功');
  2734. $order_info->is_yunst_sharing = 1;
  2735. }else{
  2736. debug_log('0佣金分账失败');
  2737. $order_info->is_yunst_sharing = 2;
  2738. }
  2739. $order_info->save();
  2740. // return;
  2741. }
  2742. }
  2743. $saas_share_money = floor_num($order->pay_price * $saas_transfer_profit / 100, 2);
  2744. // if ($saas_share_money <= 0.00) {
  2745. // return;
  2746. // }
  2747. $platform_money = floor_num($saas_share_money - $this->saas_self_money - $this->saas_user_1_money - $this->saas_user_2_money - $this->saas_user_3_money - $this->saas_store_1_money - $this->business_saas_self_money - $this->saas_custom_money - $this->saas_agent_money - $this->saas_recommend_money, 2);
  2748. // if($platform_money <= 0){
  2749. // return;
  2750. // }
  2751. $sharing = null;
  2752. /***** 服务商分账开始 *****/
  2753. $wechat_config = Option::getSaasWechat();
  2754. if (!$this->is_yunst){
  2755. $sharing = SharingReceiver::findOne([
  2756. 'store_id' => $order->store_id,
  2757. 'account' => $wechat_config['sp_mch_id'],
  2758. 'order_no' => $order->order_no,
  2759. 'from' => SharingReceiver::FROM_SERVICE_PROVIDER,
  2760. 'is_delete' => 0
  2761. ]);
  2762. if (!$sharing) {
  2763. $add_res = WechatShare::addReceivers($wechat_config['sp_mch_id'], WechatShare::RELATION_SERVICE_PROVIDER, WechatShare::RECEIVE_MERCHANT_ID, $wechat_config['sp_name'], false, $order->store_id, $order->is_platform, $order);
  2764. \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 服务商接收方添加结果 =====>', $add_res]);
  2765. }
  2766. }
  2767. if($platform_money > 0){
  2768. if ($this->adapay() || $this->is_b2b() || (isset($add_res['code']) && $add_res['code'] == 0) || $sharing || $this->is_yunst) {
  2769. if (!$sharing) {
  2770. $sharing = new SharingReceiver();
  2771. $sharing->store_id = $order->store_id;
  2772. $sharing->rate = $saas_transfer_profit;
  2773. $sharing->order_no = $order->order_no;
  2774. $sharing->transaction_id = $order->transaction_id;
  2775. $sharing->type = SharingReceiver::RECEIVE_MERCHANT_ID;
  2776. $sharing->from = SharingReceiver::FROM_SERVICE_PROVIDER;
  2777. $sharing->account = $wechat_config['sp_mch_id']??0;
  2778. $sharing->name = $wechat_config['sp_name']??0;
  2779. $sharing->execute_type = SharingReceiver::EXECUTE_TYPE_NORMAL;
  2780. $sharing->user_id = 0;
  2781. $sharing->description = '分账到服务商';
  2782. $sharing->remark = '服务商';
  2783. $sharing->is_pay = SharingReceiver::PAY_WAIT;
  2784. $sharing->is_delete = SharingReceiver::IS_DELETE_NO;
  2785. $sharing->created_at = time();
  2786. $sharing->updated_at = time();
  2787. }
  2788. $sharing->amount = $platform_money;
  2789. if (!$sharing->save()) {
  2790. \Yii::error(['<====== 订单号:' . $order->order_no .' SAAS 服务商分账详情,服务商id: ' . $wechat_config['sp_mch_id'] . ',入库失败 =====>', $sharing->errors[0]]);
  2791. }
  2792. }
  2793. } else {
  2794. SharingReceiver::updateAll([
  2795. 'is_delete' => 1
  2796. ], [
  2797. 'store_id' => $order->store_id,
  2798. 'account' => $wechat_config['sp_mch_id'],
  2799. 'order_no' => $order->order_no,
  2800. 'from' => SharingReceiver::FROM_SERVICE_PROVIDER,
  2801. 'is_delete' => 0
  2802. ]);
  2803. }
  2804. /***** 服务商分账结束 *****/
  2805. }
  2806. /**
  2807. * 抽用金额为0的情况下调用,解冻商户金额
  2808. * @return void
  2809. */
  2810. public function yunstReceiver($order)
  2811. {
  2812. $store_id = $order->store_id;
  2813. $amount =0;
  2814. //查询协议号
  2815. $stroe_cusid = Store::findOne($store_id)->cusid;
  2816. $merchant =new Merchant();
  2817. $param['cusid'] = $stroe_cusid;
  2818. // $param['cusid'] = '552290050728X3L';//进件商户号 测试用
  2819. $param['protocolType'] = '2';
  2820. $res = $merchant->protocolStaus($order->store_id,$param);
  2821. debug_log('我是协议状态查询');
  2822. debug_log($res);
  2823. $protocolNo = '';
  2824. if($res['subCode'] == 'SUCCESS'){
  2825. debug_log('我是协议号');
  2826. debug_log($res['data']['protocolNo']);
  2827. $protocolNo = $res['data']['protocolNo'];
  2828. }
  2829. debug_log('怎么不查了');
  2830. debug_log($protocolNo);
  2831. debug_log($order->pay_price *100);
  2832. debug_log($amount *100);
  2833. debug_log($store_id);
  2834. debug_log('/index.php/yunst/notify/' . $store_id);
  2835. debug_log('???');
  2836. $data["bizOrderNo"] = $order->order_no;//服务商抽佣商户订单号
  2837. // $data["bizUserId"] = 'XDQXCJSZPJYB';//卖方 bizUserId 测试用 也就是store_number
  2838. // $data["authorizedCustId"] = "082207111000028";//被授权方客户号 测试用
  2839. $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value'];
  2840. if ($oldConf){
  2841. $oldConf = json_decode($oldConf, true);
  2842. $data["bizUserId"] = Store::findOne($store_id)->store_number;//也就是store_number
  2843. $data['authorizedCustId']= $oldConf['customer_number'];
  2844. // $authorizedCustId = $merchant_info->sub_mch_id;
  2845. // $data['bizUserId'] = $store->store_number;
  2846. // $data['cusid'] = $authorizedCustId;
  2847. }
  2848. $data["protocolNo"] = $protocolNo;//协议号
  2849. $amount_info = round($order->pay_price *100 - round(($order->pay_price * 0.003),2) *100,2);//100 * 100 - round(100 * 0.003, 2) * 100 = 10000 - 30 = 9970
  2850. $data["amount"] = $amount_info;//解冻金额//9970
  2851. //平台因为垫付手续费所以扣除手续费
  2852. $data["commAmount"] =round($amount *100 - round(($order->pay_price * 0.003),2) *100,2);//抽佣金额30
  2853. $data["commRatio"] = '';//抽佣比例
  2854. // $data["notifyUrl"] = pay_notify_url('yunst/notify', $store_id);//通知地址
  2855. $data["notifyUrl"] = '/index.php/yunst/notify/' . $store_id;//通知地址
  2856. $data["remark"] = '';//备注
  2857. debug_log('我是分账请求参数');
  2858. debug_log($data);
  2859. $order_yunst = new OrderYunst();
  2860. $res_return = $order_yunst->servicerReturnComm($data,$store_id);
  2861. debug_log('我是yunst分账接口');
  2862. debug_log($res_return);
  2863. if($res_return['subCode'] == 'SUCCESS'){
  2864. debug_log($res_return['data']);
  2865. debug_log($res_return['data']['payStatus']);
  2866. if (($res_return['data']['payStatus'] == '4') || ($res_return['data']['payStatus'] == '99')){
  2867. debug_log('我是yunst分账接口返回');
  2868. debug_log($order['id']);
  2869. debug_log($order->id);
  2870. return false;
  2871. }else{
  2872. debug_log('我是yunst分账接口返回3');
  2873. debug_log($order['id']);
  2874. debug_log($order->id);
  2875. debug_log($res_return);
  2876. return true;
  2877. ;
  2878. }
  2879. }else{
  2880. debug_log('我是yunst分账接口返回2');
  2881. debug_log($order['id']);
  2882. debug_log($order->id);
  2883. debug_log($res_return);
  2884. return false;
  2885. }
  2886. }
  2887. /**
  2888. * 点餐回调
  2889. */
  2890. public function FoodNotify($res) {
  2891. $order = FoodOrder::findOne([
  2892. 'order_no' => $res['out_trade_no'],
  2893. ]);
  2894. if (!$order) {
  2895. return;
  2896. }
  2897. if ($order->is_pay == 1) {
  2898. echo "订单已支付";
  2899. return;
  2900. }
  2901. $order->is_pay = 1;
  2902. $order->pay_time = time();
  2903. $order->pay_type = $this->pay_type;
  2904. $order->transaction_id = $res['transaction_id'];
  2905. if ($order->save()) {
  2906. try {
  2907. if ($order->is_use_platform_mch == 1) {
  2908. $store = Store::findOne($order->store_id);
  2909. $profit = $order->pay_price * $store->transfer_profit / 100;
  2910. $price = $order->pay_price - $profit;
  2911. Store::addMoney($store, $price, '用户点餐下单', $order->id, $order->user_id);
  2912. }
  2913. } catch (\Exception $e) {
  2914. // Todo
  2915. }
  2916. //商盟订单
  2917. $this->addReceiver($order);
  2918. sleep(10);
  2919. $event = new OrderEvent();
  2920. $event->wechatProfitSharing($order);
  2921. // 支付完成之后,相关的操作
  2922. $form = new OrderComplete();
  2923. $form->order_id = $order->id;
  2924. $form->order_type = 1;
  2925. $form->notify();
  2926. echo 'success';
  2927. return;
  2928. } else {
  2929. echo "支付失败";
  2930. return;
  2931. }
  2932. }
  2933. /**
  2934. * 商盟会员卡支付回调
  2935. */
  2936. public function BusinessMemberOrderNotify($res) {
  2937. \Yii::error(['<====== 订单号:'.$res['out_trade_no'],'--------------------']);
  2938. $order = BusinessMemberOrder::findOne([
  2939. 'order_no' => $res['out_trade_no'],
  2940. 'is_delete'=>0
  2941. ]);
  2942. if (!$order) {
  2943. return;
  2944. }
  2945. if ($order->is_pay == 1) {
  2946. echo "订单已支付";
  2947. return;
  2948. }
  2949. $time = time();
  2950. $order->begin_time = $time;
  2951. if($order->member_type == 'month'){
  2952. $order->end_time = $time + 30 * 24 * 3600;
  2953. $addTime = 30 * 24 * 3600;
  2954. }elseif($order->member_type == 'quarter'){
  2955. $order->end_time = $time + 90 * 24 * 3600;
  2956. $addTime = 90 * 24 * 3600;
  2957. }elseif($order->member_type == 'year'){
  2958. $order->end_time = $time + 365 * 24 * 3600;
  2959. $addTime = 365 * 24 * 3600;
  2960. }elseif($order->member_type == 'perpetual'){
  2961. $order->begin_time = 0;
  2962. $order->end_time = 0;
  2963. $addTime = 0;
  2964. }
  2965. $order->is_pay = 1;
  2966. $order->pay_time = time();
  2967. $order->pay_type = $this->pay_type;
  2968. $memberId = $order->member_id;
  2969. $saas_id = $order->saas_id;
  2970. if ($order->save()) {
  2971. $rightInfo = BusinessMember::find()->where(['id'=>$memberId,'is_delete'=>0])->asArray()->one();
  2972. // $rightIds = json_decode($rightInfo['right_ids'],true);
  2973. $rightIds = [];
  2974. if (strpos($rightInfo['right_ids'], '1') !== false) {
  2975. $rightIds[] = 1;
  2976. }
  2977. if (strpos($rightInfo['right_ids'], '2') !== false) {
  2978. $rightIds[] = 2;
  2979. }
  2980. if (strpos($rightInfo['right_ids'], '3') !== false) {
  2981. $rightIds[] = 3;
  2982. }
  2983. if (strpos($rightInfo['right_ids'], '4') !== false) {
  2984. $rightIds[] = 4;
  2985. }
  2986. if (strpos($rightInfo['right_ids'], '5') !== false) {
  2987. $rightIds[] = 5;
  2988. }
  2989. if (strpos($rightInfo['right_ids'], '6') !== false) {
  2990. $rightIds[] = 6;
  2991. }
  2992. // foreach ($rightIds as $rid) {
  2993. // $duration = BusinessRightDuration::find()->where(['right_id'=>$rid,'saas_id'=>$saas_id,'is_delete'=>0])->one();
  2994. // if($duration->expire_time != 0){
  2995. // if($addTime == 0){
  2996. // $duration->expire_time = 0;
  2997. // }
  2998. // }
  2999. // }
  3000. $saasInfo = SaasUser::find()->where(['id'=>$saas_id,'is_delete'=>0])->asArray()->one();
  3001. foreach($rightIds as $rid){
  3002. //权限时间
  3003. $this->rightDuration($saas_id,$rid,$addTime,BusinessRightDuration::FROM_CARD);
  3004. //如果购买的会员卡有分消提现
  3005. if($rid == 2){
  3006. if($saasInfo['parent_id'] > 0){
  3007. $saas_parent_id = $saasInfo['parent_id'];
  3008. $level = 1;
  3009. do {
  3010. if($level == 1){
  3011. if(($rightInfo['commission_one'] > 0) && ($saasInfo['parent_id'] > 0)){
  3012. $rebate = sprintf("%.2f", ($rightInfo['commission_one'] / 100) * $order->pay_price );
  3013. $source = 1; //一级分账
  3014. }else{
  3015. break;
  3016. }
  3017. }
  3018. if($level == 2){
  3019. if($rightInfo['commission_two'] > 0){
  3020. $rebate = sprintf("%.2f", ($rightInfo['commission_two'] / 100) * $order->pay_price );
  3021. $source = 2; //二级分账
  3022. }else{
  3023. break;
  3024. }
  3025. }
  3026. if($level == 3){
  3027. if($rightInfo['commission_three'] > 0){
  3028. $rebate = sprintf("%.2f", ($rightInfo['commission_three'] / 100) * $order->pay_price );
  3029. $source = 3; //三级分账
  3030. }else{
  3031. break;
  3032. }
  3033. }
  3034. $type = 0; //类型 0--佣金 1--提现
  3035. $order->share_price += $rebate;
  3036. $order->save();
  3037. SaasShareMoney::set($rebate,$saas_parent_id, $order->id, $type, $source, 0, 0);
  3038. $saas_parent_id = $this->saasCommission($saas_parent_id,$rebate);
  3039. if(!$saas_parent_id || $saas_parent_id <= 0){
  3040. break;
  3041. }
  3042. $level++;
  3043. }
  3044. while($level <=3);
  3045. }
  3046. }
  3047. //招商入驻
  3048. if($rid == 3){
  3049. // $this->storeInfo($saasInfo,$addTime);
  3050. }
  3051. //代理
  3052. if($rid == 4){
  3053. $this->proxyInfo($saasInfo,$addTime);
  3054. }
  3055. //聚合供应链 开通云仓 跟店铺有冲突暂时去掉等需求确定
  3056. // if($rid == 6){
  3057. // $this->cloudInfo($saasInfo,$addTime);
  3058. // }
  3059. }
  3060. echo 'success';
  3061. return;
  3062. } else {
  3063. //$t->rollBack();
  3064. echo "支付失败";
  3065. return;
  3066. }
  3067. }
  3068. public function ptActivityOrderNotify($message) {
  3069. $order = PtActivityOrder::findOne(['order_no' => $message['out_trade_no']]);
  3070. if (!$order) {
  3071. return;
  3072. }
  3073. if ($order->is_pay == 1) {
  3074. echo "订单已支付";
  3075. return;
  3076. }
  3077. $order->is_pay = 1;
  3078. $order->pay_time = time();
  3079. $order->pay_type = $this->pay_type;
  3080. $order->trade_status = 0;
  3081. $order->transaction_id = $message['transaction_id'];
  3082. if ($order->save()) {
  3083. if ($order->head_integral > 0) {
  3084. $res = AccountLog::saveLog($order->user_id, floatval($order->head_integral), AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_EXPEND, 3, $order->id, "商城拼团开团支付积分,订单号为:{$order->order_no}。");
  3085. if (!$res) {
  3086. debug_log('开团积分不足' . json_encode($res), 'ptOrderLog.log');
  3087. }
  3088. $order->head_integral_status = 1;
  3089. $order->save();
  3090. }
  3091. //调用消息队列
  3092. $pt_number = $order->pt_number ?: $order->id;
  3093. //判断是团长开团
  3094. $is_first_order = false;
  3095. if ($pt_number === $order->id) {
  3096. $is_first_order = true;
  3097. }
  3098. $order->pt_number && $order = PtActivityOrder::findOne($order->pt_number);
  3099. $order_detail = PtActivityOrderDetail::find()->where(['order_id' => $pt_number])->select('goods_id, activity_id')->asArray()->one();
  3100. if (!empty($order) && !empty($order_detail)) {
  3101. $pt_activity_goods = PtActivityGoods::findOne([
  3102. 'activity_id' => $order_detail['activity_id'],
  3103. 'goods_id' => $order_detail['goods_id'],
  3104. 'is_delete' => 0
  3105. ]);
  3106. if ($pt_activity_goods) {
  3107. $pt_activity = PtActivity::findOne(['id' => $pt_activity_goods->activity_id, 'is_delete' => 0]);
  3108. if ($pt_activity) {
  3109. $query = PtActivityOrder::find()->where(['trade_status' => 0, 'is_pay' => 1])
  3110. ->andWhere(['OR', ['id' => $pt_number], ['pt_number' => $pt_number]]);
  3111. if($pt_activity->party_type == 0){
  3112. $pt_order = $query->select('id')->column();
  3113. if ($pt_activity->party_size <= count($pt_order)) {
  3114. queue_push(new CreatedPtOrderJob(['order_id' => ($pt_number ?: $order->id)]));
  3115. }
  3116. }
  3117. if($pt_activity->party_type == 1){
  3118. $pt_order = $query->all();
  3119. $goodsCount = 0;
  3120. foreach ($pt_order as $o) {
  3121. $odgc = PtActivityOrderDetail::find()->where(['order_id' => $o['id'], 'is_delete' => 0])->sum('num');
  3122. $goodsCount += (int)$odgc;
  3123. }
  3124. if($pt_activity->party_goods_count <= $goodsCount){
  3125. queue_push(new CreatedPtOrderJob(['order_id' => ($pt_number ?: $order->id)]));
  3126. }
  3127. }
  3128. //判断是团长开团
  3129. if ($is_first_order) {
  3130. $delay = $pt_activity->split_time * 60 * 60;
  3131. queue_push(new CancelPtOrderJob(['order_id' => $order->id, 'store_id' => $order->store_id]), $delay);
  3132. }
  3133. }
  3134. }
  3135. }
  3136. }
  3137. }
  3138. //用户添加佣金
  3139. private function saasCommission($saas_id,$price){
  3140. $saasInfo = SaasUser::findOne(['id' =>$saas_id,'is_delete'=>0]);
  3141. if(!$saasInfo){
  3142. return false;
  3143. }
  3144. $saasInfo->price += $price;
  3145. $saasInfo->total_price += $price;
  3146. $saasInfo->save();
  3147. return isset($saasInfo['parent_id']) ? $saasInfo['parent_id']: 0;
  3148. }
  3149. public function AdoptCostOrderNotify ($res){
  3150. $t = \Yii::$app->db->beginTransaction();
  3151. $order = AdoptCostOrder::findOne([
  3152. 'order_no' => $res['out_trade_no'],
  3153. ]);
  3154. if ($order->is_pay == 1) {
  3155. $t->rollBack();
  3156. echo "订单已支付";
  3157. return;
  3158. }
  3159. $order->is_pay = 1;
  3160. $order->pay_time = time();
  3161. $order->pay_type = $this->pay_type;
  3162. if ($order->order_type == 1) {
  3163. $order->trade_status = AdoptCostOrder::ORDER_FLOW_NO_SEND;
  3164. } else{
  3165. $order->trade_status = AdoptCostOrder::ORDER_FLOW_CONFIRM;
  3166. $adopt_order = AdoptOrderInfo::findOne(['order_id' => $order->order_id, 'store_id' => $order->store_id]);
  3167. if ($adopt_order->pick_method == AdoptOrderInfo::ORDER_FLOW_NO_PICK) {
  3168. $adopt_order->pick_method = AdoptOrderInfo::ORDER_FLOW_OTHER_PICK;
  3169. $adopt_order->pick_time = time();
  3170. if (!$adopt_order->save()) {
  3171. $t->rollBack();
  3172. echo "支付失败";
  3173. return;
  3174. }
  3175. }
  3176. }
  3177. $order->is_delete = 0;
  3178. $order->transaction_id = $res['transaction_id'];
  3179. if ($order->save()) {
  3180. $t->commit();
  3181. $form = new OrderComplete();
  3182. $form->order_id = $order->id;
  3183. $form->order_type = 0;
  3184. $form->adoptOrderCostNotify();
  3185. try {
  3186. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($order->store_id)) {
  3187. $store = Store::findOne($order->store_id);
  3188. $profit = $order->pay_price * $store->transfer_profit / 100;
  3189. $price = $order->pay_price - $profit;
  3190. Store::addMoney($store, $price, '用户认养订单', $order->id, $order->user_id);
  3191. }
  3192. } catch (\Exception $e) {
  3193. // Todo
  3194. }
  3195. echo 'success';
  3196. return;
  3197. } else {
  3198. $t->rollBack();
  3199. echo "支付失败";
  3200. return;
  3201. }
  3202. }
  3203. //权限时间
  3204. private function rightDuration($saas_id,$rid,$addTime,$from = 3){
  3205. $duration = BusinessRightDuration::find()->where(['right_id'=>$rid,'saas_id'=>$saas_id,'is_delete'=>0])->one();
  3206. if(!$duration){
  3207. $duration = new BusinessRightDuration();
  3208. $duration->saas_id = $saas_id;
  3209. $duration->right_id = $rid;
  3210. $duration->created_at = time();
  3211. $duration->expire_time = ($addTime == 0) ? 0 : time()+ $addTime;
  3212. $duration->is_delete = 0;
  3213. $duration->from = $from;
  3214. $duration->save();
  3215. }else{
  3216. if($duration->expire_time > 0){
  3217. $expire_time = $duration->expire_time;
  3218. //如果已经过期从当前时间开始
  3219. if($duration->expire_time <= time()){
  3220. $expire_time = time();
  3221. }
  3222. $duration->expire_time = ($addTime == 0) ? 0 : $expire_time + $addTime;
  3223. $duration->save();
  3224. }
  3225. }
  3226. }
  3227. //代理
  3228. private function proxyInfo($saasInfo,$addTime){
  3229. $proxy = Admin::find()->where(['type'=>Admin::ADMIN_TYPE_DEFAULT,'saas_user_id'=>$saasInfo['id']])->one();
  3230. if(!$proxy){
  3231. //不存在就创建
  3232. $proxy = new Admin();
  3233. $proxy->type = Admin::ADMIN_TYPE_BD_AGENT;
  3234. $proxy->saas_user_id = $saasInfo['id'];
  3235. $proxy->username = $saasInfo['mobile']."_p";
  3236. $proxy->access_token = \Yii::$app->security->generateRandomString();
  3237. $proxy->password = \Yii::$app->security->generatePasswordHash(substr($saasInfo['mobile'],-6));
  3238. $proxy->mobile = $saasInfo['mobile'];
  3239. $proxy->expire_time = ($addTime == 0) ? 0 : time()+$addTime;
  3240. $proxy->save();
  3241. }else{
  3242. if($proxy->expire_time > 0){
  3243. $proxy->expire_time = ($addTime == 0) ? 0 : time()+$addTime;
  3244. $proxy->save();
  3245. }
  3246. }
  3247. }
  3248. //招商入驻
  3249. private function storeInfo($saasInfo,$addTime){
  3250. //先创建store 用于绑定商户信息
  3251. // 判断是否已经存在
  3252. //$admin = Admin::find()->where(['type'=>Admin::ADMIN_TYPE_STORE, 'is_delete' => 0,'saas_user_id'=>$saasInfo['id']])->one();
  3253. $admin = Admin::find()->where(['type'=>Admin::ADMIN_TYPE_STORE, 'is_delete' => 0,'mobile'=>$saasInfo['mobile']])->one();
  3254. if(!$admin){
  3255. $admin = new Admin();
  3256. $admin->access_token = \Yii::$app->security->generateRandomString();
  3257. $admin->username = $saasInfo['mobile']."_s";
  3258. $admin->password = \Yii::$app->security->generatePasswordHash(substr($saasInfo['mobile'],-6));
  3259. $admin->mobile = $saasInfo['mobile'];
  3260. $admin->type = Admin::ADMIN_TYPE_STORE;
  3261. $admin->saas_user_id = $saasInfo['id'];
  3262. $admin->expire_time = ($addTime == 0) ? 0 : time()+$addTime;
  3263. $admin->save();
  3264. }else{
  3265. if($admin->expire_time > 0){
  3266. if($admin->expire_time < time() ){
  3267. $admin->expire_time = ($addTime == 0) ? 0 : time()+$addTime;
  3268. }else{
  3269. $admin->expire_time = ($addTime == 0) ? 0 : $admin->expire_time + $addTime;
  3270. }
  3271. $admin->expire_time = ($addTime == 0) ? 0 : time()+$addTime;
  3272. $admin->save();
  3273. }
  3274. if($admin->saas_user_id == 0){
  3275. $admin->saas_user_id = $saasInfo['id'];
  3276. $admin->save();
  3277. }
  3278. }
  3279. $store = Store::find()->where(['name'=>$saasInfo['mobile']."_s",'is_delete' => 0,'admin_id'=>$admin->id])->one();
  3280. if(!$store){
  3281. $store = new Store();
  3282. $store->name = $saasInfo['mobile']."_s";
  3283. $store->admin_id = $admin->id;
  3284. $store->store_admin = $saasInfo['id'];
  3285. $store->end_time = ($addTime == 0) ? 0 : time()+$addTime;
  3286. if ($store->save()) {
  3287. $initStore = new StoreForm();
  3288. $initStore->initStorageConfig($store->id);
  3289. $admin->type_id = $store->id;
  3290. $admin->store_id = $store->id;
  3291. $admin->save();
  3292. // 设置商城名称
  3293. Option::set('name', $saasInfo['mobile']."_s", $store->id, 'store');
  3294. // 初始化DIY页面
  3295. // 初始化diy模板
  3296. $is = NewDiyTemplate::find()
  3297. ->where([
  3298. 'store_id' => $store->id,
  3299. 'is_delete' => 0,
  3300. 'type' => 100
  3301. ])->asArray()->one();
  3302. if (!$is) {
  3303. $initStore->initDiyTemplate($store->id);
  3304. }
  3305. }
  3306. }else{
  3307. if($store->end_time > 0){
  3308. if($store->end_time < time() ){
  3309. $store->end_time = ($addTime == 0) ? 0 : time()+$addTime;
  3310. }else{
  3311. $store->end_time = ($addTime == 0) ? 0 : $store->end_time+$addTime;
  3312. }
  3313. $store->save();
  3314. }
  3315. }
  3316. }
  3317. /**
  3318. * @param Order $order
  3319. * 微信分账支付回调时添加分账接收方并计算相关分账数额
  3320. */
  3321. private function getBusiness($saas_id = 0) {
  3322. $time = time();
  3323. // 判断是否有三级分销权限
  3324. $share = BusinessRightDuration::find()->where([
  3325. 'is_delete' => 0,
  3326. 'saas_id' => $saas_id,
  3327. 'right_id' => [1, 2, 3]
  3328. ])->andWhere([
  3329. 'or',
  3330. [
  3331. '>',
  3332. 'expire_time',
  3333. $time
  3334. ],
  3335. ['expire_time' => 0]
  3336. ])->asArray()->all();
  3337. $share_right = array_unique(array_column($share, 'right_id'));
  3338. // $self_num = 0;
  3339. // if (in_array(1, $share_right)) {
  3340. // $memberInfo = BusinessMemberOrder::find()->where(['saas_id'=>$saas_id, 'is_pay' => 1])->andWhere(['or', ["<=","begin_time", $time], ['begin_time' => 0]])->andWhere(['or', [">","end_time",$time], ['end_time' => 0]])->asArray()->all();
  3341. // if(!$memberInfo) {
  3342. // $self_num = 0;
  3343. // } else {
  3344. // $memberIds = array_unique(array_column($memberInfo, 'member_id'));
  3345. // $memberInfo = BusinessMember::find()->where(['id'=>$memberIds, 'is_delete'=>0, 'give_self' => 1])->asArray()->all();
  3346. // //开启自购返利
  3347. // if($memberInfo){
  3348. // //获取返利比例
  3349. // $self_num = max(array_unique(array_column($memberInfo, 'self_info')));
  3350. // }
  3351. // }
  3352. // }
  3353. return [
  3354. 'is_share' => in_array(2, $share_right) ? 1 : 0,
  3355. 'self_num' => in_array(1, $share_right) ? 100 : 0,
  3356. 'shop_referrer' => in_array(3, $share_right) ? 1 : 0,
  3357. ];
  3358. }
  3359. /**
  3360. * 抖品信息回调
  3361. */
  3362. public function videoGoodsShare($order)
  3363. {
  3364. try {
  3365. $video_share = VideoGoodsShare::find()->where(['order_id' => $order->id, 'is_pay' => 0, 'is_send' => 0])->all();
  3366. foreach ($video_share as $item) {
  3367. $item->is_pay = 1;
  3368. if (!$item->save()) {
  3369. throw new \Exception(json_encode($item->errors));
  3370. }
  3371. }
  3372. } catch (\Exception $e) {
  3373. \Yii::error("抖品分佣回调结果失败" . $e->getMessage());
  3374. }
  3375. }
  3376. //https://opendocs.alipay.com/open/033p25
  3377. //同步券核销状态
  3378. public function OrderVoucherUse($model) {
  3379. try {
  3380. $coupon = UserCoupon::findOne($model->use_coupon_id);
  3381. if (empty($coupon['voucher_code_id'])) {
  3382. return true;
  3383. }
  3384. $alipay_code = AlipayVoucherCode::findOne($coupon['voucher_code_id']);
  3385. $order_voucher = ActivityOrdervoucher::findOne($alipay_code->activity_ordervoucher_id);
  3386. if (empty($alipay_code) || empty($order_voucher)) {
  3387. return true;
  3388. }
  3389. $order_no = OrderNo::ALIPAY_VOUCHAR_CODE;
  3390. $data = [
  3391. 'activity_id' => $order_voucher->alipay_activity_id,
  3392. 'biz_dt' => date('Y-m-d H:i:s'),
  3393. 'voucher_code' => $alipay_code->voucher_code,
  3394. 'trade_channel' => 'WX_TRADE_CHANNEL',
  3395. 'trade_no' => $model->alipay_trade_no,
  3396. 'total_fee' => $model->total_price
  3397. ];
  3398. $form = new AlipayThirdForm();
  3399. $result = $form->AlipayMarketingActivityOrdervoucherUse($data);
  3400. $file_name = \Yii::$app->runtimePath . '/logs/app_order_voucherUse.log';
  3401. file_put_contents($file_name, "\r\n" . '[核销结果推送][' . date('Y-m-d H:i:s') . ']' . json_encode($result), FILE_APPEND);
  3402. if ($result['code'] === 0) {
  3403. $alipay_code->status = 1;
  3404. $alipay_code->order_no = $order_no;
  3405. $alipay_code->save();
  3406. }
  3407. } catch (\Exception $e) {
  3408. return [
  3409. 'code' => 1,
  3410. 'msg' => $e->getMessage()
  3411. ];
  3412. }
  3413. }
  3414. }