Completed
Pull Request — feature/eco-2295/master (#7)
by Aleksey
15:33 queued 04:30
created

RefundOmsCommandRequestBuilder::getCaptureId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 19
rs 9.9
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\CrefoPay\Business\Oms\Command\Builder;
9
10
use Generated\Shared\Transfer\CrefoPayApiAmountTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayApiAmountTransfer 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\CrefoPayApiRefundRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...piRefundRequestTransfer 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\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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 Generated\Shared\Transfer\CrefoPayOmsCommandTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayOmsCommandTransfer 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...
14
use Generated\Shared\Transfer\PaymentCrefoPayOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayOrderItemTransfer 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...
15
use SprykerEco\Zed\CrefoPay\Business\Exception\InvalidItemsToRefundAggregationException;
16
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
17
18
class RefundOmsCommandRequestBuilder implements CrefoPayOmsCommandRequestBuilderInterface
19
{
20
    protected const INVALID_ITEMS_AGGREGATION_MESSAGE = 'Order items to refund have to have same captureId.';
21
22
    /**
23
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
24
     */
25
    protected $config;
26
27
    /**
28
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
29
     */
30
    public function __construct(CrefoPayConfig $config)
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
37
     *
38
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
39
     */
40
    public function buildRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
41
    {
42
        $refundRequest = $this->createRefundRequestTransfer($crefoPayOmsCommandTransfer);
43
        $refundRequest
44
            ->setCaptureID($this->getCaptureId($crefoPayOmsCommandTransfer))
45
            ->setAmount(
46
                $this->createAmountTransfer($crefoPayOmsCommandTransfer->getRefund()->getAmount())
47
            );
48
49
        $requestTransfer = (new CrefoPayApiRequestTransfer())
50
            ->setRefundRequest($refundRequest);
51
52
        return $crefoPayOmsCommandTransfer
53
            ->setRequest($requestTransfer);
54
    }
55
56
    /**
57
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
58
     *
59
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
60
     */
61
    protected function addExpensesRequest(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
62
    {
63
        if (!$this->config->getRefundExpensesWithLastItem() || !$this->isLastRefund($crefoPayOmsCommandTransfer)) {
64
            return $crefoPayOmsCommandTransfer;
65
        }
66
67
        $refundExpenseRequestTransfer = $this->createRefundRequestTransfer($crefoPayOmsCommandTransfer);
68
        $refundExpenseRequestTransfer
69
            ->setCaptureID($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getExpensesCaptureId())
70
            ->setAmount(
71
                $this->createAmountTransfer($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getExpensesCapturedAmount())
72
            );
73
74
        $expensesRequestTransfer = (new CrefoPayApiRequestTransfer())
75
            ->setRefundRequest($refundExpenseRequestTransfer);
76
77
        return $crefoPayOmsCommandTransfer
78
            ->setExpensesRequest($expensesRequestTransfer);
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
83
     *
84
     * @return \Generated\Shared\Transfer\CrefoPayApiRefundRequestTransfer
85
     */
86
    protected function createRefundRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayApiRefundRequestTransfer
87
    {
88
        return (new CrefoPayApiRefundRequestTransfer())
89
            ->setMerchantID($this->config->getMerchantId())
90
            ->setStoreID($this->config->getStoreId())
91
            ->setOrderID($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCrefoPayOrderId())
92
            ->setRefundDescription($this->getRefundDescription($crefoPayOmsCommandTransfer));
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
97
     *
98
     * @throws \SprykerEco\Zed\CrefoPay\Business\Exception\InvalidItemsToRefundAggregationException
99
     *
100
     * @return string
101
     */
102
    protected function getCaptureId(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): string
103
    {
104
        $orderItemCaptureIds = array_unique(
105
            array_map(
106
                function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) {
107
                    return $paymentCrefoPayOrderItemTransfer->getCaptureId();
108
                },
109
                $crefoPayOmsCommandTransfer
110
                    ->getPaymentCrefoPayOrderItemCollection()
111
                    ->getCrefoPayOrderItems()
112
                    ->getArrayCopy()
113
            )
114
        );
115
116
        if (count($orderItemCaptureIds) !== 1) {
117
            throw new InvalidItemsToRefundAggregationException(static::INVALID_ITEMS_AGGREGATION_MESSAGE);
118
        }
119
120
        return reset($orderItemCaptureIds);
121
    }
122
123
    /**
124
     * @param int $amount
125
     *
126
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
127
     */
128
    protected function createAmountTransfer(int $amount): CrefoPayApiAmountTransfer
129
    {
130
        return (new CrefoPayApiAmountTransfer())
131
            ->setAmount($amount);
132
    }
133
134
    /**
135
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
136
     *
137
     * @return string|null
138
     */
139
    protected function getRefundDescription(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): ?string
140
    {
141
        return $crefoPayOmsCommandTransfer->getRefund()->getComment() ?? $this->config->getRefundDescription();
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
146
     *
147
     * @return bool
148
     */
149
    protected function isLastRefund(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): bool
150
    {
151
        $refundedAmount = $crefoPayOmsCommandTransfer
152
            ->getPaymentCrefoPay()
153
            ->getRefundedAmount();
154
155
        $itemsAmountToRefund = $crefoPayOmsCommandTransfer
156
            ->getRequest()
157
            ->getRefundRequest()
158
            ->getAmount()
159
            ->getAmount();
160
161
        $expensesAmountToRefund = $crefoPayOmsCommandTransfer
162
            ->getPaymentCrefoPay()
163
            ->getExpensesCapturedAmount();
164
165
        return $refundedAmount + $itemsAmountToRefund + $expensesAmountToRefund === $crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCapturedAmount();
166
    }
167
}
168