Gravity Wiz // Auto-populate Credit Card Fields with Test Data
<?php
/**
* Auto-populate Credit Card Fields with Test Data
*
* Frustrated with entering sandbox credit card data into your Gravity Forms credit card fields? This snippet intelligently
* populates gateway-specific testing credit card data into your credit fields. The payment gateway is determined by the first
* payment gateway feed found configured for the current form.
*
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2013 Gravity Wiz
*/
class GWPopulateCreditCardField {
var $cards = array(
'authorize.net' => array(
'has_feed' => array(
array( 'GFAuthorizeNetData', 'get_feed_by_form' )
),
'card' => array(
'card_number' => '4012888818888',
'security_code' => '900'
)
),
'paypal_pro' => array(
'has_feed' => array(
array( 'GFPayPalProData', 'get_feed_by_form' )
),
'card' => array(
'card_number' => '4120435557544926',
'experiation' => array( 1, 2017 ),
'security_code' => '111'
)
),
'paypal_payments_pro' => array(
'has_feed' => array(
array( 'GFPayPalPaymentsPro', 'get_feed_by_form' )
),
'card' => array(
'card_number' => '4012888888881881'
)
)
);
var $card = array();
var $value_filter_prefix = 'gform_field_value_gwpcc_';
function __construct() {
// add default card in constructor to support dynamic exp year
$this->cards['default'] = array(
'card' => array(
'card_number' => '4242424242424242',
'security_code' => '123',
'expiration_date' => array( 1, date( 'Y', strtotime( '+1 year' ) ) ),
'cardholder_name' => 'Bilbo Baggins'
)
);
add_filter( 'gform_pre_render', array( $this, 'add_dynamic_population_parameters' ) );
foreach( $this->get_card_input_map() as $input_type ) {
add_filter( $this->value_filter_prefix . $input_type, array( $this, 'populate_card_data' ), 10, 2 );
}
}
function add_dynamic_population_parameters( $form ) {
$cc_fields = GFCommon::get_fields_by_type( $form, array( 'creditcard' ) );
if( empty( $cc_fields ) )
return $form;
$this->card[$form['id']] = $this->get_card( $form['id'] );
foreach( $form['fields'] as &$field ) {
if( $field['type'] != 'creditcard' )
continue;
$field['allowsPrepopulate'] = true;
foreach( $field['inputs'] as &$input ) {
$input['name'] = 'gwpcc_' . $this->get_dynamic_parameter( $input['id'] );
}
}
return $form;
}
function get_card( $form_id ) {
foreach( $this->cards as $card ) {
$has_feed_funcs = rgar( $card, 'has_feed' );
if( ! $has_feed_funcs )
continue;
if( ! is_array( $has_feed_funcs ) )
$has_feed_funcs = array( $has_feed_funcs );
foreach( $has_feed_funcs as $has_feed_func ) {
$has_feed = false;
if( is_callable( $has_feed_func ) )
$has_feed = call_user_func( $has_feed_func, $form_id );
if( $has_feed )
return array_merge( $this->cards['default']['card'], $card['card'] );
}
}
return $this->cards['default']['card'];
}
function get_dynamic_parameter( $input_id ) {
list( $field_id, $input_id ) = array_pad( explode( '.', $input_id ), 2, null );
return rgar( $this->get_card_input_map(), $input_id );
}
function get_card_input_map() {
return array(
'1' => 'card_number',
'2' => 'expiration_date',
'3' => 'security_code',
'5' => 'cardholder_name'
);
}
function populate_card_data( $value, $field ) {
$form_id = rgar( $field, 'formId' );
$current_filter = current_filter();
$input_type = str_replace( $this->value_filter_prefix, '', $current_filter );
return rgar( $this->card[$form_id], $input_type );
}
}
new GWPopulateCreditCardField();