You are here

function payment_commerce_payment_create in Payment for Drupal Commerce 7

Create a Payment for a Commerce order.

Parameters

$order: The order to base the payment on.

integer $pmid: The PMID of the payment method to use for the payment.

Return value

Payment

3 calls to payment_commerce_payment_create()
PaymentCommerceDeleteOrderWebTestCase::assertCreateAndDeleteOrderAndPayment in tests/PaymentCommerceDeleteOrderWebTestCase.test
Creates an order, transaction and Payment, and then deletes them.
payment_commerce_form_process_submit_form in ./payment_commerce.module
Implements form process callback for payment_commerce_submit_form().
payment_commerce_payment_method_validate in ./payment_commerce.rules.inc
Implements Rules plugin callback.

File

./payment_commerce.module, line 318
Hook implementations and shared functions.

Code

function payment_commerce_payment_create($order, $pmid) {
  $balance = commerce_payment_order_balance($order);
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_total = $order_wrapper->commerce_order_total
    ->value();
  $payment = new Payment(array(
    'context' => 'payment_commerce',
    'context_data' => array(
      'balance_amount' => $balance['amount'],
      'order_id' => $order->order_id,
    ),
    'description' => t('Order !order_number', array(
      '!order_number' => $order->order_number,
    )),
    'currency_code' => $balance['currency_code'],
    'method' => entity_load_single('payment_method', $pmid),
    'finish_callback' => 'payment_commerce_payment_finish',
  ));

  // The order is being paid in full.
  if ($balance['amount'] == $order_total['amount']) {
    foreach ($order_wrapper->commerce_line_items
      ->value() as $line_item) {
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
      $line_item_unit_price = commerce_price_component_total($line_item_wrapper->commerce_unit_price
        ->value());

      // Make sure the line item amount is in the correct currency.
      if ($line_item_unit_price['currency_code'] != $payment->currency_code) {
        $amount = commerce_currency_convert($line_item_unit_price['amount'], $line_item_unit_price['currency_code'], $payment->currency_code);
      }
      else {
        $amount = $line_item_unit_price['amount'];
      }

      // Convert the amount to a float.
      $currency = commerce_currency_load($payment->currency_code);
      $amount = (double) $amount / pow(10, $currency['decimals']);
      $payment
        ->setLineItem(new PaymentLineItem(array(
        'amount' => $amount,
        'description' => $line_item->line_item_label,
        'name' => 'payment_commerce_' . $line_item->line_item_id,
        'quantity' => $line_item->quantity,
      )));
    }
  }
  else {
    $payment
      ->setLineItem(new PaymentLineItem(array(
      'amount' => $balance['amount'],
      'description' => 'Order !order_number',
      'description_arguments' => array(
        '!order_number' => $order->order_number,
      ),
      'name' => 'payment_commerce',
    )));
  }
  return $payment;
}