CancelCall::buildResponseTransfer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
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
80
            throw $apiHttpRequestException;
81
        }
82
83
        return $this->buildResponseTransfer($jsonResponse);
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\AfterPayCancelRequestTransfer $requestTransfer
88
     *
89
     * @return string
90
     */
91
    protected function getCancelEndpointUrl(AfterPayCancelRequestTransfer $requestTransfer): string
92
    {
93
        return $this->config->getCancelApiEndpointUrl(
94
            $requestTransfer->getCancellationDetails()->getNumber(),
95
        );
96
    }
97
98
    /**
99
     * @param string $jsonResponse
100
     *
101
     * @return \Generated\Shared\Transfer\AfterPayCancelResponseTransfer
102
     */
103
    protected function buildResponseTransfer(string $jsonResponse): AfterPayCancelResponseTransfer
104
    {
105
        $apiResponseTransfer = $this->buildApiResponseTransfer($jsonResponse);
106
        $cancelResponseTransfer = $this->buildCancelResponseTransfer($jsonResponse);
107
108
        $cancelResponseTransfer->setApiResponse($apiResponseTransfer);
109
110
        return $cancelResponseTransfer;
111
    }
112
113
    /**
114
     * @param string $jsonResponse
115
     *
116
     * @return \Generated\Shared\Transfer\AfterPayCancelResponseTransfer
117
     */
118
    protected function buildCancelResponseTransfer(string $jsonResponse): AfterPayCancelResponseTransfer
119
    {
120
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
121
122
        return (new AfterPayCancelResponseTransfer())
123
            ->setTotalCapturedAmount(
124
                $this->money->convertDecimalToInteger(
125
                    $jsonResponseArray[AfterPayApiRequestConfig::CANCEL_CAPTURED_AMOUNT],
126
                ),
127
            )
128
            ->setTotalAuthorizedAmount(
129
                $this->money->convertDecimalToInteger(
130
                    $jsonResponseArray[AfterPayApiRequestConfig::CANCEL_AUTHORIZED_AMOUNT],
131
                ),
132
            );
133
    }
134
135
    /**
136
     * @param string $jsonResponse
137
     *
138
     * @return \Generated\Shared\Transfer\AfterPayApiResponseTransfer
139
     */
140
    protected function buildApiResponseTransfer(string $jsonResponse): AfterPayApiResponseTransfer
141
    {
142
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
143
144
        $outcome = isset($jsonResponseArray[AfterPayApiRequestConfig::CANCEL_AUTHORIZED_AMOUNT])
145
            ? SharedAfterPayConfig::API_TRANSACTION_OUTCOME_ACCEPTED
146
            : SharedAfterPayConfig::API_TRANSACTION_OUTCOME_REJECTED;
147
148
        return (new AfterPayApiResponseTransfer())
149
            ->setOutcome($outcome)
150
            ->setResponsePayload($jsonResponse);
151
    }
152
153
    /**
154
     * @param \Generated\Shared\Transfer\AfterPayCancelRequestTransfer $requestTransfer
155
     *
156
     * @return \Generated\Shared\Transfer\AfterPayCancelRequestTransfer
157
     */
158
    protected function prepareRequestTransferToBuildJsonRequest(AfterPayCancelRequestTransfer $requestTransfer): AfterPayCancelRequestTransfer
159
    {
160
        return (new AfterPayCancelRequestTransfer())
161
            ->setCancellationDetails($requestTransfer->getCancellationDetails());
162
    }
163
}
164