| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace api\services;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use addons\Store\common\models\StoreCouponUserRelate;
- use addons\Store\common\models\StoreOrder;
- use addons\Store\common\models\StoreOrderDetail;
- use addons\Store\common\models\StoreOrderMarketing;
- use common\enums\AddonsEnum;
- use common\enums\PayTypeEnum;
- use common\enums\StatusEnum;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\order\OrderMarketing;
- class PaymentService extends BaseService
- {
- /**
- * 排除支付方式
- *
- * @param string $tradeNo
- * @param int $source
- * @param array $payTypeList
- *
- * @return void
- */
- public function excludePayType(string $tradeNo, int $source, array &$payTypeList)
- {
- foreach ($payTypeList as &$item) {
- if ($item['pay_type'] == PayTypeEnum::BALANCE) {
- $item['is_disable'] = $this->isExcludeBalance($tradeNo, $source);
- break;
- }
- }
- }
- /**
- * 是否排除余额支付方式
- *
- * @param string $tradeNo
- * @param int $source
- *
- * @return bool
- */
- public function isExcludeBalance(string $tradeNo, int $source): bool
- {
- // 验证下单使用的优惠券是否允许余额支付
- if ($tradeNo && MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON)) {
- $orderWhere = [
- 'out_trade_no' => $tradeNo,
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED
- ];
- if ($source == 6) { // 门店订单
- if (!MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE)) {
- return false;
- }
- $orderFind = StoreOrder::find();
- $isStore = true;
- } else {
- $orderFind = Order::find();
- }
- $orderId = $orderFind
- ->where($orderWhere)
- ->select('id')
- ->scalar();
- if (!$orderId) {
- return false;
- }
- if (!empty($isStore)) {
- $orderDetailFind = StoreOrderDetail::find();
- $orderMarketingFind = StoreOrderMarketing::find();
- } else {
- $orderDetailFind = OrderDetail::find();
- $orderMarketingFind = OrderMarketing::find();
- }
- $orderDetailIds = $orderDetailFind
- ->where([
- 'order_id' => $orderId,
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $this->mall_id
- ])
- ->select('id')
- ->column();
- if (!$orderDetailIds) {
- return false;
- }
- $userCouponIdArr = $orderMarketingFind
- ->where([
- 'order_detail_id' => $orderDetailIds,
- 'status' => StatusEnum::ENABLED
- ])
- ->select('user_coupon_id')
- ->column();
- array_walk($userCouponIdArr, function (&$item) {
- $item = json_decode($item, true);
- if (!$item) {
- $item = [];
- return;
- }
- $item = array_keys($item);
- });
- if (!$userCouponIdArr) {
- return false;
- }
- $userCouponIdArr = array_filter($userCouponIdArr);
- if (!$userCouponIdArr) {
- return false;
- }
- $userCouponIdArr = array_merge(...$userCouponIdArr);
- if (!empty($isStore)) {
- $couponIds = StoreCouponUserRelate::find()
- ->where([
- 'id' => $userCouponIdArr,
- 'from_type' => 1, // 只查询平台优惠券
- ])
- ->select('coupon_id')
- ->column();
- } else {
- $couponIds = AddonsCouponUserRelate::find()
- ->where([
- 'id' => $userCouponIdArr
- ])
- ->select('coupon_id')
- ->column();
- }
- if (!$couponIds) {
- return false;
- }
- return AddonsCoupon::find()
- ->where([
- 'id' => $couponIds,
- 'is_not_allow_balance_pay' => StatusEnum::ENABLED,
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $this->mall_id
- ])
- ->andWhere(['!=', 'type', AddonsCouponEnum::EXCHANGE]) // 兑换券类型没有这个开关
- ->exists();
- }
- return false;
- }
- }
|