|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CashierSubscriptionPause\Listeners; |
|
4
|
|
|
|
|
5
|
|
|
use CashierSubscriptionPause\Eloquent\WithPauseCollection; |
|
6
|
|
|
use Laravel\Cashier\Cashier; |
|
7
|
|
|
use Laravel\Cashier\Events\WebhookHandled; |
|
8
|
|
|
|
|
9
|
|
|
class CashierWebhookHandledEventListener |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Deactivate default package listener. |
|
13
|
|
|
* |
|
14
|
|
|
* @var bool |
|
15
|
|
|
*/ |
|
16
|
|
|
public static bool $deactivateListener = false; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Webhook types. |
|
20
|
|
|
* |
|
21
|
|
|
* See https://stripe.com/docs/api/events/types |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
public static array $listenTypes = [ |
|
26
|
|
|
'customer.subscription.created', |
|
27
|
|
|
'customer.subscription.updated', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Deactivate listener. |
|
32
|
|
|
* |
|
33
|
|
|
* @return static |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function deactivateListener(bool $inactive = true) |
|
36
|
|
|
{ |
|
37
|
|
|
static::$deactivateListener = $inactive; |
|
38
|
|
|
|
|
39
|
|
|
return new static; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Handle received Stripe webhooks. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \Laravel\Cashier\Events\WebhookHandled $event |
|
46
|
|
|
* |
|
47
|
|
|
* @psalm-suppress UndefinedDocblockClass |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function handle(WebhookHandled $event) |
|
52
|
|
|
{ |
|
53
|
|
|
if (static::$deactivateListener) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$payload = $event->payload; |
|
58
|
|
|
|
|
59
|
|
|
if (in_array($payload['type'], static::$listenTypes)) { |
|
60
|
|
|
if ($user = Cashier::findBillable($payload['data']['object']['customer'])) { |
|
61
|
|
|
$data = $payload['data']['object']; |
|
62
|
|
|
|
|
63
|
|
|
/** @var WithPauseCollection $subscription */ |
|
64
|
|
|
$subscription = $user->subscriptions() |
|
65
|
|
|
->where('stripe_id', $data['id']) |
|
66
|
|
|
->first(); |
|
67
|
|
|
|
|
68
|
|
|
if ($subscription instanceof WithPauseCollection) { |
|
|
|
|
|
|
69
|
|
|
$subscription->syncStripePauseCollection(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|