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\AfterPayCancelRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ayCancelRequestTransfer 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\AfterPayCancelResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yCancelResponseTransfer 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 CancelCall extends AbstractApiCall implements CancelCallInterface
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\AfterPayCancelRequestTransfer $requestTransfer
62
     *
63
     * @throws \SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException
64
     *
65
     * @return \Generated\Shared\Transfer\AfterPayCancelResponseTransfer
66
     */
67
    public function execute(AfterPayCancelRequestTransfer $requestTransfer): AfterPayCancelResponseTransfer
68
    {
69
        $preparedRequestTransfer = $this->prepareRequestTransferToBuildJsonRequest($requestTransfer);
70
        $jsonRequest = $this->buildJsonRequestFromTransferObject($preparedRequestTransfer);
71
72
        try {
73
            $jsonResponse = $this->client->sendPost(
74
                $this->getCancelEndpointUrl($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\AfterPayCancelRequestTransfer $requestTransfer
87
     *
88
     * @return string
89
     */
90
    protected function getCancelEndpointUrl(AfterPayCancelRequestTransfer $requestTransfer): string
91
    {
92
        return $this->config->getCancelApiEndpointUrl(
93
            $requestTransfer->getCancellationDetails()->getNumber()
94
        );
95
    }
96
97
    /**
98
     * @param string $jsonResponse
99
     *
100
     * @return \Generated\Shared\Transfer\AfterPayCancelResponseTransfer
101
     */
102
    protected function buildResponseTransfer(string $jsonResponse): AfterPayCancelResponseTransfer
103
    {
104
        $apiResponseTransfer = $this->buildApiResponseTransfer($jsonResponse);
105
        $cancelResponseTransfer = $this->buildCancelResponseTransfer($jsonResponse);
106
107
        $cancelResponseTransfer->setApiResponse($apiResponseTransfer);
108
109
        return $cancelResponseTransfer;
110
    }
111
112
    /**
113
     * @param string $jsonResponse
114
     *
115
     * @return \Generated\Shared\Transfer\AfterPayCancelResponseTransfer
116
     */
117
    protected function buildCancelResponseTransfer(string $jsonResponse): AfterPayCancelResponseTransfer
118
    {
119
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
120
121
        $cancelResponseTransfer = new AfterPayCancelResponseTransfer();
122
123
        $cancelResponseTransfer
124
            ->setTotalCapturedAmount(
125
                $this->money->convertDecimalToInteger(
126
                    $jsonResponseArray[AfterPayApiRequestConfig::CANCEL_CAPTURED_AMOUNT]
127
                )
128
            )
129
            ->setTotalAuthorizedAmount(
130
                $this->money->convertDecimalToInteger(
131
                    $jsonResponseArray[AfterPayApiRequestConfig::CANCEL_AUTHORIZED_AMOUNT]
132
                )
133
            );
134
135
        return $cancelResponseTransfer;
136
    }
137
138
    /**
139
     * @param string $jsonResponse
140
     *
141
     * @return \Generated\Shared\Transfer\AfterPayApiResponseTransfer
142
     */
143
    protected function buildApiResponseTransfer(string $jsonResponse): AfterPayApiResponseTransfer
144
    {
145
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
146
147
        $apiResponseTransfer = new AfterPayApiResponseTransfer();
148
149
        $outcome = isset($jsonResponseArray[AfterPayApiRequestConfig::CANCEL_AUTHORIZED_AMOUNT])
150
            ? SharedAfterPayConfig::API_TRANSACTION_OUTCOME_ACCEPTED
151
            : SharedAfterPayConfig::API_TRANSACTION_OUTCOME_REJECTED;
152
153
        $apiResponseTransfer
154
            ->setOutcome($outcome)
155
            ->setResponsePayload($jsonResponse);
156
157
        return $apiResponseTransfer;
158
    }
159
160
    /**
161
     * @param \Generated\Shared\Transfer\AfterPayCancelRequestTransfer $requestTransfer
162
     *
163
     * @return \Generated\Shared\Transfer\AfterPayCancelRequestTransfer
164
     */
165
    protected function prepareRequestTransferToBuildJsonRequest(AfterPayCancelRequestTransfer $requestTransfer): AfterPayCancelRequestTransfer
166
    {
167
        return (new AfterPayCancelRequestTransfer())
168
            ->setCancellationDetails($requestTransfer->getCancellationDetails());
169
    }
170
}
171