Last updated on October 18, 2023

WP Forms PayPal recurring payments

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Enable recurring payments with PayPal in WP Forms.

To enable recurring payments in WP Forms using PayPal, you can utilize the PayPal recurring payments API and integrate it with your WordPress site. This functionality can be useful for businesses or organizations that offer subscription-based services or products and want to automate the payment process for their customers.

To achieve this functionality, you can follow these steps:

  1. Install and activate the WP Forms plugin on your WordPress site.
  2. Create a form using WP Forms that includes the necessary fields for collecting payment information from your users.
  3. Set up a PayPal business account if you don’t have one already. This will allow you to receive recurring payments.
  4. Generate API credentials (API username, password, and signature) from your PayPal account. These credentials will be used to authenticate your requests to the PayPal API.
  5. Add the following code snippet to your theme’s functions.php file or in a custom plugin file:
function wpsnippets_wpforms_paypal_recurring_payment( $args, $form_data ) {
    // Set up PayPal recurring payment parameters
    $args['cmd'] = '_xclick-subscriptions';
    $args['business'] = 'your_paypal_email@example.com';
    $args['item_name'] = 'Your Subscription Product';
    $args['a3'] = '10.00'; // Subscription amount
    $args['p3'] = '1'; // Subscription duration (1 month)
    $args['t3'] = 'M'; // Subscription duration unit (M = month)
    $args['src'] = '1'; // Recurring payment
    $args['sra'] = '1'; // Reattempt on failure

    return $args;
}
add_filter( 'wpforms_paypal_args', 'wpsnippets_wpforms_paypal_recurring_payment', 10, 2 );

Make sure to replace 'your_paypal_email@example.com' with your actual PayPal email address and adjust the subscription amount and duration according to your needs.

This code snippet hooks into the wpforms_paypal_args filter provided by WP Forms. It modifies the arguments passed to PayPal when processing the payment. The wpsnippets_wpforms_paypal_recurring_payment function sets up the necessary parameters for a recurring payment, including the subscription amount, duration, and other details.

By adding this code snippet, WP Forms will send the appropriate parameters to PayPal when a user submits the form, enabling recurring payments for your subscription product.

Remember to test the integration thoroughly to ensure that the recurring payments are processed correctly and that the subscription details are accurately reflected in your PayPal account.

Examples

Example 1: Creating a WP Forms PayPal recurring payment form

This use case demonstrates how to create a WP Forms form with PayPal recurring payments enabled. The code example shows how to add the necessary code to the form’s template file.

<?php
// Add the PayPal recurring payment code to the form template
function wpsnippets_wpforms_paypal_recurring_payment( $form_data, $form_id ) {
    // Check if the form has PayPal recurring payment enabled
    if ( wpforms_has_paypal_recurring( $form_id ) ) {
        // Add the PayPal recurring payment code to the form template
        $form_data['settings']['paypal_recurring'] = true;
    }
    return $form_data;
}
add_filter( 'wpforms_frontend_form_data', 'wpsnippets_wpforms_paypal_recurring_payment', 10, 2 );

This code snippet adds the necessary code to the WP Forms form template to enable PayPal recurring payments. It checks if the form has PayPal recurring payment enabled using the wpforms_has_paypal_recurring() function and sets the paypal_recurring setting to true in the form data if it is enabled.

Example 2: Modifying the PayPal recurring payment amount

This use case demonstrates how to modify the PayPal recurring payment amount dynamically based on user input. The code example shows how to use a custom PHP function to calculate the new amount.

<?php
// Modify the PayPal recurring payment amount
function wpsnippets_modify_paypal_recurring_amount( $amount, $form_data ) {
    // Calculate the new amount based on user input
    $new_amount = $amount * 2;
    return $new_amount;
}
add_filter( 'wpforms_paypal_recurring_amount', 'wpsnippets_modify_paypal_recurring_amount', 10, 2 );

This code snippet modifies the PayPal recurring payment amount by using a custom PHP function. The function takes the original amount and the form data as parameters, and calculates a new amount based on user input. The new amount is then returned and used for the PayPal recurring payment.

Example 3: Handling PayPal recurring payment IPN

This use case demonstrates how to handle the PayPal recurring payment Instant Payment Notification (IPN) to update the user’s subscription status. The code example shows how to use a custom PHP function to process the IPN data.

<?php
// Handle PayPal recurring payment IPN
function wpsnippets_handle_paypal_recurring_ipn() {
    // Get the IPN data
    $ipn_data = $_POST;

    // Process the IPN data and update the user's subscription status
    // ...

    // Send a response back to PayPal
    header( 'HTTP/1.1 200 OK' );
    exit;
}
add_action( 'wpforms_paypal_recurring_ipn', 'wpsnippets_handle_paypal_recurring_ipn' );

This code snippet handles the PayPal recurring payment Instant Payment Notification (IPN) by using a custom PHP function. The function retrieves the IPN data from the $_POST superglobal, processes the data to update the user’s subscription status, and sends a response back to PayPal.

Last updated on October 18, 2023. Originally posted on November 30, 2023.

Leave a Reply