|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * TNW_Stripe extension |
| 4 | + * NOTICE OF LICENSE |
| 5 | + * |
| 6 | + * This source file is subject to the OSL 3.0 License |
| 7 | + * that is bundled with this package in the file LICENSE.txt. |
| 8 | + * It is also available through the world-wide-web at this URL: |
| 9 | + * https://opensource.org/licenses/osl-3.0.php |
| 10 | + * |
| 11 | + * @category TNW |
| 12 | + * @package TNW_Stripe |
| 13 | + * @copyright Copyright (c) 2017-2022 |
| 14 | + * @license Open Software License (OSL 3.0) |
| 15 | + */ |
| 16 | +namespace TNW\Stripe\Model\Checks; |
| 17 | + |
| 18 | +use DateTimeZone; |
| 19 | +use Magento\Framework\Intl\DateTimeFactory; |
| 20 | +use Magento\Payment\Model\Checks\SpecificationInterface; |
| 21 | +use Magento\Payment\Model\MethodInterface; |
| 22 | +use Magento\Quote\Model\Quote; |
| 23 | +use Magento\Vault\Api\Data\PaymentTokenInterface; |
| 24 | +use Magento\Vault\Model\ResourceModel\PaymentToken\Collection; |
| 25 | +use TNW\Stripe\Model\Ui\ConfigProvider; |
| 26 | + |
| 27 | +/** |
| 28 | + * Model class for customer's saved credit cards presence check. |
| 29 | + */ |
| 30 | +class HasActiveSavedCreditCards implements SpecificationInterface |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var Collection |
| 34 | + */ |
| 35 | + private $paymentTokenCollection; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var DateTimeFactory |
| 39 | + */ |
| 40 | + private $dateTimeFactory; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param Collection $paymentTokenCollection |
| 44 | + * @param DateTimeFactory $dateTimeFactory |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + Collection $paymentTokenCollection, |
| 48 | + DateTimeFactory $dateTimeFactory |
| 49 | + ) { |
| 50 | + $this->paymentTokenCollection = $paymentTokenCollection; |
| 51 | + $this->dateTimeFactory = $dateTimeFactory; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritDoc |
| 56 | + */ |
| 57 | + public function isApplicable(MethodInterface $paymentMethod, Quote $quote) |
| 58 | + { |
| 59 | + if ($paymentMethod->getCode() !== ConfigProvider::CC_VAULT_CODE) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + $customerId = $quote->getCustomerId(); |
| 64 | + |
| 65 | + return $customerId !== null && $this->hasActiveSavedCards($customerId); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Checks if customer has active saved credit cards. |
| 70 | + * |
| 71 | + * @param int $customerId |
| 72 | + * @return bool |
| 73 | + */ |
| 74 | + private function hasActiveSavedCards($customerId) |
| 75 | + { |
| 76 | + $this->paymentTokenCollection->addFilter(PaymentTokenInterface::CUSTOMER_ID, $customerId); |
| 77 | + $this->paymentTokenCollection |
| 78 | + ->addFilter(PaymentTokenInterface::PAYMENT_METHOD_CODE, ConfigProvider::CODE); |
| 79 | + $this->paymentTokenCollection->addFilter(PaymentTokenInterface::IS_ACTIVE, 1); |
| 80 | + $this->paymentTokenCollection->addFilter(PaymentTokenInterface::IS_VISIBLE, 1); |
| 81 | + $this->paymentTokenCollection->addFieldToFilter( |
| 82 | + PaymentTokenInterface::EXPIRES_AT, |
| 83 | + [ |
| 84 | + 'gt' => $this->dateTimeFactory->create( |
| 85 | + 'now', |
| 86 | + new DateTimeZone('UTC') |
| 87 | + )->format('Y-m-d 00:00:00') |
| 88 | + ] |
| 89 | + ); |
| 90 | + return $this->paymentTokenCollection->count() > 0; |
| 91 | + } |
| 92 | +} |
0 commit comments