Integrating Razorpay Payment Gateway with .NET Core

Introduction

In the era of digital transactions, integrating a reliable payment gateway into your application is essential. Razorpay, a popular payment gateway in India, provides a robust API for seamless integration into various platforms. In this guide, we'll walk you through the process of integrating Razorpay with a .NET Core application.

Step 1. Setting Up Your Razorpay Account

Before diving into code, you need an active account with Razorpay. If you don't have one, you can sign up on their official website. Post-registration, navigate to the dashboard to generate your API keys (Key Id and Key Secret). These keys will be essential for integrating the Razorpay payment gateway with your .NET Core application.

Step 2. Creating a .NET Core Web Application

Start by creating a new .NET Core Web Application. You can do this using Visual Studio or the .NET CLI. Here's how you can create it using the CLI:

dotnet new webapp -n RazorpayIntegrationApp
cd RazorpayIntegrationApp

Step 3. Installing Razorpay .NET SDK

To interact with Razorpay APIs, you need to install the Razorpay .NET SDK. You can add it to your project via NuGet Package Manager. Execute the following command in your project directory:

dotnet add package Razorpay.Api

Step 4. Storing and Using Razorpay Credentials

For security reasons, it's a good practice not to hard-code your API keys in your source code. Instead, use environment variables or secure app settings. For simplicity, we'll store these in the appsettings.json:

{
  "RazorpayKey": "YOUR_KEY_ID",
  "RazorpaySecret": "YOUR_SECRET_KEY"
}

Replace YOUR_KEY_ID and YOUR_SECRET_KEY with your actual Razorpay API keys.

Step 5. Implementing Razorpay Payment Gateway

Create a service class that will handle the Razorpay payment interactions. Let's call this class PaymentService.cs:

using Razorpay.Api;
using Microsoft.Extensions.Configuration;

public class PaymentService
{
    private RazorpayClient _client;

    public PaymentService(IConfiguration configuration)
    {
        var apiKey = configuration.GetValue<string>("RazorpayKey");
        var apiSecret = configuration.GetValue<string>("RazorpaySecret");

        _client = new RazorpayClient(apiKey, apiSecret);
    }

    public Payment CreatePayment(int amount, string currency = "INR")
    {
        var options = new Dictionary<string, object>
        {
            { "amount", amount * 100 }, // Razorpay expects the amount in the smallest currency unit
            { "currency", currency },
            { "receipt", "order_rcptid_11" },
            { "payment_capture", 1 }
        };

        var payment = _client.Payment.Create(options);
        return payment;
    }
}

Step 6. Setting Up the Controller

Now integrate the PaymentService in an API controller which will handle the payment requests:

using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class PaymentController : ControllerBase
{
    private readonly PaymentService _paymentService;

    public PaymentController(PaymentService paymentService)
    {
        _paymentService = paymentService;
    }

    [HttpPost("create")]
    public IActionResult CreatePayment([FromBody] int amount)
    {
        var payment = _paymentService.CreatePayment(amount);
        return Ok(payment);
    }
}

Step 7. Testing the Integration

Run your application and use a tool like Postman to make a POST request to /payment/create with an amount in the request body. If all is set up correctly, this should create a payment and you'll be able to see the details in Razorpay's dashboard.

Conclusion

Integrating Razorpay in a .NET Core application involves setting up the Razorpay account, installing necessary packages, and safely handling API keys. By following these steps, you can implement a secure and efficient payment solution in your .NET Core applications, enhancing your app's commercial capabilities.


Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.