OrderInformationManagement.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Model;
  17. use Amazon\Core\Client\ClientFactoryInterface;
  18. use Amazon\Core\Exception\AmazonServiceUnavailableException;
  19. use Amazon\Core\Helper\Data as CoreHelper;
  20. use Amazon\Payment\Gateway\Config\Config;
  21. use Amazon\Payment\Api\Data\QuoteLinkInterfaceFactory;
  22. use Amazon\Payment\Api\OrderInformationManagementInterface;
  23. use Amazon\Payment\Domain\AmazonSetOrderDetailsResponse;
  24. use Amazon\Payment\Domain\AmazonSetOrderDetailsResponseFactory;
  25. use Exception;
  26. use Magento\Checkout\Model\Session;
  27. use Magento\Framework\App\ProductMetadata;
  28. use Magento\Framework\Exception\LocalizedException;
  29. use Magento\Framework\Exception\ValidatorException;
  30. use Magento\Quote\Model\Quote;
  31. use Magento\Store\Model\ScopeInterface;
  32. use AmazonPay\ResponseInterface;
  33. use Psr\Log\LoggerInterface;
  34. /**
  35. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  36. */
  37. class OrderInformationManagement implements OrderInformationManagementInterface
  38. {
  39. /**
  40. * @var Session
  41. */
  42. private $session;
  43. /**
  44. * @var ClientFactoryInterface
  45. */
  46. private $clientFactory;
  47. /**
  48. * @var CoreHelper
  49. */
  50. private $coreHelper;
  51. /**
  52. * @var AmazonSetOrderDetailsResponseFactory
  53. */
  54. private $amazonSetOrderDetailsResponseFactory;
  55. /*
  56. * @var QuoteLinkInterfaceFactory
  57. */
  58. private $quoteLinkFactory;
  59. /**
  60. * @var LoggerInterface
  61. */
  62. private $logger;
  63. /**
  64. * @var Config
  65. */
  66. private $config;
  67. /**
  68. * @var ProductMetadata
  69. */
  70. private $productMetadata;
  71. /**
  72. * OrderInformationManagement constructor.
  73. * @param Session $session
  74. * @param ClientFactoryInterface $clientFactory
  75. * @param CoreHelper $coreHelper
  76. * @param Config $config
  77. * @param AmazonSetOrderDetailsResponseFactory $amazonSetOrderDetailsResponseFactory
  78. * @param QuoteLinkInterfaceFactory $quoteLinkFactory
  79. * @param LoggerInterface $logger
  80. * @param ProductMetadata $productMetadata
  81. */
  82. public function __construct(
  83. Session $session,
  84. ClientFactoryInterface $clientFactory,
  85. CoreHelper $coreHelper,
  86. Config $config,
  87. AmazonSetOrderDetailsResponseFactory $amazonSetOrderDetailsResponseFactory,
  88. QuoteLinkInterfaceFactory $quoteLinkFactory,
  89. LoggerInterface $logger,
  90. ProductMetadata $productMetadata
  91. ) {
  92. $this->session = $session;
  93. $this->clientFactory = $clientFactory;
  94. $this->coreHelper = $coreHelper;
  95. $this->config = $config;
  96. $this->amazonSetOrderDetailsResponseFactory = $amazonSetOrderDetailsResponseFactory;
  97. $this->quoteLinkFactory = $quoteLinkFactory;
  98. $this->logger = $logger;
  99. $this->productMetadata = $productMetadata;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function saveOrderInformation($amazonOrderReferenceId, $allowedConstraints = [])
  105. {
  106. try {
  107. $quote = $this->session->getQuote();
  108. $storeId = $quote->getStoreId();
  109. $this->validateCurrency($quote->getQuoteCurrencyCode());
  110. $this->setReservedOrderId($quote);
  111. $storeName = $this->coreHelper->getStoreName(ScopeInterface::SCOPE_STORE, $storeId);
  112. if (!$storeName) {
  113. $storeName = $quote->getStore()->getName();
  114. }
  115. $data = [
  116. 'amazon_order_reference_id' => $amazonOrderReferenceId,
  117. 'amount' => $quote->getGrandTotal(),
  118. 'currency_code' => $quote->getQuoteCurrencyCode(),
  119. 'store_name' => $storeName,
  120. 'custom_information' =>
  121. 'Magento Version : ' . $this->productMetadata->getVersion() . ' ' .
  122. 'Plugin Version : ' . $this->coreHelper->getVersion()
  123. ,
  124. 'platform_id' => $this->config->getValue('platform_id')
  125. ];
  126. $responseParser = $this->clientFactory->create($storeId)->setOrderReferenceDetails($data);
  127. $response = $this->amazonSetOrderDetailsResponseFactory->create(
  128. [
  129. 'response' => $responseParser
  130. ]
  131. );
  132. $this->validateConstraints($response, $allowedConstraints);
  133. } catch (LocalizedException $e) {
  134. throw $e;
  135. } catch (Exception $e) {
  136. $this->logger->error($e);
  137. throw new AmazonServiceUnavailableException();
  138. }
  139. }
  140. protected function validateCurrency($code)
  141. {
  142. if ($this->coreHelper->getCurrencyCode() !== $code) {
  143. throw new LocalizedException(__('The currency selected is not supported by Amazon Pay'));
  144. }
  145. }
  146. protected function validateConstraints(AmazonSetOrderDetailsResponse $response, $allowedConstraints)
  147. {
  148. foreach ($response->getConstraints() as $constraint) {
  149. if (! in_array($constraint->getId(), $allowedConstraints)) {
  150. throw new ValidatorException(__($constraint->getErrorMessage()));
  151. }
  152. }
  153. }
  154. protected function setReservedOrderId(Quote $quote)
  155. {
  156. if (! $quote->getReservedOrderId()) {
  157. $quote
  158. ->reserveOrderId()
  159. ->save();
  160. }
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. public function confirmOrderReference($amazonOrderReferenceId, $storeId = null)
  166. {
  167. try {
  168. $response = $this->clientFactory->create($storeId)->confirmOrderReference(
  169. [
  170. 'amazon_order_reference_id' => $amazonOrderReferenceId
  171. ]
  172. );
  173. $this->validateResponse($response);
  174. } catch (LocalizedException $e) {
  175. throw $e;
  176. } catch (Exception $e) {
  177. $this->logger->error($e);
  178. throw new AmazonServiceUnavailableException();
  179. }
  180. }
  181. /**
  182. * {@inheritDoc}
  183. */
  184. public function closeOrderReference($amazonOrderReferenceId, $storeId = null)
  185. {
  186. try {
  187. $response = $this->clientFactory->create($storeId)->closeOrderReference(
  188. [
  189. 'amazon_order_reference_id' => $amazonOrderReferenceId
  190. ]
  191. );
  192. $this->validateResponse($response);
  193. } catch (LocalizedException $e) {
  194. throw $e;
  195. } catch (Exception $e) {
  196. $this->logger->error($e);
  197. throw new AmazonServiceUnavailableException();
  198. }
  199. }
  200. /**
  201. * {@inheritDoc}
  202. */
  203. public function cancelOrderReference($amazonOrderReferenceId, $storeId = null)
  204. {
  205. try {
  206. $response = $this->clientFactory->create($storeId)->cancelOrderReference(
  207. [
  208. 'amazon_order_reference_id' => $amazonOrderReferenceId
  209. ]
  210. );
  211. $this->validateResponse($response);
  212. } catch (LocalizedException $e) {
  213. throw $e;
  214. } catch (Exception $e) {
  215. $this->logger->error($e);
  216. throw new AmazonServiceUnavailableException();
  217. }
  218. }
  219. protected function validateResponse(ResponseInterface $response)
  220. {
  221. $data = $response->toArray();
  222. if (200 != $data['ResponseStatus']) {
  223. throw new AmazonServiceUnavailableException();
  224. }
  225. }
  226. /**
  227. * {@inheritDoc}
  228. */
  229. public function removeOrderReference()
  230. {
  231. $quote = $this->session->getQuote();
  232. if ($quote->getId()) {
  233. $quoteLink = $this->quoteLinkFactory->create()->load($quote->getId(), 'quote_id');
  234. if ($quoteLink->getId()) {
  235. $quoteLink->delete();
  236. }
  237. }
  238. }
  239. }