TxnIdPixHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 56 3
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 Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigPix;
13
use InvalidArgumentException;
14
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
15
use Magento\Payment\Gateway\Response\HandlerInterface;
16
use Magento\Sales\Model\Order\Payment\Transaction;
17
18
/**
19
 * Txn Id Pix Handler - Handles reading responses for Boleto payment.
20
 */
21
class TxnIdPixHandler implements HandlerInterface
22
{
23
    /**
24
     * Boleto Qr Code - Payment Addtional Information.
25
     */
26
    public const PAYMENT_INFO_QR_CODE = 'qr_code';
27
28
    /**
29
     * Creation Date Qrcode - Payment Addtional Information.
30
     */
31
    public const PAYMENT_INFO_CREATION_DATE_QRCODE = 'creation_date_qrcode';
32
33
    /**
34
     * Expiration Date Qrcode - Payment Addtional Information.
35
     */
36
    public const PAYMENT_INFO_EXPIRATION_DATE_QRCODE = 'expiration_date_qrcode';
37
38
    /**
39
     * Psp Code - Payment Addtional Information.
40
     */
41
    public const PAYMENT_INFO_PSP_CODE = 'psp_code';
42
43
    /**
44
     * Qr Code Image - Payment Addtional Information.
45
     */
46
    public const PAYMENT_INFO_QR_CODE_IMAGE = 'qr_code_image';
47
48
    /**
49
     * Response Pay Payment Id - Block name.
50
     */
51
    public const RESPONSE_PAYMENT_ID = 'payment_id';
52
53
    /**
54
     * Response Pay Pix - Block name.
55
     */
56
    public const RESPONSE_PIX = 'additional_data';
57
58
    /**
59
     * @var Config
60
     */
61
    private $config;
62
63
    /**
64
     * @var ConfigPix
65
     */
66
    private $configPix;
67
68
    /**
69
     * @param Config    $config
70
     * @param ConfigPix $configPix
71
     */
72
    public function __construct(
73
        Config $config,
74
        ConfigPix $configPix
75
    ) {
76
        $this->config = $config;
77
        $this->configPix = $configPix;
78
    }
79
80
    /**
81
     * Handles.
82
     *
83
     * @param array $handlingSubject
84
     * @param array $response
85
     *
86
     * @return void
87
     */
88
    public function handle(array $handlingSubject, array $response)
89
    {
90
        if (!isset($handlingSubject['payment'])
91
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
92
        ) {
93
            throw new InvalidArgumentException('Payment data object should be provided');
94
        }
95
96
        $paymentDO = $handlingSubject['payment'];
97
98
        $payment = $paymentDO->getPayment();
99
100
        $payPix = $response[self::RESPONSE_PIX];
101
102
        $qrCode = $payPix[self::PAYMENT_INFO_QR_CODE];
103
104
        $transactionId = $response[self::RESPONSE_PAYMENT_ID];
105
106
        $qrCodeImage = $this->configPix->generateImageQrCode($qrCode, $transactionId);
107
108
        $payment->setAdditionalInformation(
109
            self::PAYMENT_INFO_QR_CODE_IMAGE,
110
            $qrCodeImage
111
        );
112
113
        $payment->setAdditionalInformation(
114
            self::PAYMENT_INFO_QR_CODE,
115
            $qrCode
116
        );
117
118
        $payment->setAdditionalInformation(
119
            self::PAYMENT_INFO_CREATION_DATE_QRCODE,
120
            $payPix[self::PAYMENT_INFO_CREATION_DATE_QRCODE]
121
        );
122
123
        $payment->setAdditionalInformation(
124
            self::PAYMENT_INFO_EXPIRATION_DATE_QRCODE,
125
            $payPix[self::PAYMENT_INFO_EXPIRATION_DATE_QRCODE]
126
        );
127
128
        $payment->setAdditionalInformation(
129
            self::PAYMENT_INFO_PSP_CODE,
130
            $payPix[self::PAYMENT_INFO_PSP_CODE]
131
        );
132
133
        $payment->setTransactionId($transactionId);
134
        $payment->setIsTransactionPending(1);
135
        $payment->setIsTransactionClosed(false);
136
        $payment->setAuthorizationTransaction($transactionId);
137
        $payment->addTransaction(Transaction::TYPE_AUTH);
138
139
        $order = $payment->getOrder();
140
        $order->setState(\Magento\Sales\Model\Order::STATE_NEW);
141
        $order->setStatus('pending');
142
        $comment = __('Awaiting payment of the Pix.');
143
        $order->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());
144
    }
145
}
146