Using Environment Variables (Recommended)
It’s highly recommended to use environment variables to store your credentials. The provided example .php file uses the Dotenv library for this purpose.
- Install Dotenv
composer require vlucas/phpdotenv
- Create a .env file
In your project’s root directory, create a .env file with the following content, replacing the placeholders with your actual credentials:
CLIENT_ID=YOUR_CLIENT_ID
CLIENT_VERSION=2
CLIENT_SECRET=YOUR_CLIENT_SECRET
ENV=PRODUCTION
USERNAME=YOUR_USERNAME
PASSWORD=YOUR_PASSWORD
- Access Credentials in your PHP code
<?php
require_once "vendor/autoload.php";
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
$clientId = $_ENV["CLIENT_ID"];
$clientVersion = $_ENV["CLIENT_VERSION"];
$clientSecret = $_ENV["CLIENT_SECRET"];
$env = $_ENV["ENV"];
$username = $_ENV["USERNAME"];
$password = $_ENV["PASSWORD"];
?>
Best Practices
- Secure Credentials: Never hardcode your Client Secret or other sensitive credentials directly in your code. Use environment variables or a secure configuration management system.
- Logging: Implement comprehensive logging to track transactions and debug issues.
- Error Handling: Handle exceptions gracefully and provide informative error messages to the user.
- Asynchronous Callbacks: Always rely on the asynchronous callbacks for reliable payment confirmation. Do not solely depend on the redirect after payment.
- Idempotency: Ensure that your payment initiation and refund requests are idempotent. This means that if the same request is sent multiple times, it should only be processed once. Use your merchantOrderId and merchantRefundId to achieve idempotency.
- Use Builders: Utilize the StandardCheckoutPayRequestBuilder and StandardCheckoutRefundRequestBuilder classes to create request objects. This provides a cleaner and more maintainable way to construct your requests.
This comprehensive guide provides all the necessary information to integrate the PhonePe Standard Checkout Backend PHP SDK into your platform.