Commit a527a4eb authored by jenlampton's avatar jenlampton

Add support for billing info if mapped.

parent be19dfc5
......@@ -196,10 +196,10 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
// Get key mappings for this webform.
$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());
// We also want to access keys in the other direction.
// Get the mapping of AuthNet field key => webform field.
$flipped_map = array_flip($field_map);
//dpm($flipped_map);
// Get the amount.
$amount_key = $flipped_map['x_amount'];
......@@ -212,11 +212,14 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
$amount = ((double) $amount) * ((double) $quantity);
}
// Get keys for the the transaction nonce, and required fields.
$trans_key = $flipped_map['x_trans_id'];
$type_key = $flipped_map['x_trans_type'];
$first_key = $flipped_map['x_first_name'];
$last_key = $flipped_map['x_last_name'];
// Get keys for the the transaction nonce and other required fields.
$trans_key = $flipped_map['x_trans_id'];
$type_key = $flipped_map['x_trans_type'];
$first_key = $flipped_map['x_first_name'];
$last_key = $flipped_map['x_last_name'];
// Get keys for other submitted fields.
$field_keys = _authorizenetwebform_get_data_keys($flipped_map);
//extract($field_keys);
// Establish data to send to AuthNet.
if ($field_values[$type_key] == 'one-time') {
......@@ -240,8 +243,20 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
'billTo' => array(
'firstName' => $field_values[$first_key],
'lastName' => $field_values[$last_key],
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => 'USA', // @todo add default country setting?
),
'customerIP' => $_SERVER['REMOTE_ADDR'],
'transactionSettings' => array(
'setting' => array(
'settingName' => 'testRequest',
'settingValue' => 'false',
),
),
),
)
);
......@@ -278,50 +293,89 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
'billTo' => array(
'firstName' => $field_values[$first_key],
'lastName' => $field_values[$last_key],
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => 'USA', // @todo add default country setting?
),
),
),
);
}
if (FALSE) { // @todo
// Optionally add the customer's billing data.
if ($field_values[$type_key] == 'one-time') {
$second_key = 'transactionRequest';
}
elseif ($field_values[$type_key] == 'subscription') {
$second_key = 'subscription';
}
$data[$top_key][$second_key]['billTo']['company'] = '';
$data[$top_key][$second_key]['billTo']['address'] = '';
$data[$top_key][$second_key]['billTo']['city'] = '';
$data[$top_key][$second_key]['billTo']['state'] = '';
$data[$top_key][$second_key]['billTo']['country'] = '';
if (FALSE) { // @todo
// Optionally add the customer's shipping data.
$data[$top_key][$second_key]['shipTo'] = array(
'firstName' => '',
'lastName' => '',
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'country' => '',
);
}
// Get the type of request for the data array.
if ($field_values[$type_key] == 'one-time') {
$second_key = 'transactionRequest';
}
elseif ($field_values[$type_key] == 'subscription') {
$second_key = 'subscription';
}
// Add the optional customer's billing data, if present.
if (isset($field_keys['company']) && array_exists($field_keys['company'], $field_values)) {
$data[$top_key][$second_key]['billTo']['company'] = $field_values[$field_keys['company']];
unset($field_keys['company']);
}
if (isset($field_keys['address']) && array_key_exists($field_keys['address'], $field_values)) {
$data[$top_key][$second_key]['billTo']['address'] = $field_values[$field_keys['address']];
unset($field_keys['address']);
}
if (isset($field_keys['city']) && array_key_exists($field_keys['city'], $field_values)) {
$data[$top_key][$second_key]['billTo']['city'] = $field_values[$field_keys['city']];
unset($field_keys['city']);
}
if (isset($field_keys['state']) && array_key_exists($field_keys['state'], $field_values)) {
$data[$top_key][$second_key]['billTo']['state'] = $field_values[$field_keys['state']];
unset($field_keys['state']);
}
if (isset($field_keys['zip']) && array_key_exists($field_keys['zip'], $field_values)) {
$data[$top_key][$second_key]['billTo']['zip'] = $field_values[$field_keys['zip']];
unset($field_keys['zip']);
}
if (isset($field_keys['country']) && array_key_exists($field_keys['country'], $field_values)) {
$data[$top_key][$second_key]['billTo']['country'] = $field_values[$field_keys['country']];
unset($field_keys['country']);
}
if (FALSE) { // @todo
// Optionally add additional custom fields.
$data[$top_key]['userFields'] = array(
'userField' => array(
'name' => '',
'value' => '',
),
// Optionally add the customer's shipping data.
$data[$top_key][$second_key]['shipTo'] = array(
'firstName' => '',
'lastName' => '',
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
);
}
// Add everything else as UuserFields.
if (!empty($field_keys)) {
$data[$top_key][$second_key]['userFields'] = array();
$data[$top_key][$second_key]['userFields']['userField'] = array();
$count = 1;
foreach ($field_keys as $remaining_key) {
// Optionally add additional custom fields, if they have values.
if (!empty($field_values[$field_keys[$remaining_key]])) {
$data[$top_key][$second_key]['userFields']['userField'][] = array(
'name' => $remaining_key,
'value' => $field_values[$field_keys[$remaining_key]],
);
}
if ($count >= 20) { // API is limited to 20 items.
break;
}
$count++;
}
}
dpm($data);
$json = json_encode($data);
// Set the post URL.
......@@ -595,3 +649,36 @@ function _authorizenetwebform_sanitize(&$element) {
}
}
}
/**
* Get the webform keys for all optional mapped AithNet fields.
*
* @param array $flipped_map
* Mapping of the AuthNet to webform keys.
*
* @return array
* Array of keys names, keyed by something similar to the AuthNet identifier.
*/
function _authorizenetwebform_get_data_keys($flipped_map) {
// Remove the requred keys we won't need for this.
unset($flipped_map['x_trans_id']);
unset($flipped_map['x_trans_type']);
unset($flipped_map['x_first_name']);
unset($flipped_map['x_last_name']);
unset($flipped_map['x_card_num']);
unset($flipped_map['x_card_code']);
//unset($flipped_map['x_exp_date']);
unset($flipped_map['x_exp_month']);
unset($flipped_map['x_exp_year']);
unset($flipped_map['x_amount']);
$keys = array();
foreach ($flipped_map as $authnet_name => $key) {
// Remove the x_ from the begining.
$var_name = substr($authnet_name, 2);
$keys[$var_name] = $key;
}
return $keys;
}
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