/[drupal]/contributions/modules/uc_worldpay/uc_worldpay.module
ViewVC logotype

Contents of /contributions/modules/uc_worldpay/uc_worldpay.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Oct 28 23:51:07 2009 UTC (4 weeks, 3 days ago) by matason
Branch: MAIN
Changes since 1.4: +2 -2 lines
File MIME type: text/x-php
by matason: Tidy up, removed t() from 'title' key in hook_menu().
1 <?php
2 // $Id: uc_worldpay.module,v 1.4 2009/10/28 23:03:52 matason Exp $
3
4 /**
5 * @file
6 * Integrates Worldpay's redirected payment service with Ubercart.
7 *
8 * Original development sponsored by www.catorg.co.uk.
9 */
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function uc_worldpay_menu() {
15 $items['cart/worldpay/complete'] = array(
16 'title' => 'Order complete',
17 'page callback' => 'uc_worldpay_complete',
18 'access arguments' => array('access content'),
19 'type' => MENU_CALLBACK,
20 );
21
22 return $items;
23 }
24
25 /**
26 * Implementation of hook_payment_method().
27 */
28 function uc_worldpay_payment_method() {
29 $methods[] = array(
30 'id' => 'worldpay',
31 'name' => t('Worldpay'),
32 'title' => theme('uc_worldpay_cards'),
33 'desc' => t('Redirect to Worldpay to pay by credit card or eCheck.'),
34 'callback' => 'uc_payment_method_worldpay',
35 'weight' => 3,
36 'checkout' => TRUE,
37 'no_gateway' => TRUE,
38 );
39
40 return $methods;
41 }
42
43 function theme_uc_worldpay_cards() {
44 $image_path = drupal_get_path('module', 'uc_worldpay') .'/images/';
45 $title = variable_get('uc_worldpay_method_title', t('Worldpay'));
46 $output = theme('image', $image_path .'worldpay.png', $title .': ', $title, array('style' => 'position:relative; top:6px;'));
47
48 $card_types = variable_get('uc_worldpay_payment_methods', array('visa', 'electron', 'mastercard', 'maestro', 'switch', 'solo'));
49 foreach ($card_types as $card => $title) {
50 if ($title != "0") {
51 $output .= theme('image', $image_path . $card .'.gif', '', '', array('style' => 'position:relative; top:5px; margin-right:4px;'));
52 }
53 }
54
55 return $output;
56 }
57
58 /**
59 * Payment method settings.
60 */
61 function uc_payment_method_worldpay($op, &$arg1) {
62 switch ($op) {
63
64 case 'cart-process':
65 $_SESSION['pay_method'] = $_POST['pay_method'];
66 return;
67
68 case 'settings':
69 $form['help_text']['worldpay_settings'] = array(
70 '#type' => 'item',
71 '#prefix' => '<div class="help">',
72 '#value' => t('<h4><strong>Installation instructions</strong></h4>
73 <p>For this module to work properly you must configure a few specific options in your Worldpay account under <em>Installation Administration</em> settings:</p>
74 <ul><li><strong>Payment Response URL</strong> should be set to: %response_url</li>
75 <li><strong>Payment Response enabled?</strong> should be <em>enabled</em></li>
76 <li><strong>Enable the Shopper Response</strong> should be <em>enabled</em> to get the Ubercart response page (optional)',
77 array('%response_url' => url('cart/worldpay/complete', array()))),
78 '#suffix' => '</div>',
79 );
80 $form['uc_worldpay_sid'] = array(
81 '#type' => 'textfield',
82 '#title' => t('Installation ID'),
83 '#default_value' => variable_get('uc_worldpay_sid', ''),
84 '#size' => 16,
85 );
86 $form['uc_worldpay_debug'] = array(
87 '#type' => 'select',
88 '#title' => t('Debug mode'),
89 '#multiple' => FALSE,
90 '#options' => array('log' => t('Log'), 'screen' => t('Screen'), 'both' => t('Both'), 'none' => t('None')),
91 '#default_value' => variable_get('uc_worldpay_debug', 'log'),
92 );
93 $form['uc_worldpay_checkout_button'] = array(
94 '#type' => 'textfield',
95 '#title' => t('Order review submit button text'),
96 '#description' => t('Alter the text of the submit button on the review order page.'),
97 '#default_value' => variable_get('uc_worldpay_checkout_button', t('Submit Order')),
98 );
99 $form['payment_methods'] = array(
100 '#type' => 'fieldset',
101 '#title' => t('Payment methods'),
102 '#description' => t('Select the payment methods to display in checkout.'),
103 '#collapsible' => TRUE,
104 '#collapsed' => TRUE,
105 );
106 $form['payment_methods']['uc_worldpay_payment_methods'] = array(
107 '#type' => 'checkboxes',
108 '#default_value' => variable_get('uc_worldpay_payment_methods', array('visa', 'electron', 'mastercard', 'maestro', 'switch', 'solo')),
109 '#options' => array(
110 'visa' => t('Visa'),
111 'electron' => t('Visa Electron'),
112 'mastercard' => t('Mastercard'),
113 'maestro' => t('Maestro'),
114 'switch' => t('Switch'),
115 'solo' => t('Solo'),
116 'amex' => t('Amex'),
117 'diners' => t('Diners'),
118 'jcb' => t('JCB'),
119 'elv' => t('ELV'),
120 'laser' => t('Laser'),
121 ),
122 );
123 $form['payment_parameters'] = array(
124 '#type' => 'fieldset',
125 '#title' => t('Payment parameters'),
126 '#description' => t('These options control what parameters are sent to Worldpay when the customer submits the order.'),
127 '#collapsible' => TRUE,
128 '#collapsed' => TRUE,
129 );
130 $form['payment_parameters']['uc_worldpay_test'] = array(
131 '#type' => 'checkbox',
132 '#title' => t('Enable test mode'),
133 '#default_value' => variable_get('uc_worldpay_test', TRUE),
134 );
135 $disabled = (!variable_get('uc_worldpay_test', TRUE)) ? TRUE : FALSE;
136 $form['payment_parameters']['uc_worldpay_test_result'] = array(
137 '#type' => 'select',
138 '#title' => t('Test mode result'),
139 '#description' => t('Specify the required transaction result when working in test mode.'),
140 '#default_value' => variable_get('uc_worldpay_test_result', 'AUTHORISED'),
141 '#options' => array('AUTHORISED' => 'Authorised', 'REFUSED' => 'Refused', 'ERROR' => 'Error', 'CAPTURED' => 'Captured'),
142 '#disabled' => $disabled,
143 );
144 $form['payment_parameters']['uc_worldpay_desc'] = array(
145 '#type' => 'checkbox',
146 '#title' => t('Submit the cart contents as the order description'),
147 '#description' => t('Setting this option to true will display the cart contents on the payment page. This could help to reassure customers of exactly what they are paying for.'),
148 '#default_value' => variable_get('uc_worldpay_desc', FALSE),
149 );
150 $form['payment_parameters']['uc_worldpay_edit_contact'] = array(
151 '#type' => 'checkbox',
152 '#title' => t('Enable editing of contact details on the payment page.'),
153 '#default_value' => variable_get('uc_worldpay_show_contact', TRUE),
154 );
155 $form['payment_parameters']['uc_worldpay_show_contact'] = array(
156 '#type' => 'checkbox',
157 '#title' => t('Show the contact details on the payment page.'),
158 '#default_value' => variable_get('uc_worldpay_show_contact', TRUE),
159 '#disabled' => variable_get('uc_worldpay_show_contact', TRUE),
160 );
161 $form['payment_parameters']['uc_worldpay_lang'] = array(
162 '#type' => 'textfield',
163 '#title' => t('Payment page language'),
164 '#description' => t('Specify the payment page language. Enter a 2-character ISO 639 language code, with optional regionalisation using 2-character country code separated by hyphen. For example "en-GB" specifies UK English.'),
165 '#size' => 8,
166 '#maxlength' => 6,
167 '#default_value' => variable_get('uc_worldpay_lang', 'en-GB'),
168 );
169 $form['payment_urls'] = array(
170 '#type' => 'fieldset',
171 '#title' => t('Payment URLs'),
172 '#collapsible' => TRUE,
173 '#collapsed' => TRUE,
174 );
175 $form['payment_urls']['uc_worldpay_test_url'] = array(
176 '#type' => 'textfield',
177 '#title' => t('Test URL'),
178 '#description' => t('The Worldpay test environment URL.'),
179 '#default_value' => variable_get('uc_worldpay_test_url', 'https://select-test.wp3.rbsworldpay.com/wcc/purchase'),
180 );
181 $form['payment_urls']['uc_worldpay_live_url'] = array(
182 '#type' => 'textfield',
183 '#title' => t('Live URL'),
184 '#description' => t('The Worldpay live environment URL.'),
185 '#default_value' => variable_get('uc_worldpay_live_url', 'https://secure.wp3.rbsworldpay.com/wcc/purchase'),
186 );
187
188 return $form;
189 }
190 }
191
192 /**
193 * Implementation of hook_form_alter().
194 */
195 function uc_worldpay_form_alter(&$form, $form_state, $form_id) {
196 if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) {
197 $order = uc_order_load($order_id);
198
199 if ($order->payment_method == 'worldpay') {
200
201 $country_data = uc_get_country_data(array('country_id' => $order->billing_country));
202 $country = $country_data[0]['country_iso_code_2'];
203
204 $cart_contents = uc_cart_get_contents();
205 foreach ($cart_contents as $item) {
206 $cart_items[] = $item->qty .'x '. $item->title;
207 }
208
209 $uc_worldpay_name = substr($order->billing_first_name .' '. $order->billing_last_name, 0, 128);
210
211 $data = array();
212 if (variable_get('uc_worldpay_test', TRUE)) {
213 $uc_worldpay_name = variable_get('uc_worldpay_test_result', 'AUTHORISED');
214 $data = array(
215 'testMode' => '100',
216 );
217 }
218
219 $data += array(
220 'instId' => variable_get('uc_worldpay_sid', ''),
221 'amount' => uc_currency_format($order->order_total, FALSE, FALSE, '.'),
222 'cartId' => $order->order_id,
223 'currency' => variable_get('uc_currency_code', 'USD'),
224 'name' => $uc_worldpay_name,
225 'address' => ($order->billing_street1 ? $order->billing_street1 .',' : "\n")
226 . ($order->billing_street2 ? $order->billing_street2 .',' : "\n")
227 . ($order->billing_city ? $order->billing_city : ''),
228 'state' => uc_get_zone_code($order->billing_zone),
229 'postcode' => $order->billing_postal_code,
230 'country' => $country,
231 'email' => $order->primary_email,
232 'tel' => $order->billing_phone,
233 'M_uc_cart_id' => uc_cart_get_id(),
234 'lang' => variable_get('uc_worldpay_lang', 'en-GB'),
235 'M_http_host' => $_SERVER['HTTP_HOST'],
236 );
237
238 if (variable_get('uc_worldpay_desc', FALSE)) {
239 $data += array(
240 'desc' => t("Cart contents: \n!cart", array('!cart' => implode(",\n", $cart_items)))
241 );
242 }
243
244 if (!variable_get('uc_worldpay_edit_contact', TRUE)) {
245 $data += array(
246 'fixContact' => '',
247 );
248 }
249
250 if (!variable_get('uc_worldpay_show_contact', TRUE)) {
251 $data += array(
252 'hideContact' => '',
253 );
254 }
255
256 foreach ($data as $name => $value) {
257 $form[$name] = array(
258 '#type' => 'hidden',
259 '#value' => $value,
260 );
261 }
262
263 $test_server = variable_get('uc_worldpay_test_url', 'https://select-test.wp3.rbsworldpay.com/wcc/purchase');
264 $live_server = variable_get('uc_worldpay_live_url', 'https://secure.wp3.rbsworldpay.com/wcc/purchase');
265 $form['#action'] = (variable_get('uc_worldpay_test', TRUE)) ? $test_server : $live_server;
266 $form['submit'] = array(
267 '#type' => 'submit',
268 '#name' => '',
269 '#value' => variable_get('uc_worldpay_checkout_button', t('Submit Order')),
270 );
271 }
272 }
273 }
274
275 function uc_worldpay_complete($cart_id = 0) {
276 $cart_id = $_POST['cartId'];
277 $amount = $_POST['amount'];
278 $trans_status = $_POST['transStatus'];
279 $card_type = $_POST['cardType'];
280 $uc_cart_id = $_POST['M_uc_cart_id'];
281 global $base_url;
282 drupal_set_html_head("<base href=\"$base_url/\" />\n");
283
284 // Stop orders being processed for orders from different hosts.
285 if ($_SERVER['HTTP_HOST'] != $_POST['M_http_host']) {
286 print theme('page', t('There was an error with the transaction. The host did not match.'));
287 return;
288 }
289
290 // Log a new order notification to watchdog.
291 $log_entry = t('New order notification for order !order_id.', array('!order_id' => $cart_id));
292 $message = t('Returned parameters: <pre>!post</pre>', array('!post' => print_r($_POST, TRUE)));
293
294 // If debug mode is set appropriately, append the returned parameters to the log entry.
295 $debug = variable_get('uc_worldpay_debug', 'log');
296 if ($debug == 'log' || $debug == 'both') {
297 $log_entry .= '<br/>'. $message;
298 }
299 watchdog('uc_worldpay', $log_entry);
300
301 // If debug mode is set appropriately, print the returned parameters to the screen.
302 if ($debug == 'screen' || $debug == 'both') {
303 $output .= $message;
304 }
305
306 // If the order could not be loaded print an error message and exit.
307 if (!$order = uc_order_load($cart_id)) {
308 print theme('page', t('The order could not be found and this transaction cannot continue.'));
309 return;
310 }
311
312 // If the status of the order is not 'in_checkout' print an error and exit.
313 if (uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
314 print theme('page', t('An error has occurred during payment. Please contact us to ensure your order has been submitted.'));
315 return;
316 }
317
318 if (is_numeric($amount)) {
319 switch ($trans_status) {
320 case 'Y':
321 $output .= t('Your order is complete and payment has been confirmed.');
322 $comment = t('Paid by !type, Worldpay order #!order.', array('!type' => $card_type , '!order' => $cart_id));
323 uc_payment_enter($order->order_id, 'Worldpay', $amount, 0, NULL, $comment);
324 break;
325 case 'C':
326 $output .= t('Your order has been cancelled.');
327 drupal_set_message(t('Your order has been cancelled.'));
328 uc_order_comment_save($order->order_id, 0, t('Payment cancelled by user.'), 'admin');
329 print theme('page', $output);
330 return;
331 break;
332 default:
333 $output .= t('Your order is pending.');
334 drupal_set_message(t('Your order will be processed as soon as your payment clears at Worldpay.'));
335 uc_order_comment_save($order->order_id, 0, t('!type payment is pending approval at Worldpay.', array('!type' => $card_type)), 'admin');
336 break;
337 }
338 }
339
340 $output .= uc_cart_complete_sale($order);
341 uc_cart_empty($uc_cart_id);
342
343 // Add a comment to let sales team know this came in through the site.
344 uc_order_comment_save($order->order_id, 0, t('Order created through website.'), 'admin');
345
346 print theme('page', $output);
347 return;
348 }
349
350 /**
351 * Implementation of hook_theme().
352 */
353 function uc_worldpay_theme() {
354 return array(
355 'uc_worldpay_cards' => array(
356 'arguments' => array(),
357 ),
358 );
359 }

  ViewVC Help
Powered by ViewVC 1.1.2