You are here

function uc_recurring_cron in Ubercart 5

Implementation of hook_cron().

File

payment/uc_recurring/uc_recurring.module, line 204
Allows you to add a recurring fee to a product/SKU to handle subscription type services.

Code

function uc_recurring_cron() {
  $successes = 0;
  $fails = 0;

  // Look for any fees ready for a charge that are reference transactions
  // routed through the core recurring system.
  $result = db_query("SELECT * FROM {uc_recurring_users} WHERE fee_handler LIKE 'uc_recurring_byref:%%' AND remaining_intervals > 0 AND next_charge <= %d", time());
  while ($fee = db_fetch_array($result)) {
    $handler = explode(':', $fee['fee_handler']);
    $fee['data'] = unserialize($fee['data']);

    // Attempt to process the reference transaction.
    if (uc_recurring_charge($fee)) {

      // Update the fee in the database.
      if ($fee['remaining_intervals'] == 1) {
        $next_charge = time();
      }
      else {
        $next_charge = strtotime('+' . $fee['regular_interval'], $fee['next_charge']);
      }
      db_query("UPDATE {uc_recurring_users} SET next_charge = %d, remaining_intervals = remaining_intervals - 1, charged_intervals = charged_intervals + 1 WHERE rfid = %d", $next_charge, $fee['rfid']);
      $successes++;
    }
    else {
      $fails++;
    }
  }
  if ($successes > 0 || $fails > 0) {
    watchdog('uc_recurring', t('!successes recurring fees processed successfully; !fails failed.', array(
      '!successes' => $successes,
      '!fails' => $fails,
    )));
  }
}