Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetNonce.php
Go to the documentation of this file.
1 <?php
7 
15 use Psr\Log\LoggerInterface;
16 
20 class GetNonce extends Action
21 {
25  private $logger;
26 
30  private $session;
31 
35  private $command;
36 
43  public function __construct(
44  Context $context,
45  LoggerInterface $logger,
46  SessionManagerInterface $session,
47  GetPaymentNonceCommand $command
48  ) {
49  parent::__construct($context);
50  $this->logger = $logger;
51  $this->session = $session;
52  $this->command = $command;
53  }
54 
58  public function execute()
59  {
60  $response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
61 
62  try {
63  $publicHash = $this->getRequest()->getParam('public_hash');
64  $customerId = $this->session->getCustomerId();
65  $result = $this->command->execute(
66  ['public_hash' => $publicHash, 'customer_id' => $customerId, 'store_id' => $this->session->getStoreId()]
67  )
68  ->get();
69  $response->setData(['paymentMethodNonce' => $result['paymentMethodNonce']]);
70  } catch (\Exception $e) {
71  $this->logger->critical($e);
72  return $this->processBadRequest($response);
73  }
74 
75  return $response;
76  }
77 
83  private function processBadRequest(ResultInterface $response)
84  {
85  $response->setHttpResponseCode(Exception::HTTP_BAD_REQUEST);
86  $response->setData(['message' => __('Sorry, but something went wrong')]);
87 
88  return $response;
89  }
90 }
$response
Definition: 404.php:11
__()
Definition: __.php:13
$logger
__construct(Context $context, LoggerInterface $logger, SessionManagerInterface $session, GetPaymentNonceCommand $command)
Definition: GetNonce.php:43