2
To integrate the Bank of India payment gateway for online payments in your ASP.NET application using ADO.NET, you will typically follow these steps:
-
Obtain API Details from Bank of India: Contact Bank of India to get the necessary API credentials (Merchant ID, API key, etc.) and documentation.
-
Set Up Your Project:
- Ensure you have the necessary references in your ASP.NET project.
- Create necessary web forms or Razor pages for initiating and receiving payment responses.
-
Create a Payment Request:
- Use ADO.NET to store transaction details in your database before redirecting to the payment gateway.
-
Redirect to the Payment Gateway:
- Build an HTML form with the required parameters and post it to the Bank of India gateway URL.
-
Handle the Payment Response:
- Create an endpoint to handle the response from the payment gateway.
- Update your database with the transaction status using ADO.NET.
Sample Code
Here's a simplified example of how you might implement this:
Payment Initialization (Sending Request to Gateway):
protected void btnPay_Click(object sender, EventArgs e)
{
string merchantId = "YourMerchantId";
string apiKey = "YourApiKey";
string returnUrl = "https://yourwebsite.com/PaymentResponse";
string amount = "100.00"; // Example amount
// Save transaction details in the database using ADO.NET
using (SqlConnection con = new SqlConnection("your_connection_string"))
{
string query = "INSERT INTO Transactions (Amount, Status) OUTPUT INSERTED.ID VALUES (@Amount, @Status)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Amount", amount);
cmd.Parameters.AddWithValue("@Status", "Pending");
con.Open();
int transactionId = (int)cmd.ExecuteScalar();
con.Close();
// Prepare payment gateway parameters
string transactionIdString = transactionId.ToString();
string hash = GenerateHash(merchantId, apiKey, amount, transactionIdString);
// Redirect to payment gateway
Response.Redirect($"https://bankofindia.com/pay?merchantId={merchantId}&transactionId={transactionIdString}&amount={amount}&returnUrl={returnUrl}&hash={hash}");
}
}
}
private string GenerateHash(string merchantId, string apiKey, string amount, string transactionId)
{
// Implement hash generation logic based on Bank of India's documentation
return "generated_hash";
}
Handling the Payment Response:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string transactionId = Request.Form["transactionId"];
string status = Request.Form["status"];
string hash = Request.Form["hash"];
// Verify the hash
if (VerifyHash(transactionId, status, hash))
{
// Update transaction status in the database using ADO.NET
using (SqlConnection con = new SqlConnection("your_connection_string"))
{
string query = "UPDATE Transactions SET Status = @Status WHERE ID = @TransactionId";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Status", status);
cmd.Parameters.AddWithValue("@TransactionId", transactionId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
// Display success or failure message to the user
if (status == "Success")
{
lblMessage.Text = "Payment successful!";
}
else
{
lblMessage.Text = "Payment failed!";
}
}
else
{
lblMessage.Text = "Invalid response!";
}
}
}
private bool VerifyHash(string transactionId, string status, string hash)
{
// Implement hash verification logic based on Bank of India's documentation
return true;
}
Notes:
- Replace
"your_connection_string"
with your actual database connection string.
- Implement the
GenerateHash
and VerifyHash
methods based on the Bank of India’s API documentation.
- Handle exceptions and edge cases appropriately.
- Ensure to test the integration thoroughly in a sandbox environment before going live.
Make sure to follow the security guidelines and best practices recommended by Bank of India for handling payment data.
