瀏覽代碼

添加礼品卡验证

llp 1 月之前
父節點
當前提交
72734e0340
共有 1 個文件被更改,包括 38 次插入0 次删除
  1. 38 0
      packages/Webkul/Shop/src/Http/Controllers/API/OnepageController.php

+ 38 - 0
packages/Webkul/Shop/src/Http/Controllers/API/OnepageController.php

@@ -233,5 +233,43 @@ class OnepageController extends APIController
         if (! $cart->payment) {
             throw new \Exception(trans('shop::app.checkout.cart.specify-payment-method'));
         }
+
+        if ($cart->giftcard_number) {
+            $this->validateGiftCard($cart);
+        }
+    }
+
+    /**
+     * Validate gift card before order creation.
+     *
+     * @param  \Webkul\Checkout\Models\Cart  $cart
+     * @return void
+     * @throws \Exception
+     */
+    protected function validateGiftCard($cart)
+    {
+        $giftCard = \Longyi\Gift\Models\GiftCards::where('giftcard_number', $cart->giftcard_number)->first();
+
+        if (!$giftCard) {
+            throw new \Exception('The gift card does not exist or has been deleted. Please select a new gift card');
+        }
+
+        if ($giftCard->giftcard_status != 1) {
+            throw new \Exception('The gift card has been used or has expired. Please choose another payment method');
+        }
+
+        if ($giftCard->remaining_giftcard_amount < $cart->giftcard_amount) {
+            throw new \Exception(
+                sprintf(
+                    'The gift card balance is insufficient (current balance: %s, required payment: %s). Please reselect a payment method',
+                    core()->formatPrice($giftCard->remaining_giftcard_amount, $cart->cart_currency_code),
+                    core()->formatPrice($cart->giftcard_amount, $cart->cart_currency_code)
+                )
+            );
+        }
+
+        if ($giftCard->expirationdate && $giftCard->expirationdate->isPast()) {
+            throw new \Exception('The gift card has expired. Please choose another payment method');
+        }
     }
 }