
Introduction
In today's e-commerce landscape, seamless and secure payment processing is crucial. PayU offers a robust payment gateway solution for businesses to accept online payments. This guide will equip you with the knowledge to integrate PayU with your .NET Core API, enabling you to securely accept payments from your customers.
Prerequisites
Before proceeding, make sure you have a basic understanding of .NET Core and Web API development. Additionally, ensure that you have installed the necessary software, including Visual Studio 2022 or later
Creating a New Project in Visual Studio 2022
Creating a New Project in Visual Studio 2022 Start the Visual Studio software and select "Create a new project."
Backend Configure .NET Core API Configuration Step by Steps.
Step 1. Create a new .NET Core Web API project.
dotnet new webapi -n PayUPaymentIntegration
cd PayUPaymentIntegration
Step 2. Add necessary packages.
dotnet add package Newtonsoft.Json
dotnet add package RestSharp
Step 3. Configure PayU settings.
Create a configuration class for PayU settings in Models/PayUSettings.cs.
public class PayUSettings
{
public string Key { get; set; }
public string Salt { get; set; }
public string BaseUrl { get; set; }
public string SuccessUrl { get; set; }
public string FailureUrl { get; set; }
}
Step 4. Add settings to appsettings.json.
"PayUSettings": {
"Key": "your_key_here",
"Salt": "your_salt_here",
"BaseUrl": "https://test.payu.in/_payment",
"SuccessUrl": "http://localhost:5000/api/payment/success",
"FailureUrl": "http://localhost:5000/api/payment/failure"
}
Step 5. Create a service for PayU integration.
Create a new service in Services/IPayUService.cs. And PayUService.
Interface
public interface IPayUService
{
Task<string> InitiatePayment(PaymentRequest paymentRequest);
}
interface IPayUService: This declares the name of the interface as IPayUService. Interfaces define functionalities that a class implementing the interface must provide.
Class
public class PayUService
{
private readonly PayUSettings _settings;
public PayUService(IOptions<PayUSettings> settings)
{
_settings = settings.Value;
}
public async Task<string> InitiatePayment(PaymentRequest paymentRequest)
{
var hashString = $"{_settings.Key}|{paymentRequest.txnId}|{paymentRequest.amount}|{paymentRequest.productInfo}|{paymentRequest.firstName}|{paymentRequest.email}|||||||||||{_settings.Salt}";
var hash = GenerateHash(hashString);
var client = new RestClient(_settings.BaseUrl);
var request = new RestRequest("", Method.Post);
request.AddParameter("key", _settings.Key);
request.AddParameter("txnid", paymentRequest.txnId);
request.AddParameter("amount", paymentRequest.amount);
request.AddParameter("productinfo", paymentRequest.productInfo);
request.AddParameter("firstname", paymentRequest.firstName);
request.AddParameter("email", paymentRequest.email);
request.AddParameter("phone", paymentRequest.Phonenumber); // Replace with actual phone number
request.AddParameter("surl", _settings.SuccessUrl);
request.AddParameter("furl", _settings.FailureUrl);
request.AddParameter("hash", hash);
// request.AddParameter("service_provider", "payu_paisa");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
var response = await client.ExecuteAsync(request);
if (response.IsSuccessStatusCode)
{
return response.ResponseUri.AbsoluteUri;
}
else
{
return response.ResponseUri.AbsoluteUri;
}
}
}
InitiatePayment Method
- public async Task<string> InitiatePayment(PaymentRequest paymentRequest): This method implements the InitiatePayment method from the IPayUService interface. It's marked as public, asynchronous (async), and returns a Task<string>.
- PaymentRequest paymentRequest: This is the argument containing details about the payment to be initiated.
Hash Generation
The code constructs a string using the following information from paymentRequest and _settings.
- Merchant Key (_settings. Key)
- Transaction ID (paymentRequest.txnId)
- Amount (paymentRequest.amount)
- Product Information (paymentRequest.productInfo)
- Customer Name (paymentRequest.firstName)
- Customer Email (paymentRequest.email)
- Additional fields (empty in this example)
- Salt (_settings. Salt)
This string is then used to generate a hash using a function called GenerateHash (implementation not shown, likely uses a hashing algorithm like SHA).
Sending the Request and Handling Response
The ExecuteAsync method is called on the client object with the prepared request. This sends the request to PayU's API asynchronously.
The response from PayU is stored in a response variable.
An if statement checks the IsSuccessStatusCode property of the response.
- If successful (IsSuccessStatusCode is true), the absolute URI of the response (the redirection URL) is returned.
- If unsuccessful, the code currently returns the same redirection URL (likely for debugging purposes, you should implement proper error handling here)
Step 6. Add service and settings to the Program. cs.
builder.Services.Configure<PayUSettings>(builder.Configuration.GetSection("PayUSettings"));
builder.Services.AddTransient<IPayUService, PayUService>();
builder.Services.Configure<PayUSettings>(builder.Configuration.GetSection("PayUSettings"));
This line configures the dependency injection system in your application.
Let's break it down.







Join the conversation! Your thoughts help the community grow.