uc_payment.rules.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @file
  4. * Rules definitions.
  5. */
  6. /**
  7. * Implements hook_rules_event_info().
  8. */
  9. function uc_payment_rules_event_info() {
  10. $events['uc_payment_entered'] = array(
  11. 'label' => t('A payment gets entered for an order'),
  12. 'group' => t('Payment'),
  13. 'variables' => array(
  14. 'order' => array(
  15. 'type' => 'uc_order',
  16. 'label' => t('Order'),
  17. ),
  18. 'account' => array(
  19. 'type' => 'user',
  20. 'label' => t('User'),
  21. ),
  22. ),
  23. );
  24. return $events;
  25. }
  26. /**
  27. * Implements hook_rules_condition_info().
  28. */
  29. function uc_payment_rules_condition_info() {
  30. $conditions['uc_payment_condition_order_balance'] = array(
  31. 'label' => t('Check the order balance'),
  32. 'group' => t('Payment'),
  33. 'base' => 'uc_payment_condition_order_balance',
  34. 'parameter' => array(
  35. 'order' => array(
  36. 'type' => 'uc_order',
  37. 'label' => t('Order'),
  38. 'restriction' => 'selector',
  39. ),
  40. 'balance_comparison' => array(
  41. 'type' => 'text',
  42. 'label' => t('Operator'),
  43. 'options list' => 'uc_payment_condition_balance_options',
  44. 'restriction' => 'input',
  45. ),
  46. 'include_authorizations' => array(
  47. 'type' => 'boolean',
  48. 'label' => t('Include authorizations?'),
  49. 'description' => t('Should "authorization only" credit card transactions be used in calculating the order balance?'),
  50. 'restriction' => 'input',
  51. 'optional' => TRUE,
  52. 'default value' => FALSE,
  53. ),
  54. ),
  55. );
  56. return $conditions;
  57. }
  58. /**
  59. * Condition: Check the current order balance.
  60. */
  61. function uc_payment_condition_order_balance($order, $balance_comparison, $include_authorizations) {
  62. $balance = uc_payment_balance($order);
  63. if ($include_authorizations) {
  64. foreach ((array) $order->data['cc_txns']['authorizations'] as $auth_id => $data) {
  65. $balance -= $data['amount'];
  66. }
  67. }
  68. switch ($balance_comparison) {
  69. case 'less':
  70. return $balance < 0;
  71. case 'less_equal':
  72. return $balance <= 0.01;
  73. case 'equal':
  74. return $balance < 0.01 && $balance > -0.01;
  75. case 'greater':
  76. return $balance >= 0.01;
  77. }
  78. }
  79. /**
  80. * Returns balance options.
  81. */
  82. function uc_payment_condition_balance_options() {
  83. $zero = array('!zero' => uc_currency_format(0));
  84. $options = array(
  85. 'less' => t('Balance is less than !zero.', $zero),
  86. 'less_equal' => t('Balance is less than or equal to !zero.', $zero),
  87. 'equal' => t('Balance is equal to !zero.', $zero),
  88. 'greater' => t('Balance is greater than !zero.', $zero),
  89. );
  90. return $options;
  91. }