-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.php
253 lines (188 loc) · 7.98 KB
/
routes.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/*
ROUTES
----
https://getkirby.com/docs/reference/plugins/extensions/routes
*/
return function ($kirby) {
return [
// CREATE STRIPE CHECKOUT SESSION --------------------------------------------------------------------------------------------------
[
// PATTERN => STRIPE SLUG / ACTION NAME (SUBSCRIBE) / STRIPE TIER NAME (RAWURLENCODED)
'pattern' => Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/subscribe/(:all)',
'action' => function ($tier) {
// DETERMINE PRICE BY TIER NAME
$tier = rawurldecode($tier);
$tierIndex = array_search($tier, array_map("Str::lower", array_column(option('kreativ-anders.memberkit.tiers'), 'name')), false);
$price = option('kreativ-anders.memberkit.tiers')[$tierIndex]['price'];
// BUILD MAN-IN-THE-MIDDLE/SUCCESS URL => SITE URL / STRIPE SLUG / ACTION NAME (SUCCESS)
$successURL = kirby()->site()->url() . '/';
$successURL .= Str::lower(option('kreativ-anders.memberkit.stripeURLSlug'));
$successURL .= '/success';
$customer = kirby()->user()->stripe_customer();
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
try {
// CREATE STRIPE CHECKOUT SESSION
$checkout = $stripe->checkout->sessions->create([
'success_url' => $successURL,
'cancel_url' => option('kreativ-anders.memberkit.cancelURL'),
'payment_method_types' => ['card'],
'allow_promotion_codes' => true,
'line_items' => [
[
'price' => $price,
'quantity' => 1,
],
],
'mode' => 'subscription',
'customer' => kirby()->user()->stripe_customer(),
]);
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not create stripe checkout session!');
}
return [
'id' => $checkout->id
];
}
],
// CREATE STRIPE CUSTOMER PORTAL SESSION ----------------------------------------------------------------------------------------
[
// PATTERN => STRIPE SLUG / STRIPE PORTAL
'pattern' => Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/portal',
'action' => function () {
// BUILD MAN-IN-THE-MIDDLE/RETURN URL => SITE URL
$returnURL = kirby()->site()->url() . '/';
$customer = kirby()->user()->stripe_customer();
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
try {
// CREATE STRIPE PORTAL SESSION
$session = $stripe->billingPortal->sessions->create([
'customer' => $customer,
'return_url' => $returnURL,
]);
$url = $session->url;
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not create stripe portal session!');
}
// GO TO STRIPE PORTAL
return go($url);
}
],
// CANCEL STRIPE SUBSCRIPTION -------------------------------------------------------------------------------------------------
[
// PATTERN => STRIPE SLUG / ACTION NAME (CANCEL) / TYPE NAME (SUBSCRIPTION)
'pattern' => Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/cancel/subscription',
'action' => function () {
$subscription = kirby()->user()->stripe_subscription();
$email = kirby()->user()->email();
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
try {
// CANCEL STRIPE SUBSCRIPTION
$stripe->subscriptions->cancel(
$subscription,
[]
);
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not cancel stripe subscription!');
}
try {
// RESET KIRBY USER SUBSCRIPTION - ROOT TIER (INDEX=0)
kirby()->user($email)->update([
'stripe_subscription' => null,
'stripe_status' => null,
'tier' => option('kreativ-anders.memberkit.tiers')[0]['name']
]);
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not reset kirby user subscriptions!');
}
return go();
}
],
// UPDATE/MERGE KIRBY USER AFTER (SUCCESSFUL) CHECKOUT --------------------------------------------------------------------------
[
// PATTERN => STRIPE SLUG / ACTION NAME (SUCCESS)
'pattern' => Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/success',
'action' => function () {
try {
// MERGE STRIPE USER WITH KIRBY USER
kirby()->user()->mergeStripeCustomer();
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Could not merge stripe customer into kirby user!');
}
// REDIRECT TO CUSTOM SUCCESS PAGE
return go(option('kreativ-anders.memberkit.successURL'));
}
],
// LISTEN TO STRIPE NOTIFICATIONS AKA STRIPE WEBHOOK ----------------------------------------------------------------------------
// https://stripe.com/docs/webhooks/integration-builder
// --> NOT SECURED!!!
[
// PATTERN => STRIPE SLUG / ACTION NAME (WEBHOOK)
'pattern' => Str::lower(option('kreativ-anders.memberkit.stripeURLSlug')) . '/webhook',
'action' => function () {
\Stripe\Stripe::setApiKey(option('kreativ-anders.memberkit.secretKey'));
$endpoint_secret = option('kreativ-anders.memberkit.webhookSecret');
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch(\UnexpectedValueException $e) {
http_response_code(400);
exit();
}
// VERIFY ENPOINT INTEGRITY
if ($endpoint_secret) {
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\Stripe\Exception\SignatureVerificationException $e) {
http_response_code(400);
exit();
}
}
// HANDLE THE EVENT
// https://stripe.com/docs/api/events/types
switch ($event->type) {
case 'customer.subscription.updated':
$subscription = $event->data->object;
// UPDATE STRIPE SUBSCRIPTION FOR USER X
kirby()->site()->updateStripeSubscriptionWebhook($subscription);
break;
case 'customer.subscription.deleted':
$subscription = $event->data->object;
// RESET KIRBY USER SUBSCRIPTION INFO
kirby()->site()->cancelStripeSubscriptionWebhook($subscription);
break;
case 'customer.updated':
$customer = $event->data->object;
// UPDATE KIRBY USER EMAIL
kirby()->site()->updateStripeEmailWebhook($customer);
break;
case 'invoice.payment_failed':
$subscription = $event->data->object;
// DURING CHECKOUT PROCEDURE:
// NOTHING TO DO SINCE NOTHING WILL BE CHANGED REGARDING THE CUSTOMER STATUS
// AFTER SUCCESSFUL CHECHOUT PROCEDURE:
// UPDATE STRIPE SUBSCRIPTION FOR USER X (STATUS BECOMES 'past_due')
kirby()->site()->updateStripeSubscriptionWebhook($subscription);
break;
default:
throw new Exception('Received unknown stripe event type!');
}
http_response_code(200);
return '<html><body>✔️ Success!</body></html>';
},
// ENSURE ONLY POST REQUESTS ARE CAPTURED
'method' => 'POST'
],
];
};