From b8befaf13342f2e08c9bc91750d083cb65021dcc Mon Sep 17 00:00:00 2001 From: Trevor Simonton Date: Tue, 27 Nov 2012 12:25:52 -0700 Subject: [PATCH] Issue #1820356 by druroot and tmsimont: Replace the payment_capture rule action with a working rule. --- modules/payment/commerce_payment.module | 25 ++++++++++++++++++++++++ modules/payment/commerce_payment.rules.inc | 29 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 0 deletions(-) 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..c7ae4c6 100644 --- a/modules/payment/commerce_payment.rules.inc +++ b/modules/payment/commerce_payment.rules.inc @@ -181,6 +181,14 @@ 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'))), + ), + 'group' => t('Commerce Payment'), + ); + return $actions; } @@ -224,6 +232,27 @@ function commerce_payment_enable_method($order, $payment_method, $action_setting } /** + * Rules action: capture a payment from a previous authorization. + */ +function commerce_payment_capture($order) { + $transaction = commerce_payment_fetch_last_payment_transaction($order->order_id); + $amount = commerce_currency_amount_to_decimal($transaction->amount, $transaction->currency_code); + $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, $order, $payment_method); + } else { + $return = FALSE; + } + //@todo add watchdog reporting + + return $return; +} + +/** * Implements hook_rules_data_info(). */ function commerce_payment_rules_data_info() { -- 1.7.4.msysgit.0