-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathCreateUserFromStripeCustomer.php
More file actions
51 lines (41 loc) · 1.86 KB
/
Copy pathCreateUserFromStripeCustomer.php
File metadata and controls
51 lines (41 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace App\Jobs;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Laravel\Cashier\Cashier;
use Stripe\Customer;
class CreateUserFromStripeCustomer implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(public Customer $customer) {}
public function handle(): void
{
/** @var User $user */
if ($user = Cashier::findBillable($this->customer)) {
Log::debug("A user [{$user->id} | {$user->email}] with stripe_id [{$this->customer->id}] already exists.");
return;
}
if ($user = User::query()->where('email', $this->customer->email)->first()) {
// This could occur if a user performs/attempts multiple checkouts with the same email address.
// In the event all existing stripe customers for this email address do NOT have an active
// subscription, we could theoretically update the stripe_id for the existing user
// and continue. However, for now, we will throw an exception.
$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}].");
return;
}
$user = new User;
$user->name = $this->customer->name;
$user->email = $this->customer->email;
$user->stripe_id = $this->customer->id;
// We will create a random password for the user and expect them to reset it.
$user->password = Hash::make(Str::random(72));
$user->save();
}
}