136 lines
4.2 KiB
PHP
136 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\CheckoutSession;
|
|
use App\Models\Package;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Checkout\CheckoutSessionService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Tests\TestCase;
|
|
|
|
class CheckoutSessionLocalConfirmationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_local_confirmation_marks_checkout_completed(): void
|
|
{
|
|
$this->app['env'] = 'local';
|
|
Config::set('checkout.providers', [
|
|
CheckoutSession::PROVIDER_LEMONSQUEEZY,
|
|
CheckoutSession::PROVIDER_FREE,
|
|
]);
|
|
Mail::fake();
|
|
Notification::fake();
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
$user = User::factory()->for($tenant)->create();
|
|
$package = Package::factory()->create([
|
|
'lemonsqueezy_variant_id' => 'pri_123',
|
|
'lemonsqueezy_product_id' => 'pro_123',
|
|
'price' => 120,
|
|
]);
|
|
|
|
$sessions = app(CheckoutSessionService::class);
|
|
$session = $sessions->createOrResume($user, $package, [
|
|
'tenant' => $tenant,
|
|
]);
|
|
$sessions->selectProvider($session, CheckoutSession::PROVIDER_LEMONSQUEEZY);
|
|
|
|
$this->actingAs($user);
|
|
$this->withSession(['_token' => 'test-token']);
|
|
|
|
$response = $this->postJson(
|
|
route('checkout.session.confirm', $session),
|
|
[
|
|
'order_id' => 'ord_123',
|
|
'checkout_id' => 'chk_123',
|
|
],
|
|
[
|
|
'X-CSRF-TOKEN' => 'test-token',
|
|
]
|
|
);
|
|
|
|
$response->assertOk()
|
|
->assertJsonPath('status', 'completed');
|
|
|
|
$this->assertDatabaseHas('checkout_sessions', [
|
|
'id' => $session->id,
|
|
'status' => 'completed',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('package_purchases', [
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'provider_id' => 'ord_123',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('tenant_packages', [
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $package->id,
|
|
'active' => true,
|
|
]);
|
|
}
|
|
|
|
public function test_local_confirmation_creates_new_endcustomer_tenant_package_per_purchase(): void
|
|
{
|
|
$this->app['env'] = 'local';
|
|
Config::set('checkout.providers', [
|
|
CheckoutSession::PROVIDER_LEMONSQUEEZY,
|
|
CheckoutSession::PROVIDER_FREE,
|
|
]);
|
|
Mail::fake();
|
|
Notification::fake();
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
$user = User::factory()->for($tenant)->create();
|
|
$package = Package::factory()->create([
|
|
'type' => 'endcustomer',
|
|
'lemonsqueezy_variant_id' => 'pri_456',
|
|
'lemonsqueezy_product_id' => 'pro_456',
|
|
'price' => 120,
|
|
]);
|
|
|
|
$sessions = app(CheckoutSessionService::class);
|
|
|
|
$firstSession = $sessions->createOrResume($user, $package, ['tenant' => $tenant]);
|
|
$sessions->selectProvider($firstSession, CheckoutSession::PROVIDER_LEMONSQUEEZY);
|
|
|
|
$this->actingAs($user);
|
|
$this->withSession(['_token' => 'test-token']);
|
|
|
|
$this->postJson(
|
|
route('checkout.session.confirm', $firstSession),
|
|
[
|
|
'order_id' => 'ord_456_a',
|
|
'checkout_id' => 'chk_456_a',
|
|
],
|
|
['X-CSRF-TOKEN' => 'test-token']
|
|
)->assertOk();
|
|
|
|
$secondSession = $sessions->createOrResume($user, $package, ['tenant' => $tenant]);
|
|
$sessions->selectProvider($secondSession, CheckoutSession::PROVIDER_LEMONSQUEEZY);
|
|
|
|
$this->postJson(
|
|
route('checkout.session.confirm', $secondSession),
|
|
[
|
|
'order_id' => 'ord_456_b',
|
|
'checkout_id' => 'chk_456_b',
|
|
],
|
|
['X-CSRF-TOKEN' => 'test-token']
|
|
)->assertOk();
|
|
|
|
$this->assertSame(
|
|
2,
|
|
\App\Models\TenantPackage::query()
|
|
->where('tenant_id', $tenant->id)
|
|
->where('package_id', $package->id)
|
|
->count()
|
|
);
|
|
}
|
|
}
|