create(['price' => 10]); $response = $this->get(route('buy.packages', $package->id)); $response->assertRedirect(route('register', ['package_id' => $package->id])); } public function test_unverified_buy_redirects_to_verification() { $package = Package::factory()->create(['price' => 10]); $user = User::factory()->create(['email_verified_at' => null]); $tenant = Tenant::factory()->create(['user_id' => $user->id]); Auth::login($user); $response = $this->get(route('buy.packages', $package->id)); $response->assertRedirect(route('verification.notice')); } public function test_free_package_assigns_after_auth() { $freePackage = Package::factory()->create(['price' => 0]); $user = User::factory()->create(['email_verified_at' => now()]); $tenant = Tenant::factory()->create(['user_id' => $user->id]); Auth::login($user); $response = $this->get(route('buy.packages', $freePackage->id)); $response->assertRedirect('/admin'); $this->assertDatabaseHas('tenant_packages', [ 'tenant_id' => $tenant->id, 'package_id' => $freePackage->id, ]); $this->assertDatabaseHas('package_purchases', [ 'user_id' => $user->id, 'tenant_id' => $tenant->id, 'package_id' => $freePackage->id, ]); } public function test_paid_package_creates_stripe_session() { $paidPackage = Package::factory()->create(['price' => 10]); $user = User::factory()->create(['email_verified_at' => now()]); $tenant = Tenant::factory()->create(['user_id' => $user->id]); Auth::login($user); $response = $this->get(route('buy.packages', $paidPackage->id)); $response->assertStatus(302); // Redirect to Stripe $this->assertStringContainsString('checkout.stripe.com', $response->headers->get('Location')); } public function test_paypal_checkout_creates_order() { $paidPackage = Package::factory()->create(['price' => 10]); $user = User::factory()->create(['email_verified_at' => now()]); $tenant = Tenant::factory()->create(['user_id' => $user->id]); Auth::login($user); $mockClient = Mockery::mock(\PayPal\PayPalHttp\Client::class); $mockOrders = Mockery::mock(); $mockOrders->shouldReceive('createOrder')->andReturn(new \stdClass()); // Simplified mock $mockClient->shouldReceive('orders')->andReturn($mockOrders); $this->app->instance(\PayPal\PayPalHttp\Client::class, $mockClient); $response = $this->get(route('buy.packages', $paidPackage->id) . '?provider=paypal'); $response->assertStatus(302); $this->assertNotNull(session('paypal_order_id')); } public function test_paypal_success_captures_and_activates_package() { $paidPackage = Package::factory()->create(['price' => 10]); $user = User::factory()->create(['email_verified_at' => now()]); $tenant = Tenant::factory()->create(['user_id' => $user->id]); Auth::login($user); $metadata = json_encode([ 'user_id' => $user->id, 'tenant_id' => $tenant->id, 'package_id' => $paidPackage->id, 'type' => $paidPackage->type, ]); $mockClient = Mockery::mock(\PayPal\PayPalHttp\Client::class); $mockOrders = Mockery::mock(); $mockCapture = new \stdClass(); $mockCapture->status = 'COMPLETED'; $mockCapture->purchaseUnits = [(object)['custom_id' => $metadata]]; $mockResponse = new \stdClass(); $mockResponse->result = $mockCapture; $mockOrders->shouldReceive('captureOrder')->andReturn($mockResponse); $mockClient->shouldReceive('orders')->andReturn($mockOrders); $this->app->instance(\PayPal\PayPalHttp\Client::class, $mockClient); session(['paypal_order_id' => 'test-order-id']); $response = $this->get(route('marketing.success', $paidPackage->id)); $response->assertRedirect('/admin'); $this->assertDatabaseHas('tenant_packages', [ 'tenant_id' => $tenant->id, 'package_id' => $paidPackage->id, 'active' => true, ]); $this->assertDatabaseHas('package_purchases', [ 'tenant_id' => $tenant->id, 'package_id' => $paidPackage->id, 'provider_id' => 'paypal', ]); $this->assertNull(session('paypal_order_id')); $this->assertEquals('active', $tenant->fresh()->subscription_status); } }