RefundCall   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 139
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildResponseTransfer() 0 8 1
A __construct() 0 12 1
A buildRefundResponseTransfer() 0 13 1
A getRefundEndpointUrl() 0 3 1
A buildApiResponseTransfer() 0 11 2
A execute() 0 17 2
A prepareRequestTransferToBuildJsonRequest() 0 5 1
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
80
            throw $apiHttpRequestException;
81
        }
82
83
        return $this->buildResponseTransfer($jsonResponse);
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\AfterPayRefundRequestTransfer $requestTransfer
88
     *
89
     * @return string
90
     */
91
    protected function getRefundEndpointUrl(AfterPayRefundRequestTransfer $requestTransfer): string
92
    {
93
        return $this->config->getRefundApiEndpointUrl($requestTransfer->getOrderNumber());
94
    }
95
96
    /**
97
     * @param string $jsonResponse
98
     *
99
     * @return \Generated\Shared\Transfer\AfterPayRefundResponseTransfer
100
     */
101
    protected function buildResponseTransfer(string $jsonResponse): AfterPayRefundResponseTransfer
102
    {
103
        $apiResponseTransfer = $this->buildApiResponseTransfer($jsonResponse);
104
        $refundResponseTransfer = $this->buildRefundResponseTransfer($jsonResponse);
105
106
        $refundResponseTransfer->setApiResponse($apiResponseTransfer);
107
108
        return $refundResponseTransfer;
109
    }
110
111
    /**
112
     * @param string $jsonResponse
113
     *
114
     * @return \Generated\Shared\Transfer\AfterPayRefundResponseTransfer
115
     */
116
    protected function buildRefundResponseTransfer(string $jsonResponse): AfterPayRefundResponseTransfer
117
    {
118
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
119
120
        return (new AfterPayRefundResponseTransfer())
121
            ->setTotalCapturedAmount(
122
                $this->money->convertDecimalToInteger(
123
                    $jsonResponseArray[AfterPayApiRequestConfig::REFUND_TOTAL_CAPTURED_AMOUNT],
124
                ),
125
            )
126
            ->setTotalAuthorizedAmount(
127
                $this->money->convertDecimalToInteger(
128
                    $jsonResponseArray[AfterPayApiRequestConfig::REFUND_TOTAL_AUTHORIZE_AMOUNT],
129
                ),
130
            );
131
    }
132
133
    /**
134
     * @param string $jsonResponse
135
     *
136
     * @return \Generated\Shared\Transfer\AfterPayApiResponseTransfer
137
     */
138
    protected function buildApiResponseTransfer(string $jsonResponse): AfterPayApiResponseTransfer
139
    {
140
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
141
142
        $outcome = $jsonResponseArray[AfterPayApiRequestConfig::REFUND_TOTAL_CAPTURED_AMOUNT]
143
            ? SharedAfterPayConfig::API_TRANSACTION_OUTCOME_ACCEPTED
144
            : SharedAfterPayConfig::API_TRANSACTION_OUTCOME_REJECTED;
145
146
        return (new AfterPayApiResponseTransfer())
147
            ->setOutcome($outcome)
148
            ->setResponsePayload($jsonResponse);
149
    }
150
151
    /**
152
     * @param \Generated\Shared\Transfer\AfterPayRefundRequestTransfer $requestTransfer
153
     *
154
     * @return \Generated\Shared\Transfer\AfterPayRefundRequestTransfer
155
     */
156
    protected function prepareRequestTransferToBuildJsonRequest(AfterPayRefundRequestTransfer $requestTransfer): AfterPayRefundRequestTransfer
157
    {
158
        return (new AfterPayRefundRequestTransfer())
159
            ->setCaptureNumber($requestTransfer->getCaptureNumber())
160
            ->setOrderItems($requestTransfer->getOrderItems());
161
    }
162
}
163