uc_payment_pack.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * @file
  4. * Provides the Check/Money Order, COD, and "Other" payment methods.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function uc_payment_pack_menu() {
  10. $items['admin/store/orders/%uc_order/receive_check'] = array(
  11. 'title' => 'Receive check',
  12. 'description' => 'Record details of a check that has been received.',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('uc_payment_pack_receive_check_form', 3),
  15. 'access arguments' => array('view all orders'),
  16. 'file' => 'uc_payment_pack.admin.inc',
  17. );
  18. return $items;
  19. }
  20. /**
  21. * Implements hook_uc_payment_method().
  22. */
  23. function uc_payment_pack_uc_payment_method() {
  24. $methods['check'] = array(
  25. 'name' => t('Check', array(), array('context' => 'cheque')),
  26. 'title' => t('Check or money order'),
  27. 'desc' => t('Pay by mailing a check or money order.'),
  28. 'callback' => 'uc_payment_method_check',
  29. 'weight' => 1,
  30. 'checkout' => TRUE,
  31. 'no_gateway' => TRUE,
  32. );
  33. $methods['cod'] = array(
  34. 'name' => t('COD'),
  35. 'title' => t('Cash on delivery'),
  36. 'desc' => t('Pay cash on delivery on pick-up.'),
  37. 'callback' => 'uc_payment_method_cod',
  38. 'weight' => 1,
  39. 'checkout' => FALSE,
  40. 'no_gateway' => TRUE,
  41. );
  42. $methods['other'] = array(
  43. 'name' => t('Other'),
  44. 'title' => t('Other'),
  45. 'desc' => t('A generic payment method type.'),
  46. 'callback' => 'uc_payment_method_other',
  47. 'weight' => 10,
  48. 'checkout' => FALSE,
  49. 'no_gateway' => TRUE,
  50. );
  51. return $methods;
  52. }
  53. /**
  54. * Payment method callback for the generic payment method "Other".
  55. */
  56. function uc_payment_method_other($op, &$order) {
  57. switch ($op) {
  58. case 'order-view':
  59. case 'customer-view':
  60. // Fetch the description for the payment entered by the administrator.
  61. if ($description = db_query('SELECT description FROM {uc_payment_other} WHERE order_id = :id', array(':id' => $order->order_id))->fetchField()) {
  62. return array('#markup' => t('Description: @desc', array('@desc' => $description)));
  63. }
  64. break;
  65. case 'order-details':
  66. $form['pm_other_description'] = array(
  67. '#type' => 'textfield',
  68. '#title' => t('Description'),
  69. '#default_value' => isset($order->payment_details['description']) ? $order->payment_details['description'] : '',
  70. '#size' => 32,
  71. '#maxlength' => 64,
  72. );
  73. return $form;
  74. case 'order-load':
  75. $description = db_query('SELECT description FROM {uc_payment_other} WHERE order_id = :id', array(':id' => $order->order_id))->fetchField();
  76. if (isset($description)) {
  77. $order->payment_details['description'] = $description;
  78. }
  79. break;
  80. case 'order-save':
  81. if (empty($order->payment_details['pm_other_description'])) {
  82. db_delete('uc_payment_other')
  83. ->condition('order_id', $order->order_id)
  84. ->execute();
  85. }
  86. else {
  87. db_merge('uc_payment_other')
  88. ->key(array(
  89. 'order_id' => $order->order_id,
  90. ))
  91. ->fields(array(
  92. 'description' => $order->payment_details['pm_other_description'],
  93. ))
  94. ->execute();
  95. }
  96. break;
  97. }
  98. }
  99. /**
  100. * Payment method callback for the "Cash on Delivery" payment method.
  101. */
  102. function uc_payment_method_cod($op, &$order, $form = NULL, $form_state = NULL) {
  103. switch ($op) {
  104. case 'cart-details':
  105. $build['policy'] = array(
  106. '#markup' => '<p>' . variable_get('uc_cod_policy', t('Full payment is expected upon delivery or prior to pick-up.')) . '</p>',
  107. );
  108. if (($max = variable_get('uc_cod_max_order', 0)) > 0 && is_numeric($max)) {
  109. $build['eligibility'] = array(
  110. '#markup' => '<p>' . t('Orders totalling more than !number are <b>not eligible</b> for COD.', array('!number' => uc_currency_format($max))) . '</p>',
  111. );
  112. }
  113. if (variable_get('uc_cod_delivery_date', FALSE)) {
  114. $build += uc_payment_method_cod_form(array(), $form_state, $order);
  115. }
  116. return $build;
  117. case 'cart-process':
  118. if (variable_get('uc_cod_delivery_date', FALSE)) {
  119. $order->payment_details = $form_state['values']['panes']['payment']['details'];
  120. }
  121. return TRUE;
  122. case 'cart-review':
  123. $review = array();
  124. if (variable_get('uc_cod_delivery_date', FALSE)) {
  125. $date = uc_date_format(
  126. $order->payment_details['delivery_month'],
  127. $order->payment_details['delivery_day'],
  128. $order->payment_details['delivery_year']
  129. );
  130. $review[] = array('title' => t('Delivery date'), 'data' => $date);
  131. }
  132. return $review;
  133. case 'order-view':
  134. case 'customer-view':
  135. $build = array('#markup' => '');
  136. if (variable_get('uc_cod_delivery_date', FALSE) &&
  137. isset($order->payment_details['delivery_month']) &&
  138. isset($order->payment_details['delivery_day']) &&
  139. isset($order->payment_details['delivery_year'])) {
  140. $build['#markup'] = t('Desired delivery date:') . '<br />' .
  141. uc_date_format(
  142. $order->payment_details['delivery_month'],
  143. $order->payment_details['delivery_day'],
  144. $order->payment_details['delivery_year']
  145. );
  146. }
  147. return $build;
  148. case 'order-details':
  149. $build = array();
  150. if (variable_get('uc_cod_delivery_date', FALSE)) {
  151. $build = uc_payment_method_cod_form(array(), $form_state, $order);
  152. }
  153. return $build;
  154. case 'order-load':
  155. $result = db_query('SELECT * FROM {uc_payment_cod} WHERE order_id = :id', array(':id' => $order->order_id));
  156. if ($row = $result->fetchObject()) {
  157. $order->payment_details = array(
  158. 'delivery_month' => $row->delivery_month,
  159. 'delivery_day' => $row->delivery_day,
  160. 'delivery_year' => $row->delivery_year,
  161. );
  162. }
  163. break;
  164. case 'order-submit':
  165. if ($order->payment_method == 'cod' &&
  166. ($max = variable_get('uc_cod_max_order', 0)) > 0 &&
  167. is_numeric($max) &&
  168. $order->order_total > $max) {
  169. $result[] = array(
  170. 'pass' => FALSE,
  171. 'message' => t('Your final order total exceeds the maximum for COD payment. Please go back and select a different method of payment.'),
  172. );
  173. $_SESSION['expanded_panes'][] = 'payment';
  174. return $result;
  175. }
  176. // TODO: This falls through to the order-save case - is this deliberate?
  177. // If so, it should be documented.
  178. case 'order-save':
  179. if (isset($order->payment_details['delivery_month']) &&
  180. isset($order->payment_details['delivery_day']) &&
  181. isset($order->payment_details['delivery_year']) ) {
  182. db_merge('uc_payment_cod')
  183. ->key(array('order_id' => $order->order_id))
  184. ->fields(array(
  185. 'delivery_month' => $order->payment_details['delivery_month'],
  186. 'delivery_day' => $order->payment_details['delivery_day'],
  187. 'delivery_year' => $order->payment_details['delivery_year'],
  188. ))
  189. ->execute();
  190. }
  191. break;
  192. case 'order-delete':
  193. db_delete('uc_payment_cod')
  194. ->condition('order_id', $order->order_id)
  195. ->execute();
  196. break;
  197. case 'settings':
  198. $form['uc_cod_policy'] = array(
  199. '#type' => 'textarea',
  200. '#title' => t('Policy message'),
  201. '#default_value' => variable_get('uc_cod_policy', t('Full payment is expected upon delivery or prior to pick-up.')),
  202. '#description' => t('Help message shown at checkout.'),
  203. );
  204. $form['uc_cod_max_order'] = array(
  205. '#type' => 'textfield',
  206. '#title' => t('Maximum order total eligible for COD'),
  207. '#default_value' => variable_get('uc_cod_max_order', 0),
  208. '#description' => t('Set to 0 for no maximum order limit.'),
  209. );
  210. $form['uc_cod_delivery_date'] = array(
  211. '#type' => 'checkbox',
  212. '#title' => t('Let customers enter a desired delivery date.'),
  213. '#default_value' => variable_get('uc_cod_delivery_date', FALSE),
  214. );
  215. return $form;
  216. }
  217. }
  218. /**
  219. * Collect additional information for the "Cash on Delivery" payment method.
  220. *
  221. * @ingroup forms
  222. */
  223. function uc_payment_method_cod_form($form, &$form_state, $order) {
  224. $month = !empty($order->payment_details['delivery_month']) ? $order->payment_details['delivery_month'] : format_date(REQUEST_TIME, 'custom', 'n');
  225. $day = !empty($order->payment_details['delivery_day']) ? $order->payment_details['delivery_day'] : format_date(REQUEST_TIME, 'custom', 'j');
  226. $year = !empty($order->payment_details['delivery_year']) ? $order->payment_details['delivery_year'] : format_date(REQUEST_TIME, 'custom', 'Y');
  227. $form['description'] = array(
  228. '#markup' => '<div>' . t('Enter a desired delivery date:') . '</div>',
  229. );
  230. $form['delivery_month'] = uc_select_month(NULL, $month);
  231. $form['delivery_day'] = uc_select_day(NULL, $day);
  232. $form['delivery_year'] = uc_select_year(NULL, $year, format_date(REQUEST_TIME, 'custom', 'Y'), format_date(REQUEST_TIME, 'custom', 'Y') + 1);
  233. return $form;
  234. }
  235. /**
  236. * Payment method callback for the "Check" payment method.
  237. */
  238. function uc_payment_method_check($op, &$order, $form = NULL, &$form_state = NULL) {
  239. switch ($op) {
  240. case 'cart-details':
  241. $build['instructions'] = array(
  242. '#markup' => t('Checks should be made out to:'),
  243. );
  244. if (!variable_get('uc_check_mailing_street1', FALSE)) {
  245. $build['address'] = array(
  246. '#markup' => uc_address_format(
  247. uc_store_name(),
  248. NULL,
  249. variable_get('uc_store_company', ''),
  250. variable_get('uc_store_street1', ''),
  251. variable_get('uc_store_street2', ''),
  252. variable_get('uc_store_city', ''),
  253. variable_get('uc_store_zone', ''),
  254. variable_get('uc_store_postal_code', ''),
  255. variable_get('uc_store_country', 840)
  256. ),
  257. '#prefix' => '<p>',
  258. '#suffix' => '</p>',
  259. );
  260. }
  261. else {
  262. $build['address'] = array(
  263. '#markup' => uc_address_format(
  264. variable_get('uc_check_mailing_name', ''),
  265. NULL,
  266. variable_get('uc_check_mailing_company', ''),
  267. variable_get('uc_check_mailing_street1', ''),
  268. variable_get('uc_check_mailing_street2', ''),
  269. variable_get('uc_check_mailing_city', ''),
  270. variable_get('uc_check_mailing_zone', ''),
  271. variable_get('uc_check_mailing_postal_code', ''),
  272. variable_get('uc_check_mailing_country', 840)
  273. ),
  274. '#prefix' => '<p>',
  275. '#suffix' => '</p>',
  276. );
  277. }
  278. $build['policy'] = array(
  279. '#markup' => '<p>' . variable_get('uc_check_policy', '') . '</p>',
  280. );
  281. return $build;
  282. case 'cart-review':
  283. if (!variable_get('uc_check_mailing_street1', FALSE)) {
  284. $review[] = array(
  285. 'title' => t('Mail to'),
  286. 'data' => uc_address_format(
  287. uc_store_name(),
  288. NULL,
  289. variable_get('uc_store_company', ''),
  290. variable_get('uc_store_street1', ''),
  291. variable_get('uc_store_street2', ''),
  292. variable_get('uc_store_city', ''),
  293. variable_get('uc_store_zone', ''),
  294. variable_get('uc_store_postal_code', ''),
  295. variable_get('uc_store_country', 840)
  296. ),
  297. );
  298. }
  299. else {
  300. $review[] = array(
  301. 'title' => t('Mail to'),
  302. 'data' => uc_address_format(
  303. variable_get('uc_check_mailing_name', ''),
  304. NULL,
  305. variable_get('uc_check_mailing_company', ''),
  306. variable_get('uc_check_mailing_street1', ''),
  307. variable_get('uc_check_mailing_street2', ''),
  308. variable_get('uc_check_mailing_city', ''),
  309. variable_get('uc_check_mailing_zone', ''),
  310. variable_get('uc_check_mailing_postal_code', ''),
  311. variable_get('uc_check_mailing_country', 840)
  312. ),
  313. );
  314. }
  315. return $review;
  316. case 'order-view':
  317. $build = array('#suffix' => '<br />');
  318. $result = db_query('SELECT clear_date FROM {uc_payment_check} WHERE order_id = :id ', array(':id' => $order->order_id));
  319. if ($clear_date = $result->fetchField()) {
  320. $build['#markup'] = t('Clear Date:') . ' ' . format_date($clear_date, 'uc_store');
  321. }
  322. else {
  323. $build['#markup'] = l(t('Receive Check'), 'admin/store/orders/' . $order->order_id . '/receive_check');
  324. }
  325. return $build;
  326. case 'customer-view':
  327. $build = array();
  328. $result = db_query('SELECT clear_date FROM {uc_payment_check} WHERE order_id = :id ', array(':id' => $order->order_id));
  329. if ($clear_date = $result->fetchField()) {
  330. $build['#markup'] = t('Check received') . '<br />' .
  331. t('Expected clear date:') . '<br />' . format_date($clear_date, 'uc_store');
  332. }
  333. return $build;
  334. case 'settings':
  335. $form['check_address_info'] = array(
  336. '#markup' => '<div>' . t('Set the mailing address to display to customers who choose this payment method during checkout.') . '</div>',
  337. );
  338. $form['uc_check_mailing_name'] = uc_textfield(t('Contact'), variable_get('uc_check_mailing_name', ''), FALSE, t('Direct checks to a person or department.'), 128);
  339. $form['uc_check_address'] = array(
  340. '#type' => 'uc_address',
  341. '#default_value' => array(
  342. 'uc_check_mailing_company' => variable_get('uc_check_mailing_company', ''),
  343. 'uc_check_mailing_street1' => variable_get('uc_check_mailing_street1', ''),
  344. 'uc_check_mailing_street2' => variable_get('uc_check_mailing_street2', ''),
  345. 'uc_check_mailing_city' => variable_get('uc_check_mailing_city', ''),
  346. 'uc_check_mailing_zone' => variable_get('uc_check_mailing_zone', ''),
  347. 'uc_check_mailing_country' => isset($form_state['values']['uc_check_mailing_country']) ? $form_state['values']['uc_check_mailing_country'] : variable_get('uc_check_mailing_country', ''),
  348. 'uc_check_mailing_postal_code' => variable_get('uc_check_mailing_postal_code', ''),
  349. ),
  350. '#required' => FALSE,
  351. '#key_prefix' => 'uc_check_mailing',
  352. );
  353. $form['uc_check_policy'] = array(
  354. '#type' => 'textarea',
  355. '#title' => t('Check payment policy', array(), array('context' => 'cheque')),
  356. '#description' => t('Instructions for customers on the checkout page.'),
  357. '#default_value' => variable_get('uc_check_policy', t('Personal and business checks will be held for up to 10 business days to ensure payment clears before an order is shipped.')),
  358. '#rows' => 3,
  359. );
  360. return $form;
  361. }
  362. }