You are here

function commerce_payleap_request in Commerce Payleap 7

Submits a request to PayLeap.

Parameters

$payment_method: The payment method instance array associated with this API request.

array $info: The payment method extented info.

Return value

array Response.

7 calls to commerce_payleap_request()
CommercePayleapTest::testCommercePayleapAuthTransaction in tests/commerce_payleap.test
Test the PayLeap Auth transaction.
CommercePayleapTest::testCommercePayleapCardonFileTranscation in tests/commerce_payleap.test
CommercePayleapTest::testCommercePayleapDirectTransaction in tests/commerce_payleap.test
Test the PayLeap direct transaction.
commerce_payleap_card_profile_request in ./commerce_payleap.module
Submits a ManageCreditCardInfo request PayLeap. This function will perform a Add/Update/Delete of a credit card profile.
commerce_payleap_customer_profile_request in ./commerce_payleap.module
Submits a ManageCustomer request PayLeap. This function will perform a Add/Update/Delete of a Customer Profile

... See full list

File

./commerce_payleap.module, line 622
Implements PayLeap payment services for use in Drupal Commerce.

Code

function commerce_payleap_request($payment_method, $info = array()) {

  // Get the API endpoint URL for the method's transaction mode and type.
  $url = commerce_payleap_server_url($payment_method['settings']['txn_mode'], $payment_method['settings']['txn_payleap_type']);

  // Add the default name-value pairs to the array.
  $info += array(
    // API credentials
    'Username' => $payment_method['settings']['login'],
    'Password' => $payment_method['settings']['tran_key'],
  );
  $optional_settings = array(
    'vendor_number' => 'Vendor',
  );
  foreach ($optional_settings as $setting_name => $query_name) {
    if (!empty($payment_method['settings'][$setting_name])) {
      $info[$query_name] = $payment_method['settings'][$setting_name];
    }
  }

  // Allow modules to alter parameters of the API request.
  drupal_alter('commerce_payleap_direct_request', $info);

  // Log the request if specified.
  if ($payment_method['settings']['log']['request'] == 'request') {

    // Mask the credit card number and CVV.
    $log_nvp = $info;
    $log_nvp['Username'] = str_repeat('X', strlen($log_nvp['Username']));
    $log_nvp['Password'] = str_repeat('X', strlen($log_nvp['Password']));
    if (!empty($log_nvp['CardNum'])) {
      $log_nvp['CardNum'] = str_repeat('X', strlen($log_nvp['CardNum']) - 4) . substr($log_nvp['CardNum'], -4);
    }
    if (!empty($log_nvp['CcAccountNum'])) {
      $log_nvp['CcAccountNum'] = str_repeat('X', strlen($log_nvp['CcAccountNum']) - 4) . substr($log_nvp['CcAccountNum'], -4);
    }
    if (!empty($log_nvp['CVNum'])) {
      $log_nvp['CVNum'] = str_repeat('X', strlen($log_nvp['CVNum']));
    }
    watchdog('commerce_payleap', 'PayLeap request to @url: !param', array(
      '@url' => $url,
      '!param' => '<pre>' . check_plain(print_r($log_nvp, TRUE)) . '</pre>',
    ), WATCHDOG_DEBUG);
  }

  // Prepare the name-value pair array to be sent as a string.
  $pairs = array();
  foreach ($info as $key => $value) {
    $pairs[] = $key . '=' . urlencode($value);
  }

  // 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, implode('&', $pairs));
  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);
  $result = curl_exec($ch);

  // Log any errors to the watchdog.
  if ($error = curl_error($ch)) {
    watchdog('commerce_payleap', 'cURL error: @error', array(
      '@error' => $error,
    ), WATCHDOG_ERROR);
    $response['status'] = FALSE;
    $response['msg'] = $error;
    return $response;
  }
  curl_close($ch);

  // If we received data back from the server.
  if (empty($result)) {
    watchdog('commerce_payleap', 'cURL error empty result returned.', array(), WATCHDOG_ERROR);
    $response['status'] = FALSE;
    $response['msg'] = t('No answer from server');
  }
  else {

    // Remove non-absolute XML namespaces to prevent SimpleXML warnings.
    $result = str_replace(' xmlns="http://www.payleap.com/payments"', '', $result);

    // Extract the result into an XML response object.
    $xml = new SimpleXMLElement($result);
    $response = array();

    // Log the API response if specified.
    if ($payment_method['settings']['log']['response'] == 'response') {
      watchdog('commerce_payleap', 'API response received:<pre>@xml</pre>', array(
        '@xml' => $xml
          ->asXML(),
      ));
    }
    if ($payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_DIRECT_CAPTURE || $payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_DELAYED_CAPTURE || $payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_FORCE || $payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_VOID) {

      // 0 - mean OK.
      $response['status'] = (string) $xml->Result === '0' ? TRUE : FALSE;
      $response['msg'] = (string) $xml->RespMSG;
    }
    elseif ($payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_RECURRING_CAPTURE) {

      // 0 - mean OK.
      $response['status'] = (string) $xml->Result === '0' ? TRUE : FALSE;
      $response['msg'] = (string) $xml->Message;
    }
    elseif ($payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_ADDRECURRINGCREDITCARD || $payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_MANAGECONTRACT || $payment_method['settings']['txn_payleap_type'] == PAYLEAP_TXN_TYPE_MANAGECREDITCARDINFO) {
      $response['status'] = (string) $xml->Code == 'Ok' ? TRUE : FALSE;
      $response['msg'] = (string) $xml->Error;
    }
    else {
      $response['status'] = (string) $xml->Code == 'Fail' ? FALSE : TRUE;
      $response['msg'] = (string) $xml->Error;
    }

    // Request approved, Save original xml responce with all data.
    $response['xml'] = $xml;
  }
  return $response;
}