gform_stripe_customer_id

gform_stripe_customer_id

DescriptionUsageParametersExamples1. ID from user meta2. Create new customerPlacementSinceSource Code

Description
The gform_stripe_customer_id filter can be used to specify an existing customer ID to be used when processing the submission.
Usage
The hook which would run for all Stripe feeds can be used like so:
add_filter( 'gform_stripe_customer_id', 'your_function_name', 10, 4 );

Parameters

$customer_id string
The Stripe customer id. Defaults to an empty string causing a new customer to be created when processing the subscription feed.

$feed Feed Object
The Feed which is currently being processed.

$entry Entry Object
The Entry which is currently being processed.

$form Form Object
The Form which is currently being processed.

Examples
1. ID from user meta
The following example shows how, if the user is logged in, you can retrieve the customer id from the user meta. See the gform_stripe_customer_after_create hook for an example showing how the customer id can be saved to the user meta.
add_filter( 'gform_stripe_customer_id', 'get_stripe_customer_id' );
function get_stripe_customer_id( $customer_id ) {
if ( is_user_logged_in () ) {
$customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true );
}

return $customer_id;
}

2. Create new customer
See the Stripe API documentation for creating a customer for the supported parameters.
add_filter( 'gform_stripe_customer_id', 'create_stripe_customer', 10, 4 );
function create_stripe_customer( $customer_id, $feed, $entry, $form ) {
if ( empty( $customer_id ) ) {
$customer_params = array();

if ( rgars( $feed, 'meta/transactionType' ) === 'subscription' ) {
$customer_params['email'] = gf_stripe()->get_field_value( $form, $entry, rgar( $feed['meta'], 'customerInformation_email' ) );
} else {
$email_field = rgars( $feed, 'meta/receipt_field' );
if ( ! empty( $email_field ) && strtolower( $email_field ) !== 'do not send receipt' ) {
$customer_params['email'] = gf_stripe()->get_field_value( $form, $entry, $email_field );
}
}

$customer = gf_stripe()->create_customer( $customer_params, $feed, $entry, $form );
$customer_id = $customer->id;
gf_stripe()->log_debug( 'gform_stripe_customer_id: Returning Customer ID: ' . $customer_id );
}

return $customer_id;
}

Placement
Your code snippet should be placed in the functions.php file of your active theme.
Since
This hook was added in Stripe version 2.1.
Source Code
$customer_id = apply_filters( 'gform_stripe_customer_id', $customer_id, $feed, $entry, $form );

This hook is located in GFStripe::get_customer() in class-gf-stripe.php.

发表回复

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