Introduction:
In today’s fast-paced digital world, seamless and secure online payment processing is essential for businesses of all sizes. One popular payment gateway solution is Razorpay, which offers a wide range of features and supports multiple payment methods. In this blog post, we will guide you through the process of integrating Razorpay into your PHP web application using Express Checkout. So let’s dive in and simplify your online payment experience!
Step 1: Set Up Your Razorpay Account To get started, you need to create an account on the Razorpay platform. Visit the Razorpay website (https://razorpay.com) and sign up for an account. Once registered, you will gain access to the necessary credentials required for the integration.
Step 2: Obtain API Credentials After logging into your Razorpay account, navigate to the Dashboard. On the left-hand side, click on “Settings” and then select “API Keys.” Here, you will find your API Key and Secret Key, which you will need to integrate Razorpay into your PHP application. Keep these credentials handy for the next steps.
Step 3: Download the Razorpay PHP SDK Razorpay provides an official PHP SDK that simplifies the integration process. You can download it from the official GitHub repository (https://github.com/razorpay/razorpay-php) by either cloning the repository or downloading the ZIP file. Extract the contents of the SDK to a convenient location within your project.
Step 4: Include the Razorpay PHP SDK In your PHP project, locate the file where you want to integrate Razorpay. Begin by including the necessary Razorpay files by adding the following line at the top of your PHP file:
require_once('path/to/razorpay-php/Razorpay.php');
Step 5: Create a New Order To initiate a payment, you need to create an order. Use the Razorpay PHP SDK to generate a new order by adding the following code snippet:
use Razorpay\Api\Api;
$api = new Api($api_key, $api_secret);
$order = $api->order->create([
'amount' => 1000, // Amount in paise (e.g., 1000 paise = ₹10)
'currency' ='INR',
'payment_capture' => 1 // Auto capture the payment
]);
Make sure to replace $api_key and $api_secret with your actual API credentials obtained in Step 2.
Step 6: Display the Payment Form After creating the order, you can generate a payment form that allows users to complete the transaction. Add the following code snippet to display the payment form:
‘
Ensure that the action attribute of the form points to a PHP file (submit_payment.php in this example) where you will handle the payment submission.
Step 7: Process the Payment Create a new PHP file (e.g., submit_payment.php) to handle the payment submission. In this file, include the Razorpay PHP SDK and add the following code:
use Razorpay\Api\Api;
$api = new Api($api_key, $api_secret);
$payment_id = $_POST['razorpay_payment_id'];
$payment = $api->payment->fetch($payment_id);
if ($payment->status === 'captured') {
// Payment successful, perform necessary actions (e.g., update database, send confirmation emails, etc.)
echo 'Payment successful!';
} else {
// Payment failed or unauthorized, handle accordingly
echo 'Payment failed!';
}
Remember to replace $api_key and $api_secret with your actual API credentials.
Conclusion:
Congratulations! You have successfully integrated Razorpay into your PHP web application using Express Checkout. By following the steps outlined in this blog post, you can now accept secure online payments and enhance the overall user experience. Remember to handle payment status and perform appropriate actions based on the transaction’s outcome.
Please note that this guide provides a basic integration example. For more advanced features and customization options, refer to the official Razorpay documentation (https://razorpay.com/docs/).
Happy integrating and happy payments!