You are here

function uc_recurring_product_process_order in UC Recurring Payments and Subscriptions 7.2

Same name and namespace in other branches
  1. 6.2 modules/uc_recurring_product/uc_recurring_product.module \uc_recurring_product_process_order()

Passes the information onto the specified fee handler for processing.

Parameters

$order: The order object the fees are attached to.

$data: Optional; Data that should be added to the fee object.

Return value

FALSE on failure or array with new recurring fee IDs.

3 calls to uc_recurring_product_process_order()
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_product_order_view_update_form_submit in modules/uc_recurring_product/uc_recurring_product.module
Submit handler for the processing recurring fee.
uc_recurring_product_uc_order in modules/uc_recurring_product/uc_recurring_product.module
Implements hook_uc_order().

File

modules/uc_recurring_product/uc_recurring_product.module, line 468
Add recurring payments/fees to a product. This is imlpemented through Ubercarts product features.

Code

function uc_recurring_product_process_order($order, $data = array()) {
  global $user;
  $return = array();

  // hack not all the id's are in the order object
  $order = uc_order_load($order->order_id);

  // Get all the products that should have a recurring fee created for them.
  if ($products = uc_recurring_product_get_recurring_products_in_order($order)) {

    // Check we have an handler to deal with the recurring payment.
    $payment_method = !empty($order->payment_method) ? $order->payment_method : 'default';
    if (!($fee_handler = uc_recurring_get_recurring_info($payment_method))) {
      drupal_set_message(t('A handler for processing and renewing recurring fees cannot be found for the @payment-method payment method.', array(
        '@payment-method' => $order->payment_method,
      )), 'error');
      return FALSE;
    }

    // Create a new fee object.
    $fee_template = new stdClass();
    $fee_template->uid = $order->uid;
    $fee_template->fee_handler = $fee_handler['fee handler'];
    $fee_template->created = REQUEST_TIME;
    $fee_template->order_id = $order->order_id;
    $fee_template->module = 'uc_recurring_product';

    // Iterate over the products that require a fee.
    foreach ($products as $product) {
      $fee = clone $fee_template;
      $product_fee = $product['recurring product'];
      $order_product_id = $product['product']->order_product_id;

      // If the product fee amount is 0, it means we need to use the product
      // price. This allows recurring fees to be adjusted by attributes.
      $fee->fee_amount = $product_fee->fee_amount == 0 ? $product['product']->price : $product_fee->fee_amount;
      $fee->fee_amount *= $product['product']->qty;

      // Add the product's title as the order title.
      $fee->fee_title = t('Renewal of product @title', array(
        '@title' => $product['product']->title,
      ));
      $fee->next_charge = strtotime('+' . $product_fee->initial_charge);
      $fee->initial_charge = $product_fee->initial_charge;
      $fee->regular_interval = $product_fee->regular_interval;
      $fee->remaining_intervals = $product_fee->number_intervals;
      $fee->charged_intervals = 0;
      $fee->data = array(
        'model' => $product_fee->model,
        'nid' => $product_fee->nid,
        'qty' => $product['product']->qty,
        'extension' => 0,
        'amount' => round($fee->fee_amount, 2),
        'txn_type' => 'web_accept',
      ) + $data;
      $fee->attempts = 0;
      $fee->pfid = $product_fee->pfid;
      $fee->order_product_id = $order_product_id;
      $fee->own_handler = !empty($fee_handler['own handler']);
      drupal_alter('recurring_fee_user_create', $fee);

      // Let the implementing module process.
      if (uc_recurring_invoke($fee->fee_handler, 'process callback', array(
        $order,
        &$fee,
      ))) {

        // Recurring processing was successful, get the fee.
        // We will save all fees together after we are sure all of them were
        // processed properly.
        $fee_objects[] = $fee;
      }
      else {

        // We have an error, so break. No fee object was saved.
        return FALSE;
      }
    }
    if (!empty($fee_objects)) {

      // There was no error, so save all fee objects.
      foreach ($fee_objects as $object) {
        $rfid = uc_recurring_fee_user_save($object);
        rules_invoke_event('uc_recurring_product_renewal_created', $order, $fee);
        uc_order_comment_save($order->order_id, $user->uid, t('Recurring fee <a href="@recurring-view-fee">@rfid</a> added to order.', array(
          '@recurring-view-fee' => url('admin/store/orders/recurring/view/fee/' . $rfid),
          '@rfid' => $rfid,
        )));
        $return[] = $rfid;
      }
    }
  }
  return $return;
}