Last updated on October 18, 2023

WP Forms Stripe integration

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

Integrate Stripe payments with WP Forms.

To integrate WP Forms with Stripe, you can use the wpforms_stripe_charge hook to add custom code that will handle the Stripe payment processing. This hook is triggered when a form submission is processed and allows you to interact with the Stripe API.

Here’s an example code snippet that demonstrates how to integrate WP Forms with Stripe:

/**
 * Process Stripe payment for WP Forms submission.
 *
 * @param array $fields Submitted form fields.
 * @param array $entry  WP Forms entry data.
 */
function wpsnippets_process_stripe_payment( $fields, $entry ) {
    // Get the Stripe API key from your WP Forms settings.
    $stripe_api_key = get_option( 'wpforms_stripe_api_key' );

    // Set up the Stripe API client.
    StripeStripe::setApiKey( $stripe_api_key );

    // Create a new Stripe charge.
    $charge = StripeCharge::create( array(
        'amount'      => 1000, // Amount in cents.
        'currency'    => 'usd',
        'description' => 'WP Forms Stripe Integration',
        'source'      => $fields['stripeToken'], // Stripe token from the form submission.
    ) );

    // Check if the charge was successful.
    if ( $charge->status === 'succeeded' ) {
        // Payment was successful, update the form entry status.
        wpforms()->entry->update( $entry['id'], array( 'status' => 'complete' ) );
    } else {
        // Payment failed, update the form entry status.
        wpforms()->entry->update( $entry['id'], array( 'status' => 'spam' ) );
    }
}
add_action( 'wpforms_stripe_charge', 'wpsnippets_process_stripe_payment', 10, 2 );

In this code snippet, we define a custom function wpsnippets_process_stripe_payment that is hooked to the wpforms_stripe_charge action. This function handles the Stripe payment processing for WP Forms submissions.

Inside the function, we first retrieve the Stripe API key from the WP Forms settings using the get_option function. Then, we set up the Stripe API client by calling StripeStripe::setApiKey with the API key.

Next, we create a new Stripe charge using the StripeCharge::create method. We pass in the amount, currency, description, and the Stripe token from the form submission as parameters.

After the charge is created, we check if it was successful by examining the status property of the charge object. If the status is ‘succeeded’, we update the form entry status to ‘complete’ using the wpforms()->entry->update method. Otherwise, if the payment failed, we update the form entry status to ‘spam’.

This code snippet can be useful when you want to process Stripe payments for WP Forms submissions. It allows you to customize the payment processing logic and handle the payment status based on the response from the Stripe API.

Examples

Example 1: Stripe Payment Integration with WP Forms

This use case demonstrates how to integrate Stripe payment gateway with WP Forms plugin to enable online payments for form submissions.

/**
 * Process Stripe payment for WP Forms submission.
 *
 * @param array $fields The form fields.
 * @param int   $entry_id The form entry ID.
 */
function wpsnippets_process_stripe_payment( $fields, $entry_id ) {
    // Get the Stripe API keys
    $stripe_secret_key = get_option( 'stripe_secret_key' );

    // Set up the Stripe API
    StripeStripe::setApiKey( $stripe_secret_key );

    // Create a new Stripe customer
    $customer = StripeCustomer::create( array(
        'email' => $fields['email'],
        'source' => $fields['stripeToken']
    ) );

    // Charge the customer
    $charge = StripeCharge::create( array(
        'customer' => $customer->id,
        'amount' => $fields['amount'] * 100,
        'currency' => 'usd',
        'description' => 'Payment for WP Forms submission'
    ) );

    // Update the form entry with payment details
    wp_update_post( array(
        'ID' => $entry_id,
        'post_title' => 'Payment received',
        'post_status' => 'publish'
    ) );
}

This code example shows a custom PHP function wpsnippets_process_stripe_payment() that processes a Stripe payment for a WP Forms submission. It retrieves the Stripe API keys, sets up the Stripe API, creates a new Stripe customer, charges the customer, and updates the form entry with payment details.

Example 2: Add Stripe Payment Field to WP Forms

This use case demonstrates how to add a Stripe payment field to a WP Forms form, allowing users to make payments directly on the form.

/**
 * Add Stripe payment field to WP Forms form.
 *
 * @param array $fields The form fields.
 * @param int   $form_id The form ID.
 */
function wpsnippets_add_stripe_payment_field( $fields, $form_id ) {
    // Add a new field for Stripe payment
    $fields[] = array(
        'type' => 'stripe',
        'label' => 'Payment',
        'stripe' => array(
            'api_key' => get_option( 'stripe_publishable_key' ),
            'amount' => 1000,
            'currency' => 'usd'
        )
    );

    return $fields;
}

This code example shows a custom PHP function wpsnippets_add_stripe_payment_field() that adds a Stripe payment field to a WP Forms form. It adds a new field of type ‘stripe’ with a label and Stripe configuration options such as the API key, amount, and currency. The function returns the updated form fields array.

Example 3: Customize Stripe Payment Confirmation Message

This use case demonstrates how to customize the Stripe payment confirmation message displayed to users after a successful payment.

/**
 * Customize Stripe payment confirmation message.
 *
 * @param string $message The default confirmation message.
 * @param int    $entry_id The form entry ID.
 * @param array  $fields The form fields.
 */
function wpsnippets_customize_stripe_payment_confirmation_message( $message, $entry_id, $fields ) {
    $payment_amount = $fields['amount'];
    $payment_currency = 'USD';

    $message = "Thank you for your payment of {$payment_amount} {$payment_currency}!";

    return $message;
}

This code example shows a custom PHP function wpsnippets_customize_stripe_payment_confirmation_message() that customizes the Stripe payment confirmation message. It receives the default confirmation message, form entry ID, and form fields as parameters. The function sets the payment amount and currency variables, and then updates the confirmation message with a personalized thank you message. The function returns the customized message.

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

Leave a Reply

Your email address will not be published. Required fields are marked *