You are here

function commerce_payflow_link_itemize_order in Commerce PayPal 7.2

Returns an itemized order data array for use in a name-value pair array.

Parameters

$order: The order whose line items should be converted into name-value pairs.

$currency_code: The currency code to use to calculate all amounts.

Return value

The name-value pair array representing the line items that should be added to the name-value pair array for an API request.

1 call to commerce_payflow_link_itemize_order()
commerce_payflow_link_create_secure_token in modules/payflow/commerce_payflow.module
Requests a Secure Token from Payflow for use in follow-up API requests.

File

modules/payflow/commerce_payflow.module, line 1153
Implements PayPal Payments Advanced (U.S. only) and Payflow Link Hosted Checkout pages and Transparent Redirect.

Code

function commerce_payflow_link_itemize_order($order, $currency_code) {
  $nvp = array();
  $currency = commerce_currency_load($currency_code);

  // Extract the order total value array.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_total = $order_wrapper->commerce_order_total
    ->value();

  // Initialize the order level amount parameter.
  $amt = commerce_currency_convert($order_total['amount'], $order_total['currency_code'], $currency_code);

  // Loop over all the line items on the order.
  $i = 0;
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {

    // Extract the unit price total value array.
    $unit_price = $line_item_wrapper->commerce_unit_price
      ->value();

    // Calculate the cost as the unit price minus the tax amount and add it to
    // the running total for the order.
    $l_cost = commerce_currency_convert($unit_price['amount'], $unit_price['currency_code'], $currency_code);

    // Add the line item to the return array.
    $nvp += array(
      'L_NAME' . $i => commerce_line_item_title($line_item_wrapper
        ->value()),
      'L_COST' . $i => commerce_paypal_price_amount($l_cost, $currency_code),
      'L_QTY' . $i => round($line_item_wrapper->quantity
        ->value()),
    );

    // If it was a product line item, add the SKU.
    if (in_array($line_item_wrapper->type
      ->value(), commerce_product_line_item_types())) {
      $nvp += array(
        'L_SKU' . $i => $line_item_wrapper->line_item_label
          ->value(),
      );
    }
    $i++;
  }

  // Determine the order level item amount and tax amount line items. To prevent
  // rounding problems getting in the way, we calculate them based on the order
  // total instead of tallying it from each line item.
  if (module_exists('commerce_tax')) {
    $taxamt = commerce_round(COMMERCE_ROUND_HALF_UP, commerce_tax_total_amount($order_total['data']['components'], FALSE, $currency_code));
  }
  else {
    $taxamt = 0;
  }

  // Add payment details line items.
  $nvp += array(
    'ITEMAMT' => commerce_paypal_price_amount($amt - $taxamt, $currency_code),
    'TAXAMT' => commerce_paypal_price_amount($taxamt, $currency_code),
  );
  return $nvp;
}