-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPluginMercadopagoCallback.php
109 lines (85 loc) · 4.29 KB
/
PluginMercadopagoCallback.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
<?php
require_once 'modules/admin/models/PluginCallback.php';
require_once 'modules/admin/models/StatusAliasGateway.php';
require_once 'modules/billing/models/class.gateway.plugin.php';
require_once 'modules/billing/models/Invoice_EventLog.php';
require_once 'modules/billing/models/Invoice.php';
require_once 'modules/admin/models/Error_EventLog.php';
require_once 'plugins/gateways/mercadopago/vendor/autoload.php';
class PluginMercadopagoCallback extends PluginCallback
{
function processCallback()
{
if ($this->settings->get('plugin_mercadopago_Modo de prueba') == '1') {
$accessToken = $this->settings->get('plugin_mercadopago_Test Token');
} else {
$accessToken = $this->settings->get('plugin_mercadopago_Live Secret Key');
}
CE_Lib::log(1, 'Mercadopago callback invoked');
MercadoPago\SDK::setAccessToken($accessToken);
$response = file_get_contents("php://input");
$response = json_decode($response, true);
CE_Lib::log(4, "Mercadopago Response: {$response}");
if ($response['type'] == 'payment') {
try {
$payment = MercadoPago\Payment::find_by_id($response['id']);
} catch (Exception $e) {
CE_Lib::log(1, $e->getMessage());
return $this->user->lang("There was an error with Mercadopago.") . " " . $e->getMessage();
}
switch ($response['action']) {
case 'payment.created':
CE_Lib::log(1, "Procesando pago de mercadopago");
$cPlugin = new Plugin();
$invoice = $cPlugin->retrieveInvoiceForTransaction($payment->id);
echo $cPlugin->retrieveInvoiceForTransaction($payment->id);
if ($invoice->m_ID != $payment->external_reference || is_null($invoice->m_ID)) {
$msg = " Error de comparacion de ID Factura {$invoice->m_ID} con ID MercadoPago {$payment->external_reference}";
CE_Lib::log(1, $msg);
die();
}
if ($invoice->IsUnpaid()) {
$cPlugin->m_TransactionID = $payment->id;
$cPlugin->setAction('charge');
$cPlugin->setAmount($payment->transaction_amount);
if ($payment->status == 'approved') {
$transaction = "MercadoPago Payment of {$payment->external_reference} was accepted, trasaction {$payment->id}";
$cPlugin->PaymentAccepted($payment->transaction_amount, $transaction);
$msg = "MercadoPago: Pago acreditado por a la factura {$payment->external_reference}";
} else {
$transaction = "Payment rejected - Reason: " . $payment->status ;
$cPlugin->PaymentRejected($payment->transaction_amount, false);
$msg = "MercadoPago: Se registro un rechazo del pago para la factura {$payment->external_reference}";
}
CE_Lib::log(1, $msg);
return 'OK';
} else {
$msg = "MercadoPago: Se registro un pago ya acreditado para factura {$payment->external_reference}";
CE_Lib::log(1, $msg);
return 'OK';
}
break;
case 'payment.updated':
break;
echo "Hi";
$this->logmp();
}
CE_Lib::log(1, 'Salida Mercadopago Response: ');
}
}
public function logmp()
{
$data1 = json_decode(file_get_contents('php://input'), true);
$data = file_get_contents('php://input');
print_r($data);
print_r($_POST);
$myfile = fopen("log.txt", "a+") or die("Unable to open file!");
$txt = "----------------\n";
fwrite($myfile, $txt);
fwrite($myfile, $data);
$txt = "\n----------------";
fwrite($myfile, $txt);
fclose($myfile);
}
}
?>