Description
The gform_stripe_customer_after_create filter can be used to perform custom actions between the customer being created and subscribed to the plan.
Note: This filter runs before saving the entry to the database. Therefore, some entry properties such as id or date_created are not available for use with this filter.
Usage
The hook, which would run for all ‘subscription’ type Stripe feeds can be used:
add_action( 'gform_stripe_customer_after_create', 'your_function_name', 10, 4 );
Parameters
| Parameter | Type | Description |
|---|---|---|
| $customer | Stripe Customer Object | The Stripe Customer object that was just created. |
| $feed | Feed Object | The Stripe feed currently being processed. |
| $entry | Entry Object | The entry currently being processed (not yet saved). |
| $form | Form Object | The form currently being processed. |
Examples
Create an invoice item.
The following example demonstrates how to add a charge to the customer’s upcoming invoice. See the Stripe API Reference for more details. Note: This can’t be done with Stripe Checkout as a payment collection method.
add_action( 'gform_stripe_customer_after_create', 'add_invoice_item', 10, 4 );
function add_invoice_item( $customer, $feed, $entry, $form ) {
$feed_name = rgars( $feed, 'meta/feedName' );
if ( $feed_name !== 'feed name goes here' ) {
return;
}
$currency = rgar( $entry, 'currency' );
$amount = gf_stripe()->get_amount_export( rgar( $entry, '5' ), $currency ); // Update field ID.
$item = array(
'amount' => $amount,
'currency' => $currency,
'description' => 'Some fee',
);
gf_stripe()->log_debug( 'gform_stripe_customer_after_create: Invoice item => ' . print_r( $item, true ) );
$result = $customer->addInvoiceItem( $item );
gf_stripe()->log_debug( 'gform_stripe_customer_after_create: Result => ' . print_r( $result, true ) );
}
Save the customer ID.
The following example shows how, if the user is logged in, you can save the customer id in the user meta.
add_action( 'gform_stripe_customer_after_create', 'save_stripe_customer_id' );
function save_stripe_customer_id( $customer ) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
update_user_meta( $user_id, '_stripe_customer_id', $customer->id );
gf_stripe()->log_debug( __METHOD__ . "(): Saved Stripe Customer ID {$customer->id} to user {$user_id}" );
}
}
Update Stripe.com customer data.
The following example demonstrates how to update the ‘name’ in Stripe.com customer data, using the values from a name field in your form as the source. You can find additional parameters in the Stripe API Reference. This snippet will only work when the Stripe Field is set as the payment collection mode.
add_action( 'gform_stripe_customer_after_create', 'update_customer_data', 10, 4 );
function update_customer_data( $customer, $feed, $entry, $form ) {
if ( (int) $form['id'] !== 3 ) {
return;
}
gf_stripe()->log_debug( __METHOD__ . '(): Running' );
$name = rgar( $entry, '1.3' ) . ' ' . rgar( $entry, '1.6' );
$customer_data = array(
'name' => $name,
'address' => array(
'line1' => gf_stripe()->get_field_value( $form, $entry, rgars( $feed, 'meta/billingInformation_address_line1' ) ),
'line2' => gf_stripe()->get_field_value( $form, $entry, rgars( $feed, 'meta/billingInformation_address_line2' ) ),
'city' => gf_stripe()->get_field_value( $form, $entry, rgars( $feed, 'meta/billingInformation_address_city' ) ),
'state' => gf_stripe()->get_field_value( $form, $entry, rgars( $feed, 'meta/billingInformation_address_state' ) ),
'postal_code' => gf_stripe()->get_field_value( $form, $entry, rgars( $feed, 'meta/billingInformation_address_zip' ) ),
),
);
$country = gf_stripe()->get_field_value( $form, $entry, rgars( $feed, 'meta/billingInformation_address_country' ) );
if ( $country ) {
$code = GF_Fields::get( 'address' )->get_country_code( $country );
if ( $code ) {
$customer_data['address']['country'] = $code;
}
}
$result = $customer->update( $customer->id, $customer_data );
gf_stripe()->log_debug( __METHOD__ . '(): Result => ' . print_r( $result, true ) );
}
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Since
This hook was added in Stripe version 2.0.1.
Source Code
do_action( 'gform_stripe_customer_after_create', $customer, $feed, $entry, $form );
This hook is located in GFStripe::create_customer() in class-gf-stripe.php.