-
Notifications
You must be signed in to change notification settings - Fork 92
/
update.php
189 lines (178 loc) · 8.49 KB
/
update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
* PagBank
*
* Módulo Oficial para Integração com o PagBank via API v.4
* Pagamento com Pix, Boleto e Cartão de Crédito
* Checkout Transparente para PrestaShop 1.6.x, 1.7.x e 8.x
*
* @author
* 2011-2024 PrestaBR - https://prestabr.com.br
*
* @copyright
* 1996-2024 PagBank - https://pagseguro.uol.com.br
*
* @license
* Open Software License 3.0 (OSL 3.0) - https://opensource.org/license/osl-3-0-php/
*
*/
$ssl = true;
$useSSL = true;
include(dirname(__FILE__) . '/../../config/config.inc.php');
include(dirname(__FILE__) . '/../../init.php');
include_once(dirname(__FILE__) . "/pagbank.php");
$pagbank = new PagBank();
$context = Context::getContext();
$id_lang = $context->language->id;
$cart = $context->cart;
$customer = $context->customer;
$action = Tools::getValue("action");
$token = Tools::getValue("token");
$token_valid = false;
if (isset($token) && $token == hash('md5', _COOKIE_IV_ . Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'modules/' . $pagbank->name)) {
$token_valid = true;
}
if (Tools::getIsset('state')) {
$state = urldecode(Tools::getValue('state'));
$state_array = explode('/', $state);
$action = $state_array[2];
}
if (empty($action)) {
exit("Ação não definida");
}
if ($action == "processCard") {
$form_data = array(
'payment_type' => 'credit_card',
'ps_card_installment_value' => Tools::getValue('ps_card_installment_value'),
'ps_card_installments' => Tools::getValue('ps_card_installments'),
'ps_max_installments' => Tools::getValue('ps_max_installments'),
'ps_installments_min_value' => Tools::getValue('ps_installments_min_value'),
'ps_installments_min_type' => Tools::getValue('ps_installments_min_type'),
'get_installments_fees' => Tools::getValue('get_installments_fees'),
'ps_save_customer_card' => Tools::getValue('ps_save_customer_card'),
'saved_card' => Tools::getValue('saved_card'),
'card_name' => Tools::getValue('card_name'),
'card_bin' => Tools::getValue('card_bin'),
'card_brand' => Tools::getValue('ps_card_brand'),
'encryptedCard' => Tools::getValue('encryptedCard'),
'cardTokenId' => Tools::getValue('ps_card_token_id'),
'card_installment_qty' => Tools::getValue('card_installment_qty'),
'cpf_cnpj' => Tools::getValue('cpf_cnpj'),
'telephone' => Tools::getValue('telephone'),
'invoice_postcode' => Tools::getValue('ps_postcode_invoice'),
'invoice_address' => Tools::getValue('ps_address_invoice'),
'invoice_number' => Tools::getValue('ps_number_invoice'),
'invoice_complement' => Tools::getValue('ps_other_invoice'),
'invoice_district' => Tools::getValue('ps_address2_invoice'),
'invoice_city' => Tools::getValue('ps_city_invoice'),
'invoice_state' => Tools::getValue('ps_state_invoice'),
);
$api_response = $pagbank->processCardPayment($form_data);
} elseif ($action == "processBankSlip") {
$form_data = array(
'payment_type' => 'bankslip',
'bankslip_name' => Tools::getValue('bankslip_name'),
'cpf_cnpj' => Tools::getValue('cpf_cnpj'),
'telephone' => Tools::getValue('telephone'),
'invoice_postcode' => Tools::getValue('ps_postcode_invoice'),
'invoice_address' => Tools::getValue('ps_address_invoice'),
'invoice_number' => Tools::getValue('ps_number_invoice'),
'invoice_complement' => Tools::getValue('ps_other_invoice'),
'invoice_district' => Tools::getValue('ps_address2_invoice'),
'invoice_city' => Tools::getValue('ps_city_invoice'),
'invoice_state' => Tools::getValue('ps_state_invoice'),
);
$api_response = $pagbank->processBankSlipPayment($form_data);
} elseif ($action == "processPix") {
$form_data = array(
'payment_type' => 'pix',
'pix_name' => Tools::getValue('pix_name'),
'cpf_cnpj' => Tools::getValue('cpf_cnpj'),
'telephone' => Tools::getValue('telephone'),
'invoice_postcode' => Tools::getValue('ps_postcode_invoice'),
'invoice_address' => Tools::getValue('ps_address_invoice'),
'invoice_number' => Tools::getValue('ps_number_invoice'),
'invoice_complement' => Tools::getValue('ps_other_invoice'),
'invoice_district' => Tools::getValue('ps_address2_invoice'),
'invoice_city' => Tools::getValue('ps_city_invoice'),
'invoice_state' => Tools::getValue('ps_state_invoice'),
);
$api_response = $pagbank->processPixPayment($form_data);
} elseif ($action == "installments") {
$value = Tools::getValue("value");
$bin = Tools::getValue("credit_card_bin");
$api_response = $pagbank->callGetInstallments($value, $bin, $cart->id);
} elseif ($action == "registerUser") {
$code = Tools::getValue('code');
$adminDir = $state_array[0];
$token = $state_array[1];
$app = (string)$state_array[3];
if (!$code || $code == '') {
die('Código não recebido.');
}
$authorization = $pagbank->getAppAuthorization($code, (string)$app);
if (!$authorization) {
$pagbank->saveLog('erro', 'callback', json_encode('APP: ' . (string)$app . '.'), '', (string)$code, 'Erro ao pegar o Token de usuário para a aplicação ' . (string)$app . '.');
} else {
$appMsg = 'Usuário cadastrado com sucesso na aplicação ' . (string)$app;
$module_admin = Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . $adminDir . '/index.php?controller=AdminModules&configure=pagbank&tab_module=payments_gateways&module_name=pagbank&configure=pagbank&tab_module=payments_gateways&module_name=pagbank&token=' . $token . '&app_msg=' . $appMsg;
Tools::redirectAdmin($module_admin);
}
} elseif ($action == "deleteToken") {
$id_customer_token = Tools::getValue('id_customer_token');
$api_response = $pagbank->deleteCustomerToken($id_customer_token);
} elseif ($action == "checkOrder") {
$id_order = Tools::getValue('id_order');
$order = new Order((int)$id_order);
$api_response = $order->getHistory($id_lang);
} elseif ($action == "cancelNotPaidPix" && $token_valid === true) {
$awaiting_payment = Configuration::get('PAGBANK_AWAITING_PAYMENT');
$payment_deadline = Configuration::get('PAGBANK_PIX_TIME_LIMIT');
$queryStr = 'SELECT `id_order`, `id_cart` FROM `' . _DB_PREFIX_ . 'orders` WHERE `payment` like "PIX%" AND `current_state` = ' . (int)$awaiting_payment . ' AND `date_add` < DATE_SUB(NOW(),INTERVAL ' . $payment_deadline . ' MINUTE)';
$ordersToCancel = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($queryStr);
if (!empty($ordersToCancel)) {
$cancelStatus = Configuration::get('PAGBANK_CANCELED');
$history = new OrderHistory();
foreach ($ordersToCancel as $order) {
$history->id_order = (int)$order['id_order'];
$history->changeIdOrderState((int)$cancelStatus, (int)$order['id_order']);
if ($history->addWithemail(true)) {
$api_response = 'Status do pedido ' . $order['id_order'] . ' atualizado na loja';
$pagbank->saveLog('success', 'Cancela Pedido Pix Não Pago', $order['id_card'], '', 'Status do pedido atualizado na loja.', false, 1);
} else {
$api_response = 'Status do pedido ' . $order['id_order'] . ' não atualizado na loja';
$pagbank->saveLog('error', 'Cancela Pedido Pix Não Pago', $order['id_card'], '', 'Status do pedido não atualizado na loja.', false, 1);
}
}
} else {
$api_response = 'Sem pedidos para cancelar!';
}
} elseif ($action == "cancelNotPaidBankslip" && $token_valid === true) {
$awaiting_payment = Configuration::get('PAGBANK_AWAITING_PAYMENT');
$payment_deadline = Configuration::get('PAGBANK_BANKSLIP_DATE_LIMIT');
$queryStr = 'SELECT `id_order`, `id_cart` FROM `' . _DB_PREFIX_ . 'orders` WHERE `payment` like "Boleto%" AND `current_state` = ' . (int)$awaiting_payment . ' AND `date_add` < DATE_SUB(NOW(),INTERVAL ' . $payment_deadline . ' DAY) AND DAYOFWEEK(NOW()) NOT IN (1,7)';
$ordersToCancel = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($queryStr);
if (!empty($ordersToCancel)) {
$cancelStatus = Configuration::get('PAGBANK_CANCELED');
$history = new OrderHistory();
foreach ($ordersToCancel as $order) {
$history->id_order = (int)$order['id_order'];
$history->changeIdOrderState((int)$cancelStatus, (int)$order['id_order']);
if ($history->addWithemail(true)) {
$api_response = 'Status do pedido ' . $order['id_order'] . ' atualizado na loja';
$pagbank->saveLog('success', 'Cancela Pedido Boleto Não Pago', $order['id_card'], '', 'Status do pedido atualizado na loja.', false, 1);
} else {
$api_response = 'Status do pedido ' . $order['id_order'] . ' não atualizado na loja';
$pagbank->saveLog('error', 'Cancela Pedido Boleto Não Pago', $order['id_card'], '', 'Status do pedido não atualizado na loja.', false, 1);
}
}
} else {
$api_response = 'Sem pedidos para cancelar!';
}
}
if ($api_response) {
header("Content-type: application/json; charset=utf-8");
echo json_encode($api_response, JSON_PRETTY_PRINT);
} else {
die('Sem Retorno.');
}