Drupal Commerce Blog

What's happening in the world of Drupal Commerce.

How to switch your payment settings based on environment variables using Platform.sh

When working on a Commerce project which uses a payment gateway, you need to always make sure that your Staging and Development environments are properly targeting the sandbox or test mode of your payment gateway, and that your Production site is targeting the live account.

In fact, this is true for any third-party service integration which provides a sandbox mode where you can test. The objective is to make sure you never send test data on a live account, no matter the service you're testing on.

For this tutorial, I will focus on a specific payment method: Paypal Payment Standards (WPS), but those principles remain the same for any payment method or third-party integration.

I will start with an empty Drupal site hosted on Platform.sh and go through the following steps:

  • Enable and configure Commerce Paypal WPS
  • Export its configuration to a settings.local.php file
  • Override its configuration to a Staging environment
  • Add some custom code to get the configuration from the settings.local.php

As you see, the goal (as always with Drupal) is to read the configuration from your code so that you can easily switch from a sandbox mode to a live mode.

Enable Paypal WPS

Since I'm starting from an empty Drupal site, I need to install and enable Commerce and its dependencies and Commerce Papayl WPS. With Platform.sh you can simply push a project.make file and the site will be built for you:

api = 2
core = 7.x

; Drupal core.
projects[drupal][type] = core
projects[drupal][version] = 7.34
projects[drupal][patch][] = "https://drupal.org/files/issues/install-redirect-on-empty-database-72870... Drush make allows a default sub directory for all contributed projects.
defaults[projects][subdir] = contrib

; Platform indicator module.
projects[platform][version] = 1.3

; Commerce modules.
projects[commerce][version] = "1.10"
projects[commerce_paypal][version] = "2.3"

; Commerce dependencies.
projects[addressfield][version] = "1.0-beta5"
projects[ctools][version] = "1.4"
projects[entity][version] = "1.5"
projects[rules][version] = "2.7"
projects[views][version] = "3.8"

; Commerce Payment Settings Switcher (Github)
projects[commerce_payment_settings_switcher][download][type] = "git"
projects[commerce_payment_settings_switcher][download][branch] = "master"
projects[commerce_payment_settings_switcher][download][url] = "git://github.com/GuGuss/commerce_payment_settings_switcher.git"

Export your Paypal configuration into settings.local.php

It's best practice to version your settings.php in your Git repository, and make it include a settings.local.php file which contains the database credentials of the environment (production, staging, dev...) and which is not included in your Git repository but stays on your environment server.

Here is an example of a working settings.php:

// Include the local settings specific to the environment.
$local_settings = dirname(__FILE__) . '/settings.local.php';
if (
file_exists($local_settings)) {

Then edit your settings.local.php file to add the Paypal credentials:

// sites/default/settings.local.php
$conf['paypal_settings'] = array(
"business" => "augustin @ live.com",
"currency_code" => "EUR",
"allow_supported_currencies" => "FALSE",
"language" => "FR",
"server" => "live",
"payment_action" => "sale",
"ipn_logging" => "notification",
"receiver_emails" => "",
"ipn_create_billing_profile" => "FALSE",
"show_payment_instructions" => "FALSE",

With Platform.sh, your settings.local.php is automatically generated, so you can simply add the same variables as a JSON array directly from the UI:

{"business":"augustin@live.com","currency_code":"EUR","allow_supported_currencies":"FALSE","language":"FR","server":"live","payment_action":"sale","ipn_logging":"notification","receiver_emails":"","ipn_create_billing_profile":"FALSE","show_payment_instructions": "FALSE"}

Do the same on your Staging or Development environments but change the server to sandbox and the ipn_logging to full_ipn.

With Platform.sh, you simply need to override the existing variable that you have defined on the Master environment.

Edit: I committed this module on Github so you can use it and extend it for your own needs.

Enable the Commerce Payment Settings Switcher

This module allows you to override your payment method configuration using a Drupal variable. You can checkout the module from Github.

This module currently supports Stripe and Paypal, but it's very simple to extend it for your own payment method.

That's it, you now have a payment configuration which will switch depending on the environment that you are working on!

Augustin Delaporte
Posted: Dec 4, 2014


maciej@commerceguys.com Maciej Zgadzaj on February 26, 2015

Alternativelly, you can get this patch in, and override *any* payment method's settings from your settings.local.php like:

$conf['payment_method_instance_id']['settings']['property'] = 'value';

for example:

$conf['paypal_wps|commerce_payment_paypal_wps']['business'] = 'augustin @ live.com';