Passed
Branch [email protected] (aae97d)
by Bruno
10:40
created

TxnIdWalletHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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\ConfigWallet;
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 Wallet Handler - Handles reading responses for Wallet payment.
20
 */
21
class TxnIdWalletHandler implements HandlerInterface
22
{
23
    /**
24
     * Boleto Qr Code - Payment Addtional Information.
25
     */
26
    public const PAYMENT_INFO_QR_CODE = 'qr_code';
27
28
    /**
29
     * Qr Code Image - Payment Addtional Information.
30
     */
31
    public const PAYMENT_INFO_QR_CODE_IMAGE = 'qr_code_image';
32
33
    /**
34
     * Response Pay Payment Id - Block name.
35
     */
36
    public const RESPONSE_PAYMENT_ID = 'payment_id';
37
38
    /**
39
     * @var Config
40
     */
41
    private $config;
42
43
    /**
44
     * @var ConfigWallet
45
     */
46
    private $configWallet;
47
48
    /**
49
     * @param Config       $config
50
     * @param ConfigWallet $configWallet
51
     */
52
    public function __construct(
53
        Config $config,
54
        ConfigWallet $configWallet
55
    ) {
56
        $this->config = $config;
57
        $this->configWallet = $configWallet;
58
    }
59
60
    /**
61
     * Handles.
62
     *
63
     * @param array $handlingSubject
64
     * @param array $response
65
     *
66
     * @return void
67
     */
68
    public function handle(array $handlingSubject, array $response)
69
    {
70
        if (!isset($handlingSubject['payment'])
71
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
72
        ) {
73
            throw new InvalidArgumentException('Payment data object should be provided');
74
        }
75
76
        $paymentDO = $handlingSubject['payment'];
77
78
        $payment = $paymentDO->getPayment();
79
80
        $qrCode = $response[self::PAYMENT_INFO_QR_CODE];
81
82
        $transactionId = $response[self::RESPONSE_PAYMENT_ID];
83
84
        $qrCodeImage = $this->configWallet->generateImageQrCode($qrCode, $transactionId);
85
86
        $payment->setAdditionalInformation(
87
            self::PAYMENT_INFO_QR_CODE_IMAGE,
88
            $qrCodeImage
89
        );
90
91
        $payment->setAdditionalInformation(
92
            self::PAYMENT_INFO_QR_CODE,
93
            $qrCode
94
        );
95
96
        $payment->setTransactionId($transactionId);
97
        $payment->setIsTransactionPending(1);
98
        $payment->setIsTransactionClosed(false);
99
        $payment->setAuthorizationTransaction($transactionId);
100
        $payment->addTransaction(Transaction::TYPE_AUTH);
101
102
        $order = $payment->getOrder();
103
        $order->setState(\Magento\Sales\Model\Order::STATE_NEW);
104
        $order->setStatus('pending');
105
        $comment = __('Awaiting payment of the Wallet.');
106
        $order->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());
107
    }
108
}
109