From d5904c0ac3214d4c42f26e34e4e9f9290ec9d9c4 Mon Sep 17 00:00:00 2001
From: Aaron Van Ruler <aaron.vanruler@gmail.com>
Date: Thu, 1 Mar 2012 10:58:36 -0500
Subject: [PATCH] Adds line item reporting to Authorize.net. Only works for AIM (not Card on File)

---
 commerce_authnet.module |   40 ++++++++++++++++++++++++++++++++++++++--
 1 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/commerce_authnet.module b/commerce_authnet.module
index 0b8fb87..b898f32 100644
--- a/commerce_authnet.module
+++ b/commerce_authnet.module
@@ -230,7 +230,12 @@ function commerce_authnet_aim_submit_form_submit($payment_method, $pane_form, $p
 
   foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
     if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
-      $description[] = round($line_item_wrapper->quantity->value(), 2) . 'x ' . $line_item_wrapper->line_item_label->value();
+      $description[] = round($line_item_wrapper->quantity->value(), 2) . ' x ' . $line_item_wrapper->line_item_label->value();
+
+      // Merchant defined fields for line items
+      // provides line items to the automated administrative email from Authorize.net
+      $amount = commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount->value(), 'USD');
+      $nvp['line_item_' . $delta] = $line_item_wrapper->line_item_label->value() . " = " . $amount;
     }
   }
 
@@ -902,13 +907,15 @@ function commerce_authnet_aim_request($payment_method, $nvp = array()) {
   foreach ($nvp as $key => $value) {
     $pairs[] = $key . '=' . urlencode($value);
   }
+  $pairs = implode('&', $pairs);
+  $pairs .= "&" . commerce_authnet_line_items($nvp['x_invoice_num']);
 
   // Setup the cURL request.
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_VERBOSE, 0);
   curl_setopt($ch, CURLOPT_POST, 1);
-  curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $pairs));
+  curl_setopt($ch, CURLOPT_POSTFIELDS, $pairs);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
@@ -1240,3 +1247,32 @@ function commerce_authnet_cvv_response($code) {
 
   return '-';
 }
+
+function commerce_authnet_line_items($order_id) {
+  $order = commerce_order_load($order_id);
+  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
+  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
+    if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
+      $line_item = "x_line_item=";
+
+      $product = $line_item_wrapper->commerce_product->value();
+
+      // Item ID
+      $line_item .= substr($product->sku, 0, 31) . "<|>";
+      // Item Name
+      $line_item .= substr($product->title, 0, 31) . "<|>";
+      // Item Description
+      $line_item .= substr("test", 0, 255) . "<|>";
+      // Item Quantity
+      $line_item .= round($line_item_wrapper->quantity->value(), 2) . "<|>";
+      // Item Price
+      $line_item .= commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount->value(), 'USD') . "<|>";
+      // Item Taxable
+      $line_item .= "FALSE";
+
+      $line_items[] = $line_item;
+    }
+  }
+
+  return implode("&", $line_items);
+}
-- 
1.7.3.2

