StoreOrderSuccessServices.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\adminapi\controller\v1\marketing\StoreCouponUser;
  13. use app\dao\order\StoreOrderDao;
  14. use app\model\user\User;
  15. use app\services\activity\coupon\StoreCouponIssueServices;
  16. use app\services\activity\lottery\LuckLotteryServices;
  17. use app\services\activity\combination\StorePinkServices;
  18. use app\services\BaseServices;
  19. use app\services\pay\PayServices;
  20. use app\services\user\UserBrokerageServices;
  21. use app\services\user\UserServices;
  22. use crmeb\exceptions\ApiException;
  23. /**
  24. * Class StoreOrderSuccessServices
  25. * @package app\services\order
  26. * @method getOne(array $where, ?string $field = '*', array $with = []) 获取去一条数据
  27. */
  28. class StoreOrderSuccessServices extends BaseServices
  29. {
  30. /**
  31. *
  32. * StoreOrderSuccessServices constructor.
  33. * @param StoreOrderDao $dao
  34. */
  35. public function __construct(StoreOrderDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * 0元支付
  41. * @param array $orderInfo
  42. * @param int $uid
  43. * @return bool
  44. * @throws \think\Exception
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function zeroYuanPayment(array $orderInfo, int $uid, string $payType = PayServices::YUE_PAY)
  50. {
  51. if ($orderInfo['paid']) {
  52. throw new ApiException(410265);
  53. }
  54. return $this->paySuccess($orderInfo, $payType);//余额支付成功
  55. }
  56. /**
  57. * 支付成功
  58. * @param array $orderInfo
  59. * @param string $paytype
  60. * @param array $other
  61. * @return bool
  62. * @throws \Psr\SimpleCache\InvalidArgumentException
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function paySuccess(array $orderInfo, string $paytype = PayServices::WEIXIN_PAY, array $other = [])
  68. {
  69. $updata = ['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()];
  70. $orderInfo['pay_time'] = $updata['pay_time'];
  71. $orderInfo['pay_type'] = $paytype;
  72. if ($other && isset($other['trade_no'])) {
  73. $updata['trade_no'] = $other['trade_no'];
  74. }
  75. /** @var StoreOrderCartInfoServices $orderInfoServices */
  76. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  77. $orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  78. $res1 = $this->dao->update($orderInfo['id'], $updata);
  79. $resPink = true;
  80. if ($orderInfo['combination_id'] && $res1 && !$orderInfo['refund_status']) {
  81. /** @var StorePinkServices $pinkServices */
  82. $pinkServices = app()->make(StorePinkServices::class);
  83. /** @var StoreOrderServices $orderServices */
  84. $orderServices = app()->make(StoreOrderServices::class);
  85. $resPink = $pinkServices->createPink($orderServices->tidyOrder($orderInfo, true));//创建拼团
  86. }
  87. //缓存抽奖次数 除过线下支付
  88. if (isset($orderInfo['pay_type']) && $orderInfo['pay_type'] != 'offline') {
  89. /** @var LuckLotteryServices $luckLotteryServices */
  90. $luckLotteryServices = app()->make(LuckLotteryServices::class);
  91. $luckLotteryServices->setCacheLotteryNum((int)$orderInfo['uid'], 'order');
  92. }
  93. $orderInfo['send_name'] = $orderInfo['real_name'];
  94. //订单支付成功后置事件
  95. event('OrderPaySuccessListener', [$orderInfo]);
  96. //用户推送消息事件
  97. event('NoticeListener', [$orderInfo, 'order_pay_success']);
  98. //支付成功给客服发送消息
  99. event('NoticeListener', [$orderInfo, 'admin_pay_success_code']);
  100. // 推送订单
  101. event('OutPushListener', ['order_pay_push', ['order_id' => (int)$orderInfo['id']]]);
  102. // 小程序订单管理 (自提商品)
  103. if ($orderInfo['shipping_type'] == 2) {
  104. event('OrderShipping', ['product', $orderInfo, 4, '', '']);
  105. }
  106. $res = $res1 && $resPink;
  107. if ($orderInfo['order_type']==1){ //礼包商品订单赠送复购商品优惠卷
  108. $grant_id = sys_config('repeat_voucher',0);
  109. $grant_num = sys_config('repeat_voucher_num',0);
  110. if ($grant_id>0 && $grant_num>0){
  111. for ($i=0;$i<$grant_num;$i++){
  112. self::grantUser($grant_id,$orderInfo['uid']);
  113. }
  114. }
  115. }
  116. $spread_id = User::where('uid',$orderInfo['uid'])->value('spread_uid');
  117. if ($spread_id>0){
  118. self::spreadReward($spread_id,$orderInfo);
  119. }
  120. return false !== $res;
  121. }
  122. /**
  123. * 发放优惠券到指定个人
  124. * @return mixed
  125. */
  126. public function grantUser($id,$uid)
  127. {
  128. $data['id'] = $id;
  129. $data['uid'] = $uid;
  130. if (!$data['id']) return app('json')->fail(100100);
  131. /** @var StoreCouponIssueServices $issueService */
  132. $issueService = app()->make(StoreCouponIssueServices::class);
  133. $coupon = $issueService->get($data['id']);
  134. if (!$coupon) {
  135. return app('json')->fail(100026);
  136. } else {
  137. $coupon = $coupon->toArray();
  138. }
  139. $user = explode(',', $data['uid']);
  140. $issueService->setCoupon($coupon, $user);
  141. return true;
  142. }
  143. public function spreadReward($spread_uid, $orderInfo){
  144. try {
  145. // 获取上级的分销等级
  146. $agent_level = User::where('uid', $spread_uid)->value('agent_level');
  147. if (!$agent_level) {
  148. return false;
  149. }
  150. // 获取分销等级详细信息
  151. $agentLevelInfo = \app\model\agent\AgentLevel::where('id', $agent_level)->find();
  152. if (!$agentLevelInfo) {
  153. return false;
  154. }
  155. $agentLevelInfo = $agentLevelInfo->toArray();
  156. // 发放优惠券奖励
  157. if ($agentLevelInfo['free_issue'] > 0) {
  158. $this->grantUser($agentLevelInfo['free_issue'], $spread_uid);
  159. }
  160. // 发放佣金奖励
  161. if ($agentLevelInfo['spread_brokerage'] > 0) {
  162. // 计算佣金金额:订单价格 * spread_brokerage / 100
  163. $brokerageAmount = bcmul((string)$orderInfo['total_price'], (string)($agentLevelInfo['spread_brokerage'] / 100), 2);
  164. if ($brokerageAmount > 0) {
  165. /** @var UserServices $userServices */
  166. $userServices = app()->make(UserServices::class);
  167. /** @var UserBrokerageServices $userBrokerageServices */
  168. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  169. $balance = $userServices->value(['uid' => $spread_uid], 'brokerage_price');
  170. $nickname = User::where('uid',$orderInfo['uid'])->value('nickname');
  171. $userBrokerageServices->income('get_brokerage', $spread_uid, [
  172. 'nickname' => $nickname,
  173. 'pay_price' => $orderInfo['total_price'],
  174. 'number' => $brokerageAmount,
  175. ], bcadd((string)$balance, (string)$brokerageAmount, 2), $orderInfo['id'] ?? 0);
  176. $userServices->bcInc($spread_uid, 'brokerage_price', $brokerageAmount, 'uid');
  177. }
  178. }
  179. return true;
  180. } catch (\Exception $e) {
  181. @file_put_contents("quanju.txt", json_encode($e->getMessage())."-spreadReward报错内容\r\n", 8);
  182. @file_put_contents("quanju.txt", json_encode($e->getLine())."-spreadReward报错位置\r\n", 8);
  183. @file_put_contents("quanju.txt", json_encode($e->getFile())."-spreadReward报错文件\r\n", 8);
  184. throw $e;
  185. }
  186. }
  187. }