-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathplans.php
264 lines (213 loc) · 8.57 KB
/
plans.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
253
254
255
256
257
258
259
260
261
262
263
264
<?php
require_once(__DIR__ . '/global.php');
// This page is only used when subscriptions are enabled, no need to have it otherwise
if (!UserConfig::$useSubscriptions) {
header('Location: ' . UserConfig::$DEFAULTLOGOUTRETURN);
exit;
}
/*
* This page is used for assigning plans event if no plan is currently set for account
*/
UserConfig::$IGNORE_CURRENT_ACCOUNT_PLAN_VERIFICATION = true;
$user = User::require_login();
$account = Account::getCurrentAccount($user);
/*
* Only admin can view or change subscription plans
*/
if ($account->getUserRole($user) !== Account::ROLE_ADMIN) {
header('Location: ' . UserConfig::$DEFAULTLOGOUTRETURN);
exit;
}
/* ------------------- Handling form submission -------------------------------------- */
UserTools::preventCSRF();
$template_info = StartupAPI::getTemplateInfo();
$template_info['account_name'] = $account->getName();
if (array_key_exists('plan', $_POST)) {
$data = explode('.', $_REQUEST['plan']);
if (!isset($data[1])) {
$data[1] = NULL;
$data[2] = NULL;
}
$selected_plan_slug = $data[0];
$selected_schedule_slug = $data[1];
$selected_engine_slug = $data[2];
$engine = null;
$schedule = null;
try {
// Check if plan and schedule exists
if (!($plan = Plan::getPlanBySlug($selected_plan_slug))) {
throw new Exception("Unknown plan '" . $selected_plan_slug . '"');
}
if (!is_null($selected_schedule_slug) && !($schedule = $plan->getPaymentScheduleBySlug($selected_schedule_slug))) {
throw new Exception("Unknown schedule '" . $selected_schedule_slug . "' for plan '" . $selected_plan_slug . "'");
}
if (!is_null($selected_engine_slug) && !($engine = PaymentEngine::getEngineBySlug($selected_engine_slug))) {
throw new Exception("Unknown payment engine '" . $engine . "' for schedule schedule '" . $selected_schedule_slug . "' for plan '" . $selected_plan_slug . "'");
}
} catch (Exception $e) {
$_SESSION['message'][] = $e->getMessage();
header('Location: ' . UserConfig::$USERSROOTURL . '/plans.php');
exit;
}
if (!$engine || $engine->requiresPrePayment()) {
// Check balance
if (!is_null($schedule) && $schedule->getChargeAmount() > $account->getBalance()) {
$_SESSION['message'][] = "Not enough funds to activate plan/schedule";
} elseif (
$account->getPlanSlug() != $selected_plan_slug ||
(!is_null($account->getNextPlan()) && $account->getNextPlan()->getSlug() != $selected_plan_slug)
) {
// Not changing plan if requestfed plan is same as current or next
if ($account->planChangeRequest($selected_plan_slug, $selected_schedule_slug, $selected_engine_slug)) {
if ($account->getPlanSlug() != $selected_plan_slug) {
// Plan activation postponed
$_SESSION['message'][] = "Your request to activate plan '" . $selected_plan_slug . '/' . $selected_schedule_slug .
"' accepted. Plan will be activated on the next charge according to your current schedule.";
} else {
// Plan activated immediately
$_SESSION['message'][] = "Plan " . $selected_plan_slug . '/' . $selected_schedule_slug . " activated.";
}
} else {
$_SESSION['message'][] = "Error activating plan";
}
} elseif (!is_null($selected_schedule_slug) && ($account->getScheduleSlug() != $selected_schedule_slug ||
(!is_null($account->getNextSchedule()) && $account->getNextSchedule()->getSlug() != $selected_schedule_slug))) {
// Not changing schedule if requested schedule is same as current or next
if ($account->scheduleChangeRequest($selected_schedule_slug, $selected_engine_slug)) {
if ($account->getScheduleSlug() != $selected_schedule_slug) {
// Schedule change postponed
$_SESSION['message'][] = "Your request to change payment schedule to '" . $selected_schedule_slug .
"' accepted. Schedule will be activated on the next charge according to your current schedule.";
} else {
// Schedule changed immediately
$_SESSION['message'][] = "Payment schedule changed to " . $selected_schedule_slug;
}
} else {
$_SESSION['message'][] = "Error changing schedule";
}
}
} else {
// now we need to redirect to engine's action URL (potentially external
$action_url = $engine->getActionURL($plan, $schedule, $account);
header('Location: ' . $action_url);
exit;
}
header('Location: ' . UserConfig::$DEFAULTLOGINRETURN . '?upgraded');
exit;
}
/* ------------------- / Handling form submission -------------------------------------- */
/* ------------------- Preparing data for template ------------------------------------- */
$template_info['account'] = array('name' => $account->getName());
if (isset($_SESSION['message'])) {
$template_info['message'] = $_SESSION['message'];
unset($_SESSION['message']);
$fatal = isset($_SESSION['fatal']) ? $_SESSION['fatal'] : 0;
unset($_SESSION['fatal']);
if ($fatal) {
$template_info['fatal'] = 1;
return;
}
}
if (!$account->isActive()) {
$template_info['message'] = array('This account is not active. Please activate it first.');
$template_info['fatal'] = 1;
return;
}
$template_info['CSRF_NONCE'] = UserTools::$CSRF_NONCE;
$template_info['next_charge'] = $account->getNextCharge();
$balance = $account->getBalance();
$template_info['balance'] = $balance;
$plan_slugs = Plan::getPlanSlugs();
$current_plan = $account->getPlan(); // can be FALSE
$i = 0;
$base_plan_index = null;
$plans = array();
foreach ($plan_slugs as $plan_slug) { # Iterate over all configured plans
$this_plan = Plan::getPlanBySlug($plan_slug);
$plan = array();
$plan['slug'] = $this_plan->getSlug();
$plan['name'] = $this_plan->getName();
$plan['description'] = $this_plan->getDescription();
$plan['base_price'] = $this_plan->getBasePrice();
$plan['base_period'] = $this_plan->getBasePeriod();
$plan['details_url'] = $this_plan->getDetailsURL();
$plan['downgrade_to'] = $this_plan->getDowngradeToPlan();
$plan['grace_period'] = $this_plan->getGracePeriod();
$plan['available'] = $this_plan->isAvailable();
# Mark plan as current if slugs match
if ($current_plan && $current_plan->getSlug() == $this_plan->getSlug()) {
$plan['current'] = TRUE;
} else {
$plan['current'] = FALSE;
}
$schedule = array();
$schedule_slugs = $this_plan->getPaymentScheduleSlugs(); # Iterate over all schedules of this plan
if (!is_null($account->getNextPlan()) &&
$account->getNextPlan()->getSlug() == $this_plan->getSlug()) {
$plan['chosen'] = TRUE;
$template_info['next_chosen'] = TRUE;
} else {
$plan['chosen'] = FALSE;
}
foreach ($schedule_slugs as $s) {
$this_schedule = $this_plan->getPaymentScheduleBySlug($s);
$schedule['slug'] = $this_schedule->getSlug();
$schedule['name'] = $this_schedule->getName();
$schedule['description'] = $this_schedule->getDescription();
$schedule['charge_amount'] = $this_schedule->getChargeAmount();
$schedule['charge_period'] = $this_schedule->getChargePeriod();
$account_schedule = $account->getSchedule();
if ($plan['current'] && !is_null($account_schedule) && $account_schedule->getSlug() == $this_schedule->getSlug()) {
$schedule['current'] = TRUE;
} else {
$schedule['current'] = FALSE;
}
if ($plan['chosen'] && !is_null($account->getNextSchedule()) && $account->getNextSchedule()->getSlug() == $this_schedule->getSlug()) {
$schedule['chosen'] = TRUE;
} else {
$schedule['chosen'] = FALSE;
}
$plan['schedules'][] = $schedule;
}
$plans[] = $plan;
// Plan which is being upgraded
if ($plan['current']) {
$template_info['base_plan'] = $plan;
$base_plan_index = $i;
}
if ($plan['chosen']) {
$template_info['base_plan'] = $plan;
$base_plan_index = $i;
}
$i++;
}
$template_info['plans'] = $plans;
if (array_key_exists('base_plan', $template_info)) {
$template_info['plans'][$base_plan_index]['is_base_plan'] = true;
}
$display_template = '@startupapi/plan/plans.html.twig';
/*
* If plan was selected, show schedule and payment engine selection UI
*/
if (array_key_exists('plan', $_GET)) {
$selected_plan_slug = Plan::getPlanBySlug($_GET['plan']);
if ($selected_plan_slug) {
foreach ($template_info['plans'] as $plan) {
if ($plan['slug'] == $selected_plan_slug->getSlug()) {
$template_info['plan'] = $plan;
$display_template = '@startupapi/plan/select_payment_method.html.twig';
break;
}
}
}
}
foreach (UserConfig::$payment_modules as $payment_engine) {
$template_info['payment_engines'][] = array(
'slug' => $payment_engine->getSlug(),
'title' => $payment_engine->getTitle(),
'button_label' => $payment_engine->getActionButtonLabel(),
'requires_pre_payment' => $payment_engine->requiresPrePayment()
);
}
/* ------------------- / Preparing data for template -------------------------------------- */
StartupAPI::$template->display($display_template, $template_info);