test_gateway.module 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @file
  4. * A dummy payment gateway to use for testing or as an example.
  5. *
  6. * All payments using this test gateway will succeed, except when:
  7. * - Credit card number equals '0000000000000000'. (Note that ANY card number
  8. * that fails the Luhn algorithm check performed by uc_credit will not even be
  9. * submitted to this gateway).
  10. * - CVV equals '000'.
  11. * - Credit card is expired.
  12. * - Payment amount equals 12.34 in store currency units.
  13. * - Customer's billing first name equals 'Fictitious'.
  14. * - Customer's billing telephone number equals '8675309'.
  15. */
  16. /*******************************************************************************
  17. * Hook Functions (Ubercart)
  18. ******************************************************************************/
  19. /**
  20. * Implements hook_uc_payment_gateway().
  21. *
  22. * @see test_gateway.module
  23. * @see test_gateway_charge()
  24. */
  25. function test_gateway_uc_payment_gateway() {
  26. $gateways['test_gateway'] = array(
  27. 'title' => t('Test Gateway'),
  28. 'description' => t('Process credit card payments through the Test Gateway.'),
  29. 'credit' => 'test_gateway_charge',
  30. );
  31. return $gateways;
  32. }
  33. /*******************************************************************************
  34. * Module and Helper Functions
  35. ******************************************************************************/
  36. /**
  37. * Callback function to perform the charge operation.
  38. *
  39. * @see test_gateway.module
  40. * @see test_gateway_uc_payment_gateway()
  41. */
  42. function test_gateway_charge($order_id, $amount, $data) {
  43. global $user;
  44. $order = uc_order_load($order_id);
  45. // cc_exp_month and cc_exp_year are also validated by
  46. // _uc_credit_valid_card_expiration() on the checkout form.
  47. $month = $order->payment_details['cc_exp_month'];
  48. $year = $order->payment_details['cc_exp_year'];
  49. if ($year < 100) {
  50. $year = $year + 2000;
  51. }
  52. // Card is expired at 0:00 on the first day of the next month.
  53. $expiration_date = mktime(0, 0, 0, $month + 1, 1, $year);
  54. // Conditions for failure are described in file documentation block above.
  55. // All other transactions will succeed.
  56. if ($order->payment_details['cc_number'] == '0000000000000000' ||
  57. (isset($order->payment_details['cc_cvv']) && $order->payment_details['cc_cvv'] == '000') ||
  58. ($expiration_date - REQUEST_TIME) <= 0 ||
  59. $amount == 12.34 ||
  60. $order->billing_first_name == 'Fictitious' ||
  61. $order->billing_phone == '8675309' ) {
  62. $success = FALSE;
  63. }
  64. else {
  65. $success = TRUE;
  66. }
  67. // Uncomment this line to see the order object. The information for the
  68. // payment is in the $order->payment_details array.
  69. // drupal_set_message('<pre>' . print_r($order->payment_details, TRUE) . '</pre>');
  70. if ($success) {
  71. $message = t('Credit card charged: !amount', array('!amount' => uc_currency_format($amount)));
  72. uc_order_comment_save($order_id, $user->uid, $message, 'admin');
  73. }
  74. else {
  75. $message = t('Credit card charge failed.');
  76. uc_order_comment_save($order_id, $user->uid, $message, 'admin');
  77. }
  78. $result = array(
  79. 'success' => $success,
  80. 'comment' => t('Card charged, resolution code: 0022548315'),
  81. 'message' => $success ? t('Credit card payment processed successfully.') : t('Credit card charge failed.'),
  82. 'uid' => $user->uid,
  83. // 'data' => $data,
  84. );
  85. return $result;
  86. }