public function applyAmastyGiftCard($code, $accountModel, $quote)
    {
        // Get current gift card balance before applying it to the quote
        // in case "fixed_amount" / "pay for everything" discount type is used
        $giftAmount = $accountModel->getCurrentValue();

        $quoteId = $quote->getId();

        $isValid = $this->amastyGiftCardManagement->getInstance()->validateCode($quote, $code);

        if (!$isValid) {
            throw new LocalizedException(__('Coupon with specified code "%1" is not valid.', $code));
        }

        if ($accountModel->canApplyCardForQuote($quote)) {
            $quoteGiftCard = $this->amastyQuoteFactory->getInstance()->create();
            $this->amastyQuoteResource->getInstance()->load($quoteGiftCard, $quoteId, 'quote_id');
            $subtotal = $quoteGiftCard->getSubtotal($quote);

            if ($quoteGiftCard->getCodeId() && $accountModel->getCodeId() == $quoteGiftCard->getCodeId()) {

                throw new LocalizedException(__('This gift card account is already in the quote.'));

            } elseif ($quoteGiftCard->getGiftAmount() && $subtotal == $quoteGiftCard->getGiftAmount()) {

                throw new LocalizedException(__('Gift card can\'t be applied. Maximum discount reached.'));

            } else {
                $quoteGiftCard->unsetData($quoteGiftCard->getIdFieldName());
                $quoteGiftCard->setQuoteId($quoteId);
                $quoteGiftCard->setCodeId($accountModel->getCodeId());
                $quoteGiftCard->setAccountId($accountModel->getId());

                $this->amastyQuoteRepository->getInstance()->save($quoteGiftCard);
                $this->updateTotals($quote);

                if ($this->getAmastyPayForEverything()) {
                    // pay for everything, items, shipping, tax
                    return $giftAmount;
                } else {
                    // pay for items only
                    $totals = $quote->getTotals();
                    return $totals[self::AMASTY_GIFTCARD]->getValue();
                }
            }
        } else {
            throw new LocalizedException(__('Gift card can\'t be applied.'));
        }
    }