Commit 3c238e3c authored by jenlampton's avatar jenlampton

Refactor Data prepraration into a function that can be called twice for recurring payments.

parent 65dabd6f
......@@ -227,188 +227,27 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
return $form_state;
}
// Get the webform values.
$field_values = _authorizenetwebform_flatten_form_values($form_state['input']['submitted']);
// Get key mappings for this webform: webform field => AuthNet field.
// Get the webform node ID, for easy access.
$nid = $form_state['values']['details']['nid']; // Nice shorthand.
$field_map = _authorizenetwebform_get_key_mappings($nid);
// Get reverse key mappings AuthNet field => webform field.
$flipped_map = _authorizenetwebform_get_key_mappings($nid, TRUE);
// Get transaction type key.
$type_key = $flipped_map['x_trans_type'];
// Get the amount.
$amount_key = $flipped_map['x_amount'];
$amount = str_replace('$', '', $field_values[$amount_key]);
// Special handling for quantity.
if (array_key_exists('anwf_quantity', $flipped_map)) {
$quantity_key = $flipped_map['anwf_quantity'];
$quantity = $field_values[$quantity_key];
$amount = ((double) $amount) * ((double) $quantity);
}
// 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);
// Establish data to send to AuthNet.
if ($field_values[$type_key] == 'one-time') {
$top_key = 'createTransactionRequest';
$data = array(
'createTransactionRequest' => array(
'merchantAuthentication' => array(
'name' => variable_get('authorizenetwebform_login', NULL),
'transactionKey' => variable_get('authorizenetwebform_transaction', NULL),
),
'refId' => $nid . '-' . time(),
'transactionRequest' => array(
'transactionType' => 'authCaptureTransaction',
'amount' => $amount,
'payment' => array(
'opaqueData' => array(
'dataDescriptor' => 'COMMON.ACCEPT.INAPP.PAYMENT',
'dataValue' => $field_values[$trans_key],
),
),
'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',
),
),
),
)
);
}
elseif ($field_values[$type_key] == 'subscription') {
$top_key = 'ARBCreateSubscriptionRequest';
$data = array(
'ARBCreateSubscriptionRequest' => array(
'merchantAuthentication' => array(
'name' => variable_get('authorizenetwebform_login', NULL),
'transactionKey' => variable_get('authorizenetwebform_transaction', NULL),
),
'refId' => $nid . '-' . time(),
'subscription' => array(
'name' => 'Recurring donation',
'paymentSchedule' => array(
'interval' => array(
'length' => 1,
'unit' => 'months',
),
'startDate' => date('Y-m-d'),
'totalOccurrences' => 9999,
'trialOccurrences' => 1,
),
'amount' => $amount,
'trialAmount' => '0.00',
'payment' => array(
'opaqueData' => array(
'dataDescriptor' => 'COMMON.ACCEPT.INAPP.PAYMENT',
'dataValue' => $field_values[$trans_key],
),
),
'billTo' => array(
'firstName' => $field_values[$first_key],
'lastName' => $field_values[$last_key],
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => 'USA', // @todo add default country setting?
),
),
),
);
}
// 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 the customer's shipping data.
$data[$top_key][$second_key]['shipTo'] = array(
'firstName' => '',
'lastName' => '',
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
);
}
// Add everything else as UserFields (API only supports One-Time transactions)
if ($field_values[$type_key] == 'one-time' && !empty($field_keys)) {
$data[$top_key][$second_key]['userFields'] = array();
$data[$top_key][$second_key]['userFields']['userField'] = array();
$count = 1;
foreach ($field_keys as $rkey => $remaining_key) {
// Optionally add additional custom fields, if they have values.
if (array_key_exists($remaining_key, $field_keys) &&
!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]],
);
unset($field_keys[$rkey]);
}
if ($count >= 20) { // API is limited to 20 items.
break;
}
$count++;
}
// @todo Fix hard-coded specific webform options.
switch ($field_values[$type_key]) {
case 'one-time':
$transaction_type = 'createTransactionRequest';
break;
case 'subscription':
$transaction_type = 'ARBCreateSubscriptionRequest';
break;
}
// Prepre data to send to AuthNet, and encode as JSON.
$data = _authorizenetwebform_prepare_data($transaction_type, $nid, $field_values);
$json = json_encode($data);
// Set the post URL.
......@@ -437,13 +276,15 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
$text = '';
$failure = FALSE;
$response_info = array();
// Extrat the response info we need for our records.
if (array_key_exists('transactionResponse', $response_array)) {
if (isset($response_array['transactionResponse'])) {
$response_info = $response_array['transactionResponse'];
}
// Decliend || Error.
if ($response_info['responseCode'] == 2 || $response_info['responseCode'] == 3) {
if (!empty($response_info) && isset($response_info['responseCode']) &&
($response_info['responseCode'] == 2 || $response_info['responseCode'] == 3)) {
$failure = TRUE;
}
else {
......@@ -459,7 +300,7 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
// Failure :( Set the form error.
if ($failure) {
if (!empty($response_info['errors'])) {
if (!empty($response_info) && !empty($response_info['errors'])) {
foreach ($response_info['errors'] as $info) {
$code = $info['errorCode'];
$text = $info['errorText'];
......@@ -516,12 +357,17 @@ function authorizenetwebform_webform_client_form_submit($form, &$form_state) {
// Record the entire response if debug mode is on.
if (variable_get('authorizenetwebform_debug', FALSE)) {
$message = 'The complete response string from Auth.Net follows:<br/> !string';
// @todo: Save this data into a more permanent location than watchdog?
$message = 'The data set sent to Auth.Net follows:<br/> !string';
$string = '<pre>' . print_r($data, TRUE) . '</pre>';
$variables = array('!string' => $string);
watchdog('authorizenetwebform', $message, $variables, WATCHDOG_INFO);
// @todo: Save this data into a more permanent location than watchdog?
$message = 'The complete response string from Auth.Net follows:<br/> !string';
$string = '<pre>' . print_r($response_array, TRUE) . '</pre>';
$variables = array('!string' => $string);
watchdog('authorizenetwebform', $message, $variables, WATCHDOG_INFO);
// Also print response to screen if devel is enabled.
if (module_exists('devel')) {
......@@ -828,8 +674,7 @@ function _authorizenetwebform_get_key_mappings($nid = FALSE, $reverse = FALSE) {
}
static $authnet_field_maps;
if (is_array($authnet_field_maps) && array_key_exists($nid, $authnet_field_maps)
&& !empty($authnet_field_maps[$nid])) {
if (is_array($authnet_field_maps) && isset($authnet_field_maps[$nid])) {
$return = $authnet_field_maps[$nid];
}
else {
......@@ -870,7 +715,7 @@ function _authorizenetwebform_set_test_values(&$form, $nid) {
$form[$key]['#default_value'] = '11';
}
elseif ($key == $flipped_map['x_exp_year']) {
$form[$key]['#default_value'] = '99';
$form[$key]['#default_value'] = '25';
}
elseif ($key == $flipped_map['x_card_code']) {
$form[$key]['#default_value'] = '111';
......@@ -879,3 +724,197 @@ function _authorizenetwebform_set_test_values(&$form, $nid) {
}
}
}
/**
* Prepare data to send to AuthNet for a transaction.
*
* @param string $transaction_type
* [description]
* @param int $nid
* The Node ID for the webform being processed.
* @param array $field_values
* The webform values being submitted.
*
* @return array
* Data properly formatted (and ordered!), ready for JSON translation.
*/
function _authorizenetwebform_prepare_data($transaction_type, $nid, $field_values) {
// Get key mappings for this webform: webform key => AuthNet key.
$field_map = _authorizenetwebform_get_key_mappings($nid);
// Get reverse key mappings: AuthNet key => webform key.
$flipped_map = _authorizenetwebform_get_key_mappings($nid, TRUE);
// Get keys for other submitted fields.
$field_keys = _authorizenetwebform_get_data_keys($flipped_map);
// Get the amount.
$amount_key = $flipped_map['x_amount'];
$amount = str_replace('$', '', $field_values[$amount_key]);
// Special handling for quantity.
if (array_key_exists('anwf_quantity', $flipped_map)) {
$quantity_key = $flipped_map['anwf_quantity'];
$quantity = $field_values[$quantity_key];
$amount = ((double) $amount) * ((double) $quantity);
}
// Get webform 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'];
// One-time payment.
if ($transaction_type == 'createTransactionRequest') {
$request_type = 'transactionRequest';
$data = array(
'createTransactionRequest' => array(
'merchantAuthentication' => array(
'name' => variable_get('authorizenetwebform_login', NULL),
'transactionKey' => variable_get('authorizenetwebform_transaction', NULL),
),
'refId' => $nid . '-' . time(),
'transactionRequest' => array(
'transactionType' => 'authCaptureTransaction',
'amount' => $amount,
'payment' => array(
'opaqueData' => array(
'dataDescriptor' => 'COMMON.ACCEPT.INAPP.PAYMENT',
'dataValue' => $field_values[$trans_key],
),
),
'profile' => array(
'createProfile' => TRUE,
),
'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',
),
),
),
)
);
}
// Recurring payment.
if ($transaction_type == 'ARBCreateSubscriptionRequest') {
$request_type = 'subscription';
$data = array(
'ARBCreateSubscriptionRequest' => array(
'merchantAuthentication' => array(
'name' => variable_get('authorizenetwebform_login', NULL),
'transactionKey' => variable_get('authorizenetwebform_transaction', NULL),
),
'refId' => $nid . '-' . time(),
'subscription' => array(
'name' => 'Recurring donation',
'paymentSchedule' => array(
'interval' => array(
'length' => 1,
'unit' => 'months',
),
'startDate' => date('Y-m-d'),
'totalOccurrences' => 9999,
'trialOccurrences' => 1,
),
'amount' => $amount,
'trialAmount' => '0.00',
'payment' => array(
'opaqueData' => array(
'dataDescriptor' => 'COMMON.ACCEPT.INAPP.PAYMENT',
'dataValue' => $field_values[$trans_key],
),
),
'billTo' => array(
'firstName' => $field_values[$first_key],
'lastName' => $field_values[$last_key],
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => 'USA', // @todo add default country setting?
),
),
),
);
}
// Add the optional customer's billing data, if present.
if (isset($field_keys['company']) && isset($field_values[$field_keys['company']])) {
$data[$transaction_type][$request_type]['billTo']['company'] = $field_values[$field_keys['company']];
unset($field_keys['company']);
}
if (isset($field_keys['address']) && isset($field_values[$field_keys['address']])) {
$data[$transaction_type][$request_type]['billTo']['address'] = $field_values[$field_keys['address']];
unset($field_keys['address']);
}
if (isset($field_keys['city']) && isset($field_values[$field_keys['city']])) {
$data[$transaction_type][$request_type]['billTo']['city'] = $field_values[$field_keys['city']];
unset($field_keys['city']);
}
if (isset($field_keys['state']) && isset($field_values[$field_keys['state']])) {
$data[$transaction_type][$request_type]['billTo']['state'] = $field_values[$field_keys['state']];
unset($field_keys['state']);
}
if (isset($field_keys['zip']) && isset($field_values[$field_keys['zip']])) {
$data[$transaction_type][$request_type]['billTo']['zip'] = $field_values[$field_keys['zip']];
unset($field_keys['zip']);
}
if (isset($field_keys['country']) && isset($field_values[$field_keys['country']])) {
$data[$transaction_type][$request_type]['billTo']['country'] = $field_values[$field_keys['country']];
unset($field_keys['country']);
}
// @todo Optionally add the customer's shipping data.
if (FALSE) {
$data[$transaction_type][$request_type]['shipTo'] = array(
'firstName' => '',
'lastName' => '',
'company' => '',
'address' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
);
}
// Add everything else as UserFields (API only supports One-Time transactions)
if ($transaction_type == 'one-time' && !empty($field_keys)) {
$data[$transaction_type][$request_type]['userFields'] = array();
$data[$transaction_type][$request_type]['userFields']['userField'] = array();
$count = 1;
foreach ($field_keys as $rkey => $remaining_key) {
// Optionally add additional custom fields, if they have values.
if (array_key_exists($remaining_key, $field_keys) &&
!empty($field_values[$field_keys[$remaining_key]])) {
$data[$transaction_type][$request_type]['userFields']['userField'][] = array(
'name' => $remaining_key,
'value' => $field_values[$field_keys[$remaining_key]],
);
unset($field_keys[$rkey]);
}
if ($count >= 20) { // API is limited to 20 items.
break;
}
$count++;
}
}
return $data;
}
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