Skip to main content

ledgerely.com

View Categories

Set Razor Pay Credentials For The Test

Razorpay Credentials & Environment Variable Setup – Ledgerely #

To ensure a secure and scalable payment integration with Razorpay, environment-specific credentials will be configured and managed using GitHub environment variables. This setup will enable seamless deployment across DEV, PROD, and UAT environments.

Setting Up Environment Variables #

Define Environment Variables in GitHub #

Configure GitHub Actions to store Razorpay API credentials as environment variables:
yaml
CopyEdit
RAZORPAY_KEY_TEST=your_test_key
RAZORPAY_SECRET_TEST=your_test_secret
RAZORPAY_KEY_LIVE=your_live_key
RAZORPAY_SECRET_LIVE=your_live_secret

Similarly, app security-related variables should also be added, such as:

yaml
CopyEdit
APP_SECRET_KEY=your_secret_key

DATABASE_URL=your_database_url

JWT_SECRET=your_jwt_secret

Loading Environment Variables in PHP #

Use dotenv for Secure Loading #

Install dotenv for PHP:

sh
CopyEdit
composer require vlucas/phpdotenv

Create a .env file in the project root (for local development):

env
CopyEdit
RAZORPAY_KEY_TEST=your_test_key
RAZORPAY_SECRET_TEST=your_test_secret
RAZORPAY_KEY_LIVE=your_live_key
RAZORPAY_SECRET_LIVE=your_live_secret

Load environment variables in PHP:

php
CopyEdit
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$razorpayKey = $_ENV[‘RAZORPAY_KEY_TEST’]; // Use test key in development

Local Development Setup #

For localhost:8888/moneyPlanner/services, maintain the same .env setup to ensure smooth testing:
env
CopyEdit
RAZORPAY_KEY_TEST=your_test_key
RAZORPAY_SECRET_TEST=your_test_secret

Deployment Strategy (DEV, PROD, UAT) #

Automatic Deployment Handling #

Environment variables will be automatically injected based on the environment.

  • Deployment should follow:
    • DEV → Uses test credentials
    • UAT → Uses test/live credentials
    • PROD → Uses live credentials

In PHP, conditionally switch between environments:
php
CopyEdit
$env = $_ENV[‘APP_ENV’]; // Set as ‘dev’, ‘uat’, or ‘prod’

if ($env === ‘prod’) {
$razorpayKey = $_ENV[‘RAZORPAY_KEY_LIVE’];
} else {
$razorpayKey = $_ENV[‘RAZORPAY_KEY_TEST’];
}

Deployment Prerequisite (Dependency: #57) #

Note: Before proceeding, #57 must be completed as it is a dependency for this implementation.

Final Notes #

  • Secure storage of credentials is ensured through GitHub Actions.
  • Seamless deployment across different environments is handled using dotenv.
  • Local development remains unaffected, maintaining a consistent testing workflow.