How to programmatically retrieve configuration of the specific payment module?

I tried

$settings = rules_config_load('commerce_payment_MODULE_NAME');

It returns very basic info (name, ID etc) and empty "settings" array.

Cant also get it directly from DB .

It is stored in serialized array that can not be fully processed with the
unserialize() function

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Artusamak’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
1.87 KB

The attached patch is adding the support for payment method settings declaration in the hook_commerce_payment_method_info().
You just need to declare the settings such as below:

<?php
/**
 * Implementation of hook_commerce_payment_method_info().
 */
function mymodule_commerce_payment_method_info(){
  $payment_methods = array();

  $payment_methods['provider'] = array(
    'title' => t('Method name'),
    'base' => 'mymodule',
    'offsite' => TRUE,
    'offsite_autoredirect' => FALSE,
    'terminal' => FALSE,
    'active' => TRUE,
    'settings' => array(
      'param1' => TRUE,
      'param2' => 'value',
    ),
  );
  return $payment_methods;
}
?>

If you are just using a payment method which is previously exposing the settings you can use the hook_commerce_payment_method_info_alter() if you want to override the default values.

Haza’s picture

Title: How to get payment settings? » Import default settings for payment methods
Version: 7.x-1.0-rc2 » 7.x-1.x-dev
Category: support » task
Status: Active » Reviewed & tested by the community

Tested, works as expected ! Thanks for this patch !

rszrama’s picture

Title: Import default settings for payment methods » Allow hook_commerce_payment_method_info() to specify default payment method settings
Category: task » feature

So basically this patch just lets you define default settings inside your hook_commerce_payment_method_info() declaration (or via the alter hook)? And this in turn makes it so you can store default configuration for payment methods in code without depending on exported Rules? If so, I like it.

The one thing I see in this patch, though, is that your $action_settings variable seems to put a non-payment method setting for the action in the payment method settings array. I'm not sure how it's working. Is that a bug or was it buggy before?

Compare what you made it:

  $action_settings['payment_method']['commerce_order:select'] = 'commerce-order';

To the previous array:

  ->action('commerce_payment_enable_' . $method_id, array(
    'commerce_order:select' => 'commerce-order',
    'payment_method' => $method_id,
  ));

Seems yours should just be this, no?

  $action_settings['commerce_order:select'] = 'commerce-order';
mrfelton’s picture

+1 to this functionality. Patch works as expected.

vadym.kononenko’s picture

Issue summary: View changes
FileSize
679 bytes

But why we need to rebuild all settings structure?
I've reached identical result just made such simple patch.

It just get payment method info from modules 'hook_commerce_payment_method_info()' and 'hook_commerce_payment_method_info_alter()' and fill in the rule action form with it.

It also give me ability to modify (add some conditions to set payment method visibility) any payment rule by using 'hook_default_rules_configuration_alter()'

All works like a charm in several projects.

pixelula’s picture

Hi,

Patch #5 works for me. Thanks

bsandor’s picture

I heaven't understood the idea of patch #5.
Can you explain it please.

Am I right in followings:
If I apply this patch I can 'add' my modules configuration settings (which come in a rule) programmatically by using hook_commerce_payment_method_info_alter(); while without it I can not? Or I can but my settings need to be saved first than I can use this hook without the patch.

I need some clarification please.

Or at least can you add directions how to use patch#5 once i implemented the hook. (how to add settings in the implementation)
How can I fill the rule action form with it?

Thanks

bsandor’s picture

I've checked the code:

If I'm right this alter function runs only during rule action save.

It means you need to go to edit the rule action than save it for it to be effective. So basically you can do the same thing with hook_form_alter().

Or if you are writing your own module you can set it up in _payment_default_settings ()

I can not try if i'm right as I don't know what to do with this Patch#5. I need information.

Chris Matthews’s picture

The 5 year old patch in #5 to commerce_payment.rules_defaults.inc applied cleanly to the latest commerce 7.x-1.x-dev and already has several reviews, but there are also some outstanding questions so leaving the status as is.