Changing Stripe Billing Information

Changing Stripe Billing Information

PrerequisitesVersion 3.4+NotesOlder Versions

This snippet will allow you to change a customer』s billing information that has been stored within Stripe, using a form.
The current snippet is based on the original one provided to us by one of our customers, Shamai Greenfield.
Prerequisites

IMPORTANT: The user must be logged in before submitting any of the forms described below. Otherwise there』s no way to identify the user and associate WordPress and Stripe.com id』s.

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. It creates the customer and the gform_stripe_customer_after_create snippet saves the customer id to the user meta.
Form B will be an additional form that you can use for updating the subscription. It』s used to update the billing info. This would have a product and services feed, and hidden product with at least $0.50. On submission the gform_stripe_customer_id code snippet gets the customer id from the user meta, this will be used to get the customer object which is then used to create the charge object, updating the billing info. The gform_stripe_charge_authorization_only code snippet is then used to prevent the charge being captured.
Version 3.4+
Notes

Payment method must be set to Stripe Credit Card Field.
Due to changes done by Stripe.com to the payment intents API, the card will be saved but not set as default payment method.

// 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 ) {
gf_stripe()->log_debug( __METHOD__ . '(): Running...' );
if ( is_user_logged_in () ) {
$user_id = get_current_user_id();
gf_stripe()->log_debug( __METHOD__ . '(): Adding customer id ' . $customer->id . ' to user ' . $user_id );
update_user_meta( $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 ) {
gf_stripe()->log_debug( __METHOD__ . '(): Running...' );
if ( is_user_logged_in () && get_user_meta( get_current_user_id(), 'stripe_customer_id', true ) != ''){
$user_id = get_current_user_id();
gf_stripe()->log_debug( __METHOD__ . '(): Getting customer id from user ' . $user_id );
$customer_id = get_user_meta( $user_id, 'stripe_customer_id', true );
gf_stripe()->log_debug( __METHOD__ . '(): Customer id set to: ' . $customer_id );
}
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 ) {
gf_stripe()->log_debug( __METHOD__ . '(): Running...' );
$feed_name = rgars( $feed, 'meta/feedName' );
// The name associated with the Stripe feed.
if ( $feed_name == 'Update Billing' ) {
gf_stripe()->log_debug( __METHOD__ . '(): Authorization only for Update Billing feed.' );
return true;
}
return $authorization_only;
}

add_filter( 'gform_stripe_charge_pre_create', function( $charge_meta, $feed, $submission_data, $form, $entry ) {
$feed_name = rgars( $feed, 'meta/feedName' );
// The name associated with the Stripe feed.
if ( $feed_name == 'Update Billing' ) {
gf_stripe()->log_debug( 'gform_stripe_charge_pre_create: running for feed ' . rgars( $feed, 'meta/feedName' ) );
$charge_meta['setup_future_usage'] = 'off_session';
}

return $charge_meta;
}, 10, 5 );

Older Versions
// 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 ) {
GFCommon::log_debug( __METHOD__ . '(): Running...' );
if ( is_user_logged_in () ) {
$user_id = get_current_user_id();
GFCommon::log_debug( __METHOD__ . '(): Adding customer id ' . $customer->id . ' to user ' . $user_id );
update_user_meta( $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 ) {
GFCommon::log_debug( __METHOD__ . '(): Running...' );
if ( is_user_logged_in () && get_user_meta( get_current_user_id(), 'stripe_customer_id', true ) != ''){
$user_id = get_current_user_id();
GFCommon::log_debug( __METHOD__ . '(): Getting customer id from user ' . $user_id );
$customer_id = get_user_meta( $user_id, 'stripe_customer_id', true );
GFCommon::log_debug( __METHOD__ . '(): Customer id set to: ' . $customer_id );
}
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 ) {
GFCommon::log_debug( __METHOD__ . '(): Running...' );
$feed_name = rgars( $feed, 'meta/feedName' );
// The name associated with the Stripe feed.
if ( $feed_name == 'Update Billing' ) {
GFCommon::log_debug( __METHOD__ . '(): Authorization only for Update Billing feed.' );
return true;
}
return $authorization_only;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注