You are here

function uc_recurring_charge in Ubercart 5

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

File

payment/uc_recurring/uc_recurring.module, line 1056
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 handler's credit card gateway.
  $func = $fee['data']['charge_callback'];

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

  // Build the data array for the request.
  $data = array(
    'txn_type' => UC_CREDIT_REFERENCE_TXN,
    'ref_id' => $fee['data']['ref_id'],
  );

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

  // 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']);
    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_currency_format($fee['fee_amount']),
      '@model' => $fee['data']['model'],
    )));

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

    // Provide a couple Workflow events for folks to hook into.
    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', t('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'];
}