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.
This is actually true for any third-party service integration which provides a sandbox 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 payment method settings, but the principle remains the same for any other third-party integration.
I will start from an empty Drupal site hosted on Platform.sh and go through the following steps:
- Enable and configure Paypal WPS payment method
- Export its configuration to a settings.local.php file
- Override its Sandbox configuration on the Staging environment
- Write custom code to read 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.