bookmark_borderChanging Stripe Billing Information

This snippet will allow you to change a customer’s billing information that has been stored within Stripe, using a form.

This snippet was provided to us by one of our customers, Shamai Greenfield.

Prerequisites

To use this snippet, you will need two forms. We’ll call them Form A and Form B.

Form A will contain your standard Stripe subscription form.
Form B will be an additional form that you can use for updating the subscription.

Code

// Save the users' Stripe customer ID after a payment is made via a subscription Stripe feed.
add_action( 'gform_stripe_customer_after_create', 'save_stripe_customer_id' );
function save_stripe_customer_id( $customer ) {
    if ( is_user_logged_in () ) {
        update_user_meta( get_current_user_id(), 'stripe_customer_id', $customer->id );
    }
}

// Gets the current Stripe customer ID to change the billing details on. Tests if the user has an ID and is signed in.
add_filter( 'gform_stripe_customer_id', 'get_stripe_customer_id' );
function get_stripe_customer_id( $customer_id ) {
    if ( is_user_logged_in () &&  get_user_meta( get_current_user_id(), 'stripe_customer_id', true ) != ''){
        $customer_id = get_user_meta( get_current_user_id(), 'stripe_customer_id', true );
    }
    return $customer_id;
}

// Make a form that has a Stripe Product and Services feed, and make sure no payment is made.
add_filter( 'gform_stripe_charge_authorization_only', 'stripe_charge_authorization_only', 10, 2 );
function stripe_charge_authorization_only( $authorization_only, $feed ) {
    $feed_name  = rgars( $feed, 'meta/feedName' );
    // The name associated with the Stripe feed.
    if ( $feed_name == 'Update Billing' ) {
        return true;
    }
    return $authorization_only;
}