Skip to content

Commit 689562d

Browse files
author
Volodymyr Kublytskyi
committed
Extension and customization points for Instant Purchase module
1 parent 89c45e7 commit 689562d

File tree

100 files changed

+3725
-2434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+3725
-2434
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Braintree\Model\InstantPurchase\CreditCard;
7+
8+
use Magento\Braintree\Gateway\Config\Config;
9+
use Magento\InstantPurchase\PaymentMethodIntegration\AvailabilityCheckerInterface;
10+
11+
/**
12+
* Availability of Braintree vaults for instant purchase.
13+
*/
14+
class AvailabilityChecker implements AvailabilityCheckerInterface
15+
{
16+
/**
17+
* @var Config
18+
*/
19+
private $config;
20+
21+
/**
22+
* AvailabilityChecker constructor.
23+
* @param Config $config
24+
*/
25+
public function __construct(Config $config)
26+
{
27+
$this->config = $config;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function isAvailable(): bool
34+
{
35+
if ($this->config->isVerify3DSecure()) {
36+
// Support of 3D secure not implemented for instant purchase yet.
37+
return false;
38+
}
39+
40+
return true;
41+
}
42+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Braintree\Model\InstantPurchase\CreditCard;
7+
8+
use Magento\InstantPurchase\PaymentMethodIntegration\PaymentTokenFormatterInterface;
9+
use Magento\Vault\Api\Data\PaymentTokenInterface;
10+
11+
/**
12+
* Braintree stored credit card formatter.
13+
*/
14+
class TokenFormatter implements PaymentTokenFormatterInterface
15+
{
16+
/**
17+
* Most used credit card types
18+
* @var array
19+
*/
20+
public static $baseCardTypes = [
21+
'AE' => 'American Express',
22+
'VI' => 'Visa',
23+
'MC' => 'MasterCard',
24+
'DI' => 'Discover',
25+
'JBC' => 'JBC',
26+
'CUP' => 'China Union Pay',
27+
'MI' => 'Maestro',
28+
];
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function formatPaymentToken(PaymentTokenInterface $paymentToken): string
34+
{
35+
$details = json_decode($paymentToken->getTokenDetails() ?: '{}', true);
36+
if (!isset($details['type'], $details['maskedCC'], $details['expirationDate'])) {
37+
throw new \InvalidArgumentException('Invalid Braintree credit card token details.');
38+
}
39+
40+
if (isset(self::$baseCardTypes[$details['type']])) {
41+
$ccType = self::$baseCardTypes[$details['type']];
42+
} else {
43+
$ccType = $details['type'];
44+
}
45+
46+
$formatted = sprintf(
47+
'%s: %s, %s: %s (%s: %s)',
48+
__('Credit Card'),
49+
$ccType,
50+
__('ending'),
51+
$details['maskedCC'],
52+
__('expires'),
53+
$details['expirationDate']
54+
);
55+
56+
return $formatted;
57+
}
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Braintree\Model\InstantPurchase\PayPal;
7+
8+
use Magento\InstantPurchase\PaymentMethodIntegration\PaymentTokenFormatterInterface;
9+
use Magento\Vault\Api\Data\PaymentTokenInterface;
10+
11+
/**
12+
* Braintree PayPal token formatter.
13+
*/
14+
class TokenFormatter implements PaymentTokenFormatterInterface
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function formatPaymentToken(PaymentTokenInterface $paymentToken): string
20+
{
21+
$details = json_decode($paymentToken->getTokenDetails() ?: '{}', true);
22+
if (!isset($details['payerEmail'])) {
23+
throw new \InvalidArgumentException('Invalid Braintree PayPal token details.');
24+
}
25+
26+
$formatted = sprintf(
27+
'%s: %s',
28+
__('PayPal'),
29+
$details['payerEmail']
30+
);
31+
32+
return $formatted;
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Braintree\Model\InstantPurchase;
7+
8+
use Magento\Braintree\Gateway\Command\GetPaymentNonceCommand;
9+
use Magento\InstantPurchase\PaymentMethodIntegration\PaymentAdditionalInformationProviderInterface;
10+
use Magento\Vault\Api\Data\PaymentTokenInterface;
11+
12+
/**
13+
* Provides Braintree specific payment additional information for instant purchase.
14+
*/
15+
class PaymentAdditionalInformationProvider implements PaymentAdditionalInformationProviderInterface
16+
{
17+
/**
18+
* @var GetPaymentNonceCommand
19+
*/
20+
private $getPaymentNonceCommand;
21+
22+
/**
23+
* PaymentAdditionalInformationProvider constructor.
24+
* @param GetPaymentNonceCommand $getPaymentNonceCommand
25+
*/
26+
public function __construct(GetPaymentNonceCommand $getPaymentNonceCommand)
27+
{
28+
$this->getPaymentNonceCommand = $getPaymentNonceCommand;
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function getAdditionalInformation(PaymentTokenInterface $paymentToken): array
35+
{
36+
$paymentMethodNonce = $this->getPaymentNonceCommand->execute([
37+
PaymentTokenInterface::CUSTOMER_ID => $paymentToken->getCustomerId(),
38+
PaymentTokenInterface::PUBLIC_HASH => $paymentToken->getPublicHash(),
39+
])->get()['paymentMethodNonce'];
40+
41+
return [
42+
'payment_method_nonce' => $paymentMethodNonce,
43+
];
44+
}
45+
}

app/code/Magento/Braintree/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"magento/module-config": "100.3.*",
1616
"magento/module-customer": "100.3.*",
1717
"magento/module-directory": "100.3.*",
18+
"magento/module-instant-purchase": "100.3.*",
1819
"magento/module-payment": "100.3.*",
1920
"magento/module-paypal": "100.3.*",
2021
"magento/module-quote": "100.3.*",

app/code/Magento/Braintree/etc/config.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,21 @@
7171
<braintree_cc_vault>
7272
<model>BraintreeCreditCardVaultFacade</model>
7373
<title>Stored Cards (Braintree)</title>
74+
<instant_purchase>
75+
<available>Magento\Braintree\Model\InstantPurchase\CreditCard\AvailabilityChecker</available>
76+
<tokenFormat>Magento\Braintree\Model\InstantPurchase\CreditCard\TokenFormatter</tokenFormat>
77+
<additionalInformation>Magento\Braintree\Model\InstantPurchase\PaymentAdditionalInformationProvider</additionalInformation>
78+
</instant_purchase>
7479
</braintree_cc_vault>
7580
<braintree_paypal_vault>
7681
<model>BraintreePayPalVaultFacade</model>
7782
<title>Stored Accounts (Braintree PayPal)</title>
7883
<can_use_internal>1</can_use_internal>
84+
<instant_purchase>
85+
<available>Magento\Braintree\Model\InstantPurchase\CreditCard\AvailabilityChecker</available>
86+
<tokenFormat>Magento\Braintree\Model\InstantPurchase\CreditCard\TokenFormatter</tokenFormat>
87+
<additionalInformation>Magento\Braintree\Model\InstantPurchase\PaymentAdditionalInformationProvider</additionalInformation>
88+
</instant_purchase>
7989
</braintree_paypal_vault>
8090
</payment>
8191
</default>

app/code/Magento/InstantPurchase/Block/Button.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
use Magento\InstantPurchase\Model\Config;
1111

1212
/**
13-
* Class Button
13+
* Configuration for JavaScript instant purchase button component.
14+
*
1415
* @api
1516
*/
1617
class Button extends Template
@@ -36,15 +37,36 @@ public function __construct(
3637
}
3738

3839
/**
39-
* @return string
40+
* Checks if button enabled.
41+
*
42+
* @return bool
43+
*/
44+
public function isEnabled(): bool
45+
{
46+
return $this->instantPurchaseConfig->isModuleEnabled($this->getCurrentStoreId());
47+
}
48+
49+
/**
50+
* @inheritdoc
4051
*/
4152
public function getJsLayout(): string
4253
{
43-
$buttonText = $this->instantPurchaseConfig->getButtonText();
44-
$this->jsLayout['components']['instant-purchase']['config']['buttonText'] = $buttonText;
45-
$this->jsLayout['components']['instant-purchase']['config']['purchaseUrl'] =
46-
$this->getUrl('instantpurchase/button/placeOrder', ['_secure' => true]);
54+
$buttonText = $this->instantPurchaseConfig->getButtonText($this->getCurrentStoreId());
55+
$purchaseUrl = $this->getUrl('instantpurchase/button/placeOrder', ['_secure' => true]);
4756

57+
// String data does not require escaping here and handled on transport level and on client side
58+
$this->jsLayout['components']['instant-purchase']['config']['buttonText'] = $buttonText;
59+
$this->jsLayout['components']['instant-purchase']['config']['purchaseUrl'] = $purchaseUrl;
4860
return parent::getJsLayout();
4961
}
62+
63+
/**
64+
* Returns active store view identifier.
65+
*
66+
* @return int
67+
*/
68+
private function getCurrentStoreId(): int
69+
{
70+
return $this->_storeManager->getStore()->getId();
71+
}
5072
}

app/code/Magento/InstantPurchase/Controller/Button/Available.php

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)