Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PlaceOrder.php
Go to the documentation of this file.
1 <?php
7 
8 use Exception;
16 use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
20 use Magento\InstantPurchase\Model\PlaceOrder as PlaceOrderModel;
23 
29 class PlaceOrder extends Action
30 {
36  private static $knownRequestParams = [
37  'form_key',
38  'product',
39  'instant_purchase_payment_token',
40  'instant_purchase_shipping_address',
41  'instant_purchase_billing_address',
42  ];
43 
47  private $storeManager;
48 
52  private $customerSession;
53 
57  private $formKeyValidator;
58 
62  private $instantPurchaseOptionLoadingFactory;
63 
67  private $productRepository;
68 
72  private $placeOrder;
73 
77  private $orderRepository;
78 
89  public function __construct(
90  Context $context,
91  StoreManagerInterface $storeManager,
92  Session $customerSession,
93  FormKeyValidator $formKeyValidator,
94  InstantPurchaseOptionLoadingFactory $instantPurchaseOptionLoadingFactory,
95  ProductRepositoryInterface $productRepository,
96  PlaceOrderModel $placeOrder,
97  OrderRepositoryInterface $orderRepository
98  ) {
99  parent::__construct($context);
100 
101  $this->storeManager = $storeManager;
102  $this->customerSession = $customerSession;
103  $this->formKeyValidator = $formKeyValidator;
104  $this->instantPurchaseOptionLoadingFactory = $instantPurchaseOptionLoadingFactory;
105  $this->productRepository = $productRepository;
106  $this->placeOrder = $placeOrder;
107  $this->orderRepository = $orderRepository;
108  }
109 
115  public function execute()
116  {
117  $request = $this->getRequest();
118  if (!$this->doesRequestContainAllKnowParams($request)) {
119  return $this->createResponse($this->createGenericErrorMessage(), false);
120  }
121  if (!$this->formKeyValidator->validate($request)) {
122  return $this->createResponse($this->createGenericErrorMessage(), false);
123  }
124 
125  $paymentTokenPublicHash = (string)$request->getParam('instant_purchase_payment_token');
126  $shippingAddressId = (int)$request->getParam('instant_purchase_shipping_address');
127  $billingAddressId = (int)$request->getParam('instant_purchase_billing_address');
128  $carrierCode = (string)$request->getParam('instant_purchase_carrier');
129  $shippingMethodCode = (string)$request->getParam('instant_purchase_shipping');
130  $productId = (int)$request->getParam('product');
131  $productRequest = $this->getRequestUnknownParams($request);
132 
133  try {
134  $customer = $this->customerSession->getCustomer();
135  $instantPurchaseOption = $this->instantPurchaseOptionLoadingFactory->create(
136  $customer->getId(),
137  $paymentTokenPublicHash,
138  $shippingAddressId,
139  $billingAddressId,
140  $carrierCode,
141  $shippingMethodCode
142  );
143  $store = $this->storeManager->getStore();
144  $product = $this->productRepository->getById(
145  $productId,
146  false,
147  $store->getId(),
148  false
149  );
150 
151  $orderId = $this->placeOrder->placeOrder(
152  $store,
153  $customer,
154  $instantPurchaseOption,
155  $product,
156  $productRequest
157  );
158  } catch (NoSuchEntityException $e) {
159  return $this->createResponse($this->createGenericErrorMessage(), false);
160  } catch (Exception $e) {
161  return $this->createResponse(
162  $e instanceof LocalizedException ? $e->getMessage() : $this->createGenericErrorMessage(),
163  false
164  );
165  }
166 
167  $order = $this->orderRepository->get($orderId);
168  $message = __('Your order number is: %1.', $order->getIncrementId());
169 
170  return $this->createResponse($message, true);
171  }
172 
178  private function createGenericErrorMessage(): string
179  {
180  return (string)__('Something went wrong while processing your order. Please try again later.');
181  }
182 
189  private function doesRequestContainAllKnowParams(RequestInterface $request): bool
190  {
191  foreach (self::$knownRequestParams as $knownRequestParam) {
192  if ($request->getParam($knownRequestParam) === null) {
193  return false;
194  }
195  }
196  return true;
197  }
198 
205  private function getRequestUnknownParams(RequestInterface $request): array
206  {
207  $requestParams = $request->getParams();
208  $unknownParams = [];
209  foreach ($requestParams as $param => $value) {
210  if (!isset(self::$knownRequestParams[$param])) {
211  $unknownParams[$param] = $value;
212  }
213  }
214  return $unknownParams;
215  }
216 
224  private function createResponse(string $message, bool $successMessage): JsonResult
225  {
227  $result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
228  $result->setData([
229  'response' => $message
230  ]);
231  if ($successMessage) {
232  $this->messageManager->addSuccessMessage($message);
233  } else {
234  $this->messageManager->addErrorMessage($message);
235  }
236 
237  return $result;
238  }
239 }
$customer
Definition: customers.php:11
return false
Definition: gallery.phtml:36
__construct(Context $context, StoreManagerInterface $storeManager, Session $customerSession, FormKeyValidator $formKeyValidator, InstantPurchaseOptionLoadingFactory $instantPurchaseOptionLoadingFactory, ProductRepositoryInterface $productRepository, PlaceOrderModel $placeOrder, OrderRepositoryInterface $orderRepository)
Definition: PlaceOrder.php:89
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55
$storeManager
__()
Definition: __.php:13
$message
$value
Definition: gender.phtml:16