You are here

function uc_credit_cron in Ubercart 5

Same name and namespace in other branches
  1. 6.2 payment/uc_credit/uc_credit.module \uc_credit_cron()

Implementation of hook_cron().

File

payment/uc_credit/uc_credit.module, line 185
Defines the credit card payment method and hooks in payment gateways.

Code

function uc_credit_cron() {

  // Clear the data
  $time = strtotime(variable_get('uc_cart_anon_duration', '4') . ' ' . variable_get('uc_cart_anon_unit', 'hours') . ' ago');
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("DELETE upc.* FROM {uc_orders} AS uo LEFT JOIN {uc_payment_credit} AS upc ON uo.order_id = upc.order_id WHERE uo.modified <= %d AND uo.order_status = '%s'", $time, uc_order_state_default('in_checkout'));
      break;
    case 'pgsql':
      db_query("DELETE FROM {uc_payment_credit} WHERE order_id IN (SELECT order_id FROM {uc_orders} WHERE modified <= %d AND order_status = '%s')", $time, uc_order_state_default('in_checkout'));
      break;
  }

  // Mask old stored credit card numbers.
  $time = strtotime(variable_get('uc_credit_number_duration', '1') . ' ' . variable_get('uc_credit_number_unit', 'years') . ' ago');
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("DELETE upc.* FROM {uc_orders} AS uo LEFT JOIN {uc_payment_credit} AS upc ON uo.order_id = upc.order_id WHERE uo.modified <= %d AND uo.order_status = '%s'", $time, variable_get('uc_credit_clear_status', uc_order_state_default('completed')));
      break;
    case 'pgsql':
      db_query("DELETE FROM {uc_payment_credit} WHERE order_id IN (SELECT order_id FROM {uc_orders} WHERE modified <= %d AND order_status = '%s')", $time, variable_get('uc_credit_clear_status', uc_order_state_default('completed')));
      break;
  }
  if (variable_get('uc_credit_debug', FALSE)) {
    $key = uc_credit_encryption_key();
    $crypt = new uc_encryption_class();
    $result = db_query("SELECT order_id FROM {uc_orders} WHERE modified <= %d AND order_status = '%s'", $time, variable_get('uc_credit_clear_status', uc_order_state_default('completed')));
    while ($order = db_fetch_array($result)) {

      // Load up the existing data array.
      $data = db_result(db_query("SELECT data FROM {uc_orders} WHERE order_id = %d", $order['order_id']));
      $data = unserialize($data);
      $cache = uc_credit_cache('save', $data['cc_data']);
      $cc_data = array(
        'cc_number' => substr($data['cc_data']['cc_number'], -4),
      );

      // Stuff the serialized and encrypted CC details into the array.
      $data['cc_data'] = $crypt
        ->encrypt(uc_credit_encryption_key(), serialize($cc_data));
      uc_store_encryption_errors($crypt, 'uc_credit');

      // Save it again.
      db_query("UPDATE {uc_orders} SET data = '%s' WHERE order_id = %d", serialize($data), $order['order_id']);
    }
  }
}