To enable Stripe subscription payments in WP Forms, you can use the wpsnippets_wpforms_stripe_subscription
function. This function allows you to integrate Stripe with WP Forms and set up recurring subscription payments for your users.
Here’s an example of how you can use this function:
function wpsnippets_wpforms_stripe_subscription( $form_data, $entry, $payment_total ) {
// Get the Stripe API keys from your Stripe account
$stripe_secret_key = 'your_stripe_secret_key';
$stripe_publishable_key = 'your_stripe_publishable_key';
// Include the Stripe PHP library
require_once( 'path/to/stripe-php/init.php' );
// Set up the Stripe API keys
StripeStripe::setApiKey( $stripe_secret_key );
// Create a new customer in Stripe
$customer = StripeCustomer::create( array(
'email' => $entry['email'], // Get the customer's email from the form entry
'source' => $entry['stripeToken'], // Get the Stripe token from the form entry
) );
// Create a new subscription in Stripe
$subscription = StripeSubscription::create( array(
'customer' => $customer->id,
'items' => array(
array(
'plan' => 'your_stripe_plan_id', // Replace with your Stripe plan ID
),
),
) );
// Update the form entry with the subscription ID
$entry['subscription_id'] = $subscription->id;
return $entry;
}
add_filter( 'wpforms_stripe_subscription_charge', 'wpsnippets_wpforms_stripe_subscription', 10, 3 );
In this code snippet, we first retrieve the Stripe API keys from your Stripe account. Then, we include the Stripe PHP library and set up the API keys. Next, we create a new customer in Stripe using the customer’s email and Stripe token from the form entry. After that, we create a new subscription in Stripe using the customer ID and the Stripe plan ID. Finally, we update the form entry with the subscription ID and return the modified entry.
You can customize this code snippet by replacing 'your_stripe_secret_key'
, 'your_stripe_publishable_key'
, and 'your_stripe_plan_id'
with your actual Stripe API keys and plan ID.
This code snippet can be useful when you want to set up recurring subscription payments using WP Forms and Stripe. It allows you to seamlessly integrate Stripe with your forms and automate the subscription creation process for your users.
Examples
Example 1: Creating a Stripe Subscription Payment Form with WP Forms
This example demonstrates how to create a subscription payment form using WP Forms and Stripe. The code example shows how to integrate Stripe with WP Forms and set up a subscription payment option.
function wpsnippets_stripe_subscription_payment_form() {
// Create a new form using WP Forms
$form_id = 1;
// Get the form object
$form = wpforms()->form->get( $form_id );
// Set up Stripe settings
$stripe_settings = array(
'secret_key' => 'your_stripe_secret_key',
'publishable_key' => 'your_stripe_publishable_key',
);
// Set up Stripe subscription settings
$subscription_settings = array(
'plan_id' => 'your_stripe_plan_id',
'plan_name' => 'Your Plan Name',
'plan_amount' => 9.99,
'plan_interval' => 'month',
);
// Add Stripe subscription settings to the form
$form->settings['stripe_subscription'] = $subscription_settings;
// Add Stripe settings to the form
$form->settings['stripe'] = $stripe_settings;
// Update the form
wpforms()->form->update( $form );
}
add_action( 'wpforms_process_complete', 'wpsnippets_stripe_subscription_payment_form' );
In this code example, we create a function wpsnippets_stripe_subscription_payment_form
that is hooked to the wpforms_process_complete
action. Inside the function, we retrieve the form object using the form ID. Then, we set up the Stripe and subscription settings, including the Stripe secret and publishable keys, the plan ID, plan name, plan amount, and plan interval. Finally, we add the Stripe and subscription settings to the form and update it.
Example 2: Handling Stripe Subscription Payment Confirmation
This example demonstrates how to handle the confirmation of a Stripe subscription payment made through WP Forms. The code example shows how to retrieve the subscription details and update the user’s membership status.
function wpsnippets_handle_stripe_subscription_payment( $entry, $form_data ) {
// Get the subscription details
$subscription_id = $entry['subscription_id'];
$subscription_status = $entry['subscription_status'];
// Update the user's membership status based on the subscription status
if ( $subscription_status === 'active' ) {
// Update user's membership status to active
wpsnippets_update_membership_status( $entry['user_id'], 'active' );
} elseif ( $subscription_status === 'canceled' ) {
// Update user's membership status to canceled
wpsnippets_update_membership_status( $entry['user_id'], 'canceled' );
}
}
add_action( 'wpforms_stripe_subscription_payment', 'wpsnippets_handle_stripe_subscription_payment', 10, 2 );
In this code example, we create a function wpsnippets_handle_stripe_subscription_payment
that is hooked to the wpforms_stripe_subscription_payment
action. The function receives the entry and form data as parameters. We retrieve the subscription ID and status from the entry array. Based on the subscription status, we update the user’s membership status using the wpsnippets_update_membership_status
function.
Example 3: Customizing Stripe Subscription Payment Confirmation Email
This example demonstrates how to customize the email sent to users after a successful Stripe subscription payment. The code example shows how to modify the email subject and content.
function wpsnippets_customize_stripe_subscription_payment_email( $email, $entry, $form_data ) {
// Modify the email subject
$email['subject'] = 'Your Subscription Payment Confirmation';
// Modify the email content
$email['message'] = 'Thank you for subscribing!';
return $email;
}
add_filter( 'wpforms_stripe_subscription_payment_email', 'wpsnippets_customize_stripe_subscription_payment_email', 10, 3 );
In this code example, we create a function wpsnippets_customize_stripe_subscription_payment_email
that is hooked to the wpforms_stripe_subscription_payment_email
filter. The function receives the email, entry, and form data as parameters. We modify the email subject and content by updating the corresponding array keys. Finally, we return the modified email.