2

What is the recommended way to configure public api keys for multi-environment configurations (i.e. local, dev, stage, production)? Is there a way to set these in either the craft/config/general.php or in plugin/config.php files?

Currently I am having to include these in each of my service methods (and something similar again in my javascript files):

private function _saveStripeCustomer($account, $token = NULL)
{
    \Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxx");
    // ...
}

I am generally following the configuration method shown here (both multi-environment and multi-locale). And a plugin config.php as described here.

1 Answer 1

5

You can insert whatever settings you need either in your config/general.php file, config/pluginhandle.php, or the plugin/config.php file.

You can have different value for your setting in each of the environments you have defined. If you call your setting myplugin_stripe_key, you'd put something like this in your config file:

return array(
    '*' => array(
      ...
    ),

    'local' => array(
        'myplugin_stripe_key' => 'sk_test_xxxxxxxxxxxxxxxxxx',
        ...
    ),

    'stage' => array(
        'myplugin_stripe_key' => 'sk_stage_xxxxxxxxxxxxxxxxxx',
        ...
    ),

    'prod' => array(
        'myplugin_stripe_key' => 'sk_prod_xxxxxxxxxxxxxxxxxx',
        ...
    )
);

In your plugin:

private function _saveStripeCustomer($account, $token = NULL)
{
  \Stripe::setApiKey(craft()->config->get('myplugin_stripe_key'));
  // ...
}

If you use this several places in your service, I'd fetch all settings in the constructor and save them in an array:

class MypluginService extends BaseApplicationComponent
{
    var $settings = array();

    public function __construct($urlRecord = null)
    {
        $this->settings = $this->_initSettings();
    }

    private function _initSettings()
    {
        $settings = array();
        $settings['myplugin_stripe_key'] = craft()->config->get('myplugin_stripe_key');
        return $settings;
    }
}

Then you can access them later like this:

\Stripe::setApiKey($this->settings['myplugin_stripe_key']);

If you need this key in javascript, you could create a craft variable that gets the config setting and output it to a javascript variable somewhere in your twig templates. You'd basically use the same way to get the config variable inside the craft variable method.

3
  • If you have a sec... I meant to ask, what is $urlRecord in __construct($urlRecord = null) for? Commented Feb 6, 2015 at 2:56
  • @DouglasMcDonald In this case, nothing. :) Just forgot to remove it. Just something that was left from a previous plugin. The point of sending in a record to the service's constructor, is to be able to write tests for it. But I'm no expert on that, so you should seek advice from someone else if you want to learn more about it. ;) Commented Feb 7, 2015 at 20:07
  • Got it. By the way, this all works great. Thanks again, André. Commented Feb 7, 2015 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.