TxnIdBoletoHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
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\ConfigBoleto;
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 Boleto Handler - Handles reading responses for Boleto payment.
20
 */
21
class TxnIdBoletoHandler implements HandlerInterface
22
{
23
    /**
24
     * Boleto Line Code - Payment Addtional Information.
25
     */
26
    public const PAYMENT_INFO_BOLETO_LINE_CODE = 'boleto_line_code';
27
28
    /**
29
     * Boleto PDF Href - Payment Addtional Information.
30
     */
31
    public const PAYMENT_INFO_BOLETO_PDF_HREF = 'boleto_pdf_href';
32
33
    /**
34
     * Boleto Expiration Date - Payment Addtional Information.
35
     */
36
    public const PAYMENT_INFO_BOLETO_EXPIRATION_DATE = 'boleto_expiration_date';
37
38
    /**
39
     * Boleto Our Number - Payment Addtional Information.
40
     */
41
    public const PAYMENT_INFO_BOLETO_OUR_NUMBER = 'boleto_our_number';
42
43
    /**
44
     * Boleto Document Number - Payment Addtional Information.
45
     */
46
    public const PAYMENT_INFO_BOLETO_DOCUMENT_NUMBER = 'boleto_document_number';
47
48
    /**
49
     * Response Pay Boleto - Block name.
50
     */
51
    public const RESPONSE_BOLETO = 'boleto';
52
53
    /**
54
     * Response Pay Boleto Id - Block name.
55
     */
56
    public const RESPONSE_BOLETO_ID = 'boleto_id';
57
58
    /**
59
     * Response Pay Boleto Typeful Line - Block name.
60
     */
61
    public const RESPONSE_BOLETO_TYPEFUL_LINE = 'typeful_line';
62
63
    /**
64
     * Response Pay Boleto Expiration Date - Block name.
65
     */
66
    public const RESPONSE_BOLETO_EXPIRATION_DATE = 'expiration_date';
67
68
    /**
69
     * Response Pay Boleto Our Number - Block name.
70
     */
71
    public const RESPONSE_BOLETO_OUR_NUMBER = 'our_number';
72
73
    /**
74
     * Response Pay Boleto Document Number - Block name.
75
     */
76
    public const RESPONSE_BOLETO_DOCUMENT_NUMBER = 'document_number';
77
78
    /**
79
     * Response Pay Boleto Links Number - Block name.
80
     */
81
    public const RESPONSE_BOLETO_LINKS = '_links';
82
83
    /**
84
     * Response Pay Boleto Links HREF - Block name.
85
     */
86
    public const RESPONSE_BOLETO_LINKS_HREF = 'href';
87
88
    /**
89
     * Response Pay Boleto Links Rel - Block name.
90
     */
91
    public const RESPONSE_BOLETO_LINKS_REL = 'rel';
92
93
    /**
94
     * Response Pay Boleto Links Rel PDF - Block name.
95
     */
96
    public const RESPONSE_BOLETO_LINKS_REL_PDF = 'boleto_pdf';
97
98
    /**
99
     * @var Config
100
     */
101
    private $config;
102
103
    /**
104
     * @var ConfigBoleto
105
     */
106
    private $configBoleto;
107
108
    /**
109
     * @param Config       $config
110
     * @param ConfigBoleto $configBoleto
111
     */
112
    public function __construct(
113
        Config $config,
114
        ConfigBoleto $configBoleto
115
    ) {
116
        $this->config = $config;
117
        $this->configBoleto = $configBoleto;
118
    }
119
120
    /**
121
     * Handles.
122
     *
123
     * @param array $handlingSubject
124
     * @param array $response
125
     *
126
     * @return void
127
     */
128
    public function handle(array $handlingSubject, array $response)
129
    {
130
        if (!isset($handlingSubject['payment'])
131
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
132
        ) {
133
            throw new InvalidArgumentException('Payment data object should be provided');
134
        }
135
136
        $paymentDO = $handlingSubject['payment'];
137
138
        $payment = $paymentDO->getPayment();
139
        $payBoleto = $response[self::RESPONSE_BOLETO];
140
141
        $payment->setAdditionalInformation(
142
            self::PAYMENT_INFO_BOLETO_LINE_CODE,
143
            $payBoleto[self::RESPONSE_BOLETO_TYPEFUL_LINE]
144
        );
145
146
        $payment->setAdditionalInformation(
147
            self::PAYMENT_INFO_BOLETO_EXPIRATION_DATE,
148
            $payBoleto[self::RESPONSE_BOLETO_EXPIRATION_DATE]
149
        );
150
151
        $payment->setAdditionalInformation(
152
            self::PAYMENT_INFO_BOLETO_OUR_NUMBER,
153
            $payBoleto[self::RESPONSE_BOLETO_DOCUMENT_NUMBER]
154
        );
155
156
        $payment->setAdditionalInformation(
157
            self::PAYMENT_INFO_BOLETO_DOCUMENT_NUMBER,
158
            $payBoleto[self::RESPONSE_BOLETO_OUR_NUMBER]
159
        );
160
161
        $links = $payBoleto[self::RESPONSE_BOLETO_LINKS];
162
        foreach ($links as $link) {
163
            if ($link[self::RESPONSE_BOLETO_LINKS_REL] === self::RESPONSE_BOLETO_LINKS_REL_PDF) {
164
                $relativeLinkToPDF = $link[self::RESPONSE_BOLETO_LINKS_HREF];
165
            }
166
        }
167
168
        $linkToPDF = $this->configBoleto->getFormattedLinkBoleto($relativeLinkToPDF);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $relativeLinkToPDF does not seem to be defined for all execution paths leading up to this point.
Loading history...
169
        $payment->setAdditionalInformation(
170
            self::PAYMENT_INFO_BOLETO_PDF_HREF,
171
            $linkToPDF
172
        );
173
        $transactionId = $payBoleto[self::RESPONSE_BOLETO_ID];
174
        $payment->setTransactionId($transactionId);
175
        $payment->setIsTransactionPending(1);
176
        $payment->setIsTransactionClosed(false);
177
        $payment->setAuthorizationTransaction($transactionId);
178
        $payment->addTransaction(Transaction::TYPE_AUTH);
179
180
        $order = $payment->getOrder();
181
        $order->setState(\Magento\Sales\Model\Order::STATE_NEW);
182
        $order->setStatus('pending');
183
        $comment = __('Awaiting payment of the boleto.');
184
        $order->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());
185
    }
186
}
187