Overview
In digital payment systems, validating a UPI (Unified Payments Interface) ID before initiating a transaction is essential to ensure accuracy and reduce failures. This article explains how to integrate a UPI ID validation API using C# (ASP.NET) and JavaScript without disclosing sensitive implementation details. We'll use a dummy endpoint for demonstration.
Why Validate UPI IDs?
- Avoid transaction failures due to incorrect UPI IDs
- Improve user experience by providing real-time feedback
- Ensure the UPI ID belongs to the correct customer
Technologies Used
- C# (ASP.NET WebForms/Backend)
- JavaScript (AJAX for frontend calls)
- JSON for data communication
- Dummy API endpoint (replace with actual provider)
Example API Request
POST https://dummyapi.example.com/v1/payments/validate/vpa
Content-Type: application/json
{
"vpa": "user123@dummyupi"
}
Successful Response
{
"vpa": "user123@dummyupi",
"customer_name": "John Doe",
"success": true
}
Invalid UPI Response
{
"error": {
"code": "BAD_REQUEST_ERROR",
"description": "Invalid VPA. Please enter a valid Virtual Payment Address",
"source": "customer",
"step": "payment_initiation",
"reason": "invalid_vpa"
}
}
Backend Implementation (C#)
Here’s a simplified C# function to validate a UPI ID via HTTP POST.
public static string ValidateUPIID(string upi)
{
string responseJson = "";
try
{
var requestObj = new { vpa = upi };
string requestJson = new JavaScriptSerializer().Serialize(requestObj);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://dummyapi.example.com/v1/payments/validate/vpa");
request.Method = "POST";
request.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(requestJson);
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseJson = reader.ReadToEnd();
}
if (responseJson.Contains("success"))
{
var jsonObj = new JavaScriptSerializer().Deserialize<UPIResponse>(responseJson);
return jsonObj.success ? $"success - {jsonObj.customer_name}" : "failed - ";
}
else if (responseJson.Contains("error"))
{
var errorObj = new JavaScriptSerializer().Deserialize<UPIErrorResponse>(responseJson);
string errDesc = !string.IsNullOrEmpty(errorObj.error.description) ? errorObj.error.description : "Invalid VPA";
string errReason = !string.IsNullOrEmpty(errorObj.error.reason) ? errorObj.error.reason : "invalid_vpa";
return $"reason - {errDesc} - {errReason}";
}
return "failed - unknown error";
}
catch (Exception ex)
{
return "error - " + ex.Message;
}
}
Join the conversation! Your thoughts help the community grow.