CaptureCall::buildCaptureResponseTransfer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 22
rs 9.8333
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
80
            throw $apiHttpRequestException;
81
        }
82
83
        return $this->buildResponseTransfer($jsonResponse);
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $requestTransfer
88
     *
89
     * @return string
90
     */
91
    protected function getCaptureEndpointUrl(AfterPayCaptureRequestTransfer $requestTransfer): string
92
    {
93
        return $this->config->getCaptureApiEndpointUrl(
94
            $requestTransfer->getOrderDetails()->getNumber(),
95
        );
96
    }
97
98
    /**
99
     * @param string $jsonResponse
100
     *
101
     * @return \Generated\Shared\Transfer\AfterPayCaptureResponseTransfer
102
     */
103
    protected function buildResponseTransfer(string $jsonResponse): AfterPayCaptureResponseTransfer
104
    {
105
        $apiResponseTransfer = $this->buildApiResponseTransfer($jsonResponse);
106
        $captureResponseTransfer = $this->buildCaptureResponseTransfer($jsonResponse);
107
108
        $captureResponseTransfer->setApiResponse($apiResponseTransfer);
109
110
        return $captureResponseTransfer;
111
    }
112
113
    /**
114
     * @param string $jsonResponse
115
     *
116
     * @return \Generated\Shared\Transfer\AfterPayCaptureResponseTransfer
117
     */
118
    protected function buildCaptureResponseTransfer(string $jsonResponse): AfterPayCaptureResponseTransfer
119
    {
120
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
121
122
        return (new AfterPayCaptureResponseTransfer())
123
            ->setCapturedAmount(
124
                $this->money->convertDecimalToInteger(
125
                    $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_CAPTURED_AMOUNT],
126
                ),
127
            )
128
            ->setAuthorizedAmount(
129
                $this->money->convertDecimalToInteger(
130
                    $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_AUTHORIZED_AMOUNT],
131
                ),
132
            )
133
            ->setRemainingAuthorizedAmount(
134
                $this->money->convertDecimalToInteger(
135
                    $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_REMAINING_AUTHORIZED_AMOUNT],
136
                ),
137
            )
138
            ->setCaptureNumber(
139
                $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_CAPTURE_NUMBER],
140
            );
141
    }
142
143
    /**
144
     * @param string $jsonResponse
145
     *
146
     * @return \Generated\Shared\Transfer\AfterPayApiResponseTransfer
147
     */
148
    protected function buildApiResponseTransfer(string $jsonResponse): AfterPayApiResponseTransfer
149
    {
150
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
151
152
        $outcome = $jsonResponseArray[AfterPayApiRequestConfig::CAPTURE_CAPTURE_NUMBER]
153
            ? SharedAfterPayConfig::API_TRANSACTION_OUTCOME_ACCEPTED
154
            : SharedAfterPayConfig::API_TRANSACTION_OUTCOME_REJECTED;
155
156
        return (new AfterPayApiResponseTransfer())
157
            ->setOutcome($outcome)
158
            ->setResponsePayload($jsonResponse);
159
    }
160
161
    /**
162
     * @param \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer $requestTransfer
163
     *
164
     * @return \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer
165
     */
166
    protected function prepareRequestTransferToBuildJsonRequest(AfterPayCaptureRequestTransfer $requestTransfer): AfterPayCaptureRequestTransfer
167
    {
168
        return (new AfterPayCaptureRequestTransfer())
169
            ->setOrderDetails($requestTransfer->getOrderDetails());
170
    }
171
}
172