|
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\Request; |
|
10
|
|
|
|
|
11
|
|
|
use Getnet\PaymentMagento\Gateway\Config\Config; |
|
12
|
|
|
use Getnet\PaymentMagento\Gateway\Data\Order\OrderAdapterFactory; |
|
|
|
|
|
|
13
|
|
|
use Getnet\PaymentMagento\Gateway\SubjectReader; |
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; |
|
16
|
|
|
use Magento\Payment\Gateway\Request\BuilderInterface; |
|
17
|
|
|
use Magento\Payment\Model\InfoInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Tax Document Data Request - Fiscal document data structure. |
|
21
|
|
|
*/ |
|
22
|
|
|
class TaxDocumentDataRequest implements BuilderInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* BillingAddress block name. |
|
26
|
|
|
*/ |
|
27
|
|
|
public const TAX_DOCUMENT = 'document_type'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The street number. 1 or 10 alphanumeric digits |
|
31
|
|
|
* Required. |
|
32
|
|
|
*/ |
|
33
|
|
|
public const TAX_DOCUMENT_NUMBER = 'document_number'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var SubjectReader |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $subjectReader; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var OrderAdapterFactory |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $orderAdapterFactory; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Config |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $config; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param SubjectReader $subjectReader |
|
52
|
|
|
* @param OrderAdapterFactory $orderAdapterFactory |
|
53
|
|
|
* @param Config $config |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
SubjectReader $subjectReader, |
|
57
|
|
|
OrderAdapterFactory $orderAdapterFactory, |
|
58
|
|
|
Config $config |
|
59
|
|
|
) { |
|
60
|
|
|
$this->subjectReader = $subjectReader; |
|
61
|
|
|
$this->orderAdapterFactory = $orderAdapterFactory; |
|
62
|
|
|
$this->config = $config; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get Value For Tax Document. |
|
67
|
|
|
* |
|
68
|
|
|
* @param OrderAdapterFactory $orderAdapter |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getValueForTaxDocument($orderAdapter) |
|
73
|
|
|
{ |
|
74
|
|
|
$obtainTaxDocFrom = $this->config->getAddtionalValue('get_tax_document_from'); |
|
75
|
|
|
|
|
76
|
|
|
$taxDocument = $orderAdapter->getCustomerTaxvat(); |
|
77
|
|
|
|
|
78
|
|
|
if ($obtainTaxDocFrom === 'address') { |
|
79
|
|
|
$taxDocument = $orderAdapter->getBillingAddress()->getVatId(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $taxDocument; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Build. |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $buildSubject |
|
89
|
|
|
*/ |
|
90
|
|
|
public function build(array $buildSubject) |
|
91
|
|
|
{ |
|
92
|
|
|
if (!isset($buildSubject['payment']) |
|
93
|
|
|
|| !$buildSubject['payment'] instanceof PaymentDataObjectInterface |
|
94
|
|
|
) { |
|
95
|
|
|
throw new InvalidArgumentException('Payment data object should be provided'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$paymentDO = $this->subjectReader->readPayment($buildSubject); |
|
99
|
|
|
$payment = $paymentDO->getPayment(); |
|
100
|
|
|
$typeDocument = 'CPF'; |
|
101
|
|
|
$result = []; |
|
102
|
|
|
|
|
103
|
|
|
/** @var OrderAdapterFactory $orderAdapter * */ |
|
104
|
|
|
$orderAdapter = $this->orderAdapterFactory->create( |
|
105
|
|
|
['order' => $payment->getOrder()] |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$taxDocument = $this->getFiscalNumber($payment, $orderAdapter); |
|
109
|
|
|
$taxDocument = preg_replace('/[^0-9]/', '', $taxDocument); |
|
110
|
|
|
|
|
111
|
|
|
if (strlen($taxDocument) === 14) { |
|
112
|
|
|
$typeDocument = 'CNPJ'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ($typeDocument) { |
|
116
|
|
|
$result[CustomerDataRequest::CUSTOMER] = [ |
|
117
|
|
|
self::TAX_DOCUMENT => $typeDocument, |
|
118
|
|
|
self::TAX_DOCUMENT_NUMBER => $taxDocument, |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $result; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get Fiscal Number. |
|
127
|
|
|
* |
|
128
|
|
|
* @param InfoInterface $payment |
|
129
|
|
|
* @param OrderAdapterFactory $orderAdapter |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getFiscalNumber($payment, $orderAdapter): ?string |
|
134
|
|
|
{ |
|
135
|
|
|
$taxDocument = null; |
|
136
|
|
|
|
|
137
|
|
|
if ($payment->getAdditionalInformation('cc_holder_tax_document')) { |
|
138
|
|
|
$taxDocument = $payment->getAdditionalInformation('cc_holder_tax_document'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($payment->getAdditionalInformation('boleto_payer_tax_document')) { |
|
142
|
|
|
$taxDocument = $payment->getAdditionalInformation('boleto_payer_tax_document'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (!$taxDocument) { |
|
146
|
|
|
$taxDocument = $this->getValueForTaxDocument($orderAdapter); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $taxDocument; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths