You are here

function commerce_payment_entity_load in Commerce Core 7

Implements hook_entity_load().

Override payment method settings on the fly.

File

modules/payment/commerce_payment.module, line 1259
Defines the payment system and checkout integration.

Code

function commerce_payment_entity_load($entities, $type) {
  if ($type != 'rules_config') {
    return;
  }
  foreach ($entities as $entity) {
    if (!$entity instanceof RulesTriggerableInterface) {
      continue;
    }
    $events = $entity
      ->events();

    // Check for the "Select available payment methods for an order" event.
    if (!in_array('commerce_payment_methods', $events)) {
      continue;
    }

    // Iterate over the actions to find one with the matching element ID and
    // check if we have payment settings override.
    foreach ($entity
      ->actions() as $name => $action) {
      if (is_callable(array(
        $action,
        'getElementName',
      )) && strpos($action
        ->getElementName(), 'commerce_payment_enable_') === 0) {
        if (isset($action->settings['payment_method']) && is_array($action->settings['payment_method']) && !empty($action->settings['payment_method']['settings'])) {
          $instance_id = commerce_payment_method_instance_id($action->settings['payment_method']['method_id'], $entity);
          $settings = variable_get($instance_id, array());
          if (!empty($settings) && is_array($settings)) {
            $action->settings['payment_method']['settings'] = array_replace_recursive($action->settings['payment_method']['settings'], $settings);
            $action
              ->processSettings(TRUE);
          }
        }
      }
    }
  }
}