--- uc_paypal.module	2011-09-04 16:18:15.275999181 +0530
+++ /var/www/uberdrupal/sites/all/modules/ubercart/payment/uc_paypal/uc_paypal.module	2011-09-14 14:21:34.455972023 +0530
@@ -1,4 +1,5 @@
 <?php
+// $Id: uc_paypal.module,v 1.5.2.19 2009/11/18 21:08:06 islandusurper Exp $
 
 /**
  * @file
@@ -15,13 +16,14 @@
  * https://www.paypal.com/us/mrb/pal=6NXAPNGSS6DFJ
  */
 
+define('UC_PAYPAL_API', '60.0');
 
 /*******************************************************************************
  * Hook Functions (Drupal)
  ******************************************************************************/
 
 /**
- * Implements hook_menu().
+ * Implementation of hook_menu().
  */
 function uc_paypal_menu() {
   // Always accessible, helps for testing while site is offline.
@@ -76,15 +78,20 @@ function uc_paypal_menu() {
   return $items;
 }
 
-/**
- * Access callback. Makes sure PayPal always has access to send IPNs.
- */
+// Make sure PayPal always has access to send IPNs.
 function uc_paypal_ipn_access() {
   return TRUE;
 }
 
 /**
- * Implements hook_init().
+ * Implementation of hook_perm().
+ */
+function uc_paypal_perm() {
+  return array('administer paypal');
+}
+
+/**
+ * Implementation of hook_init().
  */
 function uc_paypal_init() {
   global $conf;
@@ -92,11 +99,10 @@ function uc_paypal_init() {
 }
 
 /**
- * Implements hook_form_alter().
- *
+ * Implementation of hook_form_alter().
  * Notice how we alter the checkout review form to post the order to PayPal.
  */
-function uc_paypal_form_alter(&$form, $form_state, $form_id) {
+function uc_paypal_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) {
     $order = uc_order_load($order_id);
 
@@ -122,7 +128,7 @@ function uc_paypal_form_alter(&$form, $f
  ******************************************************************************/
 
 /**
- * Implements hook_cart_pane().
+ * Implementation of hook_cart_pane().
  */
 function uc_paypal_cart_pane() {
   $panes[] = array(
@@ -137,14 +143,9 @@ function uc_paypal_cart_pane() {
 }
 
 /**
- * Implements hook_payment_gateway().
+ * Implementation of hook_payment_gateway().
  */
 function uc_paypal_payment_gateway() {
-  if (!module_exists('uc_credit')) {
-    drupal_set_message(t('You must <a href="@modules">enable the Credit Card module</a> to use PayPal Website Payments Pro.', array('@modules' => url('admin/modules', array('fragment' => 'edit-modules-ubercart-payment')))), 'warning');
-    return;
-  }
-
   $gateways[] = array(
     'id' => 'paypal_wpp',
     'title' => t('PayPal Website Payments Pro'),
@@ -158,19 +159,19 @@ function uc_paypal_payment_gateway() {
 }
 
 /**
- * Implements hook_payment_method().
+ * Implementation of hook_payment_method().
  */
 function uc_paypal_payment_method() {
   $path = base_path() . drupal_get_path('module', 'uc_credit');
-  $title1 = '<img src="https://www.paypal.com/en_US/i/logo/PayPal_mark_37x23.gif" style="position: relative; top: 5px; margin-right: 4px;" />'
+  $title1 = '<img src="https://www.paypal.com/en_US/i/logo/PayPal_mark_37x23.gif" style="position: relative; top: 5px; margin-right: 4px;">'
          . t('PayPal - pay without sharing your financial information.');
   $title2 = '<br /><span id="paypal-includes" style="padding-left: 5.5em;">'
           . t('<b>Includes:</b>');
   $cc_types = array('visa', 'mastercard', 'discover', 'amex', 'echeck');
   foreach ($cc_types as $type) {
-    $title2 .= ' <img src="'. $path .'/images/'. $type .'.gif" style="position: relative; top: 5px;" />';
+    $title2 .= ' <img src="'. $path .'/images/'. $type .'.gif" style="position: relative; top: 5px;">';
   }
-  $title2 .= ' <img src="https://www.paypal.com/en_US/i/logo/PayPal_mark_37x23.gif" style="position: relative; top: 5px; margin-right: 4px;" /></span>';
+  $title2 .= ' <img src="https://www.paypal.com/en_US/i/logo/PayPal_mark_37x23.gif" style="position: relative; top: 5px; margin-right: 4px;"></span>';
 
   $methods[] = array(
     'id' => 'paypal_wps',
@@ -204,9 +205,7 @@ function uc_paypal_payment_method() {
  * Callback Functions, Forms, and Tables
  ******************************************************************************/
 
-/**
- * Settings for Website Payments Pro on the Payment gateways form.
- */
+// Settings for Website Payments Pro on the Payment gateways form.
 function uc_paypal_wpp_settings_form() {
   // The DoDirectPayment API call allows fewer currencies than PayPal in general.
   $form['paypal_wpp']['uc_paypal_wpp_currency'] = array(
@@ -233,12 +232,6 @@ function uc_paypal_wpp_settings_form() {
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
   );
-  $form['paypal_wpp']['ec']['uc_paypal_wps_email'] = array(
-    '#type' => 'textfield',
-    '#title' => t('PayPal e-mail address'),
-    '#description' => t('The e-mail address you use for the PayPal account you want to receive payments.'),
-    '#default_value' => variable_get('uc_paypal_wps_email', ''),
-  );
   $form['paypal_wpp']['ec']['uc_paypal_ec_rqconfirmed_addr'] = array(
     '#type' => 'checkbox',
     '#title' => t('Require Express Checkout users to use a PayPal confirmed shipping address.'),
@@ -290,9 +283,7 @@ function uc_paypal_wpp_settings_form() {
   return $form;
 }
 
-/**
- * Processes a credit card payment through Website Payments Pro.
- */
+// Process a credit card payment through Website Payments Pro.
 function uc_paypal_wpp_charge($order_id, $amount, $data) {
   global $user;
 
@@ -489,7 +480,7 @@ function uc_paypal_wpp_charge($order_id,
 }
 
 /**
- * Builds error message(s) from PayPal failure responses.
+ * Build error message(s) from PayPal failure responses.
  */
 function _uc_paypal_build_error_messages($nvp_response) {
   $code = 0;
@@ -501,9 +492,7 @@ function _uc_paypal_build_error_messages
   return $message;
 }
 
-/**
- * Handles the Website Payments Standard payment method.
- */
+// Handles the Website Payments Standard payment method.
 function uc_payment_method_paypal_wps($op, &$arg1) {
   switch ($op) {
     case 'order-view':
@@ -612,9 +601,7 @@ function uc_payment_method_paypal_wps($o
   }
 }
 
-/**
- * Handles the Express Checkout payment method.
- */
+// Handles the Express Checkout payment method.
 function uc_payment_method_paypal_ec($op, &$arg1) {
   switch ($op) {
     case 'order-view':
@@ -628,16 +615,6 @@ function uc_payment_method_paypal_ec($op
       $form['redirect'] = array(
         '#value' => '<div>'. t('For Express Checkout, you need to <a href="!cp_link">enable the cart pane</a> and <a href="!wpp_link">configure the Website Payments Pro settings</a>.', array('!cp_link' => url('admin/store/settings/cart/edit/panes'), '!wpp_link' => url('admin/store/settings/payment/edit/gateways'))) .'</div>',
       );
-      $form['uc_paypal_ec_landingpage_style'] = array(
-        '#type' => 'radios',
-        '#title' => t('PayPal landing page style'),
-        '#description' => t('PayPal Express Checkout landing page may show a credit card submission form (allowing credit card payments without a PayPal account) or may show only a PayPal login form, thereby restricting payment to PayPal users.'),
-        '#options' => array(
-          'Billing' => t('Show credit card submission form.'),
-          'Login' => t('Do not show credit card submission form.'),
-        ),
-        '#default_value' => variable_get('uc_paypal_ec_landingpage_style', 'Billing'),
-      );
       return $form;
   }
 }
@@ -646,9 +623,7 @@ function uc_payment_method_paypal_ec($op
  * Module and Helper Functions
  ******************************************************************************/
 
-/**
- * Redirects if a customer selects PayPal Express Checkout as a payment method.
- */
+// Redirects if a customer selects PayPal Express Checkout as a payment method.
 function uc_paypal_ec_checkout($form, &$form_state) {
   if ($form_state['values']['panes']['payment']['payment_method'] != 'paypal_ec') {
     return;
@@ -704,7 +679,6 @@ function uc_paypal_ec_checkout($form, &$
     'SHIPTOCOUNTRYCODE' => $country[0]['country_iso_code_2'],
     'SHIPTOZIP' => substr($order->delivery_postal_code, 0, 20),
     'PHONENUM' => substr($order->delivery_phone, 0, 20),
-    'LANDINGPAGE' => variable_get('uc_paypal_ec_landingpage_style', 'Billing'),
   );
 
   if (!uc_cart_is_shippable()) {
@@ -729,12 +703,12 @@ function uc_paypal_ec_checkout($form, &$
   exit();
 }
 
-/**
- * Returns the form for Express Checkout Shortcut Flow.
- *
- * @see uc_paypal_ec_form_submit()
- */
+// Returns the form for Express Checkout Shortcut Flow.
 function uc_paypal_ec_form() {
+  if (!variable_get('uc_cap_uc_paypal_ec_enabled', FALSE)) {
+    return;
+  }
+
   // Hack to allow the image button to submit.
   if (isset($_POST['submit_x'])) {
     $form['submit'] = array(
@@ -750,11 +724,6 @@ function uc_paypal_ec_form() {
   return $form;
 }
 
-/**
- * Submit handler for uc_paypal_ec_form().
- *
- * @see uc_paypal_ec_form()
- */
 function uc_paypal_ec_form_submit($form, &$form_state) {
   global $user;
   $items = uc_cart_get_contents();
@@ -790,7 +759,6 @@ function uc_paypal_ec_form_submit($form,
     'REQCONFIRMSHIPPING' => variable_get('uc_paypal_ec_rqconfirmed_addr', 0),
     'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US',
     'NOTIFYURL' => url('uc_paypal/ipn/'. $order->order_id, array('absolute' => TRUE)),
-    'LANDINGPAGE' => variable_get('uc_paypal_ec_landingpage_style', 'Billing'),
   );
 
   $order->products = $items;
@@ -801,7 +769,6 @@ function uc_paypal_ec_form_submit($form,
   $_SESSION['cart_order'] = $order->order_id;
   $_SESSION['TOKEN'] = $nvp_response['TOKEN'];
 
-  $sandbox = '';
   if (strpos(variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'), 'sandbox') > 0) {
     $sandbox = 'sandbox.';
   }
@@ -810,11 +777,6 @@ function uc_paypal_ec_form_submit($form,
   exit();
 }
 
-/**
- * Additional submit handler for uc_cart_checkout_review_form().
- *
- * @see uc_cart_checkout_review_form()
- */
 function uc_paypal_ec_submit_form_submit($form, &$form_state) {
   $order = uc_order_load($_SESSION['cart_order']);
 
@@ -880,9 +842,7 @@ function uc_paypal_ec_submit_form_submit
   $form_state['redirect'] = 'cart/checkout/complete';
 }
 
-/**
- * Returns the form elements for the Website Payments Standard form.
- */
+// Returns the form elements for the Website Payments Standard form.
 function uc_paypal_wps_form($form_state, $order) {
   $shipping = 0;
   foreach ($order->line_items as $item) {
@@ -957,7 +917,7 @@ function uc_paypal_wps_form($form_state,
     // Transaction information
     'currency_code' => variable_get('uc_paypal_wps_currency', 'USD'),
     'handling_cart' => uc_price($shipping, $context, $options),
-    'invoice' => $order->order_id .'-'. uc_cart_get_id(),
+    'invoice' => $order->order_id .'-'. time(),
     'tax_cart' => uc_price($tax, $context, $options),
 
     // Shopping cart specific variables
@@ -1008,7 +968,7 @@ function uc_paypal_wps_form($form_state,
       $data['quantity_'. $i] = $item->qty;
 
       // PayPal will only display the first two...
-      if (!empty($item->data['attributes']) && count($item->data['attributes']) > 0) {
+      if (is_array($item->data['attributes']) && count($item->data['attributes']) > 0) {
         $o = 0;
         foreach ($item->data['attributes'] as $name => $setting) {
           $data['on'. $o .'_'. $i] = $name;
@@ -1017,20 +977,6 @@ function uc_paypal_wps_form($form_state,
         }
       }
     }
-
-    // Apply discounts (negative amount line items). For example, this handles line items created by uc_coupon.
-    $discount = 0;
-
-    foreach ($order->line_items as $item) {
-      if ($item['amount'] < 0) {
-        // The minus sign is not an error! The discount amount must be positive.
-        $discount -= $item['amount'];
-      }
-    }
-
-    if ($discount != 0) {
-      $data['discount_amount_cart'] = $discount;
-    }
   }
   else {
     // List the whole cart as a single item to account for fees/discounts
@@ -1058,28 +1004,21 @@ function uc_paypal_wps_form($form_state,
   return $form;
 }
 
-/**
- * Sends a request to PayPal and returns a response array.
- */
+// Sends a request to PayPal and returns a response array.
 function uc_paypal_api_request($request, $server) {
-  // We use $request += to add API credentials so that
-  // if a key already exists, it will not be overridden
-  $request += array(
-    'USER' => variable_get('uc_paypal_api_username', ''),
-    'PWD' => variable_get('uc_paypal_api_password', ''),
-    'VERSION' => '3.0',
-    'SIGNATURE' => variable_get('uc_paypal_api_signature', ''),
-  );
+  $request['USER'] = variable_get('uc_paypal_api_username', '');
+  $request['PWD'] = variable_get('uc_paypal_api_password', '');
+  $request['VERSION'] = UC_PAYPAL_API;
+  $request['SIGNATURE'] = variable_get('uc_paypal_api_signature', '');
 
   $data = '';
   foreach ($request as $key => $value) {
-    $data .= $key .'='. urlencode(str_replace(',', '', $value)) .'&';
+    $data .= $key .'='. urlencode(ereg_replace(',', '', $value)) .'&';
   }
   $data = substr($data, 0, -1);
 
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $server);
-  curl_setopt($ch, CURLOPT_VERBOSE, 0);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@@ -1095,9 +1034,7 @@ function uc_paypal_api_request($request,
   return _uc_paypal_nvp_to_array($response);
 }
 
-/**
- * Returns the description and subtotal of the products on an order.
- */
+// Returns the description and subtotal of the products on an order.
 function _uc_paypal_product_details($items) {
   $desc = '';
   $subtotal = 0;
@@ -1107,7 +1044,7 @@ function _uc_paypal_product_details($ite
       if (!empty($desc)) {
         $desc .= ' / ';
       }
-      $desc .= $item->qty .'× '. $item->title;
+      $desc .= $item->qty .'&times; '. $item->title;
       $subtotal += $item->qty * $item->price;
     }
   }
@@ -1115,9 +1052,7 @@ function _uc_paypal_product_details($ite
   return array($desc, $subtotal);
 }
 
-/**
- * Returns the PayPal approved credit card type for a card number.
- */
+// Returns the PayPal approved credit card type for a card number.
 function _uc_paypal_card_type($cc_number) {
   switch (substr(strval($cc_number), 0, 1)) {
     case '3':
@@ -1133,9 +1068,7 @@ function _uc_paypal_card_type($cc_number
   return FALSE;
 }
 
-/**
- * Turns PayPal's NVP response to an API call into an associative array.
- */
+// Turns PayPal's NVP response to an API call into an associative array.
 function _uc_paypal_nvp_to_array($nvpstr) {
   foreach (explode('&', $nvpstr) as $nvp) {
     list($key, $value) = explode('=', $nvp);
@@ -1145,9 +1078,9 @@ function _uc_paypal_nvp_to_array($nvpstr
   return $nvp_array;
 }
 
-/**
- * Returns a human readable message for the AVS code.
- */
+
+
+// Returns a human readable message for the AVS code.
 function _uc_paypal_avscode_message($code) {
   if (is_numeric($code)) {
     switch ($code) {
@@ -1200,9 +1133,7 @@ function _uc_paypal_avscode_message($cod
   }
 }
 
-/**
- * Returns a human readable message for the CVV2 match code.
- */
+// Returns a human readable message for the CVV2 match code.
 function _uc_paypal_cvvmatch_message($code) {
   if (is_numeric($code)) {
     switch ($code) {
@@ -1239,9 +1170,7 @@ function _uc_paypal_cvvmatch_message($co
   }
 }
 
-/**
- * Return a message for the pending reason of a PayPal payment.
- */
+// Return a message for the pending reason of a PayPal payment.
 function _uc_paypal_pending_message($reason) {
   switch ($reason) {
     case 'address':
@@ -1266,9 +1195,7 @@ function _uc_paypal_pending_message($rea
   }
 }
 
-/**
- * Return a message for the reason code of a PayPal reversal.
- */
+// Return a message for the reason code of a PayPal reversal.
 function _uc_paypal_reversal_message($reason) {
   switch ($reason) {
     case 'chargeback':
@@ -1285,9 +1212,8 @@ function _uc_paypal_reversal_message($re
   }
 }
 
-/**
- * Returns an array of possible currency codes.
- */
+// Returns an array of possible currency codes.
 function _uc_paypal_currency_array() {
-  return drupal_map_assoc(array('AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'SEK', 'SGD', 'THB', 'TWD', 'USD'));
+  return drupal_map_assoc(array('AUD', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'SEK', 'SGD', 'USD'));
 }
+
