36 private static $knownRequestParams = [
39 'instant_purchase_payment_token',
40 'instant_purchase_shipping_address',
41 'instant_purchase_billing_address',
47 private $storeManager;
52 private $customerSession;
57 private $formKeyValidator;
62 private $instantPurchaseOptionLoadingFactory;
67 private $productRepository;
77 private $orderRepository;
93 FormKeyValidator $formKeyValidator,
96 PlaceOrderModel $placeOrder,
99 parent::__construct($context);
102 $this->customerSession = $customerSession;
103 $this->formKeyValidator = $formKeyValidator;
104 $this->instantPurchaseOptionLoadingFactory = $instantPurchaseOptionLoadingFactory;
106 $this->placeOrder = $placeOrder;
118 if (!$this->doesRequestContainAllKnowParams(
$request)) {
119 return $this->createResponse($this->createGenericErrorMessage(),
false);
121 if (!$this->formKeyValidator->validate(
$request)) {
122 return $this->createResponse($this->createGenericErrorMessage(),
false);
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');
131 $productRequest = $this->getRequestUnknownParams(
$request);
134 $customer = $this->customerSession->getCustomer();
135 $instantPurchaseOption = $this->instantPurchaseOptionLoadingFactory->create(
137 $paymentTokenPublicHash,
143 $store = $this->storeManager->getStore();
144 $product = $this->productRepository->getById(
151 $orderId = $this->placeOrder->placeOrder(
154 $instantPurchaseOption,
159 return $this->createResponse($this->createGenericErrorMessage(),
false);
160 }
catch (Exception $e) {
161 return $this->createResponse(
162 $e instanceof
LocalizedException ? $e->getMessage() : $this->createGenericErrorMessage(),
167 $order = $this->orderRepository->get($orderId);
170 return $this->createResponse(
$message,
true);
178 private function createGenericErrorMessage(): string
180 return (
string)
__(
'Something went wrong while processing your order. Please try again later.');
189 private function doesRequestContainAllKnowParams(RequestInterface
$request): bool
191 foreach (self::$knownRequestParams as $knownRequestParam) {
192 if (
$request->getParam($knownRequestParam) ===
null) {
205 private function getRequestUnknownParams(RequestInterface
$request): array
207 $requestParams =
$request->getParams();
209 foreach ($requestParams as $param =>
$value) {
210 if (!isset(self::$knownRequestParams[$param])) {
211 $unknownParams[$param] =
$value;
214 return $unknownParams;
224 private function createResponse(
string $message,
bool $successMessage): JsonResult
231 if ($successMessage) {
232 $this->messageManager->addSuccessMessage(
$message);
234 $this->messageManager->addErrorMessage(
$message);