gform_authorizenet_transaction_pre_capture

gform_authorizenet_transaction_pre_capture

DescriptionUsageParametersExamples1. Prevent capture2. Add a custom field3. Set the taxExempt property.4. Set the invoice_num property.5. Add a Description6. Set the shipping address properties7. Set the Customer IP Address as a custom field8. Use Name field for transaction detailsPlacementSource Code

Description

This filter can be used to modify the transaction object before it is sent to Authorize.net. It can also be used to prevent capture by returning false.

Usage

The filter which would run for all 『product and services』 type Authorize.net feeds can be used like so:

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

Parameters

$transaction object
The Authorize.net transaction 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. Prevent capture

The following example shows how you can prevent the payment being captured.

add_filter( 'gform_authorizenet_transaction_pre_capture', '__return_false' );

2. Add a custom field

The following example shows how you can add a custom Authorize.net field to the transaction and pass it the value from a form field.

add_filter( 'gform_authorizenet_transaction_pre_capture', 'add_custom_field', 10, 5 );
function add_custom_field( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] == 10 ) {
$value = rgpost( 'input_5');
$transaction->setCustomField( 'your_field_name', $value );
}

return $transaction;
}

3. Set the taxExempt property.

The following example shows how you can add a custom Authorize.net field to the transaction and pass it the value from a form field.

add_filter( 'gform_authorizenet_transaction_pre_capture', 'set_tax_exempt', 10, 5 );
function set_tax_exempt( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] == 10 ) {
$transaction->tax_exempt = 'true';
}

return $transaction;
}

4. Set the invoice_num property.

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

add_filter( 'gform_authorizenet_transaction_pre_capture', function( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] == 10 ) {
$transaction->invoice_num = rgar( $entry, '4' );
}

return $transaction;
}, 10, 5 );

5. Add a Description

This example shows how to add a description (up to 255 characters) for one time transactions in a form with id 30.

add_filter( 'gform_authorizenet_transaction_pre_capture', function( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] != 30 ) { // Update 30 to your form id number
return $transaction;
}

$transaction->description = 'This is my custom description...';

return $transaction;
}, 10, 5 );

6. Set the shipping address properties

This example shows how to pass entry values to the transaction shipping address properties.

add_filter( 'gform_authorizenet_transaction_pre_capture', function( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] == 10 ) {
$transaction->ship_to_first_name = rgar( $entry, '1.3' );
$transaction->ship_to_last_name = rgar( $entry, '1.6' );
$transaction->ship_to_company = rgar( $entry, '2' );
$transaction->ship_to_address = rgar( $entry, '3.1' );
$transaction->ship_to_city = rgar( $entry, '3.3' );
$transaction->ship_to_state = rgar( $entry, '3.4' );
$transaction->ship_to_zip_code = rgar( $entry, '3.5' );
$transaction->ship_to_country = rgar( $entry, '3.6' );
}

return $transaction;
}, 10, 5 );

7. Set the Customer IP Address as a custom field

The current version of the add-on doesn』t support using the customerIP property. This example shows a workaround passing the Customer IP address using a custom field.

add_filter( 'gform_authorizenet_transaction_pre_capture', function( $transaction, $form_data, $config, $form, $entry ) {
GFCommon::log_debug( __METHOD__ . '(): running.' );

$transaction->setCustomField( 'Customer IP', rgar( $entry, 'ip' ) );

GFCommon::log_debug( __METHOD__ . '(): Customer IP set to: ' . rgar( $entry, 'ip' ) );

return $transaction;
}, 10, 5 );

8. Use Name field for transaction details

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

add_filter( 'gform_authorizenet_transaction_pre_capture', function ( $transaction, $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 $transaction;
}

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

return $transaction;
}, 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::authorize() in class-gf-authorizenet.php.

发表回复

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