-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserMethods.php
More file actions
166 lines (119 loc) · 4.96 KB
/
userMethods.php
File metadata and controls
166 lines (119 loc) · 4.96 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
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
<?php
/*
USER-METHODS
----
https://getkirby.com/docs/reference/plugins/extensions/user-methods
*/
return [
// RETURN STRIPE SUBSCRIPTION CANCEL URL ---------------------------------------------------------------------------------
'getStripeCancelURL' => function () {
if ($this->stripe_subscription()->isEmpty()) {
throw new Exception('No subscription to cancel!');
}
// BUILD URL => STRIPE SLUG / ACTION NAME (CANCEL) / TYPE NAME (SUBSCRIPTION)
$url = Str::lower(option('kreativ-anders.memberkit.stripeURLSlug'));
$url .= '/cancel/subscription';
return $url;
},
// RETURN STRIPE WEBHOOK URL
'getStripeWebhookURL' => function () {
// BUILD URL => STRIPE SLUG / ACTION NAME (WEBHOOK)
$url = Str::lower(option('kreativ-anders.memberkit.stripeURLSlug'));
$url .= '/webhook';
return $url;
},
// RETURN STRIPE SUBSCRIPTION CHECKOUT URL FOR TIER X (NAME AS PARAMETER) -----------------------------------------------------
'getStripeCheckoutURL' => function ($tier) {
// SEARCH TIER NAME AND CHECK FOR EXISTENCE
$tierIndex = array_search($tier, array_column(option('kreativ-anders.memberkit.tiers'), 'name'), false);
if (!$tierIndex || $tierIndex < 1) {
throw new Exception('Tier does not exist!');
}
// BUILD URL => STRIPE SLUG / ACTION NAME (SUBSCRIBE) / STRIPE TIER NAME (RAWURLENCODED)
$url = Str::lower(option('kreativ-anders.memberkit.stripeURLSlug'));
$url .= '/subscribe';
$url .= '/' . rawurlencode(Str::lower(Str::trim(option('kreativ-anders.memberkit.tiers')[$tierIndex]['name'])));
return $url;
},
// RETURN STRIPE CUSTOMER PORTAL URL -------------------------------------------------------------------------------------------
'getStripePortalURL' => function () {
// BUILD URL => STRIPE SLUG / ACTION NAME (PORTAL)
$url = Str::lower(option('kreativ-anders.memberkit.stripeURLSlug'));
$url .= '/portal';
return $url;
},
// RETRIEVE STRIPE CUSTOMER (WITH SUBSCRIPTIONS) -------------------------------------------------------------------------------
'retrieveStripeCustomer' => function () {
if (!option('debug')) {
throw new Exception('Retrieve stripe customer is only available in debug mode!');
}
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
$customer = null;
try {
// RETRIEVE STRIPE CUSTOMER
$customer = $stripe->customers->retrieve(
$this->stripe_customer(),
['expand' => ['subscriptions']]
);
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Retrieve stripe customer failed!');
}
return $customer;
},
// MERGE STRIPE CUSTOMER WITH KIRBY USER ----------------------------------------------------------------------------------------
'mergeStripeCustomer' => function () {
$stripe = new \Stripe\StripeClient(option('kreativ-anders.memberkit.secretKey'));
$customer = null;
try {
// RETRIEVE STRIPE CUSTOMER
$customer = $stripe->customers->retrieve(
$this->stripe_customer(),
['expand' => ['subscriptions']]
);
} catch(Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Retrieve stripe customer failed!');
}
$subscription = $customer->subscriptions['data'][0];
// DETERMINE TIER NAME BY STRIPE PRICE ID
$price = $subscription->items['data'][0]->price->id;
$priceIndex = array_search($price, array_column(option('kreativ-anders.memberkit.tiers'), 'price'), false);
$tier = option('kreativ-anders.memberkit.tiers')[$priceIndex]['name'];
try {
// UPDATE KIRBY USER
$this->update([
'stripe_subscription' => $subscription->id,
'stripe_status' => $subscription->status,
'tier' => $tier
]);
$this->changeEmail($customer->email);
return true;
} catch (Exception $e) {
// LOG ERROR SOMEWHERE !!!
throw new Exception('Update kirby user failed!');
}
return false;
},
// CHECK USER PRIVILEGES BASED ON TIER (INDEX) -----------------------------------------------------------------------------
'isAllowed' => function ($tier) {
$userTier = $this->tier()->toString();
// GET INDEX FROM USER AND TIER NAME
$userIndex = array_search($userTier, array_column(option('kreativ-anders.memberkit.tiers'), 'name'), false);
$tierIndex = array_search($tier, array_column(option('kreativ-anders.memberkit.tiers'), 'name'), false);
// NO SUBSCRIPTION OR NON-ACTIVE SUBSCRIPTION
if ($this->tier()->isEmpty() || $this->stripe_subscription()->isEmpty() || $this->stripe_status()->isEmpty() || $this->stripe_status()->toString() != 'active') {
return false;
}
// REQUESTED TIER MATCHES USER TIER
if ($userTier === $tier) {
return true;
}
// USER TIER IS HIGHER (PRIO) THAN REQUESTED TIER
if ($userIndex >= $tierIndex) {
return true;
}
// DEFAULT
return false;
},
];