You are here

function uc_recurring_charge in UC Recurring Payments and Subscriptions 6

2 calls to uc_recurring_charge()
uc_recurring_admin_charge_form_submit in ./uc_recurring.admin.inc
uc_recurring_cron in ./uc_recurring.module
Implementation of hook_cron().

File

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

Code

function uc_recurring_charge($fee) {
  static $show = TRUE;

  // Get the charge function for the default credit card gateway.
  $gateways = _payment_gateway_list('credit', TRUE);
  if (count($gateways) == 1) {
    $keys = array_keys($gateways);
    $func = $gateways[$keys[0]]['credit'];
  }
  elseif (count($gateways) > 1) {
    foreach ($gateways as $gateway) {
      if ($gateway['id'] == variable_get('uc_payment_credit_gateway', '')) {
        $func = $gateway['credit'];
      }
    }
  }

  // Whoa... bad function? ABORT! ABORT!
  if (!function_exists($func)) {
    if ($show) {
      watchdog('uc_recurring', 'Recurring payments failed to process due to invalid credit card gateway.', array(), WATCHDOG_ERROR);
      $show = FALSE;
    }
    return FALSE;
  }

  // Cache the CC details stored by the handler.
  uc_credit_cache('save', $fee['data']['payment_details'], FALSE);

  // Run the charge.
  $result = $func($fee['order_id'], $fee['fee_amount'], NULL);

  // Handle the result.
  if ($result['success'] === TRUE) {
    uc_payment_enter($fee['order_id'], 'credit', $fee['fee_amount'], 0, $result['data'], t('Recurring fee payment.') . '<br />' . $result['comment']);
    $context = array(
      'revision' => 'formatted-original',
      'location' => 'recurring-charge-comment',
    );
    uc_order_comment_save($fee['order_id'], 0, t('!amount recurring fee collected for @model. (ID: <a href="!url">!fee</a>)', array(
      '!url' => url('admin/store/orders/recurring/view/fee/' . $fee['rfid']),
      '!fee' => $fee['rfid'],
      '!amount' => uc_price($fee['fee_amount'], $context),
      '@model' => $fee['data']['model'],
    )));

    // Modules can hook into the charge process using hook_recurring_api().
    module_invoke_all('recurring_api', 'charge', $fee);

    // Needs to be updated for Conditional Actions. -RS
    // workflow_ng_invoke_event('fee_charge_successful', uc_order_load($fee['order_id']));
    // if ($fee['remaining_intervals'] == 1) {
    //   workflow_ng_invoke_event('fee_expires', uc_order_load($fee['order_id']));
    // }
  }
  else {
    uc_order_comment_save($fee['order_id'], 0, t('Error: Recurring fee <a href="!url">!fee</a> for product @model failed.', array(
      '!url' => url('admin/store/orders/recurring/view/fee/' . $fee['rfid']),
      '!fee' => $fee['rfid'],
      '@model' => $fee['data']['model'],
    )));
    watchdog('uc_recurring', 'Failed to capture recurring fee of !amount for product @model on order !order_id.', array(
      '!amount' => $fee['fee_amount'],
      '@model' => $fee['data']['model'],
      '!order_id' => $fee['order_id'],
    ), WATCHDOG_ERROR, l(t('order !order_id', array(
      '!order_id' => $fee['order_id'],
    )), 'admin/store/orders/' . $fee['order_id']));

    // Modules can hook into the charge process using hook_recurring_api().
    module_invoke_all('recurring_api', 'fail', $fee);

    // Provide a Workflow event for folks to hook into.
    // workflow_ng_invoke_event('fee_charge_fails', uc_order_load($fee['order_id']));
  }
  return $result['success'];
}