Updated checkout to wait for backend confirmation before advancing, added a “Processing payment…” state with retry/ refresh fallback, and now use Paddle totals/currency for purchase records + confirmation emails (with new email translations).

This commit is contained in:
Codex Agent
2025-12-22 09:06:48 +01:00
parent 41d29eb7d3
commit 84234bfb8e
36 changed files with 1742 additions and 187 deletions

View File

@@ -30,7 +30,16 @@ class PaddleWebhookControllerTest extends TestCase
'id' => 'txn_123',
'status' => 'completed',
'checkout_id' => 'chk_456',
'metadata' => [
'details' => [
'totals' => [
'subtotal' => ['amount' => '10000'],
'discount' => ['amount' => '1000'],
'tax' => ['amount' => '1900'],
'total' => ['amount' => '10900'],
'currency_code' => 'EUR',
],
],
'custom_data' => [
'checkout_session_id' => $session->id,
'tenant_id' => (string) $tenant->id,
'package_id' => (string) $package->id,
@@ -66,6 +75,17 @@ class PaddleWebhookControllerTest extends TestCase
->where('provider', 'paddle')
->exists()
);
$purchase = PackagePurchase::query()
->where('tenant_id', $tenant->id)
->where('package_id', $package->id)
->first();
$this->assertNotNull($purchase);
$this->assertSame(109.0, (float) $purchase->price);
$this->assertSame('EUR', Arr::get($purchase->metadata, 'currency'));
$this->assertSame(109.0, (float) Arr::get($purchase->metadata, 'paddle_totals.total'));
$this->assertSame(109.0, (float) $session->amount_total);
}
public function test_duplicate_transaction_is_idempotent(): void
@@ -80,7 +100,7 @@ class PaddleWebhookControllerTest extends TestCase
'id' => 'txn_dup',
'status' => 'completed',
'checkout_id' => 'chk_dup',
'metadata' => [
'custom_data' => [
'checkout_session_id' => $session->id,
'tenant_id' => (string) $tenant->id,
'package_id' => (string) $package->id,
@@ -107,6 +127,60 @@ class PaddleWebhookControllerTest extends TestCase
$this->assertEquals('txn_dup', Arr::get($session->provider_metadata, 'paddle_transaction_id'));
}
public function test_transaction_completed_updates_tenant_status_for_one_time_package(): void
{
config(['paddle.webhook_secret' => 'test_secret']);
$user = User::factory()->create(['email_verified_at' => now()]);
$tenant = Tenant::factory()->create([
'user_id' => $user->id,
'subscription_status' => 'free',
]);
$user->forceFill(['tenant_id' => $tenant->id])->save();
$package = Package::factory()->create([
'type' => 'endcustomer',
'price' => 49,
'paddle_price_id' => 'price_one_time',
]);
/** @var CheckoutSessionService $sessions */
$sessions = app(CheckoutSessionService::class);
$session = $sessions->createOrResume($user, $package, ['tenant' => $tenant]);
$sessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);
$payload = [
'event_type' => 'transaction.completed',
'data' => [
'id' => 'txn_one_time',
'status' => 'completed',
'details' => [
'totals' => [
'total' => ['amount' => '4900'],
'currency_code' => 'EUR',
],
],
'custom_data' => [
'checkout_session_id' => $session->id,
'tenant_id' => (string) $tenant->id,
'package_id' => (string) $package->id,
],
],
];
$signature = hash_hmac('sha256', json_encode($payload), 'test_secret');
$response = $this->withHeader('Paddle-Webhook-Signature', $signature)
->postJson('/paddle/webhook', $payload);
$response->assertOk()->assertJson(['status' => 'processed']);
$tenant->refresh();
$this->assertSame('active', $tenant->subscription_status);
$this->assertNotNull($tenant->subscription_expires_at);
}
public function test_rejects_invalid_signature(): void
{
config(['paddle.webhook_secret' => 'secret']);
@@ -152,7 +226,7 @@ class PaddleWebhookControllerTest extends TestCase
'customer_id' => 'cus_123',
'created_at' => Carbon::now()->subDay()->toIso8601String(),
'next_billing_date' => Carbon::now()->addMonth()->toIso8601String(),
'metadata' => [
'custom_data' => [
'tenant_id' => (string) $tenant->id,
'package_id' => (string) $package->id,
],
@@ -212,7 +286,7 @@ class PaddleWebhookControllerTest extends TestCase
'id' => 'sub_cancel',
'status' => 'cancelled',
'customer_id' => 'cus_cancel',
'metadata' => [
'custom_data' => [
'tenant_id' => (string) $tenant->id,
'package_id' => (string) $package->id,
],