uc_2checkout.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/ubercart/payment/uc_2checkout/uc_2checkout.module
    2. 6 contributions/ubercart/payment/uc_2checkout/uc_2checkout.module
    3. 7 contributions/ubercart/payment/uc_2checkout/uc_2checkout.module

Integrates 2Checkout.com's redirected payment service.

Functions & methods

NameDescription
uc_2checkout_complete
uc_2checkout_form
uc_2checkout_form_alterImplementation of hook_form_alter().
uc_2checkout_menuImplementation of hook_menu().
uc_2checkout_payment_methodImplementation of hook_payment_method().
uc_2checkout_ucga_displayImplementation of hook_ucga_display().
uc_payment_method_2checkoutCallback for 2Checkout payment method settings.
_2checkout_post_url

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Integrates 2Checkout.com's redirected payment service.
  5. */
  6. /*******************************************************************************
  7. * Hook Functions (Drupal)
  8. ******************************************************************************/
  9. /**
  10. * Implementation of hook_menu().
  11. */
  12. function uc_2checkout_menu($may_cache) {
  13. if ($may_cache) {
  14. $items[] = array(
  15. 'path' => 'cart/2checkout/complete',
  16. 'title' => t('Order complete'),
  17. 'callback' => 'uc_2checkout_complete',
  18. 'access' => user_access('access content'),
  19. 'type' => MENU_CALLBACK,
  20. );
  21. }
  22. return $items;
  23. }
  24. /**
  25. * Implementation of hook_ucga_display().
  26. */
  27. function uc_2checkout_ucga_display() {
  28. // Tell UC Google Analytics to display the e-commerce JS on the custom
  29. // order completion page for this module.
  30. if (arg(0) == 'cart' && arg(1) == '2checkout' && arg(2) == 'complete') {
  31. return TRUE;
  32. }
  33. }
  34. /**
  35. * Implementation of hook_form_alter().
  36. */
  37. function uc_2checkout_form_alter($form_id, &$form) {
  38. if ($form_id == 'uc_cart_checkout_review_form' && ($order_id = intval($_SESSION['cart_order'])) > 0) {
  39. $order = uc_order_load($order_id);
  40. if ($order->payment_method == '2checkout') {
  41. unset($form['submit']);
  42. $form['#prefix'] = '<table style="display: inline; padding-top: 1em;"><tr><td>';
  43. $form['#suffix'] = '</td><td>'. drupal_get_form('uc_2checkout_form', $order) .'</td></tr></table>';
  44. }
  45. }
  46. }
  47. /*******************************************************************************
  48. * Hook Functions (Ubercart)
  49. ******************************************************************************/
  50. /**
  51. * Implementation of hook_payment_method().
  52. */
  53. function uc_2checkout_payment_method() {
  54. $path = base_path() . drupal_get_path('module', 'uc_2checkout');
  55. $title = variable_get('uc_2checkout_method_title', t('Credit card on a secure server:'));
  56. $title .= '<br /><img src="'. $path .'/2cocc05.gif" style="position: relative; left: 2.5em;">';
  57. $methods[] = array(
  58. 'id' => '2checkout',
  59. 'name' => t('2Checkout'),
  60. 'title' => $title,
  61. 'review' => variable_get('uc_2checkout_check', FALSE) ? t('Credit card/eCheck') : t('Credit card'),
  62. 'desc' => t('Redirect to 2Checkout to pay by credit card or eCheck.'),
  63. 'callback' => 'uc_payment_method_2checkout',
  64. 'weight' => 3,
  65. 'checkout' => TRUE,
  66. 'no_gateway' => TRUE,
  67. );
  68. return $methods;
  69. }
  70. /*******************************************************************************
  71. * Callback Functions, Forms, and Tables
  72. ******************************************************************************/
  73. /**
  74. * Callback for 2Checkout payment method settings.
  75. */
  76. function uc_payment_method_2checkout($op, &$arg1) {
  77. switch ($op) {
  78. case 'cart-details':
  79. if (variable_get('uc_2checkout_check', FALSE)) {
  80. if ($_SESSION['pay_method'] == 'CK') {
  81. $sel[2] = ' selected="selected"';
  82. }
  83. else {
  84. $sel[1] = ' selected="selected"';
  85. }
  86. unset($_SESSION['pay_method']);
  87. $details = '<div class="form-item"><b>'. t('Select your payment type:')
  88. .'</b> <select name="pay_method" class="form-select" id="edit-pay-method">'
  89. .'<option value="CC"'. $sel[1] .'>'. t('Credit card') .'</option>'
  90. .'<option value="CK"'. $sel[2] .'>'. t('Online check') .'</option></select></div>';
  91. }
  92. return $details;
  93. case 'cart-process':
  94. $_SESSION['pay_method'] = $_POST['pay_method'];
  95. return;
  96. case 'settings':
  97. $form['uc_2checkout_sid'] = array(
  98. '#type' => 'textfield',
  99. '#title' => t('Vendor account number'),
  100. '#description' => t('Your 2Checkout vendor account number.'),
  101. '#default_value' => variable_get('uc_2checkout_sid', ''),
  102. '#size' => 16,
  103. );
  104. $form['uc_2checkout_secret_word'] = array(
  105. '#type' => 'textfield',
  106. '#title' => t('Secret word for order verification'),
  107. '#description' => t('The secret word entered in your 2Checkout account Look and Feel settings.'),
  108. '#default_value' => variable_get('uc_2checkout_secret_word', 'tango'),
  109. '#size' => 16,
  110. );
  111. $form['uc_2checkout_demo'] = array(
  112. '#type' => 'checkbox',
  113. '#title' => t('Enable demo mode, allowing you to process fake orders for testing purposes.'),
  114. '#default_value' => variable_get('uc_2checkout_demo', TRUE),
  115. );
  116. $form['uc_2checkout_language'] = array(
  117. '#type' => 'select',
  118. '#title' => t('Language preference'),
  119. '#description' => t('Adjust language on 2Checkout pages.'),
  120. '#options' => array(
  121. 'en' => t('English'),
  122. 'sp' => t('Spanish'),
  123. ),
  124. '#default_value' => variable_get('uc_2checkout_language', 'en'),
  125. );
  126. $form['uc_2checkout_check'] = array(
  127. '#type' => 'checkbox',
  128. '#title' => t('Allow customers to choose to pay by credit card or online check.'),
  129. '#default_value' => variable_get('uc_2checkout_check', FALSE),
  130. );
  131. $form['uc_2checkout_method_title'] = array(
  132. '#type' => 'textfield',
  133. '#title' => t('Payment method title'),
  134. '#default_value' => variable_get('uc_2checkout_method_title', t('Credit card on a secure server:')),
  135. );
  136. $form['uc_2checkout_method_title_icons'] = array(
  137. '#type' => 'checkbox',
  138. '#title' => t('Show credit card icons beside the payment method title.'),
  139. '#default_value' => variable_get('uc_2checkout_method_title_icons', TRUE),
  140. );
  141. $form['uc_2checkout_checkout_button'] = array(
  142. '#type' => 'textfield',
  143. '#title' => t('Order review submit button text'),
  144. '#description' => t('Provide 2Checkout specific text for the submit button on the order review page.'),
  145. '#default_value' => variable_get('uc_2checkout_checkout_button', t('Submit Order')),
  146. );
  147. $form['uc_2checkout_checkout_type'] = array(
  148. '#type' => 'select',
  149. '#title' => t('2Checkout.com checkout type'),
  150. '#description' => t('Single page checkout only works for stores selling intangible products using credit card payments.'),
  151. '#options' => array(
  152. 'multi' => t('Multi-page checkout'),
  153. 'single' => t('Single page checkout'),
  154. ),
  155. '#default_value' => variable_get('uc_2checkout_checkout_type', 'multi'),
  156. );
  157. return $form;
  158. }
  159. }
  160. // Form to build the submission to 2Checkout.com.
  161. function uc_2checkout_form($order) {
  162. $country = uc_get_country_data(array('country_id' => $order->billing_country));
  163. if ($country === FALSE) {
  164. $country = array(0 => array('country_iso_code_3' => 'USA'));
  165. }
  166. $data = array(
  167. 'sid' => variable_get('uc_2checkout_sid', ''),
  168. 'total' => uc_currency_format($order->order_total, FALSE, FALSE, '.'),
  169. 'cart_order_id' => $order->order_id,
  170. 'demo' => variable_get('uc_2checkout_demo', TRUE) ? 'Y' : 'N',
  171. 'fixed' => 'Y',
  172. 'lang' => variable_get('uc_2checkout_language', 'en'),
  173. 'x_receipt_link_url' => url('cart/2checkout/complete/'. uc_cart_get_id(), NULL, NULL, TRUE),
  174. 'merchant_order_id' => $order->order_id,
  175. 'pay_method' => isset($_SESSION['pay_method']) ? $_SESSION['pay_method'] : 'CC',
  176. 'card_holder_name' => substr($order->billing_first_name .' '. $order->billing_last_name, 0, 128),
  177. 'street_address' => substr($order->billing_street1, 0, 64),
  178. 'street_address2' => substr($order->billing_street2, 0, 64),
  179. 'city' => substr($order->billing_city, 0, 64),
  180. 'state' => uc_get_zone_code($order->billing_zone),
  181. 'zip' => substr($order->billing_postal_code, 0, 16),
  182. 'country' => $country[0]['country_iso_code_3'],
  183. 'email' => substr($order->primary_email, 0, 64),
  184. 'phone' => substr($order->billing_phone, 0, 16),
  185. 'id_type' => 1,
  186. );
  187. $i = 0;
  188. foreach ($order->products as $product) {
  189. $i++;
  190. $data['c_prod_'. $i] = $product->model .','. $product->qty;
  191. $data['c_name_'. $i] = $product->title;
  192. $data['c_description_'. $i] = $desc;
  193. $data['c_price_'. $i] = uc_currency_format($product->price, FALSE, FALSE, '.');
  194. }
  195. $form['#action'] = _2checkout_post_url(variable_get('uc_2checkout_checkout_type', 'multi'));
  196. foreach ($data as $name => $value) {
  197. $form[$name] = array('#type' => 'hidden', '#value' => $value);
  198. }
  199. $form['submit'] = array(
  200. '#type' => 'submit',
  201. '#value' => variable_get('uc_2checkout_checkout_button', t('Submit Order')),
  202. );
  203. return $form;
  204. }
  205. function uc_2checkout_complete($cart_id = 0) {
  206. watchdog('2Checkout', t('Receiving new order notification for order !order_id.', array('!order_id' => check_plain($_POST['merchant_order_id']))));
  207. $order = uc_order_load($_POST['merchant_order_id']);
  208. if ($order === FALSE || uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
  209. return t('An error has occurred during payment. Please contact us to ensure your order has submitted.');
  210. }
  211. $key = $_POST['key'];
  212. $valid = md5(variable_get('uc_2checkout_secret_word', 'tango') . $_POST['sid'] . $_POST['merchant_order_id'] . $_POST['total']);
  213. if (strtolower($key) != strtolower($valid)) {
  214. uc_order_comment_save($order->order_id, 0, t('Attempted unverified 2Checkout completion for this order.'), 'admin');
  215. return MENU_ACCESS_DENIED;
  216. }
  217. if ($_POST['demo'] == 'Y' xor variable_get('uc_2checkout_demo', TRUE)) {
  218. watchdog('uc_2checkout', 'The 2checkout payment for order <a href="!order_url">@order_id</a> demo flag was set to %flag, but the module is set to %mode mode.',
  219. array('!order_url' => url('admin/store/orders/'. $order->order_id), '@order_id' => $order->order_id, '%flag' => $_POST['demo'] == 'Y' ? 'Y' : 'N', '%mode' => variable_get('uc_2checkout_demo', TRUE) ? 'Y' : 'N'), WATCHDOG_ERROR);
  220. if (!variable_get('uc_2checkout_demo', TRUE)) {
  221. return MENU_ACCESS_DENIED;
  222. }
  223. }
  224. $order->billing_street1 = $_POST['street_address'];
  225. $order->billing_street2 = $_POST['street_address2'];
  226. $order->city = $_POST['city'];
  227. $order->billing_postal_code = $_POST['zip'];
  228. $order->billing_phone = $_POST['phone'];
  229. $zone_id = db_result(db_query("SELECT zone_id FROM {uc_zones} WHERE zone_code LIKE '%s'", $_POST['state']));
  230. if (!empty($zone_id)) {
  231. $order->billing_zone = $zone_id;
  232. }
  233. $country_id = db_result(db_query("SELECT country_id FROM {uc_countries} WHERE country_name LIKE '%s'", $_POST['country']));
  234. if (!empty($zone_id)) {
  235. $order->billing_country = $country_id;
  236. }
  237. if (strtolower($_POST['email']) !== strtolower($order->primary_email)) {
  238. uc_order_comment_save($order->order_id, 0, t('Customer used a different e-mail address during payment: !email', array('!email' => check_plain($_POST['email']))), 'admin');
  239. }
  240. if ($_POST['credit_card_processed'] == 'Y' && is_numeric($_POST['total'])) {
  241. $comment = t('Paid by !type, 2Checkout.com order #!order.', array('!type' => $_POST['pay_method'] == 'CC' ? t('credit card') : t('echeck'), '!order' => check_plain($_POST['order_number'])));
  242. uc_payment_enter($order->order_id, '2checkout', $_POST['total'], 0, NULL, $comment);
  243. }
  244. else {
  245. drupal_set_message(t('Your order will be processed as soon as your payment clears at 2Checkout.com.'));
  246. uc_order_comment_save($order_id, 0, t('!type payment is pending approval at 2Checkout.com.', array('!type' => $_POST['pay_method'] == 'CC' ? t('Credit card') : t('eCheck'))), 'admin');
  247. }
  248. // Empty that cart...
  249. uc_cart_empty($cart_id);
  250. // Save changes to order without it's completion.
  251. uc_order_save($order);
  252. // Add a comment to let sales team know this came in through the site.
  253. uc_order_comment_save($order->order_id, 0, t('Order created through website.'), 'admin');
  254. $output = uc_cart_complete_sale($order, variable_get('uc_new_customer_login', FALSE));
  255. $page = variable_get('uc_cart_checkout_complete_page', '');
  256. if (!empty($page)) {
  257. drupal_goto($page);
  258. }
  259. return $output;
  260. }
  261. function _2checkout_post_url($type) {
  262. switch ($type) {
  263. case 'single':
  264. return 'https://www.2checkout.com/checkout/spurchase';
  265. case 'multi':
  266. default:
  267. return 'https://www.2checkout.com/2co/buyer/purchase';
  268. }
  269. }