SandboxSimulation.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Observer;
  17. use Amazon\Core\Helper\Data;
  18. use Amazon\Payment\Api\Data\QuoteLinkInterfaceFactory;
  19. use Magento\Framework\Event\Observer;
  20. use Magento\Framework\Event\ObserverInterface;
  21. use Magento\Payment\Model\InfoInterface;
  22. class SandboxSimulation implements ObserverInterface
  23. {
  24. /**
  25. * @var Data
  26. */
  27. private $coreHelper;
  28. /**
  29. * @var QuoteLinkInterfaceFactory
  30. */
  31. private $quoteLinkFactory;
  32. /**
  33. * @param Data $coreHelper
  34. * @param QuoteLinkInterfaceFactory $quoteLinkFactory
  35. */
  36. public function __construct(
  37. Data $coreHelper,
  38. QuoteLinkInterfaceFactory $quoteLinkFactory
  39. ) {
  40. $this->coreHelper = $coreHelper;
  41. $this->quoteLinkFactory = $quoteLinkFactory;
  42. }
  43. public function execute(Observer $observer)
  44. {
  45. if ($this->coreHelper->isSandboxEnabled()) {
  46. $context = $observer->getEvent()->getContext();
  47. $payment = $observer->getEvent()->getPayment();
  48. $simulationReference = $this->getSimulationReference($payment);
  49. if (! empty($simulationReference)) {
  50. $simulationString = $this->getSimulationString($simulationReference, $context);
  51. if (! empty($simulationString)) {
  52. $requestParameter = $this->getRequestParameter($context);
  53. $observer->getTransport()->addData([$requestParameter => $simulationString]);
  54. $this->clearSimulationReference($payment);
  55. }
  56. }
  57. }
  58. }
  59. protected function clearSimulationReference(InfoInterface $payment)
  60. {
  61. $additionalInformation = $payment->getAdditionalInformation();
  62. if (is_array($additionalInformation) && isset($additionalInformation['sandbox_simulation_reference'])) {
  63. unset($additionalInformation['sandbox_simulation_reference']);
  64. $payment->setAdditionalInformation($additionalInformation);
  65. }
  66. $quoteLink = $this->getQuoteLink($payment);
  67. if ($quoteLink->getSandboxSimulationReference()) {
  68. $quoteLink->setSandboxSimulationReference(null)->save();
  69. }
  70. }
  71. /**
  72. * @param $payment
  73. * @return string
  74. */
  75. protected function getSimulationReference($payment)
  76. {
  77. $simulationReference = $this->getSimulationReferenceFromPayment($payment);
  78. $quoteLink = $this->getQuoteLink($payment);
  79. if ($simulationReference) {
  80. $quoteLink->setSandboxSimulationReference($simulationReference)->save();
  81. } else {
  82. $simulationReference = $quoteLink->getSandboxSimulationReference();
  83. }
  84. return $simulationReference;
  85. }
  86. /**
  87. * @param $payment
  88. * @return string
  89. */
  90. protected function getSimulationReferenceFromPayment($payment)
  91. {
  92. $simulationReference = null;
  93. $additionalInformation = $payment->getAdditionalInformation();
  94. if (is_array($additionalInformation) &&
  95. array_key_exists('sandbox_simulation_reference', $additionalInformation)
  96. ) {
  97. $simulationReference = $additionalInformation['sandbox_simulation_reference'];
  98. }
  99. return $simulationReference;
  100. }
  101. /**
  102. * @param $payment
  103. * @return \Amazon\Payment\Api\Data\QuoteLinkInterface
  104. */
  105. protected function getQuoteLink($payment)
  106. {
  107. $quoteId = $payment->getOrder()->getQuoteId();
  108. $quoteLink = $this->quoteLinkFactory->create();
  109. $quoteLink->load($quoteId, 'quote_id');
  110. return $quoteLink;
  111. }
  112. /**
  113. * @return array
  114. */
  115. protected function getRequestParameters()
  116. {
  117. $requestParameters = [
  118. 'authorization' => 'seller_authorization_note',
  119. 'authorization_capture' => 'seller_authorization_note',
  120. 'capture' => 'seller_capture_note',
  121. 'refund' => 'seller_refund_note'
  122. ];
  123. return $requestParameters;
  124. }
  125. /**
  126. * @param string $context
  127. * @return string
  128. */
  129. protected function getRequestParameter($context)
  130. {
  131. $requestParameter = null;
  132. $requestParameters = $this->getRequestParameters();
  133. if (array_key_exists($context, $requestParameters)) {
  134. $requestParameter = $requestParameters[$context];
  135. }
  136. return $requestParameter;
  137. }
  138. /**
  139. * @param string $simulationReference
  140. * @param string|null $context
  141. * @return string
  142. */
  143. protected function getSimulationString($simulationReference, $context = null)
  144. {
  145. $simulationString = null;
  146. $simulationStrings = $this->coreHelper->getSandboxSimulationStrings($context);
  147. if (array_key_exists($simulationReference, $simulationStrings)) {
  148. $simulationString = $simulationStrings[$simulationReference];
  149. }
  150. return $simulationString;
  151. }
  152. }