Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RequestSecureToken.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
18 
25 class RequestSecureToken extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
26 {
30  protected $resultJsonFactory;
31 
35  private $sessionTransparent;
36 
40  private $secureTokenService;
41 
45  private $sessionManager;
46 
50  private $transparent;
51 
60  public function __construct(
61  Context $context,
63  Generic $sessionTransparent,
64  SecureToken $secureTokenService,
65  SessionManager $sessionManager,
66  Transparent $transparent
67  ) {
68  $this->resultJsonFactory = $resultJsonFactory;
69  $this->sessionTransparent = $sessionTransparent;
70  $this->secureTokenService = $secureTokenService;
71  $this->sessionManager = $sessionManager;
72  $this->transparent = $transparent;
73  parent::__construct($context);
74  }
75 
81  public function execute()
82  {
84  $quote = $this->sessionManager->getQuote();
85 
86  if (!$quote or !$quote instanceof Quote) {
87  return $this->getErrorResponse();
88  }
89 
90  $this->sessionTransparent->setQuoteId($quote->getId());
91  try {
92  $token = $this->secureTokenService->requestToken($quote);
93  if (!$token->getData('securetoken')) {
94  throw new \LogicException();
95  }
96 
97  return $this->resultJsonFactory->create()->setData(
98  [
99  $this->transparent->getCode() => ['fields' => $token->getData()],
100  'success' => true,
101  'error' => false
102  ]
103  );
104  } catch (\Exception $e) {
105  return $this->getErrorResponse();
106  }
107  }
108 
112  private function getErrorResponse()
113  {
114  return $this->resultJsonFactory->create()->setData(
115  [
116  'success' => false,
117  'error' => true,
118  'error_messages' => __('Your payment has been declined. Please try again.')
119  ]
120  );
121  }
122 }
return false
Definition: gallery.phtml:36
__construct(Context $context, JsonFactory $resultJsonFactory, Generic $sessionTransparent, SecureToken $secureTokenService, SessionManager $sessionManager, Transparent $transparent)
$quote
__()
Definition: __.php:13