Formidable PayPal

Knowledge BaseExtend Formidable ProAdd Ons → Formidable PayPal
  1. Download latest version at http://formidablepro.com/formidable-add-on-downloads/
    Important: Versions 2.0rc2+ require Formidable v1.6+
  2. In your WordPress admin, go to “Plugins” -> “Add New” and click the “Upload” link at the top of the page.
  3. Click the “Browse” button and select the zip file you just downloaded.
  4. Go the the “Plugins” page, find “Formidable PayPal” and click “Activate”.
  5. Go to “Formidable” -> “Settings” and click the “Payment” tab.
  6. Insert your PayPal address and update any other settings. PayPal settings
  7. Go to “Formidable” -> “Forms” and click “Settings” for the form you would like to use with PayPal.
  8. Click to the “Payment” tab on the form settings page. Payment options
  9. Check the box to Send users to PayPal after submitting this form.
  10. Choose an item name that will appear on the PayPal page and on the PayPal receipt.
  11. Select the “Amount field” from the field in your form. The selected field should contain the payment amount. This is the price that is sent to PayPal.
  12. Click the “Update” button at the bottom of the page.

Set up Instant Payment Notifications

In order for your payments to get correctly marked as paid, you must configure your PayPal account to send the payment notifications.

  1. Log into your PayPal account.
  2. Go to the “Profile” page and then “My Selling Tools”.
  3. Click the “Update” link next to “Instant Payment Notifications”.
  4. Edit and enter a notification URL. It doesn’t matter what URL you insert since Formidable PayPal will override it.
  5. Enable IPN Messages.
  6. Click Save.

These code examples can be added to your theme functions.php file or a new plugin. If you want to insert code into your theme functions.php, add it at the very end of the file, before the last ?> if there is one. If you plan to update your theme, be sure to put it in a new plugin so the code won’t be lost on update.

Automatically Publish a post after payment.

If your entry is set up to create WordPress posts, you can prevent the post from being created until payment is successful:

add_action('frm_payment_paypal_ipn', 'publish_paid_post');

function publish_paid_post($args){ if(!$args['pay_vars']['completed']) return; //don't publish if the payment was not completed

if(!$args['entry']->post_id) return; //don't publish if not linked to a post

wp_update_post(array('ID' => $args['entry']->post_id, 'post_status' => 'publish'));
}

If your form is not setup to create WordPress posts, add a field to your form to hold the payment status. For example, a drop-down or radio field with two options: Yes, and No. Set this as an “admin only” field, and select “no” as the default value. Then, you’ll need a little custom code in your theme functions.php or a new plugin to change the answer when the payment is completed. Something like this:

add_action('frm_payment_paypal_ipn', 'mark_as_complete');
function mark_as_complete($vars){
   global $frmdb, $wpdb;
   $wpdb->update($frmdb->entry_metas, array('meta_value' => 'Yes'), array('item_id' => $vars['payment']->item_id, 'field_id' => 25)); //change 25 to the ID of the completed field
}

Postpone email notification until after payment.

Stop the first email, and allow it to send if clicking the “Resend Email notification” link or if a payment was received:

add_filter('frm_to_email', 'stop_the_email', 20, 3 );
function stop_the_email($emails, $values, $form_id){
if($form_id == 5){ //change 5 to the ID of your form
   if(isset($_POST) and ((isset($_POST['payment_completed']) and $_POST['payment_completed']) or (isset($_POST['action']) and $_POST['action'] == 'send_email')))
      return $emails;
   else
      $emails = array();
}
return $emails;
}

//Trigger the email to send after a payment is completed:
add_action('frm_payment_paypal_ipn', 'send_email_now');
function send_email_now($vars){
   if(!$vars['pay_vars']['completed']) //only send the email if payment is completed
      return;

   global $frmdb, $frmpro_notification;
   $entry_id = $vars['payment']->item_id;
   $form_id = $frmdb->get_var($frmdb->entries, array('id' => $entry_id), 'form_id');
   $_POST['payment_completed'] = true; //to let the other function know to send the email
   $frmpro_notification->entry_created($entry_id, $form_id);
}

Change the user role after payment

The only change needed in the example is the user role the user should have: $new_role = ‘contributor’;

add_action('frm_payment_paypal_ipn', 'change_paid_user_role');

function change_paid_user_role($args){
    $new_role = 'contributor'; //change this to the role paid users should have
        
    if(!$args['pay_vars']['completed'])
       return; //don't continue if the payment was not completed

    if(!$args['entry']->user_id or !is_numeric($args['entry']->user_id))
       return; //don't continue if not linked to a user

    $user = get_userdata($args['entry']->user_id);
    if(!$user)
        return; //don't continue if user doesn't exist

    $updated_user = (array)$user;

    // Get the highest/primary role for this user  
    $user_roles = $user->roles;
    $user_role = array_shift($user_roles);
    if ( $user_role == 'administrator' ) 
        return; //make sure we don't downgrade any admins

    $updated_user['role'] = $new_role;
	
    wp_update_user($updated_user);
}

Change return URL

add_filter('formidable_paypal_url', 'formidable_paypal_url', 10, 3);
function formidable_paypal_url($paypal_url, $entry_id, $form_id){
  if($form_id == 5) //change 5 to the ID of your form
    $paypal_url .= '&return='. urlencode('http://example.com');
  return $paypal_url;
}