PaymentService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace api\services;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Coupon\common\models\AddonsCouponUserRelate;
  6. use addons\Store\common\models\StoreCouponUserRelate;
  7. use addons\Store\common\models\StoreOrder;
  8. use addons\Store\common\models\StoreOrderDetail;
  9. use addons\Store\common\models\StoreOrderMarketing;
  10. use common\enums\AddonsEnum;
  11. use common\enums\PayTypeEnum;
  12. use common\enums\StatusEnum;
  13. use common\models\mall\MallAddons;
  14. use common\models\order\Order;
  15. use common\models\order\OrderDetail;
  16. use common\models\order\OrderMarketing;
  17. class PaymentService extends BaseService
  18. {
  19. /**
  20. * 排除支付方式
  21. *
  22. * @param string $tradeNo
  23. * @param int $source
  24. * @param array $payTypeList
  25. *
  26. * @return void
  27. */
  28. public function excludePayType(string $tradeNo, int $source, array &$payTypeList)
  29. {
  30. foreach ($payTypeList as &$item) {
  31. if ($item['pay_type'] == PayTypeEnum::BALANCE) {
  32. $item['is_disable'] = $this->isExcludeBalance($tradeNo, $source);
  33. break;
  34. }
  35. }
  36. }
  37. /**
  38. * 是否排除余额支付方式
  39. *
  40. * @param string $tradeNo
  41. * @param int $source
  42. *
  43. * @return bool
  44. */
  45. public function isExcludeBalance(string $tradeNo, int $source): bool
  46. {
  47. // 验证下单使用的优惠券是否允许余额支付
  48. if ($tradeNo && MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON)) {
  49. $orderWhere = [
  50. 'out_trade_no' => $tradeNo,
  51. 'mall_id' => $this->mall_id,
  52. 'status' => StatusEnum::ENABLED
  53. ];
  54. if ($source == 6) { // 门店订单
  55. if (!MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE)) {
  56. return false;
  57. }
  58. $orderFind = StoreOrder::find();
  59. $isStore = true;
  60. } else {
  61. $orderFind = Order::find();
  62. }
  63. $orderId = $orderFind
  64. ->where($orderWhere)
  65. ->select('id')
  66. ->scalar();
  67. if (!$orderId) {
  68. return false;
  69. }
  70. if (!empty($isStore)) {
  71. $orderDetailFind = StoreOrderDetail::find();
  72. $orderMarketingFind = StoreOrderMarketing::find();
  73. } else {
  74. $orderDetailFind = OrderDetail::find();
  75. $orderMarketingFind = OrderMarketing::find();
  76. }
  77. $orderDetailIds = $orderDetailFind
  78. ->where([
  79. 'order_id' => $orderId,
  80. 'status' => StatusEnum::ENABLED,
  81. 'mall_id' => $this->mall_id
  82. ])
  83. ->select('id')
  84. ->column();
  85. if (!$orderDetailIds) {
  86. return false;
  87. }
  88. $userCouponIdArr = $orderMarketingFind
  89. ->where([
  90. 'order_detail_id' => $orderDetailIds,
  91. 'status' => StatusEnum::ENABLED
  92. ])
  93. ->select('user_coupon_id')
  94. ->column();
  95. array_walk($userCouponIdArr, function (&$item) {
  96. $item = json_decode($item, true);
  97. if (!$item) {
  98. $item = [];
  99. return;
  100. }
  101. $item = array_keys($item);
  102. });
  103. if (!$userCouponIdArr) {
  104. return false;
  105. }
  106. $userCouponIdArr = array_filter($userCouponIdArr);
  107. if (!$userCouponIdArr) {
  108. return false;
  109. }
  110. $userCouponIdArr = array_merge(...$userCouponIdArr);
  111. if (!empty($isStore)) {
  112. $couponIds = StoreCouponUserRelate::find()
  113. ->where([
  114. 'id' => $userCouponIdArr,
  115. 'from_type' => 1, // 只查询平台优惠券
  116. ])
  117. ->select('coupon_id')
  118. ->column();
  119. } else {
  120. $couponIds = AddonsCouponUserRelate::find()
  121. ->where([
  122. 'id' => $userCouponIdArr
  123. ])
  124. ->select('coupon_id')
  125. ->column();
  126. }
  127. if (!$couponIds) {
  128. return false;
  129. }
  130. return AddonsCoupon::find()
  131. ->where([
  132. 'id' => $couponIds,
  133. 'is_not_allow_balance_pay' => StatusEnum::ENABLED,
  134. 'status' => StatusEnum::ENABLED,
  135. 'mall_id' => $this->mall_id
  136. ])
  137. ->andWhere(['!=', 'type', AddonsCouponEnum::EXCHANGE]) // 兑换券类型没有这个开关
  138. ->exists();
  139. }
  140. return false;
  141. }
  142. }