You are here

function commerce_paypal_ec_itemize_order in Commerce PayPal 7.2

Returns a name-value pair array of information to the API request.

Parameters

object $order: The order to itemized

string $currency_code: The currency to return the different amounts.

Return value

array A name-value pair array.

2 calls to commerce_paypal_ec_itemize_order()
commerce_paypal_ec_do_payment in modules/ec/commerce_paypal_ec.module
Confirm an Express Checkout payment for an order for the specified charge amount with a DoExpressCheckoutPayment API request.
commerce_paypal_ec_set_express_checkout in modules/ec/commerce_paypal_ec.module
Submits a SetExpressCheckout request to PayPal for the given order.

File

modules/ec/commerce_paypal_ec.module, line 1320
Implements PayPal Express Checkout in Drupal Commerce checkout.

Code

function commerce_paypal_ec_itemize_order($order, $currency_code) {
  $nvp = array();

  // 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 to determine the total item
  // amount and shipping amount.
  $i = 0;
  $shippingamt = 0;
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {

    // If the current line item is a shipping line item, track its amount value
    // without taxes separately from products.
    if (module_exists('commerce_shipping') && $line_item_wrapper->type
      ->value() == 'shipping') {

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

      // Track the total costs of all shipping line items on the order to add to
      // the final payment request as a single amount, though typically this
      // should be limited to one shipping line item per order.
      $shippingamt += $shipping_price['amount'];
    }
    else {

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

      // Convert the unit price to the propery currency.
      $l_amt = commerce_currency_convert($unit_price['amount'], $unit_price['currency_code'], $currency_code);

      // Add payment details line items.
      $nvp += array(
        'L_PAYMENTREQUEST_0_NAME' . $i => commerce_line_item_title($line_item_wrapper
          ->value()),
        'L_PAYMENTREQUEST_0_AMT' . $i => commerce_paypal_price_amount($l_amt, $currency_code),
        'L_PAYMENTREQUEST_0_QTY' . $i => round($line_item_wrapper->quantity
          ->value()),
        'L_PAYMENTREQUEST_0_NUMBER' . $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 the total item and tax amounts to the payment request
  $nvp += array(
    'PAYMENTREQUEST_0_ITEMAMT' => commerce_paypal_price_amount($amt - $taxamt - $shippingamt, $currency_code),
    'PAYMENTREQUEST_0_TAXAMT' => commerce_paypal_price_amount($taxamt, $currency_code),
  );
  if ($shippingamt > 0) {
    $nvp['PAYMENTREQUEST_0_SHIPPINGAMT'] = commerce_paypal_price_amount($shippingamt, $currency_code);
  }
  return $nvp;
}