|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\BraintreeGraphQl\Plugin; |
| 9 | + |
| 10 | +use Magento\Braintree\Gateway\Command\GetPaymentNonceCommand; |
| 11 | +use Magento\Braintree\Model\Ui\ConfigProvider; |
| 12 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Plugin creating nonce from Magento Vault Braintree public hash |
| 17 | + */ |
| 18 | +class SetVaultPaymentNonce |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var GetPaymentNonceCommand |
| 22 | + */ |
| 23 | + private $command; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var LoggerInterface |
| 27 | + */ |
| 28 | + private $logger; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param GetPaymentNonceCommand $command |
| 32 | + * @param LoggerInterface $logger |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + GetPaymentNonceCommand $command, |
| 36 | + LoggerInterface $logger |
| 37 | + ) { |
| 38 | + $this->command = $command; |
| 39 | + $this->logger = $logger; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Set Braintree nonce from public hash |
| 44 | + * |
| 45 | + * @param \Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart $subject |
| 46 | + * @param \Magento\Quote\Model\Quote $quote |
| 47 | + * @param array $paymentData |
| 48 | + * @return array |
| 49 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 50 | + */ |
| 51 | + public function beforeExecute( |
| 52 | + \Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart $subject, |
| 53 | + \Magento\Quote\Model\Quote $quote, |
| 54 | + array $paymentData |
| 55 | + ): array { |
| 56 | + if ($paymentData['code'] !== ConfigProvider::CC_VAULT_CODE |
| 57 | + || !isset($paymentData[ConfigProvider::CC_VAULT_CODE]) |
| 58 | + || !isset($paymentData[ConfigProvider::CC_VAULT_CODE]['public_hash']) |
| 59 | + ) { |
| 60 | + return [$quote, $paymentData]; |
| 61 | + } |
| 62 | + |
| 63 | + $subject = [ |
| 64 | + 'public_hash' => $paymentData[ConfigProvider::CC_VAULT_CODE]['public_hash'], |
| 65 | + 'customer_id' => $quote->getCustomerId(), |
| 66 | + 'store_id' => $quote->getStoreId(), |
| 67 | + ]; |
| 68 | + |
| 69 | + try { |
| 70 | + $result = $this->command->execute($subject)->get(); |
| 71 | + $paymentData[ConfigProvider::CC_VAULT_CODE]['payment_method_nonce'] = $result['paymentMethodNonce']; |
| 72 | + } catch (\Exception $e) { |
| 73 | + $this->logger->critical($e); |
| 74 | + throw new GraphQlInputException(__('Sorry, but something went wrong')); |
| 75 | + } |
| 76 | + |
| 77 | + return [$quote, $paymentData]; |
| 78 | + } |
| 79 | +} |
0 commit comments