-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.php
106 lines (79 loc) · 3.22 KB
/
hooks.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/*
HOOKS
----
https://getkirby.com/docs/reference/plugins/hooks
*/
return [
// CREATE STRIPE USER -----------------------------------------------------------------------------------------
// https://stripe.com/docs/api/customers/create
'user.create:after' => function ($user) {
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
try {
// CREATE STRIPE CUSTOMER
$customer = $stripe->customers->create([
'email' => $user->email()
]);
} catch (Exception $e) {
// LOG ERROR SOMEWHERE
throw new Exception('Could not create stripe customer!');
}
try {
// UPDATE KIRBY USER - ROOT TIER (INDEX=0)
kirby()->user($user->email())->update([
'stripe_customer' => $customer->id,
'tier' => option('kreativ-anders.memberkit.tiers')[0]['name']
]);
} catch (Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not update kirby user!');
}
},
// CHANGE STRIPE USER EMAIL -------------------------------------------------------------------------------------
// https://stripe.com/docs/api/customers/update
'user.changeEmail:after' => function ($newUser, $oldUser) {
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
try {
// UPDATE STRIPE CUSTOMER
$stripe->customers->update(
$oldUser->stripe_customer(),
['email' => $newUser->email()]
);
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not update stripe customer!');
}
},
// DELETE KIRBY USER & CANCEL ALL STRIPE SUBSCRIPTIONS ------------------------------------------------------------
// https://stripe.com/docs/api/customers/delete
'user.delete:after' => function ($status, $user) {
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
try {
// DELETE STRIPE CUSTOMER
$stripe->customers->delete(
$user->stripe_customer(),
[]
);
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not delete stripe customer!');
}
},
// RESERVE STRIPE ROUTES TO LOGGED-IN USERS
// https://getkirby.com/docs/guide/routing#before-and-after-hooks__route-before
'route:before' => function ($route, $path, $method) {
// DETERMINE ROUTE PATH AS BEST AS POSSIBLE (TRUE = MATCH)
$subscribe = Str::contains($path, Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/subscribe/');
$portal = Str::contains($path, Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/portal');
$checkout = Str::contains($path, Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/success');
$cancel = Str::contains($path, Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/cancel/subscription');
// CANCEL ROUTE IS DEBUG MODE EXCLUSIVE
if ($cancel && !option('debug')) {
throw new Exception('Cancel stripe subscription via URL is only available in debug mode!');
}
// REDIRECT TO HOMEPAGE WHEN USER IS NOT LOGGED-IN
if (($subscribe || $portal || $checkout || $cancel) && !kirby()->user()) {
go();
}
}
];