View source
<?php
function commerce_payment_rules_event_info() {
module_load_include('inc', 'entity', 'entity.rules');
$events = array();
$events['commerce_payment_methods'] = array(
'label' => t('Select available payment methods for an order'),
'group' => t('Commerce Payment'),
'variables' => entity_rules_events_variables('commerce_order', t('Order', array(), array(
'context' => 'a drupal commerce order',
))),
'access callback' => 'commerce_order_rules_access',
);
$variables = array_merge(entity_rules_events_variables('commerce_order', t('Order', array(), array(
'context' => 'a drupal commerce order',
)), TRUE, TRUE), entity_rules_events_variables('commerce_payment_transaction', t('Last completed transaction'), TRUE));
$events['commerce_payment_order_paid_in_full'] = array(
'label' => t('When an order is first paid in full'),
'group' => t('Commerce Payment'),
'variables' => $variables,
'access callback' => 'commerce_order_rules_access',
);
return $events;
}
function commerce_payment_rules_condition_info() {
$conditions = array();
$conditions['commerce_payment_order_balance_comparison'] = array(
'label' => t('Order balance comparison'),
'parameter' => array(
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Order'),
'description' => t('The order whose balance should be compared (calculated as the order total minus completed payment amounts).'),
),
'operator' => array(
'type' => 'text',
'label' => t('Operator'),
'description' => t('The comparison operator.'),
'optional' => TRUE,
'default value' => '<=',
'options list' => 'commerce_numeric_comparison_operator_options_list',
'restriction' => 'input',
),
'value' => array(
'type' => 'text',
'label' => t('Value'),
'description' => t('Integer representing a value in minor currency units to compare against, such as 1000 for $10. A balance of 0 or less indicates the order has been paid in full.'),
'default value' => '0',
),
),
'group' => t('Commerce Payment'),
'callbacks' => array(
'execute' => 'commerce_payment_rules_compare_balance',
),
);
$conditions['commerce_payment_selected_payment_method'] = array(
'label' => t('Selected payment method comparison'),
'parameter' => array(
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Order'),
'description' => t('The order whose selected payment method (if any) should be compared against the method specified below.'),
),
'method_id' => array(
'type' => 'text',
'label' => t('Payment method'),
'description' => t('This condition will perform a simple equivalency check to see if the payment method you specify matches what a customer selected on the checkout form.'),
'options list' => 'commerce_payment_rules_payment_method_options_list',
'restriction' => 'input',
),
),
'group' => t('Commerce Payment'),
'callbacks' => array(
'execute' => 'commerce_payment_rules_compare_selected_payment_method',
),
);
return $conditions;
}
function commerce_payment_rules_compare_balance($order, $operator, $value) {
$balance = commerce_payment_order_balance($order);
if ($balance === FALSE) {
$balance = entity_metadata_wrapper('commerce_order', $order)->commerce_order_total
->value();
}
switch ($operator) {
case '<':
return $balance['amount'] < $value;
case '<=':
return $balance['amount'] <= $value;
case '=':
return $balance['amount'] == $value;
case '>=':
return $balance['amount'] >= $value;
case '>':
return $balance['amount'] > $value;
}
return FALSE;
}
function commerce_payment_rules_payment_method_options_list() {
return array(
'-none-' => t('None'),
) + commerce_payment_method_get_title();
}
function commerce_payment_rules_compare_selected_payment_method($order, $method_id) {
if (!empty($order->data['payment_method'])) {
list($selected_method_id, ) = explode('|', $order->data['payment_method']);
}
else {
$selected_method_id = '-none-';
}
return $method_id == $selected_method_id;
}
function commerce_payment_rules_action_info() {
$actions = array();
foreach (commerce_payment_methods() as $payment_method) {
$actions['commerce_payment_enable_' . $payment_method['method_id']] = array(
'label' => t('Enable payment method: @method', array(
'@method' => $payment_method['title'],
)),
'parameter' => array(
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Order', array(), array(
'context' => 'a drupal commerce order',
)),
'skip save' => TRUE,
),
'payment_method' => array(
'type' => 'commerce_payment_settings',
'restriction' => 'input',
'label' => t('Payment settings'),
'payment_method' => $payment_method['method_id'],
),
),
'group' => t('Commerce Payment'),
'callbacks' => array(
'execute' => 'commerce_payment_enable_method',
),
);
}
$actions['commerce_payment_redirect_pane_previous_page'] = array(
'label' => t('Redirect the checkout to the previous pane'),
'parameter' => array(
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Order', array(), array(
'context' => 'a drupal commerce order',
)),
'skip save' => TRUE,
),
),
'group' => t('Commerce Payment'),
'callbacks' => array(
'execute' => 'commerce_payment_rules_redirect_pane_previous_page',
),
);
$actions['commerce_payment_redirect_pane_next_page'] = array(
'label' => t('Redirect the checkout to the next pane'),
'parameter' => array(
'commerce_order' => array(
'type' => 'commerce_order',
'label' => t('Order', array(), array(
'context' => 'a drupal commerce order',
)),
'skip save' => TRUE,
),
),
'group' => t('Commerce Payment'),
'callbacks' => array(
'execute' => 'commerce_payment_rules_redirect_pane_next_page',
),
);
return $actions;
}
function commerce_payment_rules_redirect_pane_previous_page($order) {
commerce_payment_redirect_pane_previous_page($order);
}
function commerce_payment_rules_redirect_pane_next_page($order) {
commerce_payment_redirect_pane_next_page($order);
}
function commerce_payment_enable_method($order, $payment_method, $action_settings, $rule_state, $action, $callback_type) {
$rule = $action
->parentElement();
while (!$rule instanceof RulesActionContainer) {
if ($rule
->parentElement()) {
$rule = $rule
->parentElement();
}
else {
return;
}
}
if (is_array($payment_method)) {
$method_id = $payment_method['method_id'];
$settings = !empty($payment_method['settings']) ? $payment_method['settings'] : array();
}
else {
$method_id = $payment_method;
$settings = array();
}
$instance_id = commerce_payment_method_instance_id($method_id, $rule);
$order->payment_methods[$instance_id] = array(
'method_id' => $method_id,
'settings' => $settings,
'rule_name' => $rule->name,
'weight' => $rule->weight,
);
}
function commerce_payment_rules_data_info() {
$data['commerce_payment_settings'] = array(
'label' => t('Payment settings'),
'ui class' => 'RulesDataUIPaymentSettings',
);
return $data;
}
class RulesDataUIPaymentSettings extends RulesDataUI implements RulesDataDirectInputFormInterface {
public static function getDefaultMode() {
return 'input';
}
public static function inputForm($name, $info, $settings, RulesPlugin $element) {
if (!empty($info['payment_method']) && ($payment_method = commerce_payment_method_load($info['payment_method']))) {
$form[$name]['method_id'] = array(
'#type' => 'value',
'#value' => $info['payment_method'],
);
if ($callback = commerce_payment_method_callback($payment_method, 'settings_form')) {
$method_settings = !empty($settings[$name]['settings']) && is_array($settings[$name]['settings']) ? $settings[$name]['settings'] : array();
$form[$name]['settings'] = $callback($method_settings);
}
else {
$form[$name]['settings']['no_settings']['#markup'] = t('No settings for this payment method.');
}
}
else {
$form[$name]['invalid']['#markup'] = t('Invalid or missing payment method.');
}
return $form;
}
public static function render($value) {
return array();
}
}