Index: worldpay.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/contrib/worldpay/worldpay.module,v
retrieving revision 1.2
diff -u -r1.2 worldpay.module
--- worldpay.module	4 Mar 2006 00:34:55 -0000	1.2
+++ worldpay.module	17 Mar 2006 20:27:30 -0000
@@ -15,51 +15,129 @@
   }
 }
 
-
 /**
  * Implementation of hook_settings().
  */
 function worldpay_settings() {
-	
-    $output = '';
-    // Installation ID (instId)
-    $group = form_textfield(t('Installation ID (instId)'), 'worldpay_instId',
-    variable_get('worldpay_instId', '00000'), 20, 255,
-    t('WorldPay Installation ID. This should be set to your unique ID, which was given to 
-       you by WorldPay.'));
+
+  $output = '';
+  // Installation ID (instId)
+  $form['wp_main'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Main settings'),
+  );
+  $form['wp_main']['worldpay_instId'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Installation ID (instId)'),
+    '#default_value' => variable_get('worldpay_instId', '00000'),
+    '#size' => 20,
+    '#maxlength' => 255,
+    '#description' => t('WorldPay Installation ID. This should be set to your unique ID, which was given to you by WorldPay.'),
+  );
 
   // Testing Mode (testMode)
-    $testMode_codes = array('0' => 'Live payment processing (0)', '100' => 'Test mode (100) - return success', '101' => 'Test mode (101) - return failure');
-    $group .= form_select(t('Testing Mode (testMode)'), 'worldpay_testMode', 
-    variable_get('worldpay_testMode', '100'), $testMode_codes,
-    t('WorldPay testMode code. Set to 0 for live shop, 100 or 101 for testing.'));
+  $testMode_codes = array('0' => 'Live payment processing (0)', '100' => 'Test mode (100) - return success', '101' => 'Test mode (101) - return failure');
+  $form['wp_main']['worldpay_testMode'] = array(
+    '#type' => 'select',
+    '#title' => t('Testing Mode (testMode)'),
+    '#default_value' => variable_get('worldpay_testMode', '100'),
+    '#options' => $testMode_codes,
+    '#description' => t('WorldPay testMode code. Set to 0 for live shop, 100 or 101 for testing.'),
+  );
 
   // Currency code (currency)
-    $currency_codes = array('USD' => 'USD - U.S. Dollars', 'EUR' => 'EUR - Euros', 'AUD' => 'AUD - Australian Dollars', 'CAD' => 'CAD - Canadian Dollars', 'GBP'=> 'GBP - Pounds Sterling');
-    $group .= form_select(t('Currency code (currency)'), 'worldpay_currency_code', 
-    variable_get('worldpay_currency_code', 'GBP'), $currency_codes, t('The currecy code that WorldPay should process the payment in.'));
+  $currency_codes = array('USD' => 'USD - U.S. Dollars', 'EUR' => 'EUR - Euros', 'AUD' => 'AUD - Australian Dollars', 'CAD' => 'CAD - Canadian Dollars', 'GBP'=> 'GBP - Pounds Sterling');
+  $form['wp_main']['worldpay_currency_code'] = array(
+    '#type' => 'select',
+    '#title' => t('Currency code (currency)'),
+    '#default_value' => variable_get('worldpay_currency_code', 'EUR'),
+    '#options' => $currency_codes,
+    '#description' => t('The currecy code that WorldPay should process the payment in.'),
+  );
+
+  // Currency code (currency)
+  $currency_codes = array('USD' => 'USD - U.S. Dollars', 'EUR' => 'EUR - Euros', 'AUD' => 'AUD - Australian Dollars', 'CAD' => 'CAD - Canadian Dollars', 'GBP'=> 'GBP - Pounds Sterling');
+  $form['wp_main']['worldpay_currency_code'] = array(
+    '#type' => 'select',
+    '#title' => t('Currency code (currency)'),
+    '#default_value' => variable_get('worldpay_currency_code', 'EUR'),
+    '#options' => $currency_codes,
+    '#description' => t('The currecy code that WorldPay should process the payment in.'),
+  );
 
   // WorldPay Minimum Amount
-    $group .= form_textfield(t('WorldPay Minimum Amount'), 'worldpay_min_amount',
-    variable_get('worldpay_min_amount', '5.00'), 10, 10, t('The minimum purchase amount (gross) that will be accepted for WorldPay payments.'));
-    $output .= form_group(t('Main settings'),$group);
+  $form['wp_main']['worldpay_min_amount'] = array(
+    '#type' => 'textfield',
+    '#title' => t('WorldPay Minimum Amount'),
+    '#default_value' => variable_get('worldpay_min_amount', '5.00'),
+    '#size' => 10,
+    '#maxlength' => 10,
+    '#description' => t('The minimum purchase amount (gross) that will be accepted for WorldPay payments.'),
+  );
+
+  // WorldPay processing URL      
+  $form['wp_main']['worldpay_url'] = array(
+    '#type' => 'textfield',
+    '#title' => t('WorldPay processing URL'),
+    '#default_value' => variable_get('worldpay_url', 'https://select.worldpay.com/wcc/purchase'),
+    '#size' => 60,
+    '#maxlength' => 180,
+    '#description' => t('URL of the secure payment page customers are sent to for payment processing (Select Junior). If unsure leave at default setting.'),
+  );
+  // WorldPay callback URL
+  $form['wp_main']['worldpay_callback_url'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Thank you page/WorldPay callback URL'),
+    '#default_value' => variable_get('worldpay_callback_url', 'node'),
+    '#size' => 60,
+    '#maxlength' => 180,
+    '#description' => t('<P><strong>BASIC USAGE (recommended):</strong> Redirect users to a specific thank you page and process your orders manually from Worldpay emails and your worldpay account.</p><P><strong>ADVANCED USAGE:</strong> Insert <strong>worldpay_callback</strong> as the link that is used for processing payment confirmation (Select Junior). This also needs to be configured in the settings on the WorldPay server as the full URL. Please refer to the README.TXT that came with the worldpay.module for more detailed instructions.</p>'),
+  );
+
+  $form['wp_redirect'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Worldpay redirect page settings'),
+    '#collapsible' => true,
+    '#collapsed' => true,
+  );
 
   // WorldPay processing form settings
-    $group = form_textarea(t("Worldpay Submission Form (top)"), "worldpay_submit_form_top", variable_get("worldpay_submit_form_top", "<P>Name: %name</p><P>Address: %address</p><p>Order description: %order_description</p><p>Total amount: %total_amount, </p><p>reference number: %order_reference_number</p>"), 60, 10, t("Paste your HTML here for the top of your Worldpay submission form. This is the confirmation page users will see before being directed to the Worldpay secure server and complete their transaction. Available variables are: %name, %address, %order_description, %total_amount, %order_reference_number"));
-    $group .= form_textarea(t("Make Payment Button help text"), "worldpay_submit_button_text", variable_get("worldpay_submit_button_text", "You will now be transferred to the Worldpay secure server to enter your credit or debit card details.Please click on the MAKE PAYMENT button now:"), 60, 4, t("Edit the text you would like to display to users before clicking on the MAKE PAYMENT button."));
-    $group .= form_textfield(t('Make Payment button title'), 'worldpay_button_label', variable_get('worldpay_button_label', 'Make Payment'), 70, 70, t('Change the text that appears on your MAKE PAYMENT button'));
-    $group .= form_textarea(t("Worldpay Submission Form (bottom)"), "worldpay_submit_form_bottom", variable_get("worldpay_submit_form_bottom", "We accept payment using all the credit and debit cards shown below, however we would appreciate it if you can pay using a debit card as we incur a smaller transaction charge."), 60, 5, t("Paste your HTML here for the bottom of the Worldpay submission form. this text appears below the MAKE PAYMENT button and above the auto-generated credit & debit card icons your Worldpay account is able to accept."));
+  $form['wp_redirect']['worldpay_submit_form_top'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Worldpay Submission Form (top)'),
+    '#default_value' => variable_get('worldpay_submit_form_top', '<P>Name: %name</p><P>Address: %address</p><p>Order description: %order_description</p><p>Total amount: %total_amount, </p><p>reference number: %order_reference_number</p>'),
+    '#cols' => 60,
+    '#rows' => 10,
+    '#description' => t('Paste your HTML here for the top of your Worldpay submission form. This is the confirmation page users will see before being directed to the Worldpay secure server and complete their transaction. Available variables are: %name, %address, %order_description, %total_amount, %order_reference_number'),
+  );
+  $form['wp_redirect']['worldpay_submit_button_text'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Make Payment Button help text'),
+    '#default_value' => variable_get('worldpay_submit_button_text', 'You will now be transferred to the Worldpay secure server to enter your credit or debit card details.Please click on the MAKE PAYMENT button now:'),
+    '#cols' => 60,
+    '#rows' => 4,
+    '#description' => t('Edit the text you would like to display to users before clicking on the MAKE PAYMENT button.'),
+  );
+  $form['wp_redirect']['worldpay_button_label'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Make Payment button title'),
+    '#default_value' => variable_get('worldpay_button_label', 'Make Payment'),
+    '#size' => 70,
+    '#maxlength' => 70,
+    '#description' => t('Change the text that appears on your MAKE PAYMENT button'),
+  );
+  $form['wp_redirect']['worldpay_submit_form_bottom'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Worldpay Submission Form (bottom)'),
+    '#default_value' => variable_get('worldpay_submit_form_bottom', 'We accept payment using all the credit and debit cards shown below, however we would appreciate it if you can pay using a debit card as we incur a smaller transaction charge.'),
+    '#cols' => 60,
+    '#rows' => 5,
+    '#description' => t('Paste your HTML here for the bottom of the Worldpay submission form. this text appears below the MAKE PAYMENT button and above the auto-generated credit & debit card icons your Worldpay account is able to accept.'),
+  );
 
-      // WorldPay processing URL      
-    $group .= form_textfield(t('WorldPay processing URL'), 'worldpay_url',variable_get('worldpay_url', 'https://select.worldpay.com/wcc/purchase'), 60, 180, t('URL of the secure payment page customers are sent to for payment processing (Select Junior). If unsure leave at default setting.'));
-  // WorldPay callback URL
-    $group .= form_textfield(t('Thank you page/WorldPay callback URL'), 'worldpay_callback_url',variable_get('worldpay_callback_url', 'node'), 60, 180, t('<P><strong>BASIC USAGE (recommended):</strong> Redirect users to a specific thank you page and process your orders manually from Worldpay emails and your worldpay account.</p><P><strong>ADVANCED USAGE:</strong> Insert <strong>worldpay_callback</strong> as the link that is used for processing payment confirmation (Select Junior). This also needs to be configured in the settings on the WorldPay server as the full URL. Please refer to the README.TXT that came with the worldpay.module for more detailed instructions.</p>'));
-    $output .= form_group(t('Worldpay checkout settings'),$group);
-    return $output;
+  return $form;
 }
 
-
-
 /***
  * Implementation of ecommerce paymentapi (payment.module)
  */
@@ -69,26 +147,24 @@
     case 'display name':
       return t('Credit/Debit Card (WorldPay)');
 
-    case 'update':
-    case 'insert':
-     // return worldpay_save($data);
-
     case 'on checkout':
       return worldpay_verify_checkout($data);
 
     case 'payment page':
-      if ($data['gross'] > 0) {
+      if ($data->gross > 0) {
        return worldpay_form($data);
-       // return 'We apologise that credit cards can not yet be accepted online.  This should be available in the next few days.  Thankyou for your patience.';
       }
       break;
 
+    case 'update':
+    case 'insert':
+     // return worldpay_save($data);
+
     case 'delete':
      // return worldpay_delete($data);
     }
 }
 
-
 /**
  * Implementation of hook_menu() - for worldpay callback
  */
@@ -96,139 +172,113 @@
   $worldpay_callback_url = variable_get('worldpay_callback_url', 'worldpay_callback');
 
   $items = array();
-//  if ($may_cache) {
+  if ($may_cache) {
     $items[] = array('path' => $worldpay_callback_url, 'title' => t('WorldPay callback'),
       'callback' => 'worldpay_callback_process', 'access' => true, 'type' => MENU_CALLBACK);
-//  }
+  }
 
   return $items;
 }
 
-
-
 /***
  * WorldPay verify checkout (check minimum purchase amount is met).
  */
 function worldpay_verify_checkout($data) {
-  if (variable_get('worldpay_min_amount', '0') > $data['gross']) {
-    //form_set_error('gross', t('Your purchase total must be at least %min-purchase-amount for online Credit card purchases.', array('%min-purchase-amount' => payment_format(variable_get('paypal_min_amount', '0')))));
-    form_set_error('gross', t('Your purchase total must be at least ??%min-purchase-amount for online Credit card purchases.', array('%min-purchase-amount' => variable_get('worldpay_min_amount', '0'))));
-    //form_set_error('gross', t('We apologise that credit and debit card purchases can not yet be accepted online.  This should be available in the next few days.  Thank you for your patience.  Please choose an alternate payment method.'));
+  if (variable_get('worldpay_min_amount', '0') > $data->gross) {
+    form_set_error('gross', t('Your purchase total must be at least %min-purchase-amount for online Credit card purchases.', array('%min-purchase-amount' => variable_get('worldpay_min_amount', '0'))));
   }
 }
 
-
 /***
  * WorldPay payment form.
  */
 function worldpay_form($data) {
 
   // Get relevant settings for form
-  $worldpay_url = variable_get('worldpay_url', '');
-  $worldpay_testMode = variable_get('worldpay_testMode', '');
-  $worldpay_currency_code = variable_get('worldpay_currency_code', '');
-  $worldpay_instId = variable_get('worldpay_instId', '');
-
-  // Format billing address as 1 field for worldpay  
-  $address = $data[billing_street1];
-  if ($data[billing_street2]) {
-    $address .= '&#10;' . $data[billing_street2];
+  $worldpay_url = variable_get('worldpay_url', 'https://select.worldpay.com/wcc/purchase');
+  $worldpay_testMode = variable_get('worldpay_testMode', '100');
+  $worldpay_currency_code = variable_get('worldpay_currency_code', 'EUR');
+  $worldpay_instId = variable_get('worldpay_instId', '00000');
+
+  // Format billing address as 1 field for worldpay
+  $address = $data->billing_street1;
+  if ($data->billing_street2) {
+    $address .= "\n" . $data->billing_street2;
   }
-  if ($data[billing_city]) {
-    $address .= '&#10;' . $data[billing_city];
+  if ($data->billing_city) {
+    $address .= "\n" . $data->billing_city;
   }
-  if ($data[billing_state]) {
-    $address .= '&#10;' . $data[billing_state];
+  if ($data->billing_state) {
+    $address .= "\n" . $data->billing_state;
   }
 
   // Format billing name as 1 field for worldpay
-  $name = $data[billing_firstname] . ' ' . $data[billing_lastname];
-
-  // readable data dump for debugging
-  $data_print_r = print_r($data, true);
+  $name = $data->billing_firstname . ' ' . $data->billing_lastname;
 
   // Build a summary description of items being purchased.
-  $description_summary = '';
   $items = cart_get_items();
+  $token = '';
   foreach ($items as $product) {
     $node = node_load($product->nid);
-    $description_summary .= $node->title;
-    /* if ($product->sku) {
-      $description_summary .= ' [' . $product->sku . ']';
-    } */
-    $description_summary .= ' (x' . $product->qty . '), <br>&#10;';
+    if(isset($description_summary)) {
+      $description_summary .= ', ';
+    }  
+    $description_summary .= $node->title . ' (x' . $product->qty . ')';
   }
 
   // Payment symbol
   $payment_symbol = variable_get('payment_symbol', '');
   
   // Submit button label
-  $submit_label = variable_get("worldpay_button_label", "Make Payment Now");
+  $submit_label = variable_get('worldpay_button_label', 'Make Payment Now');
   
   // Submit button help text
-  $button_help = variable_get("worldpay_submit_button_text", "You will now be transferred to the Worldpay secure server to enter your credit or debit card details.Please click on the MAKE PAYMENT button now:");
+  $button_help = variable_get('worldpay_submit_button_text', 'You will now be transferred to the Worldpay secure server to enter your credit or debit card details.Please click on the MAKE PAYMENT button now:');
   
-  // Submit form top
-  $form_top = variable_get("worldpay_submit_form_top", "<P>Name: %name</p><P>Address: %address</p><p>Order description: %order_description</p><p>Total amount: %total_amount, </p><p>reference number: %order_reference_number</p>");
-  $variables = (array('%name' => $name, '%address' => $address, '%order_description' => $description_summary, '%total_amount' => $data[gross], '%order_reference_number' => $data[txnid] ));
-  $top_message = strtr($form_top, $variables);
-  
-
+  // build the msgs
+  $variables = array('%name' => $name, '%address' => $address, '%order_description' => $description_summary, '%total_amount' => $data->gross, '%order_reference_number' => $data->txnid );
+  $form_top = t(variable_get('worldpay_submit_form_top','<P>Name: %name</p><P>Address: %address</p><p>Order description: %order_description</p><p>Total amount: %total_amount</p><p>reference number: %order_reference_number</p>'),
+    $variables);
+  $form_bottom = drupal_eval(variable_get('worldpay_Submit_form_bottom', ''));
   
-  // Submit form bottom
-  $form_bottom = variable_get("worldpay_submit_form_bottom", "We accept payment using all the credit and debit cards shown below, however we would appreciate it if you can pay using a debit card as we incur a smaller transaction charge.");
-  
-  //Test message (when in testing mode)
+  // Test message (when in testing mode)
   if ($worldpay_testMode > 0) {
     drupal_set_message(t('This is a test transaction.'));
   }
 
-  // The worldpay submit form!
-  $form_top = variable_get("worldpay_Submit_form_top", "");
-  $output = drupal_eval($form_top);  
-  $output .= <<<ENDOFFORM
-<!-- 
-<h2>data dump (debug)</h2>
-<pre>
-$data_print_r
-</pre>
--->
-$top_message
-<form action="$worldpay_url" method="POST">
-<input type="hidden" name="instId" value="$worldpay_instId">
-<input type="hidden" name="cartId" value="$data[txnid]">
-<input type="hidden" name="amount" value="$data[gross]">
-<input type="hidden" name="currency" value="$worldpay_currency_code">
-<input type="hidden" name="name" value="$name">
-<input type="hidden" name="address" value="$address">
-<input type="hidden" name="postcode" value="$data[billing_zip]">
-<input type="hidden" name="country" value="GB">
-<input type="hidden" name="email" value="$data[mail]">
-<!-- <input type="hidden" name="tel" value=""> -->
-<input type="hidden" name="desc" value="$description_summary">
-<input type="hidden" name="testMode" value="$worldpay_testMode">
-<!-- custom fields -->
-<input type="hidden" name="MC_txnid" value="$data[txnid]">
-<input type="hidden" name="MC_user_id" value="$data[uid]">
-$button_help
-<p align="right"> <input type="submit" value="$submit_label"></p>
-</form>
-$form_bottom
-<p>
-<script language="JavaScript" src="http://www.worldpay.com/cgenerator/cgenerator.php?instId=$worldpay_instId"></script>
-</p>
-ENDOFFORM;
-// END OF CUSTOMISATION
-  $form_bottom = variable_get("worldpay_Submit_form_bottom", "");
-  $output .= drupal_eval($form_bottom); 
-  print theme('page', $output);
-  
-}
-
+  // Build form
+  $form['#action'] = $worldpay_url;
+  $form['#method'] = 'POST';
+  $form['top_msg'] = array('#value' => $form_top);
+  $form['instID'] = array('#type' => 'hidden', '#value' => $worldpay_instId);
+  $form['cartId'] = array('#type' => 'hidden', '#value' => $data->txnid);
+  $form['amount'] = array('#type' => 'hidden', '#value' => $data->gross);
+  $form['currency'] = array('#type' => 'hidden', '#value' => $worldpay_currency_code);
+  $form['name'] = array('#type' => 'hidden', '#value' => $name);
+  $form['address'] = array('#type' => 'hidden', '#value' => $address);
+  $form['postcode'] = array('#type' => 'hidden', '#value' => $data->billing_zip);
+  $form['country'] = array('#type' => 'hidden', '#value' => $data->billing_country);
+  $form['email'] = array('#type' => 'hidden', '#value' => $data->mail);
+//  $form['tel'] = array('#type' => 'hidden', '#value' => '');
+  $form['desc'] = array('#type' => 'hidden', '#value' => $description_summary);
+  $form['testMode'] = array('#type' => 'hidden', '#value' => $worldpay_testMode);
+  $form['MC_txnid'] = array('#type' => 'hidden', '#value' => $data->txnid);
+  $form['MC_user_id'] = array('#type' => 'hidden', '#value' => $description_summary);
+  $form['desc'] = array('#type' => 'hidden', '#value' => $description_summary);
+  $form['button'] = array('#value' => $button_help);
+  $form['submit'] = array('#type' => 'submit', '#value' => $submit_label);
+  $form['bottom_msg'] = array('#value' => $form_bottom);
+  $form['js'] = array(
+    '#value' => '<script language="JavaScript" src="http://www.worldpay.com/cgenerator/cgenerator.php?instId=' .
+      $worldpay_instId .
+      '"></script>');
 
+  return drupal_get_form('worldpay_redirect', $form);
+}
 
 /**
  * Callback processing for worldpay_callback_url - see worldpay_menu()
+ * @TODO This page needs some work...
  */
 function worldpay_callback_process() {
 
@@ -335,7 +385,4 @@
   print $output;
 }
 
-
-
-
-?>
\ No newline at end of file
+?>
