DenyPaymentHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 56
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 21 4
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
15
/**
16
 * Deny Payment Handler - Set the flow when denying a payment.
17
 */
18
class DenyPaymentHandler implements HandlerInterface
19
{
20
    /**
21
     * Result Code - Block name.
22
     */
23
    public const RESULT_CODE = 'RESULT_CODE';
24
25
    /**
26
     * Response Pay Cancel Request Id - Block Name.
27
     */
28
    public const RESPONSE_CANCEL_REQUEST_ID = 'cancel_request_id';
29
30
    /**
31
     * Response Pay Status - Block Name.
32
     */
33
    public const RESPONSE_STATUS = 'status';
34
35
    /**
36
     * Response Pay Status Approved - Value.
37
     */
38
    public const RESPONSE_STATUS_ACCEPTED = 'ACCEPTED';
39
40
    /**
41
     * Response Pay Status Denied - Value.
42
     */
43
    public const RESPONSE_STATUS_DENIED = 'DENIED';
44
45
    /**
46
     * Handles.
47
     *
48
     * @param array $handlingSubject
49
     * @param array $response
50
     *
51
     * @return void
52
     */
53
    public function handle(array $handlingSubject, array $response)
54
    {
55
        if (!isset($handlingSubject['payment'])
56
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
57
        ) {
58
            throw new InvalidArgumentException('Payment data object should be provided');
59
        }
60
61
        if ($response[self::RESULT_CODE]) {
62
            $paymentDO = $handlingSubject['payment'];
63
            $payment = $paymentDO->getPayment();
64
65
            $order = $payment->getOrder();
66
            $amount = $order->getBaseGrandTotal();
67
68
            $payment->setPreparedMessage(__('Order Canceled.'));
69
            $payment->setIsTransactionPending(false);
70
            $payment->setIsTransactionDenied(true);
71
            $payment->setAmountCanceled($amount);
72
            $payment->setBaseAmountCanceled($amount);
73
            $payment->setShouldCloseParentTransaction(true);
74
        }
75
    }
76
}
77