Index: uc_worldpay.module =================================================================== --- uc_worldpay.module (revision 1695) +++ uc_worldpay.module (working copy) @@ -18,6 +18,12 @@ 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['cart/worldpay/finish'] = array( + 'title' => t('Finished order'), + 'page callback' => 'uc_worldpay_finish_page', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -262,8 +268,8 @@ // Stop orders being processed for orders from different hosts. if ($_SERVER['HTTP_HOST'] != $_POST['M_http_host']) { - print t('There was an error with the transaction. The host did not match.'); - exit(); + _uc_worldpay_redirect(t('There was an error with the transaction. The host did not match.')); + return; } // Log a new order notification to watchdog. @@ -284,14 +290,14 @@ // If the order could not be loaded print an error message and exit. if (!$order = uc_order_load($cart_id)) { - print t('The order could not be found and this transaction cannot continue.'); - exit(); + _uc_worldpay_redirect(t('The order could not be found and this transaction cannot continue.')); + return; } // If the status of the order is not 'in_checkout' print an error and exit. if (uc_order_status_data($order->order_status, 'state') != 'in_checkout') { - print t('An error has occurred during payment. Please contact us to ensure your order has submitted.'); - exit(); + _uc_worldpay_redirect(t('An error has occurred during payment. Please contact us to ensure your order has submitted.')); + return; } if (is_numeric($amount)) { @@ -303,26 +309,121 @@ break; case 'C': $output .= t('Your order has been cancelled.'); - drupal_set_message(t('Your order has been cancelled.')); uc_order_comment_save($order->order_id, 0, t('Payment cancelled by user.'), 'admin'); - print $output; - exit(); - break; + _uc_worldpay_redirect($output); + return; default: - $output .= t('Your order is pending.'); - drupal_set_message(t('Your order will be processed as soon as your payment clears at Worldpay.')); + $output .= t('Your order is pending. Your order will be processed as soon as your payment clears at Worldpay.'); uc_order_comment_save($order->order_id, 0, t('!type payment is pending approval at Worldpay.', array('!type' => $card_type)), 'admin'); break; } } - $output .= uc_cart_complete_sale($order); - uc_cart_empty($uc_cart_id); + // Make sure we register the fact the payment happened and pull relevant triggers. Then + // redirect to the site itself for the well-done message. + if ($order->uid == 0) { + // Check for an existing user account with the e-mail address from checkout. + $result = db_query("SELECT uid FROM {users} WHERE mail = '%s'", $order->primary_email); + // If it was found, update the order. + if ($account = db_fetch_object($result)) { + $order->uid = $account->uid; + db_query("UPDATE {uc_orders} SET uid = %d WHERE order_id = %d", $order->uid, $order->order_id); + } + else { + // Get a valid new username. + if (empty($order->data['new_user']['name'])) { + $name = uc_store_email_to_username($order->primary_email); + } + else { + $name = $order->data['new_user']['name']; + } + + // Setup the account fields array and save it as a new user. + $fields = array( + 'name' => $name, + 'mail' => $order->primary_email, + 'init' => $order->primary_email, + 'pass' => empty($order->data['new_user']['pass']) ? user_password(variable_get('uc_pwd_length', 6)) : $order->data['new_user']['pass'], + 'roles' => array(), + 'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0, + ); + $account = user_save('', $fields); + + // Send the customer their account details if enabled. + if (variable_get('uc_new_customer_email', TRUE)) { + // Manually set the password so it appears in the e-mail. + $account->password = $fields['pass']; + + // Send the e-mail through the user module. + drupal_mail('user', 'register_no_approval_required', $order->primary_email, NULL, array('account' => $account), uc_store_email_from()); + } + + // Update the order's uid in this request and in the database. + $order->uid = $account->uid; + unset($order->data['new_user']['pass']); + db_query("UPDATE {uc_orders} SET uid = %d, data = '%s' WHERE order_id = %d", $order->uid, serialize($order->data), $order->order_id); + } + } + + $status = db_result(db_query("SELECT order_status FROM {uc_orders} WHERE order_id = %d", $order->order_id)); + if (uc_order_status_data($status, 'state') == 'in_checkout') { + uc_order_update_status($order->order_id, uc_order_state_default('post_checkout')); + } + + // Empty that cart... + uc_cart_empty($order->uid); + + // Pull triggers + $account = user_load($order->uid); + module_invoke_all('uc_checkout_complete', $order, $account); + ca_pull_trigger('uc_checkout_complete', $order, $account); + // Add a comment to let sales team know this came in through the site. uc_order_comment_save($order->order_id, 0, t('Order created through website.'), 'admin'); - print $output; + _uc_worldpay_redirect($output, $cart_id); + return; +} + +/* + * This function displays the confirmation page to redirect the user to the website. + */ +function _uc_worldpay_redirect($txt, $cart_id = 0) { + global $base_url; + + if ($cart_id == 0) { + $url = $base_url; + $message = "