Skip to content

Commit 6504b5f

Browse files
authored
Fixed TNW-747: Stripe Vault is appear as available payment method during the Order creation for a new customer in Admin. (#1)
- Added check for customer's saved credit cards availability to payment block.
1 parent 0fbc35b commit 6504b5f

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}

etc/di.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,20 @@
393393
type="TNW\Stripe\Plugin\Quote\Api\CartManagement"
394394
sortOrder="1"/>
395395
</type>
396+
397+
<type name="Magento\Payment\Model\Checks\SpecificationFactory">
398+
<arguments>
399+
<argument name="mapping" xsi:type="array">
400+
<item name="has_saved_credit_cards" xsi:type="object">TNW\Stripe\Model\Checks\HasActiveSavedCreditCards</item>
401+
</argument>
402+
</arguments>
403+
</type>
404+
405+
<type name="Magento\Payment\Block\Form\Container">
406+
<arguments>
407+
<argument name="additionalChecks" xsi:type="array">
408+
<item name="0" xsi:type="string">has_saved_credit_cards</item>
409+
</argument>
410+
</arguments>
411+
</type>
396412
</config>

0 commit comments

Comments
 (0)