gform_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.

Note: This filter runs before saving the entry to the database. Therefore some entry properties like 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 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 Form Object

    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. Note: This can’t be done with Stripe Checkout as 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' ) { // Update this line to your feed name
        return;
    }
  
    // get the currency code for this entry.
    $currency = rgar( $entry, 'currency' );
  
    // get the amount from a Number field set to currency with id 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 ); // Update 5 to your field id
  
    $item = array(
        'amount'      => $amount,
        'currency'    => $currency,
        'description' => 'Some fee', // Update this to put your description for the charge
    );
  
    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() ) {
        $user_id = get_current_user_id();
        update_user_meta( $user_id, '_stripe_customer_id', $customer->id );
        gf_stripe()->log_debug( __METHOD__ . "(): Added Stripe Customer ID {$customer->id} to WP user ID {$user_id}" );
    }
}

3. Update Stripe.com customer data

The following example shows how to update the ‘name’ in the Stripe.com customer data taking as source the values in a name field of your form. You can find additional parameters in the Stripe API Reference. This snippet will work only when 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 ) {
  
    // Run only for form id 3
    if ( $form['id'] != 3 ) {
        return;
    }
  
    gf_stripe()->log_debug( __METHOD__ . '(): Running' );
  
    // First Name and Last Name from field id 1 joined into one string
    $name = rgar( $entry, '1.3' ) . ' ' . rgar( $entry, '1.6' );
  
    // Array containing the data you want to update
    $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 ( ! empty( $country ) ) {
        $code = GF_Fields::get( 'address' )->get_country_code( $country );
        if ( ! empty( $code ) ) {
            $customer_data['address']['country'] = $code;
        }
    }
  
    $result = $customer->update( $customer->id, $customer_data );
  
    gf_stripe()->log_debug( __METHOD__ . '(): Result => ' . print_r( $result, 1 ) );
  
}

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.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.