Passed
Push — dev ( 41bd40...5a851b )
by Ruslan
03:36
created

prepareRequestTransferToBuildJsonRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\AfterPay\Business\Api\Adapter\ApiCall;
9
10
use Generated\Shared\Transfer\AfterPayApiResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rPayApiResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\AfterPayCaptureRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yCaptureRequestTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\AfterPayCaptureResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...CaptureResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SprykerEco\Shared\AfterPay\AfterPayApiRequestConfig;
14
use SprykerEco\Shared\AfterPay\AfterPayConfig as SharedAfterPayConfig;
15
use SprykerEco\Zed\AfterPay\AfterPayConfig;
16
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface;
17
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface;
18
use SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException;
19
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface;
20
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface;
21
22
class CaptureCall extends AbstractApiCall implements CaptureCallInterface
23
{
24
    /**
25
     * @var \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface
26
     */
27
    protected $client;
28
29
    /**
30
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
31
     */
32
    protected $config;
33
34
    /**
35
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface
36
     */
37
    protected $money;
38
39
    /**
40
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface $client
41
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Converter\TransferToCamelCaseArrayConverterInterface $transferConverter
42
     * @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface $utilEncoding
43
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface $money
44
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
45
     */
46
    public function __construct(
47
        ClientInterface $client,
48
        TransferToCamelCaseArrayConverterInterface $transferConverter,
49
        AfterPayToUtilEncodingServiceInterface $utilEncoding,
50
        AfterPayToMoneyFacadeInterface $money,
51
        AfterPayConfig $config
52
    ) {
53
        $this->client = $client;
54
        $this->transferConverter = $transferConverter;
55
        $this->utilEncoding = $utilEncoding;
56
        $this->config = $config;
57
        $this->money = $money;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $requestTransfer
62
     *
63
     * @throws \SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException
64
     *
65
     * @return \Generated\Shared\Transfer\AfterPayCaptureResponseTransfer
66
     */
67
    public function execute(AfterPayCaptureRequestTransfer $requestTransfer): AfterPayCaptureResponseTransfer
68
    {
69
        $preparedRequestTransfer = $this->prepareRequestTransferToBuildJsonRequest($requestTransfer);
70
        $jsonRequest = $this->buildJsonRequestFromTransferObject($preparedRequestTransfer);
71
72
        try {
73
            $jsonResponse = $this->client->sendPost(
74
                $this->getCaptureEndpointUrl($requestTransfer),
75
                $jsonRequest
76
            );
77
        } catch (ApiHttpRequestException $apiHttpRequestException) {
78
            $this->logApiException($apiHttpRequestException);
79
            throw $apiHttpRequestException;
80
        }
81
82
        return $this->buildResponseTransfer($jsonResponse);
83
    }
84
85
    /**
86
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $requestTransfer
87
     *
88
     * @return string
89
     */
90
    protected function getCaptureEndpointUrl(AfterPayCaptureRequestTransfer $requestTransfer): string
91
    {
92
        return $this->config->getCaptureApiEndpointUrl(
93
            $requestTransfer->getOrderDetails()->getNumber()
94
        );
95
    }
96
97
    /**
98
     * @param string $jsonResponse
99
     *
100
     * @return \Generated\Shared\Transfer\AfterPayCaptureResponseTransfer
101
     */
102
    protected function buildResponseTransfer(string $jsonResponse): AfterPayCaptureResponseTransfer
103
    {
104
        $apiResponseTransfer = $this->buildApiResponseTransfer($jsonResponse);
105
        $captureResponseTransfer = $this->buildCaptureResponseTransfer($jsonResponse);
106
107
        $captureResponseTransfer->setApiResponse($apiResponseTransfer);
108
109
        return $captureResponseTransfer;
110
    }
111
112
    /**
113
     * @param string $jsonResponse
114
     *
115
     * @return \Generated\Shared\Transfer\AfterPayCaptureResponseTransfer
116
     */
117
    protected function buildCaptureResponseTransfer(string $jsonResponse): AfterPayCaptureResponseTransfer
118
    {
119
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
120
121
        $captureResponseTransfer = new AfterPayCaptureResponseTransfer();
122
123
        $captureResponseTransfer
124
            ->setCapturedAmount(
125
                $this->money->convertDecimalToInteger(
126
                    $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_CAPTURED_AMOUNT]
127
                )
128
            )
129
            ->setAuthorizedAmount(
130
                $this->money->convertDecimalToInteger(
131
                    $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_AUTHORIZED_AMOUNT]
132
                )
133
            )
134
            ->setRemainingAuthorizedAmount(
135
                $this->money->convertDecimalToInteger(
136
                    $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_REMAINING_AUTHORIZED_AMOUNT]
137
                )
138
            )
139
            ->setCaptureNumber(
140
                $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_CAPTURE_NUMBER]
141
            );
142
143
        return $captureResponseTransfer;
144
    }
145
146
    /**
147
     * @param string $jsonResponse
148
     *
149
     * @return \Generated\Shared\Transfer\AfterPayApiResponseTransfer
150
     */
151
    protected function buildApiResponseTransfer(string $jsonResponse): AfterPayApiResponseTransfer
152
    {
153
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
154
155
        $apiResponseTransfer = new AfterPayApiResponseTransfer();
156
157
        $outcome = $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_CAPTURE_NUMBER]
158
            ? SharedAfterPayConfig::API_TRANSACTION_OUTCOME_ACCEPTED
159
            : SharedAfterPayConfig::API_TRANSACTION_OUTCOME_REJECTED;
160
161
        $apiResponseTransfer
162
            ->setOutcome($outcome)
163
            ->setResponsePayload($jsonResponse);
164
165
        return $apiResponseTransfer;
166
    }
167
168
    /**
169
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $requestTransfer
170
     *
171
     * @return \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer
172
     */
173
    protected function prepareRequestTransferToBuildJsonRequest(AfterPayCaptureRequestTransfer $requestTransfer): AfterPayCaptureRequestTransfer
174
    {
175
        return (new AfterPayCaptureRequestTransfer())
176
            ->setOrderDetails($requestTransfer->getOrderDetails());
177
    }
178
}
179