Passed
Branch [email protected] (3810d0)
by Bruno
11:09
created

TwoCcRefundHandler::handle()   B

Complexity

Conditions 9
Paths 41

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
c 1
b 0
f 0
nc 41
nop 2
dl 0
loc 40
rs 8.0555
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\Response;
10
11
use InvalidArgumentException;
12
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
13
use Magento\Payment\Gateway\Response\HandlerInterface;
14
use Magento\Sales\Model\Order\Creditmemo;
15
16
/**
17
 * Class Two Cc Refund Handler - Defines the refund of an order.
18
 */
19
class TwoCcRefundHandler implements HandlerInterface
20
{
21
    /**
22
     * Result Code - Block name.
23
     */
24
    public const RESULT_CODE = 'RESULT_CODE';
25
26
    /**
27
     * Payments - Block name.
28
     */
29
    public const PAYMENTS = 'payments';
30
31
    /**
32
     * Response Pay Cancel Request Id - Block Name.
33
     */
34
    public const RESPONSE_COMBINED_ID = 'combined_id';
35
36
    /**
37
     * Response Pay Status - Block Name.
38
     */
39
    public const RESPONSE_STATUS = 'status';
40
41
    /**
42
     * Response Pay Status Approved - Value.
43
     */
44
    public const RESPONSE_STATUS_ACCEPTED = 'CANCELED';
45
46
    /**
47
     * Response Pay Status Denied - Value.
48
     */
49
    public const RESPONSE_STATUS_DENIED = 'ERROR';
50
51
    /**
52
     * Handles.
53
     *
54
     * @param array $handlingSubject
55
     * @param array $response
56
     *
57
     * @return void
58
     */
59
    public function handle(array $handlingSubject, array $response)
60
    {
61
        $isAccept = false;
62
        $isDenied = false;
63
64
        if (!isset($handlingSubject['payment'])
65
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
66
        ) {
67
            throw new InvalidArgumentException('Payment data object should be provided');
68
        }
69
70
        $paymentDO = $handlingSubject['payment'];
71
        $payment = $paymentDO->getPayment();
72
73
        foreach ($response[self::PAYMENTS] as $paymentGetnet) {
74
            if ($paymentGetnet[self::RESPONSE_STATUS] === self::RESPONSE_STATUS_ACCEPTED) {
75
                $isAccept = true;
76
                $isDenied = false;
77
            }
78
79
            if ($paymentGetnet[self::RESPONSE_STATUS] === self::RESPONSE_STATUS_DENIED) {
80
                $isAccept = false;
81
                $isDenied = true;
82
            }
83
        }
84
85
        $payment->setTransactionId($response[self::RESPONSE_COMBINED_ID]);
86
87
        if ($isAccept) {
88
            $creditmemo = $payment->getCreditmemo();
89
            $creditmemo->setState(Creditmemo::STATE_REFUNDED);
90
        }
91
92
        if ($isDenied) {
93
            $creditmemo = $payment->getCreditmemo();
94
            $creditmemo->setState(Creditmemo::STATE_CANCELED);
95
        }
96
97
        if ($response[self::RESULT_CODE]) {
98
            $paymentDO->getPayment();
99
        }
100
    }
101
}
102