home
/
u529748449
/
domains
/
borabilhete.com
/
public_html
/
public
/
api
➕ New
📤 Upload
✎ Editing:
refresh_mp.php
← Back
<?php // public/api/refresh_mp.php declare(strict_types=1); ini_set('display_errors','1'); error_reporting(E_ALL); header('Content-Type: application/json; charset=utf-8'); require __DIR__.'/../../admin/conexao.php'; require __DIR__.'/../../vendor/autoload.php'; use MercadoPago\SDK; use MercadoPago\Payment; SDK::setAccessToken('APP_USR-3015882569703700-071711-383174b1d312d3019b1612810d746d03-213132560'); 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 detectFormaFromPayment($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'; 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 saveForma(mysqli $c,int $id,string $nova):void{ $sql="UPDATE pedidos SET forma_pagamento = CASE WHEN LOWER(forma_pagamento)='pix' THEN 'Pix' WHEN LOWER(?)='pix' THEN 'Pix' WHEN LOWER(forma_pagamento) IN ('','mercadopago','não informado','nao informado','transferência','transferencia') THEN ? ELSE forma_pagamento END WHERE id=?"; $u=$c->prepare($sql); $u->bind_param("ssi",$nova,$nova,$id); $u->execute(); $u->close(); } function saveStatus(mysqli $c,int $id,string $st):void{ $u=$c->prepare("UPDATE pedidos SET status_pagamento=? WHERE id=?"); $u->bind_param("si",$st,$id); $u->execute(); $u->close(); } $token = $_GET['token'] ?? ''; $pid = $_GET['payment_id'] ?? ($_GET['collection_id'] ?? null); if (!$token || strlen($token)<16){ echo json_encode(['ok'=>false,'err'=>'token']); exit; } $st=$conn->prepare("SELECT id, status_pagamento, forma_pagamento FROM pedidos WHERE token_seguro=? LIMIT 1"); $st->bind_param("s",$token); $st->execute(); $pedido=$st->get_result()->fetch_assoc(); $st->close(); if(!$pedido){ echo json_encode(['ok'=>false,'err'=>'pedido']); exit; } $idPedido=(int)$pedido['id']; $status =(string)$pedido['status_pagamento']; $forma = (string)$pedido['forma_pagamento']; if ($pid){ try{ $p = Payment::find_by_id($pid); if ($p){ $stNew = mpStatusToLocal($p->status ?? null); $fmNew = detectFormaFromPayment($p); saveForma($conn,$idPedido,$fmNew); saveStatus($conn,$idPedido,$stNew); $status = $stNew; $forma = $fmNew; } }catch(Throwable $e){} } echo json_encode(['ok'=>true,'status'=>$status,'forma'=>$forma]);
💾 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