This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate nonce from public hash server-side
This simplifies the implementation requirements when setting the payment method to Braintree Vault. In the current frontend implementation the nonce is created server-side, but using an ajax request when the place order button is activated. The current storefront flow executes: 1. User selects vault method and places order 2. Ajax request with public hash is made to server returning the nonce 3. Nonce is submitted with order placement request in payment details The second step is uncessecary and is removed from the graphql flow with this commit.
- Loading branch information
Showing
7 changed files
with
103 additions
and
23 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
app/code/Magento/BraintreeGraphQl/Plugin/SetVaultPaymentNonce.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\BraintreeGraphQl\Plugin; | ||
|
||
use Magento\Braintree\Gateway\Command\GetPaymentNonceCommand; | ||
use Magento\Braintree\Model\Ui\ConfigProvider; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Plugin creating nonce from Magento Vault Braintree public hash | ||
*/ | ||
class SetVaultPaymentNonce | ||
{ | ||
/** | ||
* @var GetPaymentNonceCommand | ||
*/ | ||
private $command; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param GetPaymentNonceCommand $command | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
GetPaymentNonceCommand $command, | ||
LoggerInterface $logger | ||
) { | ||
$this->command = $command; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Set Braintree nonce from public hash | ||
* | ||
* @param \Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart $subject | ||
* @param \Magento\Quote\Model\Quote $quote | ||
* @param array $paymentData | ||
* @return array | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function beforeExecute( | ||
\Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart $subject, | ||
\Magento\Quote\Model\Quote $quote, | ||
array $paymentData | ||
): array { | ||
if ($paymentData['code'] !== ConfigProvider::CC_VAULT_CODE) { | ||
return [$quote, $paymentData]; | ||
} | ||
|
||
$subject = [ | ||
'public_hash' => $paymentData[ConfigProvider::CC_VAULT_CODE]['public_hash'], | ||
'customer_id' => $quote->getCustomerId(), | ||
'store_id' => $quote->getStoreId(), | ||
]; | ||
|
||
try { | ||
$result = $this->command->execute($subject)->get(); | ||
$paymentData[ConfigProvider::CC_VAULT_CODE]['payment_method_nonce'] = $result['paymentMethodNonce']; | ||
} catch (\Exception $e) { | ||
$this->logger->critical($e); | ||
throw new GraphQlInputException(__('Sorry, but something went wrong')); | ||
} | ||
|
||
return [$quote, $paymentData]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters