bookmark_bordergform_stripe_customer_after_create

Description

The gform_stripe_customer_after_create hook can be used to perform custom actions between the customer being created and subscribed to the plan.

Usage

The hook which would run for all ‘subscription’ type Stripe feeds can be used like so:

add_action( 'gform_stripe_customer_after_create', 'your_function_name', 10, 4 );

Parameters

  • $customer Stripe Customer Object

    The Stripe Customer Object containing the customer properties.

  • $feed Feed Object

    The Feed which is currently being processed.

  • $entry Entry Object

    The Entry which is currently being processed.

  • $form

    The Form which is currently being processed.

Examples

1. Create invoice item

The following example shows how you can add a charge to the customers upcoming invoice. See the Stripe API Reference for more details.

add_action( 'gform_stripe_customer_after_create', 'add_invoice_item', 10, 4 );
function add_invoice_item( $customer, $feed, $entry, $form ) {
	// get the currency code for this entry.
	$currency = rgar( $entry, 'currency' );

	// get the amount from field 5 and convert it to the smallest unit required by Stripe for the currency being used.
	$amount = gf_stripe()->get_amount_export( rgar( $entry, '5' ), $currency );

	$item = array(
		'amount'      => $amount,
		'currency'    => $currency,
		'description' => 'Some fee',
	);

	gf_stripe()->log_debug( 'gform_stripe_customer_after_create: Invoice item to be added => ' . print_r( $item, 1 ) );

	$result = $customer->addInvoiceItem( $item );

	gf_stripe()->log_debug( 'gform_stripe_customer_after_create: Result => ' . print_r( $result, 1 ) );

}

2. 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 () ) {
		update_user_meta( get_current_user_id(), '_stripe_customer_id', $customer->id );
	}
}

Placement

Your code snippet should be placed in the functions.php file of your active theme.

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

Since

This hook was added in Stripe version 2.0.1.