session = $session; $this->clientFactory = $clientFactory; $this->coreHelper = $coreHelper; $this->config = $config; $this->amazonSetOrderDetailsResponseFactory = $amazonSetOrderDetailsResponseFactory; $this->quoteLinkFactory = $quoteLinkFactory; $this->logger = $logger; $this->productMetadata = $productMetadata; } /** * {@inheritDoc} */ public function saveOrderInformation($amazonOrderReferenceId, $allowedConstraints = []) { try { $quote = $this->session->getQuote(); $storeId = $quote->getStoreId(); $this->validateCurrency($quote->getQuoteCurrencyCode()); $this->setReservedOrderId($quote); $storeName = $this->coreHelper->getStoreName(ScopeInterface::SCOPE_STORE, $storeId); if (!$storeName) { $storeName = $quote->getStore()->getName(); } $data = [ 'amazon_order_reference_id' => $amazonOrderReferenceId, 'amount' => $quote->getGrandTotal(), 'currency_code' => $quote->getQuoteCurrencyCode(), 'store_name' => $storeName, 'custom_information' => 'Magento Version : ' . $this->productMetadata->getVersion() . ' ' . 'Plugin Version : ' . $this->coreHelper->getVersion() , 'platform_id' => $this->config->getValue('platform_id') ]; $responseParser = $this->clientFactory->create($storeId)->setOrderReferenceDetails($data); $response = $this->amazonSetOrderDetailsResponseFactory->create( [ 'response' => $responseParser ] ); $this->validateConstraints($response, $allowedConstraints); } catch (LocalizedException $e) { throw $e; } catch (Exception $e) { $this->logger->error($e); throw new AmazonServiceUnavailableException(); } } protected function validateCurrency($code) { if ($this->coreHelper->getCurrencyCode() !== $code) { throw new LocalizedException(__('The currency selected is not supported by Amazon Pay')); } } protected function validateConstraints(AmazonSetOrderDetailsResponse $response, $allowedConstraints) { foreach ($response->getConstraints() as $constraint) { if (! in_array($constraint->getId(), $allowedConstraints)) { throw new ValidatorException(__($constraint->getErrorMessage())); } } } protected function setReservedOrderId(Quote $quote) { if (! $quote->getReservedOrderId()) { $quote ->reserveOrderId() ->save(); } } /** * {@inheritDoc} */ public function confirmOrderReference($amazonOrderReferenceId, $storeId = null) { try { $response = $this->clientFactory->create($storeId)->confirmOrderReference( [ 'amazon_order_reference_id' => $amazonOrderReferenceId ] ); $this->validateResponse($response); } catch (LocalizedException $e) { throw $e; } catch (Exception $e) { $this->logger->error($e); throw new AmazonServiceUnavailableException(); } } /** * {@inheritDoc} */ public function closeOrderReference($amazonOrderReferenceId, $storeId = null) { try { $response = $this->clientFactory->create($storeId)->closeOrderReference( [ 'amazon_order_reference_id' => $amazonOrderReferenceId ] ); $this->validateResponse($response); } catch (LocalizedException $e) { throw $e; } catch (Exception $e) { $this->logger->error($e); throw new AmazonServiceUnavailableException(); } } /** * {@inheritDoc} */ public function cancelOrderReference($amazonOrderReferenceId, $storeId = null) { try { $response = $this->clientFactory->create($storeId)->cancelOrderReference( [ 'amazon_order_reference_id' => $amazonOrderReferenceId ] ); $this->validateResponse($response); } catch (LocalizedException $e) { throw $e; } catch (Exception $e) { $this->logger->error($e); throw new AmazonServiceUnavailableException(); } } protected function validateResponse(ResponseInterface $response) { $data = $response->toArray(); if (200 != $data['ResponseStatus']) { throw new AmazonServiceUnavailableException(); } } /** * {@inheritDoc} */ public function removeOrderReference() { $quote = $this->session->getQuote(); if ($quote->getId()) { $quoteLink = $this->quoteLinkFactory->create()->load($quote->getId(), 'quote_id'); if ($quoteLink->getId()) { $quoteLink->delete(); } } } }