gform_authorizenet_subscription_pre_create

Description

This filter can be used to modify the subscription object before it is sent to Authorize.net.

Usage

The filter which would run for all ‘subscription’ type Authorize.net feeds can be used like so:

add_filter( 'gform_authorizenet_subscription_pre_create', 'your_function_name', 10, 5 );

Parameters

  • $subscription object

    The Authorize.net subscription object.

  • $form_data Form Data

    An associative array containing the form title, billing address, payment amount, setup fee amount, line items created using the submitted pricing field values and any discounts from coupons.

  • $config Authorize Net Config

    The feed which is currently being processed.

  • $form Form Object

    The form which is currently being processed.

  • $entry Entry Object

    The entry which is currently being processed. Since version 2.1.8.

Examples

1. Change startDate

The following example shows how you can set the start date. Please note Authorize.net requires the startDate be in the YYYY-MM-DD format.

add_filter( 'gform_authorizenet_subscription_pre_create', function ( $subscription, $form_data, $config, $form, $entry ) {
    if ( $form['id'] != 30 ) { // Update 30 to your form id number
        return $subscription;
    }   
 
    $subscription->startDate = rgar( $entry, '10' );
 
    return $subscription;
}, 10, 5 );

2. Change Trial Period Cycles

The following example shows how you can set the Trial Period Cycles. Please check the following doc page from Authorize.net to understand how they handle Trial periods: What is a Trial Period?

add_filter( 'gform_authorizenet_subscription_pre_create', function ( $subscription, $form_data, $config, $form, $entry ) {
    if ( $form['id'] != 30 ) { // Update 30 to your form id number
        return $subscription;
    }   
 
    $subscription->trialOccurrences = 15; // This will set a 15 days trial period if Billing Cycle is using Days
    return $subscription;
}, 10, 5 );

3. Add a Description

The following example shows how you can set a description (up to 255 characters) for the subscription being created in a form with id 30. According to Authorize.net API documentation the description will be associated with each payment in the subscription.

add_filter( 'gform_authorizenet_subscription_pre_create', function ( $subscription, $form_data, $config, $form, $entry ) {
    if ( $form['id'] != 30 ) { // Update 30 to your form id number
        return $subscription;
    }   
 
    $subscription->orderDescription = 'This is my custom description...';
    return $subscription;
}, 10, 5 );

4. Set the invoice number

This example shows how to pass an entry value as the subscription invoice number.

add_filter( 'gform_authorizenet_subscription_pre_create', function ( $subscription, $form_data, $config, $form, $entry ) {
    if ( $form['id'] != 1 ) { // Update 1 to your form id number
        return $subscription;
    }   
     
    GFCommon::log_debug( __METHOD__ . '(): Setting custom invoice number.' );
 
    // Update 46 to the id number of the field containing your invoice number
    $subscription->orderInvoiceNumber = rgar( $entry, '46' );
    return $subscription;
}, 10, 5 );

4. Use Name field for subscription details

This example shows how to use a Name field as source for Authorize.net’s subscription details.

add_filter( 'gform_authorizenet_subscription_pre_create', function ( $subscription, $form_data, $config, $form, $entry ) {
	GFCommon::log_debug( __METHOD__ . '(): running.' );
	if ( $form['id'] != 1 ) { // Update 1 to your form id number
		GFCommon::log_debug( __METHOD__ . '(): Not our desired form, leaving without changes.' );
		return $subscription;
	}

	// Set values for subscription First Name and Last Name from a name field with ID 1.
	$subscription->first_name = rgar( $entry, '1.3' );
	$subscription->last_name  = rgar( $entry, '1.6' );

	return $subscription;
}, 10, 5 );

Placement

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

Source Code

This filter is located in GFAuthorizeNet::subscribe() in class-gf-authorizenet.php.