function commerce_authnet_cim_request in Commerce Authorize.Net 7
Submits a CIM XML API request to Authorize.Net.
Parameters
$payment_method: The payment method instance array associated with this API request.
$request_type: The name of the request type to submit.
$api_request_data: An associative array of data to be turned into a CIM XML API request.
11 calls to commerce_authnet_cim_request()
- commerce_authnet_acceptjs_cardonfile_create in ./
commerce_authnet.module - Commerce Card on File create callback.
- commerce_authnet_acceptjs_create_customer_payment_profile_request in includes/
commerce_authnet.acceptjs.inc - Submits a createCustomerPaymentProfileRequest XML CIM API request.
- commerce_authnet_acceptjs_create_customer_profile_request in includes/
commerce_authnet.acceptjs.inc - Submits a createCustomerProfileRequest XML CIM API request to Authorize.Net.
- commerce_authnet_cim_cardonfile_charge in ./
commerce_authnet.module - Card on file callback: background charge payment
- commerce_authnet_cim_cardonfile_create in ./
commerce_authnet.module - Commerce Card on File create callback.
File
- ./
commerce_authnet.module, line 1873 - Implements Authorize.Net payment services for use in Drupal Commerce.
Code
function commerce_authnet_cim_request($payment_method, $request_type, $api_request_data) {
// Get the API endpoint URL for the method's transaction mode.
$url = commerce_authnet_cim_server_url($payment_method['settings']['txn_mode']);
// Add default data to the API request data array.
if (!isset($api_request_data['merchantAuthentication'])) {
$api_request_data = array(
'merchantAuthentication' => array(
'name' => $payment_method['settings']['login'],
'transactionKey' => $payment_method['settings']['tran_key'],
),
) + $api_request_data;
}
// Determine if it is necessary to add a validation mode to the API request.
$validation_mode = '';
switch ($request_type) {
case 'createCustomerProfileRequest':
if (empty($api_request_data['profile']['paymentProfiles'])) {
$validation_mode = 'none';
}
else {
$validation_mode = $payment_method['settings']['txn_mode'] == AUTHNET_TXN_MODE_LIVE ? 'liveMode' : 'testMode';
}
break;
case 'createCustomerPaymentProfileRequest':
case 'updateCustomerPaymentProfileRequest':
case 'validateCustomerPaymentProfileRequest':
$validation_mode = $payment_method['settings']['txn_mode'] == AUTHNET_TXN_MODE_LIVE ? 'liveMode' : 'testMode';
break;
default:
break;
}
// Add the validation mode now if one was found.
if (!empty($validation_mode)) {
$api_request_data['validationMode'] = $validation_mode;
}
// Build and populate the API request SimpleXML element.
$api_request_element = new SimpleXMLElement('<' . $request_type . '/>');
$api_request_element
->addAttribute('xmlns', 'AnetApi/xml/v1/schema/AnetApiSchema.xsd');
commerce_simplexml_add_children($api_request_element, $api_request_data);
// Allow modules an opportunity to alter the request before it is sent.
drupal_alter('commerce_authnet_cim_request', $api_request_element, $payment_method, $request_type);
// Generate an XML string.
$xml = $api_request_element
->asXML();
// Log the request if specified.
if ($payment_method['settings']['log']['request'] == 'request') {
// Mask the credit card number and CVV.
$log_element = clone $api_request_element;
$log_element->merchantAuthentication->name = str_repeat('X', strlen((string) $log_element->merchantAuthentication->name));
$log_element->merchantAuthentication->transactionKey = str_repeat('X', strlen((string) $log_element->merchantAuthentication->transactionKey));
if (!empty($log_element->profile->paymentProfiles->payment->creditCard->cardNumber)) {
$card_number = (string) $log_element->profile->paymentProfiles->payment->creditCard->cardNumber;
$log_element->profile->paymentProfiles->payment->creditCard->cardNumber = str_repeat('X', strlen($card_number) - 4) . substr($card_number, -4);
}
if (!empty($log_element->paymentProfile->payment->creditCard->cardNumber)) {
$card_number = (string) $log_element->paymentProfile->payment->creditCard->cardNumber;
$log_element->paymentProfile->payment->creditCard->cardNumber = str_repeat('X', strlen($card_number) - 4) . substr($card_number, -4);
}
if (!empty($log_element->profile->paymentProfiles->payment->creditCard->cardCode)) {
$log_element->profile->paymentProfiles->payment->creditCard->cardCode = str_repeat('X', strlen((string) $log_element->profile->paymentProfiles->payment->creditCard->cardCode));
}
if (!empty($log_element->paymentProfile->payment->creditCard->cardCode)) {
$log_element->paymentProfile->payment->creditCard->cardCode = str_repeat('X', strlen((string) $log_element->paymentProfile->payment->creditCard->cardCode));
}
watchdog('commerce_authnet', 'Authorize.Net CIM @type to @url: @xml', array(
'@type' => $request_type,
'@url' => $url,
'@xml' => $log_element
->asXML(),
), WATCHDOG_DEBUG);
}
// Build the array of header information for the request.
$header = array();
$header[] = 'Content-type: text/xml; charset=utf-8';
// Setup the cURL request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
// Commerce Authnet requires SSL peer verification, which may prevent out of
// date servers from successfully processing API requests. If you get an error
// related to peer verification, you may need to:
// * Update libraries like openssl and libcurl.
// * Use https://github.com/paragonie/certainty to get updated certificates.
// * Update your php.ini to point to your CA certificate bundle with the
// curl.cainfo setting.
// * Download the CA certificate bundle file from
// http://curl.haxx.se/docs/caextract.html, place it in a safe location on
// your web server, and update your settings.php to set the
// commerce_authnet_cacert variable to contain the absolute path of the
// file.
if (variable_get('commerce_authnet_cacert', FALSE)) {
curl_setopt($ch, CURLOPT_CAINFO, variable_get('commerce_authnet_cacert', ''));
}
// Log any errors to the watchdog.
if ($error = curl_error($ch)) {
watchdog('commerce_authnet', 'cURL error: @error', array(
'@error' => $error,
), WATCHDOG_ERROR);
return FALSE;
}
curl_close($ch);
// If we received data back from the server...
if (!empty($result)) {
// Remove non-absolute XML namespaces to prevent SimpleXML warnings.
$result = str_replace(' xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"', '', $result);
// Extract the result into an XML response object.
$response = new SimpleXMLElement($result);
// Log the API response if specified.
if ($payment_method['settings']['log']['response'] == 'response') {
watchdog('commerce_authnet', 'API response received:<pre>@xml</pre>', array(
'@xml' => $response
->asXML(),
));
}
return $response;
}
else {
return FALSE;
}
}