Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Jobs/CreateAnystackLicenseJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CreateAnystackLicenseJob implements ShouldQueue
public function __construct(
public User $user,
public Subscription $subscription,
public ?string $subscriptionItemId = null,
public ?int $subscriptionItemId = null,
public ?string $firstName = null,
public ?string $lastName = null,
) {}
Expand Down
6 changes: 0 additions & 6 deletions app/Listeners/StripeWebhookReceivedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Facades\Log;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Events\WebhookReceived;
use Stripe\Customer;

class StripeWebhookReceivedListener
{
Expand All @@ -16,11 +15,6 @@ public function handle(WebhookReceived $event): void
Log::debug('Webhook received', $event->payload);

match ($event->payload['type']) {
// 'customer.created' must be dispatched sync so the user is
// created before the cashier webhook handling is executed.
'customer.created' => dispatch_sync(new CreateUserFromStripeCustomer(
Customer::constructFrom($event->payload['data']['object'])
)),
'customer.subscription.created' => $this->createUserIfNotExists($event->payload['data']['object']['customer']),
default => null,
};
Expand Down
95 changes: 89 additions & 6 deletions tests/Feature/StripePurchaseHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void
}

#[Test]
public function a_user_is_created_when_a_stripe_customer_is_created()
public function a_user_is_not_created_when_a_stripe_customer_is_created()
{
Bus::fake();

Expand All @@ -49,9 +49,89 @@ public function a_user_is_created_when_a_stripe_customer_is_created()

$this->postJson('/stripe/webhook', $payload);

Bus::assertNotDispatched(CreateUserFromStripeCustomer::class);
}

#[Test]
public function a_user_is_created_when_a_stripe_customer_subscription_is_created_and_a_matching_user_doesnt_exist()
{
Bus::fake();

$this->mockStripeClient();

$payload = [
'id' => 'evt_test_webhook',
'type' => 'customer.subscription.created',
'data' => [
'object' => [
'id' => 'sub_test123',
'customer' => 'cus_test123',
'status' => 'active',
'items' => [
'object' => 'list',
'data' => [
[
'id' => 'si_test',
'price' => [
'id' => Subscription::Max->stripePriceId(),
'product' => 'prod_test',
],
'quantity' => 1,
],
],
],
],
],
];

$this->postJson('/stripe/webhook', $payload);

Bus::assertDispatched(CreateUserFromStripeCustomer::class);
}

#[Test]
public function a_user_is_not_created_when_a_stripe_customer_subscription_is_created_if_a_matching_user_already_exists()
{
Bus::fake();

$user = User::factory()->create([
'stripe_id' => 'cus_test123',
'name' => 'John Doe',
'email' => '[email protected]',
]);

$this->mockStripeClient($user);

$payload = [
'id' => 'evt_test_webhook',
'type' => 'customer.subscription.created',
'data' => [
'object' => [
'id' => 'sub_test123',
'customer' => $user->stripe_id,
'status' => 'active',
'items' => [
'object' => 'list',
'data' => [
[
'id' => 'si_test',
'price' => [
'id' => Subscription::Max->stripePriceId(),
'product' => 'prod_test',
],
'quantity' => 1,
],
],
],
],
],
];

$this->postJson('/stripe/webhook', $payload);

Bus::assertNotDispatched(CreateUserFromStripeCustomer::class);
}

#[Test]
public function a_license_is_created_when_a_stripe_subscription_is_created()
{
Expand Down Expand Up @@ -95,6 +175,7 @@ public function a_license_is_created_when_a_stripe_subscription_is_created()
Bus::assertDispatched(CreateAnystackLicenseJob::class, function (CreateAnystackLicenseJob $job) {
return $job->user->email === '[email protected]' &&
$job->subscription === Subscription::Max &&
$job->subscriptionItemId === $job->user->subscriptions->first()->items()->first()->id &&
$job->firstName === 'John' &&
$job->lastName === 'Doe';
});
Expand All @@ -105,7 +186,7 @@ public function a_license_is_created_when_a_stripe_subscription_is_created()
$this->assertNotEmpty($user->subscriptions->first()->items);
}

protected function mockStripeClient(User $user): void
protected function mockStripeClient(?User $user = null): void
{
$mockStripeClient = $this->createMock(StripeClient::class);
$mockStripeClient->customers = new class($user)
Expand All @@ -120,13 +201,15 @@ public function __construct($user)
public function retrieve()
{
return Customer::constructFrom([
'id' => $this->user->stripe_id,
'name' => $this->user->name,
'email' => $this->user->email,
'id' => $this->user?->stripe_id ?: 'cus_test123',
'name' => $this->user?->name ?: 'Test Customer',
'email' => $this->user?->email ?: '[email protected]',
]);
}
};

$this->app->instance(StripeClient::class, $mockStripeClient);
$this->app->bind(StripeClient::class, function ($app, $parameters) use ($mockStripeClient) {
return $mockStripeClient;
});
}
}