Skip to content

Commit 2de1697

Browse files
committed
improve handling & timing of new stripe customers
1 parent 598157d commit 2de1697

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

app/Jobs/CreateUserFromStripeCustomer.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Queue\InteractsWithQueue;
1010
use Illuminate\Queue\SerializesModels;
1111
use Illuminate\Support\Facades\Hash;
12+
use Illuminate\Support\Facades\Log;
1213
use Illuminate\Support\Str;
1314
use Laravel\Cashier\Cashier;
1415
use Stripe\Customer;
@@ -21,14 +22,19 @@ public function __construct(public Customer $customer) {}
2122

2223
public function handle(): void
2324
{
24-
if (Cashier::findBillable($this->customer)) {
25-
$this->fail("A user already exists for Stripe customer [{$this->customer->id}].");
25+
/** @var User $user */
26+
if ($user = Cashier::findBillable($this->customer)) {
27+
Log::debug("A user [{$user->id} | {$user->email}] with stripe_id [{$this->customer->id}] already exists.");
2628

2729
return;
2830
}
2931

30-
if (User::query()->where('email', $this->customer->email)->exists()) {
31-
$this->fail("A user already exists for email [{$this->customer->email}].");
32+
if ($user = User::query()->where('email', $this->customer->email)->first()) {
33+
// This could occur if a user performs/attempts multiple checkouts with the same email address.
34+
// In the event all existing stripe customers for this email address do NOT have an active
35+
// subscription, we could theoretically update the stripe_id for the existing user
36+
// and continue. However, for now, we will throw an exception.
37+
$this->fail("A user with email [{$user->email}] already exists but the current stripe_id [{$user->stripe_id}] does not match the new customer id [{$this->customer->id}].");
3238

3339
return;
3440
}

app/Listeners/StripeWebhookReceivedListener.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace App\Listeners;
44

55
use App\Jobs\CreateUserFromStripeCustomer;
6+
use Exception;
67
use Illuminate\Support\Facades\Log;
8+
use Laravel\Cashier\Cashier;
79
use Laravel\Cashier\Events\WebhookReceived;
810
use Stripe\Customer;
911

@@ -19,7 +21,25 @@ public function handle(WebhookReceived $event): void
1921
'customer.created' => dispatch_sync(new CreateUserFromStripeCustomer(
2022
Customer::constructFrom($event->payload['data']['object'])
2123
)),
24+
'customer.subscription.created' => $this->createUserIfNotExists($event->payload['data']['object']['customer']),
2225
default => null,
2326
};
2427
}
28+
29+
private function createUserIfNotExists(string $stripeCustomerId): void
30+
{
31+
if (Cashier::findBillable($stripeCustomerId)) {
32+
return;
33+
}
34+
35+
$customer = Customer::retrieve($stripeCustomerId);
36+
37+
if (! $customer->email) {
38+
throw new Exception(
39+
'A user needed to be created for customer.subscription.created but was unable to retrieve the customer from Stripe.'
40+
);
41+
}
42+
43+
dispatch_sync(new CreateUserFromStripeCustomer($customer));
44+
}
2545
}

0 commit comments

Comments
 (0)