StoreOrderSuccessServices.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\services\activity\coupon\StoreCouponIssueServices;
  15. use app\services\activity\lottery\LuckLotteryServices;
  16. use app\services\activity\combination\StorePinkServices;
  17. use app\services\BaseServices;
  18. use app\services\pay\PayServices;
  19. use crmeb\exceptions\ApiException;
  20. /**
  21. * Class StoreOrderSuccessServices
  22. * @package app\services\order
  23. * @method getOne(array $where, ?string $field = '*', array $with = []) 获取去一条数据
  24. */
  25. class StoreOrderSuccessServices extends BaseServices
  26. {
  27. /**
  28. *
  29. * StoreOrderSuccessServices constructor.
  30. * @param StoreOrderDao $dao
  31. */
  32. public function __construct(StoreOrderDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 0元支付
  38. * @param array $orderInfo
  39. * @param int $uid
  40. * @return bool
  41. * @throws \think\Exception
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @throws \think\exception\DbException
  45. */
  46. public function zeroYuanPayment(array $orderInfo, int $uid, string $payType = PayServices::YUE_PAY)
  47. {
  48. if ($orderInfo['paid']) {
  49. throw new ApiException(410265);
  50. }
  51. return $this->paySuccess($orderInfo, $payType);//余额支付成功
  52. }
  53. /**
  54. * 支付成功
  55. * @param array $orderInfo
  56. * @param string $paytype
  57. * @param array $other
  58. * @return bool
  59. * @throws \Psr\SimpleCache\InvalidArgumentException
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function paySuccess(array $orderInfo, string $paytype = PayServices::WEIXIN_PAY, array $other = [])
  65. {
  66. $updata = ['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()];
  67. $orderInfo['pay_time'] = $updata['pay_time'];
  68. $orderInfo['pay_type'] = $paytype;
  69. if ($other && isset($other['trade_no'])) {
  70. $updata['trade_no'] = $other['trade_no'];
  71. }
  72. /** @var StoreOrderCartInfoServices $orderInfoServices */
  73. $orderInfoServices = app()->make(StoreOrderCartInfoServices::class);
  74. $orderInfo['storeName'] = $orderInfoServices->getCarIdByProductTitle((int)$orderInfo['id']);
  75. $res1 = $this->dao->update($orderInfo['id'], $updata);
  76. $resPink = true;
  77. if ($orderInfo['combination_id'] && $res1 && !$orderInfo['refund_status']) {
  78. /** @var StorePinkServices $pinkServices */
  79. $pinkServices = app()->make(StorePinkServices::class);
  80. /** @var StoreOrderServices $orderServices */
  81. $orderServices = app()->make(StoreOrderServices::class);
  82. $resPink = $pinkServices->createPink($orderServices->tidyOrder($orderInfo, true));//创建拼团
  83. }
  84. //缓存抽奖次数 除过线下支付
  85. if (isset($orderInfo['pay_type']) && $orderInfo['pay_type'] != 'offline') {
  86. /** @var LuckLotteryServices $luckLotteryServices */
  87. $luckLotteryServices = app()->make(LuckLotteryServices::class);
  88. $luckLotteryServices->setCacheLotteryNum((int)$orderInfo['uid'], 'order');
  89. }
  90. $orderInfo['send_name'] = $orderInfo['real_name'];
  91. //订单支付成功后置事件
  92. event('OrderPaySuccessListener', [$orderInfo]);
  93. //用户推送消息事件
  94. event('NoticeListener', [$orderInfo, 'order_pay_success']);
  95. //支付成功给客服发送消息
  96. event('NoticeListener', [$orderInfo, 'admin_pay_success_code']);
  97. // 推送订单
  98. event('OutPushListener', ['order_pay_push', ['order_id' => (int)$orderInfo['id']]]);
  99. // 小程序订单管理 (自提商品)
  100. if ($orderInfo['shipping_type'] == 2) {
  101. event('OrderShipping', ['product', $orderInfo, 4, '', '']);
  102. }
  103. $res = $res1 && $resPink;
  104. if ($orderInfo['order_type']==1){ //礼包商品订单赠送复购商品优惠卷
  105. $grant_id = sys_config('repeat_voucher',0);
  106. $grant_num = sys_config('repeat_voucher_num',0);
  107. if ($grant_id>0 && $grant_num>0){
  108. for ($i=0;$i<$grant_num;$i++){
  109. self::grantUser($grant_id,$orderInfo['uid']);
  110. }
  111. }
  112. }
  113. return false !== $res;
  114. }
  115. /**
  116. * 发放优惠券到指定个人
  117. * @return mixed
  118. */
  119. public function grantUser($id,$uid)
  120. {
  121. $data['id'] = $id;
  122. $data['uid'] = $uid;
  123. if (!$data['id']) return app('json')->fail(100100);
  124. /** @var StoreCouponIssueServices $issueService */
  125. $issueService = app()->make(StoreCouponIssueServices::class);
  126. $coupon = $issueService->get($data['id']);
  127. if (!$coupon) {
  128. return app('json')->fail(100026);
  129. } else {
  130. $coupon = $coupon->toArray();
  131. }
  132. $user = explode(',', $data['uid']);
  133. $issueService->setCoupon($coupon, $user);
  134. return true;
  135. }
  136. }