AbstractClient.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Gateway\Helper\SubjectReader;
  22. use Amazon\Core\Helper\Data;
  23. use Amazon\Payment\Model\Adapter\AmazonPaymentAdapter;
  24. /**
  25. * Class AbstractClient
  26. * Base class for gateway client classes
  27. */
  28. abstract class AbstractClient implements ClientInterface
  29. {
  30. /**
  31. * @var SubjectReader
  32. */
  33. protected $subjectReader;
  34. /**
  35. * @var Logger
  36. */
  37. protected $logger;
  38. /**
  39. * @var ClientFactoryInterface
  40. */
  41. protected $clientFactory;
  42. /**
  43. * @var Data
  44. */
  45. protected $coreHelper;
  46. /**
  47. * @var AmazonPaymentAdapter
  48. */
  49. protected $adapter;
  50. /**
  51. * AbstractClient constructor.
  52. * @param Logger $logger
  53. * @param ClientFactoryInterface $clientFactory
  54. * @param SubjectReader $subjectReader
  55. * @param Data $coreHelper
  56. * @param AmazonPaymentAdapter $adapter
  57. */
  58. public function __construct(
  59. Logger $logger,
  60. ClientFactoryInterface $clientFactory,
  61. SubjectReader $subjectReader,
  62. Data $coreHelper,
  63. AmazonPaymentAdapter $adapter
  64. ) {
  65. $this->subjectReader = $subjectReader;
  66. $this->clientFactory = $clientFactory;
  67. $this->logger = $logger;
  68. $this->coreHelper = $coreHelper;
  69. $this->adapter = $adapter;
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. public function placeRequest(TransferInterface $transferObject)
  75. {
  76. $data = $transferObject->getBody();
  77. $log = [
  78. 'request' => $transferObject->getBody(),
  79. 'client' => static::class
  80. ];
  81. $response = [];
  82. try {
  83. $response = $this->process($data);
  84. } catch (\Exception $e) {
  85. $message = $e->getMessage() ? $e->getMessage() : "Something went wrong during Gateway request.";
  86. $log['error'] = $message;
  87. $this->logger->debug($log);
  88. }
  89. return $response;
  90. }
  91. /**
  92. * Process http request
  93. *
  94. * @param array $data
  95. */
  96. abstract protected function process(array $data);
  97. }