create(['name' => 'Login Anzeigename']); BillingAddress::factory()->create([ 'user_id' => $user->id, 'company' => 'Beispiel GmbH', 'name' => 'Max Mustermann', ]); expect($user->fresh()->stripeName())->toBe('Beispiel GmbH'); }); test('the stripe recipient name falls back to the billing name, then the display name', function () { $user = User::factory()->create(['name' => 'Login Anzeigename']); BillingAddress::factory()->create([ 'user_id' => $user->id, 'company' => null, 'name' => 'Max Mustermann', ]); expect($user->fresh()->stripeName())->toBe('Max Mustermann'); $withoutAddress = User::factory()->create(['name' => 'Nur Anzeigename']); expect($withoutAddress->stripeName())->toBe('Nur Anzeigename'); }); test('the stripe address mirrors the billing address', function () { $user = User::factory()->create(); BillingAddress::factory()->create([ 'user_id' => $user->id, 'address1' => 'Rechnungsweg 12', 'address2' => 'Hinterhaus', 'postal_code' => '33739', 'city' => 'Bielefeld', 'country_code' => 'DE', ]); expect($user->fresh()->stripeAddress())->toBe([ 'line1' => 'Rechnungsweg 12', 'line2' => 'Hinterhaus', 'postal_code' => '33739', 'city' => 'Bielefeld', 'country' => 'DE', ]); expect(User::factory()->create()->stripeAddress())->toBeNull(); }); test('checkout session options leave the stripe customer untouched when a billing address exists', function () { $user = User::factory()->create(); BillingAddress::factory()->create(['user_id' => $user->id]); $options = invokeSessionOptions($user->fresh()); expect($options['billing_address_collection'])->toBe('auto') ->and($options['automatic_tax'])->toBe(['enabled' => true]) ->and($options)->not->toHaveKeys(['customer_update', 'tax_id_collection']); }); test('checkout session options collect the address in stripe when no billing address exists', function () { $options = invokeSessionOptions(User::factory()->create()); expect($options['billing_address_collection'])->toBe('required') ->and($options['tax_id_collection'])->toBe(['enabled' => true]) ->and($options['customer_update'])->toBe(['address' => 'auto', 'name' => 'auto']); }); /** * @return array */ function invokeSessionOptions(User $user): array { $service = app(StripeCheckoutService::class); $method = new ReflectionMethod($service, 'sessionOptions'); return $method->invoke($service, $user); }