3

I am working on a project where users need to pay to use site.Site have a registration form where user can register and redirected to the payment page.All the resisted users are blocked until they make payment. The registration process working fine but, when user redirects to payment page there is no method to get resisted user id. How to get registered user id on payment page?

6
  • 2
    I believe your setup will have security issues if you need to provide user ids to (currently still) anonymous users. A better way imho would be to activate the user accounts but restrict access to other contents by using some access control module. The user may then be logged in (without access to the rest of the site) and the systems automatically has the correct user id.
    – Paul
    Commented Sep 16, 2014 at 7:06
  • global $user didn't help?
    – xurshid29
    Commented Sep 16, 2014 at 7:11
  • Thanks Paul.I will look into your solution as it seems more secure and proper way to handle this requirement. Can you please suggest me a method to control the access until user pay?
    – Swati
    Commented Sep 16, 2014 at 7:13
  • 1
    You can control access using roles, create a role who can access the sections and grant this role to user just after payment completion, you also can create a custom module to control access until users make payments.
    – Vikram
    Commented Sep 16, 2014 at 7:27
  • 1
    i am saving payement details in a custom table and redirecting user to a payment for if they have not made a payment yet. i have written a custom module for it.
    – Swati
    Commented Sep 16, 2014 at 7:29

2 Answers 2

1

This approach have two major issues.

  1. Security issue - Passing userid to page without login is not considered as a secure method.
  2. Spam registration - as it is clear from your requirement that user will need to fill the registration form first and then make payments to access site, but you mentioned that your need to pass user id to payment page it means user is already registered but is inactive (i guess), so now if users leave site without making payments he will be a users in database that should not exist ( if there is not other access area for these type of users )

Proper approach to handle this type of requirement

  1. Create registration form and submit it to payment form page (after validation).
  2. Grab all details submitted from registration form and hold them on payment form.
  3. Ask user for payment details and accepts payments. if successful use form values to save/register user and send them confirmation emails
  4. If payment failed, display message and ask for correct details.

Registration should be done with drupal hooks and functions but not be custom code. Make a module to handle payments ( validate payment form details and accept/manage payments)

With above approach users will be registered only if they have paid to access site.

4

The user registration form is generated by user_register_form(), and like normal Drupal forms, it can be altered via hook_form_alter or hook_form_FORM_ID_alter. When not explicitely set, the submit handler of a form is named form_id_submit(), however, in user_register_form, a submit handler is added to the #submit property of the form - user_register_submit().

In the function user_register_submit, you'll notice the following codes:

...
$form_state['user'] = $account;
$form_state['values']['uid'] = $account->uid;
...

which saves the new user object and ID in $form_state.

To access this after the registration form is submitted, simply add a custom submit handler via hook_form_alter() or hook_form_FORM_ID_alter(). Example:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters the user registration form by adding a custom submit handler.
 */
function foo_form_user_register_form_alter(&$form, &$form_state) {
  $form['actions']['submit']['#submit'][] 'foo_user_register_form_submit';
}

/**
 * Custom submit handler for the user registration form.
 */
function foo_user_register_form_submit($form, &$form_state) {
  $account = $form_state['user'];

  $uid = $form_state['values']['uid'];

  // Do something meaningful
}
2
  • how can he get that uid after the redirection?
    – xurshid29
    Commented Sep 16, 2014 at 7:41
  • That would depend on what code is in the custom submit handler. If the user is going to be forced blocked and hence not logged in, you won't be able to source $user obviously. What you can do is find other ways to preserve data (e.g. via query string, session, or cookies), for instance you can $_SESSION['foo_uid'] = $uid or immediately update the order and set its uid.
    – angheloko
    Commented Sep 16, 2014 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.