diff --git a/modules/payment/commerce_payment.module b/modules/payment/commerce_payment.module index 54e9368..3d1a133 100644 --- a/modules/payment/commerce_payment.module +++ b/modules/payment/commerce_payment.module @@ -1119,3 +1119,28 @@ function commerce_payment_transaction_set_properties($transaction, $name, $value break; } } + +/** + * Util to fetch the last payment transaction made against an order + * @param $order_id int + * The order id + * + * @return int + * commerce_payment_transaction_id +*/ +function commerce_payment_fetch_last_payment_transaction($order_id) { + $query = db_select("commerce_payment_transaction", 'cpt'); + $query->fields('cpt', array('transaction_id')); + $query->range(0, 1); + $query->condition('cpt.order_id', $order_id); + $query->orderBy('cpt.created', 'DESC'); + $result = $query->execute(); + foreach ($result as $row) { + $transaction_id = $row->transaction_id; + } + + if ($transaction = commerce_payment_transaction_load($transaction_id)) { + return $transaction; + } + return FALSE; +} diff --git a/modules/payment/commerce_payment.rules.inc b/modules/payment/commerce_payment.rules.inc index 4868b6e..bdc0864 100644 --- a/modules/payment/commerce_payment.rules.inc +++ b/modules/payment/commerce_payment.rules.inc @@ -181,6 +181,15 @@ function commerce_payment_rules_action_info() { ); } + $actions['commerce_payment_capture'] = array( + 'label' => t('Capture from a prior authorization'), + 'parameter' => array( + 'commerce_order' => array('type' => 'commerce_order', 'label' => t('Order', array(), array('context' => 'a drupal commerce order'))), + 'amount' => array('type' => 'text', 'label' => t('Amount')), + ), + 'group' => t('Commerce Payment'), + ); + return $actions; } @@ -224,6 +233,29 @@ function commerce_payment_enable_method($order, $payment_method, $action_setting } /** + * Rules action: capture a payment from a previous authorization. + */ +function commerce_payment_capture($order, $amount) { + $transaction = commerce_payment_fetch_last_payment_transaction($order->order_id); + //For some reason, amount comes through a a string from the rule config, so + //until that gets fixed, we'll just get the amount from the transaction data + $amount = $transaction->amount / 100; + $instance_id = $transaction->instance_id; + $payment_method = commerce_payment_method_instance_load($instance_id); + $base = $payment_method['base']; + $callback = $base . '_capture'; + + if(function_exists($callback)) { + $return = $callback($transaction, $amount); + } else { + $return = FALSE; + } + //@todo add watchdog reporting + + return $return; +} + +/** * Implements hook_rules_data_info(). */ function commerce_payment_rules_data_info() {