You are here

function uc_recurring_cron in UC Recurring Payments and Subscriptions 6

Same name and namespace in other branches
  1. 6.2 uc_recurring.module \uc_recurring_cron()
  2. 7.2 uc_recurring.module \uc_recurring_cron()

Implementation of hook_cron().

File

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

Code

function uc_recurring_cron() {
  if (variable_get('uc_recurring_handler', 'uc_recurring') == 'uc_recurring') {
    $successes = 0;
    $fails = 0;
    $result = db_query("SELECT * FROM {uc_recurring_users} WHERE fee_handler = 'uc_recurring' AND remaining_intervals > 0 AND next_charge <= %d", time());
    while ($fee = db_fetch_array($result)) {
      $fee['data'] = unserialize($fee['data']);
      if ($key = uc_credit_encryption_key()) {
        $crypt = new uc_encryption_class();
        $fee['data']['payment_details']['cc_number'] = $crypt
          ->decrypt($key, $fee['data']['payment_details']['cc_number']);
        if (variable_get('uc_credit_debug', FALSE)) {
          $fee['data']['payment_details']['cc_cvv'] = $crypt
            ->decrypt($key, $fee['data']['payment_details']['cc_cvv']);
        }
        $fee['data']['payment_details']['cc_exp_month'] = $crypt
          ->decrypt($key, $fee['data']['payment_details']['cc_exp_month']);
        $fee['data']['payment_details']['cc_exp_year'] = $crypt
          ->decrypt($key, $fee['data']['payment_details']['cc_exp_year']);
        uc_store_encryption_errors($crypt, 'uc_recurring');
      }

      // Attempt to process the charge.
      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']);
        }
        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', '!successes recurring fees processed successfully; !fails failed.', array(
        '!successes' => $successes,
        '!fails' => $fails,
      ));
    }
  }
}