GetNonce.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Controller\Payment;
  7. use Magento\Braintree\Gateway\Command\GetPaymentNonceCommand;
  8. use Magento\Framework\App\Action\Action;
  9. use Magento\Framework\App\Action\Context;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\Framework\Controller\ResultInterface;
  12. use Magento\Framework\Session\SessionManagerInterface;
  13. use Magento\Framework\Webapi\Exception;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Class GetNonce
  17. */
  18. class GetNonce extends Action
  19. {
  20. /**
  21. * @var LoggerInterface
  22. */
  23. private $logger;
  24. /**
  25. * @var SessionManagerInterface
  26. */
  27. private $session;
  28. /**
  29. * @var GetPaymentNonceCommand
  30. */
  31. private $command;
  32. /**
  33. * @param Context $context
  34. * @param LoggerInterface $logger
  35. * @param SessionManagerInterface $session
  36. * @param GetPaymentNonceCommand $command
  37. */
  38. public function __construct(
  39. Context $context,
  40. LoggerInterface $logger,
  41. SessionManagerInterface $session,
  42. GetPaymentNonceCommand $command
  43. ) {
  44. parent::__construct($context);
  45. $this->logger = $logger;
  46. $this->session = $session;
  47. $this->command = $command;
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function execute()
  53. {
  54. $response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  55. try {
  56. $publicHash = $this->getRequest()->getParam('public_hash');
  57. $customerId = $this->session->getCustomerId();
  58. $result = $this->command->execute(
  59. ['public_hash' => $publicHash, 'customer_id' => $customerId, 'store_id' => $this->session->getStoreId()]
  60. )
  61. ->get();
  62. $response->setData(['paymentMethodNonce' => $result['paymentMethodNonce']]);
  63. } catch (\Exception $e) {
  64. $this->logger->critical($e);
  65. return $this->processBadRequest($response);
  66. }
  67. return $response;
  68. }
  69. /**
  70. * Return response for bad request
  71. * @param ResultInterface $response
  72. * @return ResultInterface
  73. */
  74. private function processBadRequest(ResultInterface $response)
  75. {
  76. $response->setHttpResponseCode(Exception::HTTP_BAD_REQUEST);
  77. $response->setData(['message' => __('Sorry, but something went wrong')]);
  78. return $response;
  79. }
  80. }