uc_2checkout.module 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * @file
  4. * Integrates 2Checkout.com's redirected payment service.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function uc_2checkout_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/store/settings/payment/method/%':
  12. if ($arg[5] == '2checkout') {
  13. return '<p>' . t('To accept PayPal payments in 2Checkout, please ensure that demo mode is disabled and your store currency is one of USD, AUD, CAD, EUR or GBP.') . '</p>';
  14. }
  15. }
  16. }
  17. /**
  18. * Implements hook_menu().
  19. */
  20. function uc_2checkout_menu() {
  21. $items = array();
  22. $items['cart/2checkout/complete'] = array(
  23. 'title' => 'Order complete',
  24. 'page callback' => 'uc_2checkout_complete',
  25. 'access callback' => TRUE,
  26. 'type' => MENU_CALLBACK,
  27. 'file' => 'uc_2checkout.pages.inc',
  28. );
  29. $items['cart/2checkout/notification'] = array(
  30. 'page callback' => 'uc_2checkout_process_notification',
  31. 'access callback' => TRUE,
  32. 'file' => 'uc_2checkout.pages.inc',
  33. );
  34. return $items;
  35. }
  36. /**
  37. * Implements hook_init().
  38. */
  39. function uc_2checkout_init() {
  40. global $conf;
  41. $conf['i18n_variables'][] = 'uc_2checkout_method_title';
  42. }
  43. /**
  44. * Implements hook_ucga_display().
  45. */
  46. function uc_2checkout_ucga_display() {
  47. // Tell UC Google Analytics to display the e-commerce JS on the custom
  48. // order completion page for this module.
  49. if (arg(0) == 'cart' && arg(1) == '2checkout' && arg(2) == 'complete') {
  50. return TRUE;
  51. }
  52. }
  53. /**
  54. * Implements hook_uc_payment_method().
  55. *
  56. * @see uc_payment_method_2checkout()
  57. */
  58. function uc_2checkout_uc_payment_method() {
  59. $path = base_path() . drupal_get_path('module', 'uc_2checkout');
  60. $title = variable_get('uc_2checkout_method_title', t('Credit card on a secure server:'));
  61. $title .= '<br />' . theme('image', array(
  62. 'path' => drupal_get_path('module', 'uc_2checkout') . '/2co_logo.jpg',
  63. 'attributes' => array('class' => array('uc-2checkout-logo')),
  64. ));
  65. $methods['2checkout'] = array(
  66. 'name' => t('2Checkout'),
  67. 'title' => $title,
  68. 'review' => variable_get('uc_2checkout_check', FALSE) ? t('Credit card/eCheck') : t('Credit card'),
  69. 'desc' => t('Redirect to 2Checkout to pay by credit card or eCheck.'),
  70. 'callback' => 'uc_payment_method_2checkout',
  71. 'redirect' => 'uc_2checkout_form',
  72. 'weight' => 3,
  73. 'checkout' => TRUE,
  74. 'no_gateway' => TRUE,
  75. );
  76. return $methods;
  77. }
  78. /**
  79. * Adds 2Checkout settings to the payment method settings form.
  80. *
  81. * @see uc_2checkout_uc_payment_method()
  82. */
  83. function uc_payment_method_2checkout($op, &$order, $form = NULL, &$form_state = NULL) {
  84. switch ($op) {
  85. case 'cart-details':
  86. $build = array();
  87. if (variable_get('uc_2checkout_check', FALSE)) {
  88. $build['pay_method'] = array(
  89. '#type' => 'select',
  90. '#title' => t('Select your payment type:'),
  91. '#default_value' => $_SESSION['pay_method'] == 'CK' ? 'CK' : 'CC',
  92. '#options' => array(
  93. 'CC' => t('Credit card'),
  94. 'CK' => t('Online check'),
  95. ),
  96. );
  97. unset($_SESSION['pay_method']);
  98. }
  99. return $build;
  100. case 'cart-process':
  101. if (isset($form_state['values']['panes']['payment']['details']['pay_method'])) {
  102. $_SESSION['pay_method'] = $form_state['values']['panes']['payment']['details']['pay_method'];
  103. }
  104. return;
  105. case 'settings':
  106. $form['uc_2checkout_sid'] = array(
  107. '#type' => 'textfield',
  108. '#title' => t('Vendor account number'),
  109. '#description' => t('Your 2Checkout vendor account number.'),
  110. '#default_value' => variable_get('uc_2checkout_sid', ''),
  111. '#size' => 16,
  112. );
  113. $form['uc_2checkout_secret_word'] = array(
  114. '#type' => 'textfield',
  115. '#title' => t('Secret word for order verification'),
  116. '#description' => t('The secret word entered in your 2Checkout account Look and Feel settings.'),
  117. '#default_value' => variable_get('uc_2checkout_secret_word', 'tango'),
  118. '#size' => 16,
  119. );
  120. $form['uc_2checkout_demo'] = array(
  121. '#type' => 'checkbox',
  122. '#title' => t('Enable demo mode, allowing you to process fake orders for testing purposes.'),
  123. '#default_value' => variable_get('uc_2checkout_demo', TRUE),
  124. );
  125. $form['uc_2checkout_language'] = array(
  126. '#type' => 'select',
  127. '#title' => t('Language preference'),
  128. '#description' => t('Adjust language on 2Checkout pages.'),
  129. '#options' => array(
  130. 'en' => t('English'),
  131. 'sp' => t('Spanish'),
  132. ),
  133. '#default_value' => variable_get('uc_2checkout_language', 'en'),
  134. );
  135. $form['uc_2checkout_currency_code'] = array(
  136. '#type' => 'select',
  137. '#title' => t('Currency for the sale'),
  138. '#options' => array(
  139. '' => t('Auto detected by 2CO'),
  140. 'USD', 'EUR', 'ARS', 'AUD', 'BRL', 'GBP', 'CAD', 'DKK', 'HKD', 'INR', 'ILS', 'JPY', 'LTL', 'MYR', 'MXN', 'NZD', 'NOK', 'PHP', 'RON', 'RUB', 'SGD', 'ZAR', 'SEK', 'CHF', 'TRY', 'AED',
  141. ),
  142. '#default_value' => variable_get('uc_2checkout_currency_code', ''),
  143. );
  144. $form['uc_2checkout_check'] = array(
  145. '#type' => 'checkbox',
  146. '#title' => t('Allow customers to choose to pay by credit card or online check.'),
  147. '#default_value' => variable_get('uc_2checkout_check', FALSE),
  148. );
  149. $form['uc_2checkout_method_title'] = array(
  150. '#type' => 'textfield',
  151. '#title' => t('Payment method title'),
  152. '#default_value' => variable_get('uc_2checkout_method_title', t('Credit card on a secure server:')),
  153. );
  154. $form['uc_2checkout_checkout_type'] = array(
  155. '#type' => 'radios',
  156. '#title' => t('Checkout type'),
  157. '#options' => array(
  158. 'dynamic' => t('Dynamic checkout (user is redirected to 2CO)'),
  159. 'direct' => t('Direct checkout (payment page opens in iframe popup)'),
  160. ),
  161. '#default_value' => variable_get('uc_2checkout_checkout_type', 'dynamic'),
  162. );
  163. $form['uc_2checkout_notification_url'] = array(
  164. '#type' => 'textfield',
  165. '#title' => t('Instant notification settings URL'),
  166. '#description' => t('Pass this URL to the <a href="@help_url">instant notification settings</a> parameter in your 2Checkout account. This way, any refunds or failed fraud reviews will automatically cancel the Ubercart order.', array('@help_url' => 'https://www.2checkout.com/static/va/documentation/INS/index.html')),
  167. '#default_value' => url('cart/2checkout/notification', array('absolute' => TRUE)),
  168. '#attributes' => array('readonly' => 'readonly'),
  169. );
  170. return $form;
  171. }
  172. }
  173. /**
  174. * Form to build the submission to 2Checkout.com.
  175. */
  176. function uc_2checkout_form($form, &$form_state, $order) {
  177. $country = uc_get_country_data(array('country_id' => $order->billing_country));
  178. if ($country === FALSE) {
  179. $country = array(0 => array('country_iso_code_3' => 'USA'));
  180. }
  181. $data = array(
  182. 'sid' => variable_get('uc_2checkout_sid', ''),
  183. 'mode' => '2CO',
  184. 'card_holder_name' => drupal_substr($order->billing_first_name . ' ' . $order->billing_last_name, 0, 128),
  185. 'street_address' => drupal_substr($order->billing_street1, 0, 64),
  186. 'street_address2' => drupal_substr($order->billing_street2, 0, 64),
  187. 'city' => drupal_substr($order->billing_city, 0, 64),
  188. 'state' => uc_get_zone_code($order->billing_zone),
  189. 'zip' => drupal_substr($order->billing_postal_code, 0, 16),
  190. 'country' => $country[0]['country_iso_code_3'],
  191. 'email' => drupal_substr($order->primary_email, 0, 64),
  192. 'phone' => drupal_substr($order->billing_phone, 0, 16),
  193. 'purchase_step' => 'payment-method',
  194. 'demo' => variable_get('uc_2checkout_demo', TRUE) ? 'Y' : 'N',
  195. 'lang' => variable_get('uc_2checkout_language', 'en'),
  196. 'merchant_order_id' => $order->order_id,
  197. 'pay_method' => 'CC',
  198. 'x_receipt_link_url' => url('cart/2checkout/complete/' . uc_cart_get_id(), array('absolute' => TRUE)),
  199. 'total' => uc_currency_format($order->order_total, FALSE, FALSE, '.'),
  200. 'cart_order_id' => $order->order_id,
  201. );
  202. if ($currency_code = variable_get('uc_2checkout_currency_code', '')) {
  203. $data['currency_code'] = $currency_code;
  204. }
  205. $i = 0;
  206. foreach ($order->products as $product) {
  207. $i++;
  208. $data['li_' . $i . '_name'] = $product->title;
  209. $data['li_' . $i . '_price'] = uc_currency_format($product->price, FALSE, FALSE, '.');
  210. }
  211. if (variable_get('uc_2checkout_checkout_type', 'dynamic') == 'direct') {
  212. $form['#suffix'] = '<script src="https://www.2checkout.com/static/checkout/javascript/direct.min.js"></script>';
  213. }
  214. $form['#action'] = variable_get('uc_2checkout_server_url', 'https://www.2checkout.com/checkout/purchase');
  215. foreach ($data as $name => $value) {
  216. $form[$name] = array('#type' => 'hidden', '#value' => $value);
  217. }
  218. $form['actions'] = array('#type' => 'actions');
  219. $form['actions']['submit'] = array(
  220. '#type' => 'submit',
  221. '#value' => t('Submit order'),
  222. );
  223. return $form;
  224. }