This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chargify_sites.pages.inc
513 lines (450 loc) · 15.2 KB
/
chargify_sites.pages.inc
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
// $Id$
/**
* @file
* Page callbacks and admin forms.
*/
/**
* Configuration settings form.
*/
function chargify_sites_settings_form() {
$form = array();
/*
$form['chargify_sites_products'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => t('Products'),
'#description' => t('Select which products should be available on the site signup form.'),
);
$products = variable_get('chargify_sites_products', array());
foreach(chargify_api_products_get() as $product) {
$handle = $product->getHandle();
$name = $product->getName();
$price = $product->getPriceInDollars();
$args = array(
'@name' => $name,
'@price' => $price,
);
$form['chargify_sites_products'][$handle] = array(
'#type' => 'checkbox',
'#title' => t('@name | @price', $args),
'#default_value' => $products[$handle],
);
}
*/
$form['chargify_sites_tos'] = array(
'#type' => 'textfield',
'#title' => t('Terms of Service URL'),
'#default_value' => variable_get('chargify_sites_tos', ''),
'#size' => 40,
'#description' => t('Specify the relative URL to the Terms of Service page.'),
'#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
);
$form['#validate'][] = 'chargify_sites_settings_form_validate';
$form = system_settings_form($form);
return $form;
}
/**
* Validation handler for chargify_sites_settings_form.
*/
function chargify_sites_settings_form_validate($form, &$form_state) {
// Validate front page path.
$item = array('link_path' => $form_state['values']['chargify_sites_tos']);
$normal_path = drupal_get_normal_path($item['link_path']);
if ($item['link_path'] != $normal_path) {
$args = array('%link_path' => $item['link_path'], '%normal_path' => $normal_path);
drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', $args));
$item['link_path'] = $normal_path;
}
if (!empty($item) && !menu_valid_path($item)) {
form_set_error('chargify_sites_tos', t("The path '@path' is either invalid or you do not have access to it.", array('@path' => $item['link_path'])));
}
}
/**
* Page callback for the New Site page.
*/
function chargify_sites_new_site_page($product) {
return theme('chargify_sites_product_page', $product);
}
function chargify_sites_login_page($node) {
if ($node) {
$product_handle = isset($_GET['product_handle']) ? $_GET['product_handle'] : $node->subscription->product_handle;
if (user_is_anonymous()) {
drupal_add_js(drupal_get_path('module', 'chargify_sites') .'/fancyForm.js');
return drupal_get_form('chargify_sites_login_form', $node, $product_handle);
}
else {
$path = "new-site/{$node->nid}/time-to-pay";
drupal_goto($path, 'product_handle='. urlencode($product_handle));
}
}
else {
drupal_access_denied();
}
}
function chargify_sites_login_form(&$form_state, $node, $product_handle) {
if (!user_is_anonymous()) {
drupal_access_denied();
}
$form['#node'] = $node;
$form['product_handle'] = array(
'#type' => 'hidden',
'#value' => $product_handle,
);
// @todo: check to make sure user registration is turned on.
$form['register'] = array(
'#type' => 'fieldset',
'#title' => t('Create an account'),
'#attributes' => array('class' => 'register'),
'#tree' => TRUE,
'#prefix' => "<div class='login-register-wrapper'>",
);
$form['register']['mail'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#default_value' => '',
'#maxlength' => EMAIL_MAX_LENGTH,
);
$form['register']['pass'] = array(
'#type' => 'password_confirm',
);
$form['login'] = array(
'#type' => 'fieldset',
'#title' => t('Login to an existing account'),
'#attributes' => array('class' => 'login'),
'#tree' => TRUE,
'#suffix' => "</div>",
);
$form['login']['name'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#default_value' => '',
'#maxlength' => EMAIL_MAX_LENGTH,
);
$form['login']['pass'] = array(
'#type' => 'password',
'#title' => t('Password'),
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
return $form;
}
/**
* Implementation of hook_form_validate().
*
* Errors should be signaled with form_set_error().
*/
function chargify_sites_login_form_validate($form, &$form_state) {
$values = &$form_state['values'];
$user_form_state = array();
if ($values['login']['name']) {
if ($name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $values['login']['name']))) {
form_set_value($form['login']['name'], $name, $form_state);
}
$user_form_state['values'] = &$values['login'];
foreach (user_login_default_validators() as $fnc) {
$fnc($form['login'], $user_form_state);
}
}
else if ($values['register']['mail']) {
$values['register']['name'] = $values['register']['mail'];
$user_form_state['values'] = &$values['register'];
user_register_validate($form['register'], $user_form_state);
}
else {
form_set_error('register', t('Whoops, you need to log in or register.'));
}
}
/**
* Submit handler for chargify_sites_login_form.
*/
function chargify_sites_login_form_submit($form, &$form_state) {
$values = $form_state['values'];
$node = $form['#node'];
global $user;
if (isset($values['register']) && $values['register']['mail']) {
$values['register']['name'] = $values['register']['mail'];
$dummy_form_state = array('values' => &$values['register']);
user_register_submit($form, $dummy_form_state);
}
// Update the node creator
$node->uid = $user->uid;
// Re-save the node.
node_save($node);
$form_state['redirect'] = array("new-site/{$node->nid}/time-to-pay", 'product_handle='. urlencode($values['product_handle']));
}
/**
* Page callback for the Payment page.
*/
function chargify_sites_payment_page($node) {
if ($node) {
$product_handle = isset($_GET['product_handle']) ? $_GET['product_handle'] : $node->subscription->product_handle;
if ($product_handle && !(isset($node->subscription) && $node->subscription->state)) {
drupal_add_js(drupal_get_path('module', 'chargify_sites') .'/fancyForm.js');
return drupal_get_form('chargify_sites_payment_form', $node, $product_handle);
}
else if (empty($product_handle)) {
drupal_set_title(t("Whoopsy"));
return t("Hmm, something happened there that shouldn't have. Please contact the someone that sent you here. :(");
}
else {
drupal_set_title(t("You're paid up!"));
return t('It looks like this site is already paid for. Congrats!');
}
}
}
/**
* Payment form.
*/
function chargify_sites_payment_form(&$form_state, $node, $product_handle) {
if (user_is_anonymous()) {
drupal_access_denied();
}
$form = array();
global $user;
$form['mail'] = array(
'#type' => 'hidden',
'#value' => $user->mail,
);
// The user object doesn't have the customer ID yet, so we need to load it.
if ($customer_id = chargify_uid_to_customer_id($user->uid)) {
$form['customer_id'] = array(
'#type' => 'hidden',
'#value' => $customer_id,
);
}
$form['#node'] = $node;
$form['product_handle'] = array(
'#type' => 'hidden',
'#value' => $product_handle,
);
$form['payment'] = array(
'#type' => 'fieldset',
'#title' => t('Payment info'),
'#attributes' => array('class' => 'payment'),
);
$form['payment']['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#default_value' => '',
'#required' => TRUE,
'#prefix' => '<div class="name">',
);
$form['payment']['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#default_value' => '',
'#required' => TRUE,
'#suffix' => '</div>',
);
$form['payment']['card_number'] = array(
'#type' => 'textfield',
'#title' => t('Credit Card Number'),
'#required' => TRUE,
'#prefix' => '<div class="cc-info">',
'#attributes' => array('autocomplete' => 'off'),
);
/* $form['payment']['cvv'] = array(
'#type' => 'textfield',
'#title' => t('CVV'),
'#size' => 4,
'#maxlength' => 4,
'#description' => t('The 3 or 4 digit verification number, usually on the back.')
);
$form['payment']['billing_zip'] = array(
'#type' => 'textfield',
'#title' => t('Billing Zip Code'),
'#maxlength' => 10,
'#required' => TRUE,
);*/
$form['payment']['expiration_month'] = array(
'#type' => 'select',
'#title' => t('Expiration'),
'#options' => _chargify_sites_months(),
'#prefix' => '<div class="expiration">',
);
$form['payment']['expiration_year'] = array(
'#type' => 'select',
'#options' => _chargify_sites_years(),
'#suffix' => '</div></div>',
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Build it!'));
return $form;
}
/**
* Implementation of hook_form_validate().
*
* Errors should be signaled with form_set_error().
*/
function chargify_sites_payment_form_validate($form, &$form_state) {
$values = &$form_state['values'];
// Verify numeric on certain fields.
$fields = array('card_number' => t('credit card number')); //, 'cvv' => t('CVV'), 'billing_zip' => t('billing zip code'));
foreach ($fields as $field => $label) {
$value = $values[$field];
if (is_string($value)) {
$chars = array('-', '.', ' ');
$value = str_replace($chars, '', $value);
}
if (!preg_match('!^[0-9]+$!', $value)) {
form_set_error($field, t('The !field must be a number.', array('!field' => $label)));
}
}
// Verify the date fields
$year = $values['expiration_year'];
$month = $values['expiration_month'];
if ($month == 0) {
form_set_error('expiration_month', t('You must select a month.'));
}
if ($year == 0) {
form_set_error('expiration_year', t('You must select a year.'));
}
// Verify that the expiration date is a future date.
$this_year = date('Y', time());
$this_month = date('n', time());
if ($year == $this_year && $month < $this_month) {
form_set_error('expiration_month', t('You appear to have selected an expiration date prior to now.'));
}
}
/**
* Submit handler.
*/
function chargify_sites_payment_form_submit($form, &$form_state) {
$values = $form_state['values'];
$node = $form['#node'];
global $user;
if (isset($values['customer_id'])) {
$user->customer_id = $values['customer_id'];
// Unset the customer ID so chargify.module doesn't try to add it again.
unset($values['customer_id']);
}
if ($user && $subscription = chargify_sites_subscription_create($values, $user, $node)) {
$form_state['redirect'] = "node/{$node->nid}";
}
else {
drupal_set_message(t("Hmm. Something didn't work right. Can you please verify everything and try again?"), 'error');
}
// Re-save the node.
node_save($node);
}
/**
* Helper function to output an array of months.
*/
function _chargify_sites_months() {
// I left the number out of the t function, thinking that there's likely already
// translations for the months this way.
return array(
0 => t('Month'),
1 => '1 - ' . t('Jan'),
2 => '2 - ' . t('Feb'),
3 => '3 - ' . t('Mar'),
4 => '4 - ' . t('Apr'),
5 => '5 - ' . t('May'),
6 => '6 - ' . t('Jun'),
7 => '7 - ' . t('Jul'),
8 => '8 - ' . t('Aug'),
9 => '9 - ' . t('Sep'),
10 => '10 - ' . t('Oct'),
11 => '11 - ' . t('Nov'),
12 => '12 - ' . t('Dec'),
);
}
/**
* Helper function to output an array of years.
*/
function _chargify_sites_years() {
$this_year = date('Y', time());
$years = array(
0 => t('Year'),
$this_year => $this_year,
);
foreach (range(1, 10) as $i) {
$year = $this_year + $i;
$years[$year] = $year;
}
return $years;
}
/**
* Update a user with subscription data.
*/
function _chargify_sites_user_update(&$account, $subscription) {
// Update the user if necessary
$array = array('subscriptions' => array($subscription));
if (!isset($account->customer_id)) {
$customer = $subscription->getCustomer();
$array['customer_id'] = $customer->getId();
}
user_save($account, $array);
}
/**
* Creates a subscription for a node and user.
*/
function chargify_sites_subscription_create($values, $account, &$node) {
$success = TRUE;
$link = l($node->title, "node/{$node->nid}");
$json = _chargify_sites_subscription_create($values, $account);
if ($subscription = chargify_api_subscription_create($json, 'json')) {
// Check for errors.
if (isset($subscription->error)) {
$success = FALSE;
foreach($subscription->children() as $error) {
drupal_set_message(t((string) $error), 'error');
}
}
else {
// Set the subscription id on the node.
if ($sub_id = $subscription->getId()) {
$node->subscription->subscription_id = $sub_id;
}
// Make sure the subscription is Active or Trialing.
if ($subscription->getState() == CHARGIFY_ACTIVE || $subscription->getState() == CHARGIFY_TRIALING) {
$message = "Successful subscription for !node by <a href='!user_url'>%customer</a>.";
$vars = array(
'!node' => $link,
'!user_url' => url("user/{$account->uid}"),
'%customer' => $account->name,
);
watchdog('subscription', $message, $vars, WATCHDOG_INFO, $link);
drupal_set_message(t('Yay! You are now all paid up on !title.', array('!title' => $node->title)));
_chargify_sites_user_update($account, $subscription);
}
// Otherwise we have a subscription in some other state. Notify the user.
else {
$success = FALSE;
drupal_set_message(t('This subscription state is currently %state.', array('%state' => chargify_subscription_state($subscription->getState()))));
}
}
}
// Watchdog entry for non-success.
if (!$success) {
$message = "An error was detected during subscription for !node:<br /><pre>!error</pre>";
watchdog('subscription', $message, array('!node' => $link, '!error' => print_r($subscription, 1)), WATCHDOG_ERROR, $link);
}
return $success ? $subscription : FALSE;
}
/**
* Creates the subscription post data to send to Chargify.
*
* @param $values
* The submitted data.
* @param $account
* The user account that this subscription will be associated with.
* @return
* json data to post to chargify.
*/
function _chargify_sites_subscription_create($values, $account) {
$object = new stdClass();
$object->subscription->product_handle = $values['product_handle'];
if (isset($account->customer_id)) {
$object->subscription->customer_id = $account->customer_id;
}
else {
$object->subscription->customer_attributes->first_name = $values['first_name'];
$object->subscription->customer_attributes->last_name = $values['last_name'];
$object->subscription->customer_attributes->email = $account->mail;
$object->subscription->customer_attributes->reference = $account->uid;
}
$object->subscription->credit_card_attributes->full_number = $values['card_number'];
$object->subscription->credit_card_attributes->expiration_month = $values['expiration_month'];
$object->subscription->credit_card_attributes->expiration_year = $values['expiration_year'];
return json_encode($object);
}