home
/
u529748449
/
domains
/
borabilhete.com
/
public_html
/
webhooks
➕ New
📤 Upload
✎ Editing:
mercadopago.php
← Back
<?php // webhooks/mercadopago.php (versão simplificada e robusta) declare(strict_types=1); ini_set('display_errors','1'); error_reporting(E_ALL); require __DIR__ . '/../admin/conexao.php'; require __DIR__ . '/../vendor/autoload.php'; use MercadoPago\SDK; use MercadoPago\Payment; use MercadoPago\MerchantOrder; // Mesmo token do checkout SDK::setAccessToken('APP_USR-3015882569703700-071711-383174b1d312d3019b1612810d746d03-213132560'); // Health-check if (isset($_GET['ping'])) { header('Content-Type:text/plain; charset=utf-8'); echo 'webhook-v6'; exit; } // --- log cru $raw = file_get_contents('php://input') ?: ''; $qs = $_SERVER['QUERY_STRING'] ?? ''; $payload = json_decode($raw, true) ?: []; $type = $_GET['type'] ?? ($_GET['topic'] ?? ($payload['type'] ?? ($payload['topic'] ?? ''))); $idParam = $_GET['id'] ?? ($_GET['data_id'] ?? ($payload['data']['id'] ?? ($payload['id'] ?? null))); $logStmt = $conn->prepare("INSERT INTO mp_logs (query_string, raw_body) VALUES (?, ?)"); $logStmt->bind_param("ss", $qs, $raw); $logStmt->execute(); $logId = (int)$conn->insert_id; $logStmt->close(); function mpStatusToLocal(?string $s): string { $s = strtolower((string)$s); if ($s === 'approved') return 'pago'; if (in_array($s, ['cancelled','rejected','refunded','charged_back'], true)) return 'cancelado'; return 'pendente'; } function detectarForma($p): string { $type = strtolower((string)($p->payment_type_id ?? '')); $method = strtolower((string)($p->payment_method_id ?? '')); $poi = strtolower((string)($p->point_of_interaction->type ?? '')); $fi = strtolower((string)($p->transaction_details->financial_institution ?? '')); $qrA = isset($p->point_of_interaction->transaction_data->qr_code) || isset($p->point_of_interaction->transaction_data->qr_code_base64); $qrB = isset($p->transaction_details->qr_code) || isset($p->transaction_details->qr_code_base64); $descr = strtolower((string)($p->statement_descriptor ?? '')); if ($method==='pix' || $poi==='pix' || $fi==='pix' || $qrA || $qrB || strpos($descr,'pix')!==false) return 'Pix'; if ($type==='bank_transfer') return 'Pix'; // Checkout Pro costuma mandar bank_transfer no PIX if ($type==='credit_card') return 'Crédito'; if ($type==='debit_card') return 'Débito'; if ($type==='ticket') return 'Boleto'; if ($type==='account_money') return 'Saldo MP'; return 'mercadopago'; } function atualizarPedido(mysqli $c, int $id, string $status, string $forma, int $logId): void { // Atualização direta, sem CASE/IN (evita problemas de collation) $u = $c->prepare("UPDATE pedidos SET status_pagamento = ?, forma_pagamento = ? WHERE id = ?"); if (!$u) { $msg = 'SQL PREPARE FAIL: '.$c->error; $st = $c->prepare("UPDATE mp_logs SET status=? WHERE id=?"); $st->bind_param("si", $msg, $logId); $st->execute(); $st->close(); return; } $u->bind_param("ssi", $status, $forma, $id); if (!$u->execute()) { $msg = 'SQL EXEC FAIL: '.$c->error; $st = $c->prepare("UPDATE mp_logs SET status=? WHERE id=?"); $st->bind_param("si", $msg, $logId); $st->execute(); $st->close(); } $u->close(); } function aplicarPagamento(mysqli $c, int $paymentId, int $logId): void { $p = Payment::find_by_id($paymentId); if (!$p) return; $pedidoId = (int)($p->external_reference ?? 0); if ($pedidoId <= 0) return; $status = mpStatusToLocal($p->status ?? null); $forma = detectarForma($p); atualizarPedido($c, $pedidoId, $status, $forma, $logId); // log rico $ptype = (string)($p->payment_type_id ?? ''); $pmethod = (string)($p->payment_method_id ?? ''); $poi = (string)($p->point_of_interaction->type ?? ''); $fi = (string)($p->transaction_details->financial_institution ?? ''); $extRef = (string)$pedidoId; $statusLog = sprintf('%s | ptype=%s | pmethod=%s | poi=%s | fi=%s | detected=%s', (string)($p->status ?? ''), $ptype, $pmethod, $poi, $fi, $forma); $st = $c->prepare("UPDATE mp_logs SET resolved_payment_id=?, status=?, external_reference=? WHERE id=?"); $st->bind_param("sssi", $paymentId, $statusLog, $extRef, $logId); $st->execute(); $st->close(); } // ---------------------------------- http_response_code(200); header('Content-Type:text/plain; charset=utf-8'); try { $t = strtolower((string)$type); if ($t === 'payment' && $idParam) { aplicarPagamento($conn, (int)$idParam, $logId); echo 'ok:payment'; exit; } if ($t === 'merchant_order' && $idParam) { $mo = MerchantOrder::find_by_id((int)$idParam); if (!empty($mo) && !empty($mo->payments) && is_array($mo->payments)) { foreach ($mo->payments as $pp) { if (!empty($pp->id)) aplicarPagamento($conn, (int)$pp->id, $logId); } } echo 'ok:merchant_order'; exit; } if (!empty($payload['data']['id'])) { aplicarPagamento($conn, (int)$payload['data']['id'], $logId); echo 'ok:data.id'; exit; } echo 'ok:no-op'; } catch (Throwable $e) { $msg = 'ERR: '.$e->getMessage(); $st = $conn->prepare("UPDATE mp_logs SET status=? WHERE id=?"); $st->bind_param("si", $msg, $logId); $st->execute(); $st->close(); echo 'err'; }
💾 Save Changes
Cancel
📤 Upload File
×
Select File
Upload
Cancel
➕ Create New
×
Type
📄 File
📁 Folder
Name
Create
Cancel
✎ Rename Item
×
Current Name
New Name
Rename
Cancel
🔐 Change Permissions
×
Target File
Permission (e.g., 0755, 0644)
0755
0644
0777
Apply
Cancel