You are here

function commerce_recurring_process_payment in Commerce Recurring Framework 7

Rules callback to process a payment against an order

Parameters

$order object: The Commerce Order Object

File

./commerce_recurring.rules.inc, line 379

Code

function commerce_recurring_process_payment($order) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $parent_order = $wrapper->commerce_recurring_parent_order
    ->value();
  if ($parent_order && !empty($parent_order->order_id) && ($last_payment_transaction_id = commerce_recurring_fetch_last_payment_transaction_id($parent_order->order_id))) {

    // We load the payment details and process it here
    $last_payment_transaction = commerce_payment_transaction_load($last_payment_transaction_id);

    // Get the payment instance and method
    $instance_id = $parent_order->data['payment_method'];
    $payment_method = commerce_payment_method_instance_load($instance_id);
    if (!empty($payment_method['cardonfile']['charge callback']) && ($function = $payment_method['cardonfile']['charge callback']) && function_exists($payment_method['cardonfile']['charge callback'])) {
      $result = $function($order, $parent_order);
      if (!$result) {
        watchdog('commerce_recurring', "Error occured whilst processing recurring transaction payment for order @order_id's - the payment method @method commerce card on file charge callback reported an error.", array(
          '@order_id' => $order->order_id,
          '@method' => $payment_method['short_title'],
        ), WATCHDOG_ERROR);
        module_invoke_all('commerce_recurring_failed_payment', $order);

        // Invoke the product prepare event with the shopping cart order.
        rules_invoke_all('commerce_recurring_rules_event_failed_payment', $order);
      }
      else {
        watchdog('commerce_recurring', "Processed recurring transaction payment for order @order_id's - using @method payment method - return code: @return.", array(
          '@order_id' => $order->order_id,
          '@return' => $result,
          '@method' => $payment_method['short_title'],
        ), WATCHDOG_INFO);

        // Now we mark the order as payment processed so it does not load again.
        $wrapper->commerce_recurring_payment = 1;
        $wrapper
          ->save();
      }
    }
    else {
      watchdog('commerce_recurring', "Could not fetch process recurring transaction for order @order_id's - the payment method @method does not provide a valid commerce card on file charge callback.", array(
        '@order_id' => $order->order_id,
        '@method' => $payment_method['short_title'],
      ), WATCHDOG_ERROR);
    }
  }
  else {
    watchdog('commerce_recurring', "Could not fetch payment transaction for order @order_id's parent order.", array(
      '@order_id' => $order->order_id,
    ), WATCHDOG_ERROR);
    return;
  }
}