feat: add total field to purchases and update related logic for payment processing
This commit is contained in:
@@ -158,7 +158,7 @@ class TelepagosIntegrationService extends BaseIntegrationService
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getCashinDetails(int $cashinId): array
|
||||
public function getCashinDetails(string $cashinId): array
|
||||
{
|
||||
$response = $this->sendRequest('get', "/v2/payment/cashin/{$cashinId}");
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace App\Domains\Integration\Services;
|
||||
|
||||
use App\Domains\Purchase\Services\CheckoutService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Models\TelepagosPayment;
|
||||
use App\Domains\Purchase\Models\TelepagosQr;
|
||||
use App\Domains\Purchase\Services\CheckoutService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -27,18 +27,15 @@ class TelepagosWebhookService
|
||||
public function handleWebhook(string $tenantCodigo, string $cashinId): void
|
||||
{
|
||||
$tenant = Tenant::where('codigo', $tenantCodigo)->firstOrFail();
|
||||
|
||||
|
||||
$telepagosService = new TelepagosIntegrationService();
|
||||
$telepagosService->forTenant($tenant);
|
||||
$telepagosService->forTenant($tenant->codigo);
|
||||
|
||||
try {
|
||||
$details = $telepagosService->getCashinDetails($cashinId);
|
||||
|
||||
// Extract the qr_order_id from the response
|
||||
// The exact path depends on Telepagos response structure, assuming it's inside 'data'
|
||||
// Need to handle potential nested structures based on actual API
|
||||
|
||||
$qrOrderId = $details['data']['qr_order_id'] ?? $details['qr_order_id'] ?? null;
|
||||
$amount = $details['data']['amount'] ?? $details['amount'] ?? 0;
|
||||
$amount = $this->normalizeAmount($details['data']['amount'] ?? $details['amount'] ?? 0);
|
||||
$operationId = $details['data']['operation_id'] ?? $details['operation_id'] ?? null;
|
||||
|
||||
$transferenciaOperationIds = [1, 3, 11];
|
||||
@@ -46,59 +43,51 @@ class TelepagosWebhookService
|
||||
|
||||
$compra = null;
|
||||
|
||||
if (in_array((int)$operationId, $transferenciaOperationIds, true)) {
|
||||
if (in_array((int) $operationId, $transferenciaOperationIds, true)) {
|
||||
$cuit = $details['data']['buyer']['cuit'] ?? $details['buyer']['cuit'] ?? null;
|
||||
|
||||
if (!$cuit) {
|
||||
|
||||
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)
|
||||
|
||||
$compra = Purchase::where('tenant_codigo', $tenantCodigo)
|
||||
->where('dni', $dni)
|
||||
->where('status', 'pendiente')
|
||||
->where('payment_method', 'transferencia')
|
||||
->where('status', 'pending')
|
||||
->where('payment_method', 'transfer')
|
||||
->where('total', $amount)
|
||||
->latest()
|
||||
->get();
|
||||
|
||||
foreach ($compras as $c) {
|
||||
if ($c->getTotalAmount() == $amount) {
|
||||
$compra = $c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$compra) {
|
||||
->first();
|
||||
|
||||
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) {
|
||||
} elseif (in_array((int) $operationId, $qrOperationIds, true)) {
|
||||
if (! $qrOrderId) {
|
||||
Log::warning("Telepagos webhook: qr_order_id not found for QR cashin {$cashinId}");
|
||||
return;
|
||||
}
|
||||
|
||||
$telepagosQr = TelepagosQr::where('qr_order_id', $qrOrderId)->first();
|
||||
|
||||
if (!$telepagosQr) {
|
||||
if (! $telepagosQr) {
|
||||
Log::warning("Telepagos webhook: QR {$qrOrderId} not found in database for cashin {$cashinId}");
|
||||
return;
|
||||
}
|
||||
|
||||
$compra = $telepagosQr->compra;
|
||||
|
||||
if (!$compra) {
|
||||
if (! $compra) {
|
||||
Log::warning("Telepagos webhook: Purchase not found for QR {$qrOrderId}");
|
||||
return;
|
||||
}
|
||||
|
||||
$totalAmount = $compra->getTotalAmount();
|
||||
|
||||
if ($amount != $totalAmount) {
|
||||
$totalAmount = $this->normalizeAmount($compra->getTotalAmount());
|
||||
|
||||
if ($amount !== $totalAmount) {
|
||||
Log::warning("Telepagos webhook: Amount mismatch. Cashin amount: {$amount}, Purchase amount: {$totalAmount}");
|
||||
return;
|
||||
}
|
||||
@@ -107,7 +96,6 @@ class TelepagosWebhookService
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the TelepagosPayment and finalize the purchase in a transaction
|
||||
$paymentData = [
|
||||
'compra_id' => $compra->id,
|
||||
'cuit_buyer' => $details['data']['cuit_buyer'] ?? $details['cuit_buyer'] ?? null,
|
||||
@@ -128,10 +116,14 @@ class TelepagosWebhookService
|
||||
});
|
||||
|
||||
Log::info("Telepagos webhook: Successfully processed cashin {$cashinId} for purchase {$compra->id}");
|
||||
|
||||
} catch (Exception $e) {
|
||||
Log::error("Telepagos webhook error: " . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
protected function normalizeAmount(mixed $amount): string
|
||||
{
|
||||
return number_format((float) $amount, 2, '.', '');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user