You are here

function uc_recurring_renew in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 uc_recurring.module \uc_recurring_renew()

Process a renewal, either from the cron job or manually from a fee handler.

Parameters

$fee: The fee object.

Return value

The new order ID or FALSE if unable to renew fee.

4 calls to uc_recurring_renew()
uc_recurring_admin_charge_form_submit in ./uc_recurring.admin.inc
@todo Please document this function.
uc_recurring_cron in ./uc_recurring.module
Implements hook_cron().
uc_recurring_hosted_paypal_ipn in modules/uc_recurring_hosted/uc_recurring_hosted.paypal_ipn.inc
Handle IPN callbacks for PayPal recurring payments
uc_recurring_hosted_uc_auth_arb_payment in modules/uc_recurring_hosted/uc_recurring_hosted.module
Implements hook_uc_auth_arb_payment(). Called during an ARB silent post.

File

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

Code

function uc_recurring_renew($fee) {
  global $user;
  if ($fee->attempts == 0) {

    // create a new order for the renewal
    $order = uc_recurring_create_renewal_order($fee);
  }
  else {

    // renewal order already created on first charge attempt
    $order = FALSE;
    if (isset($fee->data['retry_order_id']) && is_numeric($fee->data['retry_order_id'])) {
      $order = uc_order_load($fee->data['retry_order_id']);
    }
    if ($order == FALSE) {

      // could not find the last renewal order so create a new one
      $order = uc_recurring_create_renewal_order($fee);
    }
  }
  if (uc_recurring_charge_profile($fee, $order) !== FALSE) {
    $order = uc_order_load($order->order_id);

    // Set new intervals.
    uc_recurring_set_intervals($fee);

    // Add the new order ID to database.
    $recurring_order = new stdClass();
    $recurring_order->original_order_id = $fee->order_id;
    $recurring_order->renewal_order_id = $order->order_id;
    drupal_write_record('uc_recurring_orders', $recurring_order);
    if (isset($fee->data['retry_order_id'])) {
      unset($fee->data['retry_order_id']);
    }

    // Save the fee object.
    uc_recurring_fee_user_save($fee);
    uc_recurring_renewal_module_invoke('recurring_renewal_completed', $order, $fee);

    // @todo - replace with rules

    //ca_pull_trigger('uc_recurring_renewal_complete', $order, $fee);
    rules_invoke_event('uc_recurring_renewal_complete', $order, $fee);

    // Return the new order ID.
    return $order->order_id;
  }
  else {

    // Charging failed.
    if (!$fee->own_handler) {
      uc_recurring_process_extensions($fee);
    }
    if ($fee->module == 'uc_recurring_product') {
      $message = t('Error: Recurring fee <a href="@orders-recurring-view">@fee</a> for product @model failed.', array(
        '@orders-recurring-view' => url('admin/store/orders/recurring/view/fee/' . $fee->rfid),
        '@fee' => $fee->rfid,
        '@model' => $fee->data['model'],
      ));
    }
    else {
      $message = t('Error: Recurring fee <a href="@orders-recurring-view">@fee</a> for order @order_id failed.', array(
        '@orders-recurring-view' => url('admin/store/orders/recurring/view/fee/' . $fee->rfid),
        '@fee' => $fee->rfid,
        '@order_id' => $fee->order_id,
      ));
    }

    // Add comment to both new and original orders.
    uc_order_comment_save($fee->order_id, $user->uid, $message);
    uc_order_comment_save($order->order_id, $user->uid, $message);
    if ($fee->module == 'uc_recurring_product') {
      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));
    }
    else {
      watchdog('uc_recurring', 'Failed to capture recurring fee of @amount for order @order_id.', array(
        '@amount' => $fee->fee_amount,
        '@order_id' => $fee->order_id,
      ), WATCHDOG_ERROR, l(t('order !order_id', array(
        '!order_id' => $fee->order_id,
      )), 'admin/store/orders/' . $fee->order_id));
    }

    // check if there will be a retry
    $extension = uc_recurring_get_extension($fee->pfid, $fee->attempts);
    if ($extension) {
      $fee->data['retry_order_id'] = $order->order_id;
      uc_recurring_fee_user_save($fee);
    }
    uc_recurring_renewal_module_invoke('recurring_renewal_failed', $order, $fee);

    // @todo - replace with rules

    //ca_pull_trigger('uc_recurring_renewal_failed', $order, $fee);
    rules_invoke_event('uc_recurring_renewal_failed', $order, $fee);
    uc_order_comment_save($fee->order_id, $user->uid, t('New recurring fee failed on order <a href="@store-orders">@order_id</a>.', array(
      '@store-orders' => url('admin/store/orders/' . $order->order_id),
      '@order_id' => $order->order_id,
    )));
  }
  return FALSE;
}