RefundRequest::build()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 35
rs 9.2888
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Gateway\Request;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
13
use InvalidArgumentException;
14
use Magento\Payment\Gateway\ConfigInterface;
15
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
16
use Magento\Payment\Gateway\Request\BuilderInterface;
17
18
/**
19
 * Class Refund Request - Refund data structure.
20
 */
21
class RefundRequest implements BuilderInterface
22
{
23
    /**
24
     * External Payment Id - Block Name.
25
     */
26
    public const GETNET_PAYMENT_ID = 'payment_id';
27
28
    /**
29
     * Amount block name.
30
     */
31
    public const CANCEL_AMOUNT = 'cancel_amount';
32
33
    /**
34
     * @var ConfigInterface
35
     */
36
    protected $config;
37
38
    /**
39
     * @var Config
40
     */
41
    protected $configPayment;
42
43
    /**
44
     * @var ConfigCc
45
     */
46
    protected $configCc;
47
48
    /**
49
     * @param ConfigInterface $config
50
     * @param Config          $configPayment
51
     * @param ConfigCc        $configCc
52
     */
53
    public function __construct(
54
        ConfigInterface $config,
55
        Config $configPayment,
56
        ConfigCc $configCc
57
    ) {
58
        $this->config = $config;
59
        $this->configPayment = $configPayment;
60
        $this->configCc = $configCc;
61
    }
62
63
    /**
64
     * Build.
65
     *
66
     * @param array $buildSubject
67
     */
68
    public function build(array $buildSubject)
69
    {
70
        if (!isset($buildSubject['payment'])
71
            || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
72
        ) {
73
            throw new InvalidArgumentException('Payment data object should be provided');
74
        }
75
76
        $paymentDO = $buildSubject['payment'];
77
78
        $payment = $paymentDO->getPayment();
79
80
        $creditmemo = $payment->getCreditMemo();
81
82
        $totalCreditmemo = $creditmemo->getGrandTotal();
83
84
        $installment = $payment->getAdditionalInformation('cc_installments') ?: 1;
85
86
        $result = [
87
            self::GETNET_PAYMENT_ID => str_replace('-capture', '', $payment->getLastTransId()),
88
            self::CANCEL_AMOUNT     => $this->configPayment->formatPrice($totalCreditmemo),
89
        ];
90
91
        if ($installment > 1) {
92
            $order = $paymentDO->getOrder();
93
            $storeId = $order->getStoreId();
94
            $amountInterest = $this->configCc->getInterestToAmount($installment, $totalCreditmemo, $storeId);
95
            $total = $totalCreditmemo + $amountInterest;
96
            $result = [
97
                self::GETNET_PAYMENT_ID => str_replace('-capture', '', $payment->getLastTransId()),
98
                self::CANCEL_AMOUNT     => $this->configPayment->formatPrice($total),
99
            ];
100
        }
101
102
        return $result;
103
    }
104
}
105