You are here

function uc_cybersource_charge in Ubercart 5

Same name and namespace in other branches
  1. 6.2 payment/uc_cybersource/uc_cybersource.module \uc_cybersource_charge()
  2. 7.3 payment/uc_cybersource/uc_cybersource.module \uc_cybersource_charge()
1 string reference to 'uc_cybersource_charge'
uc_cybersource_payment_gateway in payment/uc_cybersource/uc_cybersource.module

File

payment/uc_cybersource/uc_cybersource.module, line 215
A module used for CyberSource's Silent Order POST method of payment.

Code

function uc_cybersource_charge($order_id, $amount, $data) {
  global $user;
  $order = uc_order_load($order_id);
  $amount = uc_currency_format($amount, FALSE, FALSE, '.');
  $cc_type = NULL;
  if (isset($order->payment_details['cc_type'])) {
    switch (strtolower($order->payment_details['cc_type'])) {
      case 'amex':
      case 'american express':
        $cc_type = '003';
        break;
      case 'visa':
        $cc_type = '001';
        break;
      case 'mastercard':
      case 'master card':
        $cc_type = '002';
        break;
      case 'discover':
        $cc_type = '004';
        break;
    }
  }
  if (is_null($cc_type)) {
    $cc_type = _uc_cybersource_card_type($order->payment_details['cc_number']);
    if ($cc_type === FALSE && in_array($data['txn_type'], array(
      UC_CREDIT_AUTH_ONLY,
      UC_CREDIT_AUTH_CAPTURE,
    ))) {
      drupal_set_message(t('The credit card type did not pass validation.'), 'error');
      watchdog('uc_cybersource', t('Could not figure out cc type: @number / @type', array(
        '@number' => $order->payment_details['cc_number'],
        '@type' => $order->payment_details['cc_type'],
      )), WATCHDOG_ERROR);
      return array(
        'success' => FALSE,
      );
    }
  }
  $country = uc_get_country_data(array(
    'country_id' => $order->billing_country,
  ));
  if ($country === FALSE) {
    $country = array(
      0 => array(
        'country_iso_code_2' => 'US',
      ),
    );
  }

  // Process the charge differently depending on the CyberSource method.
  switch (variable_get('uc_cybersource_method', 'post')) {

    // Support for the Silent Order POST.
    case 'post':
      return _uc_cybersource_post_charge($order, $amount, $data, $cc_type, $country);

    // Support for the SOAP Toolkit API.
    case 'soap':

      // TODO: Refactor to use separate function for each API type.
      // - i.e. _uc_cybersource_charge_request_soap($order, $amount, $data);
      // require_once(drupal_get_path('module', 'uc_cybersource') .'/SOAP.php');
      return _uc_cybersource_soap_charge($order, $amount, $data, $cc_type, $country);
    case 'api':
      $config = cybs_load_config('cybs.ini');
      if (variable_get('uc_cybersource_server', 'test') == 'test') {
        $config['sendToProduction'] = 'false';
      }
      $request['ccAuthService_run'] = 'true';
      if (variable_get('uc_cybersource_transaction_type', 'sale') == 'sale') {
        $request['ccCaptureService_run'] = 'true';
      }
      $request['merchantReferenceCode'] = $order_id;
      $request['purchaseTotals_currency'] = 'USD';
      $request['purchaseTotals_grandTotalAmount'] = $amount;
      drupal_set_message('<pre>' . print_r($config, TRUE) . '</pre>');
      drupal_set_message('<pre>' . print_r($request, TRUE) . '</pre>');
      break;
  }
}