How to Approve a User After a PayPal Payment

Would you like to approve a user after receiving a complete PayPal payment? Using a small PHP snippet you can easily do this and we’ll show you how! With the User Registration addon, you have the ability to let users register for your site with either automatic or manual approval. But if you were creating a membership site and didn’t want a user approved until after their successful PayPal payment has been processed, you’ll need a small code snippet for that and we’re going to show you how!

Creating your form

First, you’ll need to create your user registration form. If you need any assistance with this, please review this documentation.

Once you’ve created the form, click on the Settings tab inside the form builder and select User Registration. You’ll need to click the Enable User Activation and set the User Activation Method to Manual Approval.

set the user activation method to manual activation on the user registration tab in the form builder settings

Enabling PayPal to collect your membership fee

Next, you’ll need to add a Payment field to your form to specify what amount you are wanting to receive to activate the membership.

Once you’ve added the field, go to the Payments tab of the form builder and set up your PayPal settings. If you need further assistance with that, please see this documentation.

enable PayPal on your form

Setting up the PayPal IPN

For the purpose of this tutorial, we’re going to enable Instant Payment Notification (IPN) with PayPal to ensure that when PayPal returns the status of the payment a message will be sent to your site and the snippet will be triggered.

To learn more about Instant Payment Notifications, please take a look at PayPal’s documentation on the IPN.

Approving users after completed payment

Now it’s time to add the snippet that will pull this all together. If you need any help in adding snippets to your site, please see this tutorial.

/**
 * Approve user after PayPal payment status is Complete
 *
 * @link https://wpforms.com/developers/how-to-approve-a-user-after-a-paypal-payment/
 */

function wpf_dev_activate_user_after_paypal_complete( $fields, $form_data, $payment_id, $data ){
	
	// Add the field ID for the user's account email
	$email_field = 3;

	// Stop editing
	$user = get_user_by( 'email', $form_data[ $email_field ][ 'value' ] );
	
	delete_user_meta( $user->ID, 'wpforms-activate' );
	delete_user_meta( $user->ID, 'wpforms-pending' );
	delete_user_meta( $user->ID, 'wpforms-confirmation' );

	// Check if we need to assign new role.
	$role = get_user_meta( $user->ID, 'wpforms-role', true );

	if ( $role ) {

		wp_update_user(
			[
				'ID'   => $user->ID,
				'role' => $role,
			]
		);
		delete_user_meta( $user->ID, 'wpforms-role' );
	}

	wpforms_user_registration()->get( 'email_notifications' )->after_activation( $user->ID );

}

add_action( 'wpforms_paypal_standard_process_complete', 'wpf_activate_user_after_paypal_complete', 10, 4 );

The snippet above will only run on the form ID 5. If you would like to run this snippet on all user registration forms, you can just remove or comment out that block of the snippet.

The only other ID you’ll need to know is the field ID for the Email form field. In this tutorial, the field ID for our Email form field is 3.

update the snippet for the field ID that you are using for the email form field

If you need help in finding your field or form ID, please see this tutorial.

When a user submits a form, if the payment status returns anything other than Completed, the user will remain Unapproved.

this snippet will only approve user after paypal payment is marked as completed

And that’s all you need! Would you like to exclude failed PayPal payments from your Form Locker settings? Take a look at our tutorial on How to Exclude Failed Payments From PayPal Inside Form Locker Settings.

Would you like to create a coupon field on your form that needs to be validated before processing the discount? Check out our tutorial on How to Add Coupon Code Field Validation on Your Forms.

Reference Action

wpforms_paypal_standard_process_complete