feat(telepagos): enhance webhook handling

This commit is contained in:
2026-08-19 15:26:44 -03:00
parent 32d2b182c6
commit 161693509f
5 changed files with 145 additions and 13 deletions

View File

@@ -14,6 +14,7 @@ use App\Domains\Catalog\Models\Variant;
use App\Domains\Integration\Models\ClientIntegration;
use App\Domains\Integration\Models\Integration;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Purchase\Models\TelepagosPayment;
use App\Domains\Purchase\Services\CheckoutService;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -261,6 +262,84 @@ class TelepagosWebhookTest extends TestCase
]);
}
public function test_transfer_webhook_records_unmatched_payment_when_multiple_purchases_match(): void
{
$tenant = $this->createTenant('ambiguous', 'Ambiguous', 'ambiguous.com.ar');
$this->configureTelepagosIntegration($tenant);
$variant = $this->createVariantForTenant('ambiguous', 10, '50.00');
$firstPurchase = $this->createPendingTransferPurchase(
$tenant,
User::factory()->create()->id,
$variant->id,
1,
'12345678',
);
$secondPurchase = $this->createPendingTransferPurchase(
$tenant,
User::factory()->create()->id,
$variant->id,
1,
'12345678',
);
$thirdPurchase = $this->createPendingTransferPurchase(
$tenant,
User::factory()->create()->id,
$variant->id,
1,
'12345678',
);
Http::fake([
'https://api.telepagos.com.ar/v2/auth/token' => Http::response([
'status' => 'ok',
'token' => 'test-token',
'expires_at' => now()->addHour()->toIso8601String(),
]),
'https://api.telepagos.com.ar/v2/payment/cashin/ambiguous' => Http::response([
'status' => 'ok',
'data' => [
'amount' => 50,
'operation_id' => 1,
'transaction_id' => 'tx-ambiguous',
'buyer' => [
'cuit' => '20123456789',
],
],
]),
]);
$this->postJson('/api/webhooks/telepagos/ambiguous', [
'id' => 'ambiguous',
])->assertOk()->assertJsonPath('status', 'success');
$this->assertDatabaseHas('telepagos_payments', [
'compra_id' => null,
'amount' => 50,
'operation_id' => 1,
'transaction_id' => 'tx-ambiguous',
]);
$payment = TelepagosPayment::query()
->where('transaction_id', 'tx-ambiguous')
->firstOrFail();
$this->assertEqualsCanonicalizing(
[$firstPurchase->id, $secondPurchase->id, $thirdPurchase->id],
$payment->matched_purchase_ids,
);
$this->assertDatabaseHas('compras', [
'id' => $firstPurchase->id,
'status' => Purchase::STATUS_PENDING_PAYMENT,
]);
$this->assertDatabaseHas('compras', [
'id' => $secondPurchase->id,
'status' => Purchase::STATUS_PENDING_PAYMENT,
]);
$this->assertDatabaseHas('compras', [
'id' => $thirdPurchase->id,
'status' => Purchase::STATUS_PENDING_PAYMENT,
]);
}
public function test_webhook_confirms_a_purchase_with_tickets_enabled(): void
{
$tenant = $this->createTenant('expired-ticket', 'Expired Ticket', 'expired-ticket.com.ar');