You are here

function payment_ubercart_payment_create in Payment for Ubercart 7

Same name and namespace in other branches
  1. 7.2 payment_ubercart.module \payment_ubercart_payment_create()

Creates a payment for an order.

Parameters

object $order:

Return value

Payment

1 call to payment_ubercart_payment_create()
payment_ubercart_callback in ./payment_ubercart.module
Implements Ubercart payment method callback.

File

./payment_ubercart.module, line 299
Hook implementations and shared functions.

Code

function payment_ubercart_payment_create($order) {

  // Check for an existing payment;
  $pids = payment_ubercart_pids_load($order->order_id);
  $payment = $pids ? entity_load_single('payment', end($pids)) : NULL;
  $pid = $payment && payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_NEW) ? $payment->pid : 0;
  $pmid = (int) str_replace('payment_ubercart_', '', $order->payment_method);
  $payment = new Payment(array(
    'context' => 'payment_ubercart',
    'currency_code' => $order->currency,
    'description' => t('Order #!order_id', array(
      '!order_id' => $order->order_id,
    )),
    'finish_callback' => 'payment_ubercart_finish',
    'method' => entity_load_single('payment_method', $pmid),
    'payment_ubercart_uc_order_id' => $order->order_id,
    'pid' => $pid,
  ));

  // Add orders, line items, and hook_uc_order() totals to the payment as line items.
  $order->order_total = uc_order_get_total($order);
  $balance = uc_payment_balance($order);
  if ($order->order_total == $balance) {
    foreach ($order->products as $product) {
      $payment
        ->setLineItem(new PaymentLineItem(array(
        'amount' => $product->price,
        'description' => $product->title,
        'quantity' => $product->qty,
        'name' => 'payment_ubercart_product_' . $product->nid,
      )));
    }
    if (is_array($order->line_items)) {
      foreach ($order->line_items as $line_item) {
        if (_uc_line_item_data($line_item['type'], 'calculated') == TRUE) {
          $payment
            ->setLineItem(new PaymentLineItem(array(
            'amount' => $line_item['amount'],
            'description' => $line_item['title'],
            'quantity' => 1,
            'name' => 'payment_ubercart_line_item_' . $line_item['line_item_id'],
          )));
        }
      }
    }
    $hook_order_total = 0;
    foreach (module_implements('uc_order') as $module) {
      $function = $module . '_uc_order';

      // $order must be passed by reference.
      if ($value = $function('total', $order, NULL) && is_numeric($value)) {
        $hook_order_total += $value;
      }
    }
    if ($hook_order_total) {
      $payment
        ->setLineItem(new PaymentLineItem(array(
        'amount' => $hook_order_total,
        'description' => 'Other',
        'quantity' => 1,
        'name' => 'payment_ubercart_hook_uc_order_total_',
        $order->order_id,
      )));
    }
  }
  else {
    $payment
      ->setLineItem(new PaymentLineItem(array(
      'amount' => $balance,
      'description' => 'Order !order_id',
      'description_arguments' => array(
        '!order_id' => $order->order_id,
      ),
      'quantity' => 1,
      'name' => 'payment_ubercart_order_balance_',
      $order->order_id,
    )));
  }
  return $payment;
}