AuthorizationRequestBuilder.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\Gateway\Request;
  17. use Magento\Payment\Gateway\ConfigInterface;
  18. use Magento\Payment\Gateway\Request\BuilderInterface;
  19. use Magento\Framework\App\ProductMetadata;
  20. use Amazon\Payment\Gateway\Helper\SubjectReader;
  21. use Amazon\Core\Helper\Data;
  22. use Amazon\Core\Model\AmazonConfig;
  23. use Magento\Framework\Event\ManagerInterface;
  24. use Magento\Framework\DataObject;
  25. use Amazon\Payment\Plugin\AdditionalInformation;
  26. use Amazon\Core\Helper\CategoryExclusion;
  27. class AuthorizationRequestBuilder implements BuilderInterface
  28. {
  29. /**
  30. * @var ConfigInterface
  31. */
  32. private $config;
  33. /**
  34. * @var ProductMetadata
  35. */
  36. private $productMetaData;
  37. /**
  38. * @var SubjectReader
  39. */
  40. private $subjectReader;
  41. /**
  42. * @var Data
  43. */
  44. private $coreHelper;
  45. /**
  46. * @var AmazonConfig
  47. */
  48. private $amazonConfig;
  49. /**
  50. * @var ManagerInterface
  51. */
  52. private $eventManager;
  53. /**
  54. * @var CategoryExclusion
  55. */
  56. private $categoryExclusion;
  57. /**
  58. * AuthorizationRequestBuilder constructor.
  59. *
  60. * @param ConfigInterface $config
  61. * @param ProductMetadata $productMetadata
  62. * @param SubjectReader $subjectReader
  63. * @param Data $coreHelper
  64. * @param AmazonConfig $amazonConfig
  65. * @param ManagerInterface $eventManager
  66. * @param CategoryExclusion $categoryExclusion
  67. */
  68. public function __construct(
  69. ConfigInterface $config,
  70. ProductMetaData $productMetadata,
  71. SubjectReader $subjectReader,
  72. Data $coreHelper,
  73. AmazonConfig $amazonConfig,
  74. ManagerInterface $eventManager,
  75. CategoryExclusion $categoryExclusion
  76. ) {
  77. $this->config = $config;
  78. $this->coreHelper = $coreHelper;
  79. $this->amazonConfig = $amazonConfig;
  80. $this->productMetaData = $productMetadata;
  81. $this->subjectReader = $subjectReader;
  82. $this->eventManager = $eventManager;
  83. $this->categoryExclusion = $categoryExclusion;
  84. }
  85. /**
  86. * Builds ENV request
  87. *
  88. * @param array $buildSubject
  89. * @return array
  90. */
  91. public function build(array $buildSubject)
  92. {
  93. $data = [];
  94. $paymentDO = $this->subjectReader->readPayment($buildSubject);
  95. $payment = $paymentDO->getPayment();
  96. $orderDO = $paymentDO->getOrder();
  97. $storeId = $orderDO->getStoreId();
  98. $storeName = '';
  99. $currencyCode = $orderDO->getCurrencyCode();
  100. $total = $buildSubject['amount'];
  101. // capture sale or new auth/capture for partial capture
  102. if (isset($buildSubject['multicurrency']) && $buildSubject['multicurrency']['multicurrency']) {
  103. $currencyCode = $buildSubject['multicurrency']['order_currency'];
  104. $total = $buildSubject['multicurrency']['total'];
  105. $storeName = $buildSubject['multicurrency']['store_name'];
  106. $storeId = $buildSubject['multicurrency']['store_id'];
  107. } else {
  108. // auth has not happened for this order yet
  109. if ($this->amazonConfig->useMultiCurrency($storeId)) {
  110. $quote = $this->subjectReader->getQuote();
  111. $total = $quote->getGrandTotal();
  112. $currencyCode = $quote->getQuoteCurrencyCode();
  113. }
  114. }
  115. if (isset($buildSubject['amazon_order_id']) && $buildSubject['amazon_order_id']) {
  116. $amazonId = $buildSubject['amazon_order_id'];
  117. } else {
  118. $quote = $this->subjectReader->getQuote();
  119. if (!$quote->getReservedOrderId()) {
  120. try {
  121. $quote->reserveOrderId()->save();
  122. } catch (\Exception $e) {
  123. $this->logger->debug($e->getMessage());
  124. }
  125. }
  126. $storeName = $quote->getStore()->getName();
  127. $amazonId = $this->subjectReader->getAmazonId();
  128. }
  129. if ($amazonId) {
  130. $data = [
  131. 'amazon_order_reference_id' => $amazonId,
  132. 'amount' => $total,
  133. 'currency_code' => $currencyCode,
  134. 'store_name' => $storeName,
  135. 'custom_information' =>
  136. 'Magento Version : ' . $this->productMetaData->getVersion() . ' ' .
  137. 'Plugin Version : ' . $this->coreHelper->getVersion(),
  138. 'platform_id' => $this->config->getValue('platform_id'),
  139. 'request_payment_authorization' => true
  140. ];
  141. }
  142. if ($this->coreHelper->isSandboxEnabled('store', $storeId)) {
  143. $data['additional_information'] =
  144. $payment->getAdditionalInformation(AdditionalInformation::KEY_SANDBOX_SIMULATION_REFERENCE);
  145. $eventData = [
  146. 'amazon_order_reference_id' => $amazonId,
  147. 'authorization_amount' => $total,
  148. 'currency_code' => $currencyCode,
  149. 'authorization_reference_id' => $amazonId . '-A' . time(),
  150. 'capture_now' => false,
  151. ];
  152. $transport = new DataObject($eventData);
  153. $this->eventManager->dispatch(
  154. 'amazon_payment_authorize_before',
  155. [
  156. 'context' => 'authorization',
  157. 'payment' => $paymentDO->getPayment(),
  158. 'transport' => $transport
  159. ]
  160. );
  161. }
  162. return $data;
  163. }
  164. }