RefundClient.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Http\Client;
  17. use Magento\Payment\Gateway\Http\ClientInterface;
  18. use Magento\Payment\Gateway\Http\TransferInterface;
  19. use Magento\Payment\Model\Method\Logger;
  20. use Amazon\Core\Client\ClientFactoryInterface;
  21. use Amazon\Payment\Domain\AmazonRefundResponseFactory;
  22. /**
  23. * Class RefundClient
  24. * Amazon Pay refund client
  25. */
  26. class RefundClient implements ClientInterface
  27. {
  28. const SUCCESS_CODES = ['Open', 'Closed', 'Completed', 'Pending'];
  29. /**
  30. * @var ClientFactoryInterface
  31. */
  32. private $clientFactory;
  33. /**
  34. * @var Logger
  35. */
  36. private $logger;
  37. /**
  38. * @var AmazonRefundResponseFactory
  39. */
  40. private $refundResponseFactory;
  41. /**
  42. * RefundClient constructor.
  43. *
  44. * @param Logger $logger
  45. * @param ClientFactoryInterface $clientFactory
  46. * @param AmazonRefundResponseFactory $refundResponseFactory
  47. */
  48. public function __construct(
  49. Logger $logger,
  50. ClientFactoryInterface $clientFactory,
  51. AmazonRefundResponseFactory $refundResponseFactory
  52. ) {
  53. $this->refundResponseFactory = $refundResponseFactory;
  54. $this->logger = $logger;
  55. $this->clientFactory = $clientFactory;
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function placeRequest(TransferInterface $transferObject)
  61. {
  62. $data = $transferObject->getBody();
  63. $log = [
  64. 'request' => $transferObject->getBody(),
  65. 'client' => static::class
  66. ];
  67. $response = [];
  68. try {
  69. $response = $this->process($data);
  70. } catch (\Exception $e) {
  71. $message = __($e->getMessage() ?: "Something went wrong during Gateway request.");
  72. $log['error'] = $message;
  73. $this->logger->debug($log);
  74. } finally {
  75. $log['response'] = (array)$response;
  76. $this->logger->debug($log);
  77. }
  78. return $response;
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. protected function process(array $data)
  84. {
  85. $store_id = $data['store_id'];
  86. unset($data['store_id']);
  87. $response = [
  88. 'status' => false
  89. ];
  90. try {
  91. $client = $this->clientFactory->create($store_id);
  92. $responseParser = $client->refund($data);
  93. $refundResponse = $this->refundResponseFactory->create(['response' => $responseParser]);
  94. $refund = $refundResponse->getDetails();
  95. } catch (\Exception $e) {
  96. $log['error'] = $e->getMessage();
  97. $this->logger->debug($log);
  98. }
  99. $response['state'] = $refund->getRefundStatus()->getState();
  100. if (in_array($refund->getRefundStatus()->getState(), self::SUCCESS_CODES)) {
  101. $response['status'] = true;
  102. $response['refund_id'] = $refund->getRefundId();
  103. } else {
  104. $response['response_code'] = $refund->getRefundStatus()->getReasonCode();
  105. }
  106. // Gateway expects response to be in form of array
  107. return $response;
  108. }
  109. }