123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- /**
- * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License").
- * You may not use this file except in compliance with the License.
- * A copy of the License is located at
- *
- * http://aws.amazon.com/apache2.0
- *
- * or in the "license" file accompanying this file. This file is distributed
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
- * express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
- namespace Amazon\Payment\Model;
- use Amazon\Core\Client\ClientFactoryInterface;
- use Amazon\Core\Exception\AmazonServiceUnavailableException;
- use Amazon\Core\Helper\Data as CoreHelper;
- use Amazon\Payment\Gateway\Config\Config;
- use Amazon\Payment\Api\Data\QuoteLinkInterfaceFactory;
- use Amazon\Payment\Api\OrderInformationManagementInterface;
- use Amazon\Payment\Domain\AmazonSetOrderDetailsResponse;
- use Amazon\Payment\Domain\AmazonSetOrderDetailsResponseFactory;
- use Exception;
- use Magento\Checkout\Model\Session;
- use Magento\Framework\App\ProductMetadata;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Exception\ValidatorException;
- use Magento\Quote\Model\Quote;
- use Magento\Store\Model\ScopeInterface;
- use AmazonPay\ResponseInterface;
- use Psr\Log\LoggerInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class OrderInformationManagement implements OrderInformationManagementInterface
- {
- /**
- * @var Session
- */
- private $session;
- /**
- * @var ClientFactoryInterface
- */
- private $clientFactory;
- /**
- * @var CoreHelper
- */
- private $coreHelper;
- /**
- * @var AmazonSetOrderDetailsResponseFactory
- */
- private $amazonSetOrderDetailsResponseFactory;
- /*
- * @var QuoteLinkInterfaceFactory
- */
- private $quoteLinkFactory;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @var Config
- */
- private $config;
- /**
- * @var ProductMetadata
- */
- private $productMetadata;
- /**
- * OrderInformationManagement constructor.
- * @param Session $session
- * @param ClientFactoryInterface $clientFactory
- * @param CoreHelper $coreHelper
- * @param Config $config
- * @param AmazonSetOrderDetailsResponseFactory $amazonSetOrderDetailsResponseFactory
- * @param QuoteLinkInterfaceFactory $quoteLinkFactory
- * @param LoggerInterface $logger
- * @param ProductMetadata $productMetadata
- */
- public function __construct(
- Session $session,
- ClientFactoryInterface $clientFactory,
- CoreHelper $coreHelper,
- Config $config,
- AmazonSetOrderDetailsResponseFactory $amazonSetOrderDetailsResponseFactory,
- QuoteLinkInterfaceFactory $quoteLinkFactory,
- LoggerInterface $logger,
- ProductMetadata $productMetadata
- ) {
- $this->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();
- }
- }
- }
- }
|