uc_payment.api.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Payment module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Takes action when a payment is entered for an order.
  12. *
  13. * @param $order
  14. * The order object.
  15. * @param $method
  16. * The name of the payment method used.
  17. * @param $amount
  18. * The value of the payment.
  19. * @param $account
  20. * The user account that entered the order. When the payment is entered
  21. * during checkout, this is probably the order's user. Otherwise, it is
  22. * likely a store administrator.
  23. * @param $data
  24. * Extra data associated with the transaction.
  25. * @param $comment
  26. * Any comments from the user about the transaction.
  27. */
  28. function hook_uc_payment_entered($order, $method, $amount, $account, $data, $comment) {
  29. drupal_set_message(t('User @uid entered a @method payment of @amount for order @order_id.',
  30. array(
  31. '@uid' => $account->uid,
  32. '@method' => $method,
  33. '@amount' => uc_currency_format($amount),
  34. '@order_id' => $order->order_id,
  35. ))
  36. );
  37. }
  38. /**
  39. * Registers credit card payment gateway callbacks.
  40. *
  41. * Payment gateways handle credit card payments directly, without needing to
  42. * redirect off-site.
  43. *
  44. * @see http://www.ubercart.org/docs/api/hook_uc_payment_gateway
  45. * @see hook_uc_payment_gateway_charge()
  46. *
  47. * @return
  48. * Returns an array of payment gateways, keyed by the gateway ID, and with
  49. * the following members:
  50. * - "title": the human-readable name of the payment method.
  51. * - "description": a human-readable description of the payment method.
  52. * - "settings": A callback function that returns the gateway settings form.
  53. * - "credit": A callback function that processes the credit card. See
  54. * hook_uc_payment_gateway_charge() for details.
  55. */
  56. function hook_uc_payment_gateway() {
  57. $gateways['test_gateway'] = array(
  58. 'title' => t('Test gateway'),
  59. 'description' => t('Process credit card payments through the Test Gateway.'),
  60. 'credit' => 'test_gateway_charge',
  61. );
  62. return $gateways;
  63. }
  64. /**
  65. * Credit card charge callback.
  66. *
  67. * Called when a credit card should be processed. Credit card details supplied
  68. * by the user are available in $order->payment_details[].
  69. *
  70. * @see hook_uc_payment_gateway()
  71. * @see uc_authorizenet_charge()
  72. * @see test_gateway_charge()
  73. *
  74. * @param $order_id
  75. * The order ID that the payment relates to.
  76. * @param $amount
  77. * The amount that should be charged.
  78. * @param $data
  79. * An array of data related to the charge. By default, includes a 'txn_type'
  80. * key which defines the transaction type, usually UC_CREDIT_AUTH_ONLY
  81. * or UC_CREDIT_AUTH_CAPTURE.
  82. *
  83. * @return
  84. * Returns an associative array with the following members:
  85. * - "success": TRUE if the transaction succeeded, FALSE otherwise.
  86. * - "message": a human-readable message describing the result of the
  87. * transaction.
  88. * - "log_payment": TRUE if the transaction should be regarded as a
  89. * successful payment.
  90. */
  91. function hook_uc_payment_gateway_charge($order_id, $amount, $data) {
  92. }
  93. /**
  94. * Alters payment gateways.
  95. *
  96. * @param $gateways
  97. * Array of payment gateways passed by reference. Array is structured like
  98. * the return value of hook_uc_payment_gateway().
  99. */
  100. function hook_uc_payment_gateway_alter(&$gateways) {
  101. // Change the title of the test gateway.
  102. $gateways['test_gateway']['title'] = t('Altered test gateway title.');
  103. }
  104. /**
  105. * Registers callbacks for payment methods.
  106. *
  107. * Payment methods are different ways to collect payment. By default, Ubercart
  108. * comes with support for check, credit card, and generic payments. Payment
  109. * methods show up at checkout or on the order administration screens, and they
  110. * collect different sorts of information from the user that is used to process
  111. * or track the payment.
  112. *
  113. * @see hook_uc_payment_method_callback()
  114. *
  115. * @return
  116. * An array of payment methods. The array contains a sub-array for each
  117. * payment method, with the machine-readable type name as the key. Required
  118. * attributes:
  119. * - "name": the human-readable name of the payment method.
  120. * - "title": the human-readable title of the payment method, displayed
  121. * during checkout.
  122. * - "desc": a human-readable description of the payment method.
  123. * - "callback": a callback function that handles operations that the method
  124. * may need to perform. See hook_uc_payment_method_callback()
  125. * - "weight": the default weight of the payment method.
  126. * - "checkout": if TRUE, the payment method will be enabled by default.
  127. * - "no_gateway": should be set to TRUE, except for uc_credit which uses
  128. * payment gateways.
  129. * - "redirect": if set, this payment method redirects off site; this key
  130. * specifies a callback function which will be used to generate the form
  131. * that redirects the user to the payment gateway pages.
  132. */
  133. function hook_uc_payment_method() {
  134. $methods['check'] = array(
  135. 'name' => t('Check'),
  136. 'title' => t('Check or money order'),
  137. 'desc' => t('Pay by mailing a check or money order.'),
  138. 'callback' => 'uc_payment_method_callback',
  139. 'weight' => 1,
  140. 'checkout' => TRUE,
  141. );
  142. return $methods;
  143. }
  144. /**
  145. * Callback function to perform various operations for a payment method.
  146. *
  147. * Possible operations are as follows:
  148. * - "cart-details": The payment method has been selected at checkout. Return
  149. * a form or render array to be displayed in the payment method pane.
  150. * - "cart-process": Called when the user submits the checkout form with this
  151. * payment method selected, used to process any form elements output by the
  152. * 'cart-details' op. Return FALSE to abort the checkout process, or NULL or
  153. * TRUE to continue with checkout.
  154. * - "cart-review": Called when the checkout review page is being displayed.
  155. * Return an array of data to be displayed below the payment method title on
  156. * the checkout review page.
  157. * - "customer-view": Called when the order is being displayed to a customer.
  158. * Return a render array to be displayed to customers.
  159. * - "order-delete": Called when an order is being deleted. Payment methods
  160. * should clean up any extra data they stored related to the order.
  161. * - "order-details": Called when an order is being edited by an administrator.
  162. * Return a string or a form array to be displayed to the administator.
  163. * - "order-load": Called from hook_uc_order('load') when this payment method
  164. * is selected for the order.
  165. * - "order-process": Called when an order has been edited by an administrator.
  166. * Process any form elements returned by the "order-details" op.
  167. * - "order-save": Called from hook_uc_order('save') when this payment method
  168. * is selected for the order.
  169. * - "order-submit": Called from hook_uc_order('submit') when this payment
  170. * method is selected for the order.
  171. * - "order-view": Called when the order is being displayed on the order admin
  172. * pages. Return a render array to be displayed to administrators.
  173. * - "settings": Called when the payment methods page is being displayed.
  174. * Return a system settings form array to configure the payment method.
  175. *
  176. * @see hook_uc_payment_method()
  177. *
  178. * @param $op
  179. * The operation being performed.
  180. * @param &$order
  181. * The order object that relates to this operation.
  182. * @param $form
  183. * Where applicable, the form object that relates to this operation.
  184. * @param &$form_state
  185. * Where applicable, the form state that relates to this operation.
  186. *
  187. * @return
  188. * Dependent on $op.
  189. */
  190. function hook_uc_payment_method_callback($op, &$order, $form = NULL, &$form_state = NULL) {
  191. switch ($op) {
  192. case 'cart-details':
  193. return array('#markup' => t('Continue with checkout to complete payment.'));
  194. case 'settings':
  195. $form['uc_payment_method_account_number'] = array(
  196. '#type' => 'textfield',
  197. '#title' => t('Payment gateway account number'),
  198. '#default_value' => variable_get('uc_payment_method_account_number', ''),
  199. );
  200. return $form;
  201. }
  202. }
  203. /**
  204. * Alter payment methods.
  205. *
  206. * @param $methods
  207. * Array of payment methods passed by reference. Array is structured like
  208. * the return value of hook_uc_payment_method().
  209. */
  210. function hook_uc_payment_method_alter(&$methods) {
  211. // Change the title of the Check payment method.
  212. $methods['check']['title'] = t('Cheque');
  213. }
  214. /**
  215. * Alter payment methods available at checkout.
  216. *
  217. * @param $methods
  218. * Array of payment methods passed by reference. Keys are payment method IDs,
  219. * strings are payment method titles.
  220. * @param $order
  221. * The order that is being checked out.
  222. */
  223. function hook_uc_payment_method_checkout_alter(&$methods, $order) {
  224. // Remove the Check payment method for orders under $100.
  225. if ($order->order_total < 100) {
  226. unset($methods['check']);
  227. }
  228. }
  229. /**
  230. * @} End of "addtogroup hooks".
  231. */