Преглед изворни кода

礼品卡功能: restful 与 graphql 调试

fogwind пре 5 дана
родитељ
комит
6192e2e466

+ 65 - 0
packages/Longyi/Gift/src/Services/GiftCardService.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace Longyi\Gift\Services;
+
+use Webkul\Checkout\Facades\Cart;
+use Longyi\Gift\Models\GiftCards;
+
+class GiftCardService
+{
+    /**
+     * 激活礼品卡
+     */
+    public function activate(string $code): void
+    {
+        $customerId = auth()->user()->id;
+
+        $giftCard = GiftCards::where('giftcard_number', $code)
+            ->where('customer_id', $customerId)
+            ->first();
+
+        if (! $giftCard) {
+            throw new \Exception('Gift card not found.');
+        }
+
+        $cart = Cart::getCart();
+
+        if (!empty($cart->giftcard_amount)) {
+            throw new \Exception(
+                'The shopping cart has been paid with a gift card.'
+            );
+        }
+
+        GiftCards::setGiftCardCode($giftCard);
+
+        Cart::collectTotals();
+    }
+
+
+    /**
+     * 删除礼品卡
+     */
+    public function remove(): void
+    {
+        $cart = Cart::getCart();
+
+        GiftCards::removeGiftCardCode();
+
+        Cart::collectTotals();
+
+        if ($cart->giftcard_number) {
+            $giftCard = GiftCards::where(
+                'giftcard_number',
+                $cart->giftcard_number
+            )
+            ->where('customer_id', auth()->user()->id)
+            ->first();
+
+            if ($giftCard) {
+                $giftCard->used_giftcard_amount = 0;
+                $giftCard->remaining_giftcard_amount = $giftCard->giftcard_amount;
+                $giftCard->save();
+            }
+        }
+    }
+}

+ 5 - 0
packages/Webkul/BagistoApi/src/Dto/SaveCheckoutCartInput.php

@@ -30,4 +30,9 @@ class SaveCheckoutCartInput
     #[ApiProperty(description: 'Payment method code. Pass "-1" to leave unchanged.')]
     #[SerializedName('paymentMethod')]
     public ?string $paymentMethod = null;
+
+    #[Groups(['mutation'])]
+    #[ApiProperty(description: 'Gift Card Number. Pass "-1" to leave unchanged.')]
+    #[SerializedName('giftCardNumber')]
+    public ?string $giftCardNumber = null;
 }

+ 1 - 0
packages/Webkul/BagistoApi/src/Providers/BagistoApiServiceProvider.php

@@ -254,6 +254,7 @@ class BagistoApiServiceProvider extends ServiceProvider
         $this->app->singleton(SaveCheckoutCartProcessor::class, function ($app) {
             return new SaveCheckoutCartProcessor(
                 $app->make('Webkul\CartRule\Repositories\CartRuleCouponRepository'),
+                $app->make(\Longyi\Gift\Services\GiftCardService::class)
             );
         });
 

+ 27 - 0
packages/Webkul/BagistoApi/src/State/SaveCheckoutCartProcessor.php

@@ -15,6 +15,7 @@ use Webkul\BagistoApi\Facades\TokenHeaderFacade;
 use Webkul\CartRule\Repositories\CartRuleCouponRepository;
 use Webkul\Checkout\Facades\Cart;
 use Webkul\Shipping\Facades\Shipping;
+use Longyi\Gift\Services\GiftCardService;
 
 /**
  * GraphQL processor for the SaveCheckoutCart mutation.
@@ -32,6 +33,7 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
 
     public function __construct(
         protected CartRuleCouponRepository $cartRuleCouponRepository,
+        protected GiftCardService $giftCardService,
     ) {}
 
     /**
@@ -71,6 +73,10 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
                 $this->applyCoupon((string) $data->couponCode);
             }
 
+            if ($this->shouldUpdate($data->giftCardNumber)) {
+                $this->applyGiftCard((string) $data->giftCardNumber);
+            }
+
             Cart::collectTotals();
         } catch (OperationFailedException $e) {
             throw $e;
@@ -161,4 +167,25 @@ class SaveCheckoutCartProcessor implements ProcessorInterface
             throw new OperationFailedException(trans('shop::app.checkout.coupon.error'));
         }
     }
+
+    /**
+     * Apply or remove a gift card. An empty string removes the active gift card.
+     */
+    private function applyGiftCard(string $code): void
+    {
+        // GiftController::activateGiftCard(); // destroyGiftCard
+
+        $code = trim($code);
+
+        if ($code === '') {
+            $this->giftCardService->remove();
+
+            return;
+        }
+
+        if (Cart::getCart()?->giftcard_number === $code) {
+            return;
+        }
+        $this->giftCardService->activate($code);
+    }
 }