feat: enhance Telepagos webhook handling for transferencia and QR operations with improved purchase matching logic
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Domains\Integration\Services;
|
namespace App\Domains\Integration\Services;
|
||||||
|
|
||||||
use App\Domains\Purchase\Services\CheckoutService;
|
use App\Domains\Purchase\Services\CheckoutService;
|
||||||
|
use App\Domains\Purchase\Models\Purchase;
|
||||||
use App\Domains\Purchase\Models\TelepagosPayment;
|
use App\Domains\Purchase\Models\TelepagosPayment;
|
||||||
use App\Domains\Purchase\Models\TelepagosQr;
|
use App\Domains\Purchase\Models\TelepagosQr;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
@@ -38,9 +39,46 @@ class TelepagosWebhookService
|
|||||||
// Need to handle potential nested structures based on actual API
|
// Need to handle potential nested structures based on actual API
|
||||||
$qrOrderId = $details['data']['qr_order_id'] ?? $details['qr_order_id'] ?? null;
|
$qrOrderId = $details['data']['qr_order_id'] ?? $details['qr_order_id'] ?? null;
|
||||||
$amount = $details['data']['amount'] ?? $details['amount'] ?? 0;
|
$amount = $details['data']['amount'] ?? $details['amount'] ?? 0;
|
||||||
|
$operationId = $details['data']['operation_id'] ?? $details['operation_id'] ?? null;
|
||||||
|
|
||||||
|
$transferenciaOperationIds = [1, 3, 11];
|
||||||
|
$qrOperationIds = [31, 37, 47];
|
||||||
|
|
||||||
|
$compra = null;
|
||||||
|
|
||||||
|
if (in_array((int)$operationId, $transferenciaOperationIds, true)) {
|
||||||
|
$cuit = $details['data']['buyer']['cuit'] ?? $details['buyer']['cuit'] ?? null;
|
||||||
|
|
||||||
|
if (!$cuit) {
|
||||||
|
Log::warning("Telepagos webhook: CUIT not found for Transferencia cashin {$cashinId}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extraer el DNI del cuit (sacando los primeros 2 y el último dígito)
|
||||||
|
$dni = substr($cuit, 2, -1);
|
||||||
|
|
||||||
|
// Buscar la compra pendiente, con método transferencia, más reciente
|
||||||
|
$compras = Purchase::where('tenant_codigo', $tenantCodigo)
|
||||||
|
->where('dni', $dni)
|
||||||
|
->where('status', 'pendiente')
|
||||||
|
->where('payment_method', 'transferencia')
|
||||||
|
->latest()
|
||||||
|
->get();
|
||||||
|
|
||||||
|
foreach ($compras as $c) {
|
||||||
|
if ($c->getTotalAmount() == $amount) {
|
||||||
|
$compra = $c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$compra) {
|
||||||
|
Log::warning("Telepagos webhook: No matching purchase found for DNI {$dni} and amount {$amount} for cashin {$cashinId}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} elseif (in_array((int)$operationId, $qrOperationIds, true)) {
|
||||||
if (!$qrOrderId) {
|
if (!$qrOrderId) {
|
||||||
Log::warning("Telepagos webhook: qr_order_id not found for cashin {$cashinId}");
|
Log::warning("Telepagos webhook: qr_order_id not found for QR cashin {$cashinId}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,11 +98,12 @@ class TelepagosWebhookService
|
|||||||
|
|
||||||
$totalAmount = $compra->getTotalAmount();
|
$totalAmount = $compra->getTotalAmount();
|
||||||
|
|
||||||
// Depending on precision, we might need a looser comparison, but == should work for floats here
|
|
||||||
if ($amount != $totalAmount) {
|
if ($amount != $totalAmount) {
|
||||||
Log::warning("Telepagos webhook: Amount mismatch. Cashin amount: {$amount}, Purchase amount: {$totalAmount}");
|
Log::warning("Telepagos webhook: Amount mismatch. Cashin amount: {$amount}, Purchase amount: {$totalAmount}");
|
||||||
// In a real scenario, you might want to record a partial payment or flag it,
|
return;
|
||||||
// but the instructions say to finalize ONLY if the amount is correct.
|
}
|
||||||
|
} else {
|
||||||
|
Log::warning("Telepagos webhook: Unknown operation_id {$operationId} for cashin {$cashinId}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user