Commit 335fce67 authored by jenlampton's avatar jenlampton

Add default values for credit card details when in test mode (sandbox account).

parent 5c143a6a
......@@ -151,6 +151,11 @@ function authorizenetwebform_form_webform_client_form_alter(&$form, &$form_state
'sensitive' => array(),
);
// Pre-populate the fields with test credit card info.
if (variable_get('authorizenetwebform_mode', 'main') == 'test') {
_authorizenetwebform_set_test_values($form['submitted'], $node->nid);
}
// Add Accept.js data attributes to each mapped field.
_authorizenetwebform_add_attributes($form['submitted'], $settings['mapping'], $node->nid);
......@@ -189,13 +194,11 @@ function authorizenetwebform_form_webform_client_form_alter(&$form, &$form_state
*/
function authorizenetwebform_webform_client_form_validate($form, &$form_state) {
// Get key mappings for this webform.
// Get key mappings for this webform: webform field => AuthNet field.
$nid = $form_state['values']['details']['nid']; // Nice shorthand.
$variable_name = 'authorizenetwebform_key_map_' . $nid;
// Get the mapping of webform field => AuthNet field key.
$field_map = variable_get($variable_name, array());
// Get the mapping of AuthNet field key => webform field.
$flipped_map = array_flip($field_map);
$field_map = _authorizenetwebform_get_key_mappings($nid);
// Get reverse key mappings AuthNet field => webform field.
$flipped_map = _authorizenetwebform_get_key_mappings($nid, TRUE);
// Get the amount.
$field_values = _authorizenetwebform_flatten_form_values($form_state['values']['submitted']);
......@@ -224,13 +227,11 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
$field_values = _authorizenetwebform_flatten_form_values($form_state['input']['submitted']);
// Get key mappings for this webform.
// Get key mappings for this webform: webform field => AuthNet field.
$nid = $form_state['values']['details']['nid']; // Nice shorthand.
$variable_name = 'authorizenetwebform_key_map_' . $nid;
// Get the mapping of webform field => AuthNet field key.
$field_map = variable_get($variable_name, array());
// Get the mapping of AuthNet field key => webform field.
$flipped_map = array_flip($field_map);
$field_map = _authorizenetwebform_get_key_mappings($nid);
// Get reverse key mappings AuthNet field => webform field.
$flipped_map = _authorizenetwebform_get_key_mappings($nid, TRUE);
// Get the amount.
$amount_key = $flipped_map['x_amount'];
......@@ -521,10 +522,10 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
// @todo: Save this data into a more permanent location than watchdog?
// Also print response to screen.
//if (module_exists('devel')) {
//dpm($response_array);
//}
// Also print response to screen if devel is enabled.
if (module_exists('devel')) {
dpm($response_array);
}
}
}
......@@ -808,3 +809,72 @@ function _authorizenetwebform_get_component_ids($node) {
}
return $component_ids;
}
/**
* Gets key mappings for a particular webform.
*
* @param int $nid
* Node ID for the webform.
* @param bool $reverse
* Whether or not to reverse the key mappings.
*
* @return array
* Webform keys for authorize.net values.
*/
function _authorizenetwebform_get_key_mappings($nid = FALSE, $reverse = FALSE) {
if (!$nid) {
return;
}
static $authnet_field_maps;
if (is_array($authnet_field_maps) && array_key_exists($nid, $authnet_field_maps)
&& !empty($authnet_field_maps[$nid])) {
$return = $authnet_field_maps[$nid];
}
else {
$authnet_field_maps = array();
$variable_name = 'authorizenetwebform_key_map_' . $nid;
// Get the mapping of webform field => AuthNet field key.
$authnet_field_maps[$nid] = variable_get($variable_name, array());
$return = $authnet_field_maps[$nid];
}
if ($reverse) {
$return = array_flip($return);
}
return $return;
}
/**
* Sets test credit card numbers for faster testing.
*
* @param array $element
* Form array or form element (for example, fieldset).
*/
function _authorizenetwebform_set_test_values(&$form, $nid) {
// Get reverse key mappings AuthNet field => webform field.
$flipped_map = _authorizenetwebform_get_key_mappings($nid, TRUE);
foreach ($form as $key => &$element) {
if (substr($key, 0, 1) != '#') {
if ($element['#type'] == 'fieldset') {
_authorizenetwebform_set_test_values($element, $nid);
}
else {
if ($key == $flipped_map['x_card_num']) {
$form[$key]['#default_value'] = '4111111111111111';
}
elseif ($key == $flipped_map['x_exp_month']) {
$form[$key]['#default_value'] = '11';
}
elseif ($key == $flipped_map['x_exp_year']) {
$form[$key]['#default_value'] = '99';
}
elseif ($key == $flipped_map['x_card_code']) {
$form[$key]['#default_value'] = '111';
}
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment