11<?php
22
3- namespace App \Jobs \ StripeWebhooks ;
3+ namespace App \Jobs ;
44
5- use App \Jobs \CreateAnystackLicenseJob ;
65use Illuminate \Bus \Queueable ;
76use Illuminate \Contracts \Queue \ShouldQueue ;
87use Illuminate \Foundation \Bus \Dispatchable ;
98use Illuminate \Queue \InteractsWithQueue ;
109use Illuminate \Queue \SerializesModels ;
11- use Spatie \WebhookClient \Models \WebhookCall ;
12- use Stripe \Event ;
13- use Stripe \StripeClient ;
10+ use Laravel \Cashier \Cashier ;
11+ use Laravel \Cashier \Events \WebhookHandled ;
1412use Stripe \Subscription ;
1513
1614class HandleCustomerSubscriptionCreatedJob implements ShouldQueue
1715{
1816 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1917
20- public function __construct (public WebhookCall $ webhook ) {}
18+ public function __construct (public WebhookHandled $ webhook ) {}
2119
2220 public function handle (): void
2321 {
@@ -29,19 +27,17 @@ public function handle(): void
2927 return ;
3028 }
3129
32- $ customer = app (StripeClient::class)
33- ->customers
34- ->retrieve ($ stripeSubscription ->customer );
30+ $ user = Cashier::findBillable ($ stripeSubscription ->customer );
3531
36- if (! $ customer || ! ($ email = $ customer ->email )) {
37- $ this ->fail ('Failed to retrieve customer information or customer has no email . ' );
32+ if (! $ user || ! ($ email = $ user ->email )) {
33+ $ this ->fail ('Failed to find user from Stripe subscription customer . ' );
3834
3935 return ;
4036 }
4137
4238 $ subscriptionPlan = \App \Enums \Subscription::fromStripeSubscription ($ stripeSubscription );
4339
44- $ nameParts = explode (' ' , $ customer ->name ?? '' , 2 );
40+ $ nameParts = explode (' ' , $ user ->name ?? '' , 2 );
4541 $ firstName = $ nameParts [0 ] ?: null ;
4642 $ lastName = $ nameParts [1 ] ?? null ;
4743
@@ -55,6 +51,6 @@ public function handle(): void
5551
5652 protected function constructStripeSubscription (): ?Subscription
5753 {
58- return Event ::constructFrom ($ this ->webhook ->payload )-> data ?-> object;
54+ return Subscription ::constructFrom ($ this ->webhook ->payload [ ' data ' ][ ' object ' ]) ;
5955 }
6056}
0 commit comments