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?
2 Answers
This approach have two major issues.
- Security issue - Passing userid to page without login is not considered as a secure method.
- 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
- Create registration form and submit it to payment form page (after validation).
- Grab all details submitted from registration form and hold them on payment form.
- Ask user for payment details and accepts payments. if successful use form values to save/register user and send them confirmation emails
- 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.
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
}
-
-
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. Commented Sep 16, 2014 at 8:12
global $user
didn't help?