SettlementRequestBuilder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 Amazon\Payment\Gateway\Config\Config;
  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 Magento\Payment\Model\Method\Logger;
  23. use Magento\Sales\Api\OrderRepositoryInterface;
  24. use Magento\Quote\Api\CartRepositoryInterface;
  25. class SettlementRequestBuilder implements BuilderInterface
  26. {
  27. /**
  28. * @var Config
  29. */
  30. private $config;
  31. /**
  32. * @var Logger
  33. */
  34. private $logger;
  35. /**
  36. * @var ProductMetadata
  37. */
  38. private $productMetaData;
  39. /**
  40. * @var SubjectReader
  41. */
  42. private $subjectReader;
  43. /**
  44. * @var Data
  45. */
  46. private $coreHelper;
  47. /**
  48. * @var OrderRepositoryInterface
  49. */
  50. private $orderRepository;
  51. /**
  52. * @var CartRepositoryInterface
  53. */
  54. private $quoteRepository;
  55. /**
  56. * SettlementRequestBuilder constructor.
  57. *
  58. * @param Config $config
  59. * @param ProductMetadata $productMetadata
  60. * @param OrderRepositoryInterface $orderRepository
  61. * @param CartRepositoryInterface $quoteRepository
  62. * @param SubjectReader $subjectReader
  63. * @param Data $coreHelper
  64. * @param Logger $logger
  65. */
  66. public function __construct(
  67. Config $config,
  68. ProductMetaData $productMetadata,
  69. OrderRepositoryInterface $orderRepository,
  70. CartRepositoryInterface $quoteRepository,
  71. SubjectReader $subjectReader,
  72. Data $coreHelper,
  73. Logger $logger
  74. ) {
  75. $this->config = $config;
  76. $this->orderRepository = $orderRepository;
  77. $this->quoteRepository = $quoteRepository;
  78. $this->coreHelper = $coreHelper;
  79. $this->productMetaData = $productMetadata;
  80. $this->subjectReader = $subjectReader;
  81. $this->logger = $logger;
  82. }
  83. /**
  84. * @param array $buildSubject
  85. * @return array
  86. * @throws \Magento\Framework\Exception\NoSuchEntityException
  87. */
  88. public function build(array $buildSubject)
  89. {
  90. $data = [];
  91. $paymentDO = $this->subjectReader->readPayment($buildSubject);
  92. $orderDO = $paymentDO->getOrder();
  93. $currencyCode = $orderDO->getCurrencyCode();
  94. $total = $buildSubject['amount'];
  95. if ($buildSubject['multicurrency']['multicurrency']) {
  96. $currencyCode = $buildSubject['multicurrency']['order_currency'];
  97. $total = $buildSubject['multicurrency']['total'];
  98. }
  99. if (isset($buildSubject['amazon_order_id']) && $buildSubject['amazon_order_id']) {
  100. $data = [
  101. 'amazon_authorization_id' => $paymentDO->getPayment()->getParentTransactionId(),
  102. 'capture_amount' => $total,
  103. 'currency_code' => $currencyCode,
  104. 'amazon_order_reference_id' => $buildSubject['amazon_order_id'],
  105. 'store_id' => $buildSubject['multicurrency']['store_id'],
  106. 'store_name' => $buildSubject['multicurrency']['store_name'],
  107. 'custom_information' =>
  108. 'Magento Version : ' . $this->productMetaData->getVersion() . ' ' .
  109. 'Plugin Version : ' . $this->coreHelper->getVersion(),
  110. 'platform_id' => $this->config->getValue('platform_id'),
  111. 'request_payment_authorization' => false
  112. ];
  113. if (isset($buildSubject['request_payment_authorization']) && $buildSubject['request_payment_authorization']) {
  114. $data['request_payment_authorization'] = true;
  115. }
  116. }
  117. return $data;
  118. }
  119. }