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

prepareRequestTransferToBuildJsonRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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