Here is a step-by-step guide on how to integrate the PayUMoney payment gateway in an ASP.NET MVC application:
Step 1: Set Up Your ASP.NET MVC Project
- Create a new ASP.NET MVC project in Visual Studio.
- Install the required NuGet packages, such as
Newtonsoft.Json
andSystem.Net.Http
.
Step 2: Register with PayUMoney
- Go to the PayUMoney website and create an account.
- Generate your merchant key and salt from the PayUMoney merchant panel.
Step 3: Create the PayUMoney Integration Class
Create a new class called PayUMoneyIntegration
to handle the payment integration logic.
csharp
public class demo_PayUMoneyIntegration
{
private readonly string _merchantKey;
private readonly string _salt;
private readonly string _baseUrl;
public PayUMoneyIntegration(string merchantKey, string salt, string baseUrl)
{
_merchantKey = merchantKey;
_salt = salt;
_baseUrl = baseUrl;
}
public async Task<string> InitiatePayment(PaymentDetails paymentDetails)
{
// Generate the hash value
string hash = GenerateHash(paymentDetails);
// Create the payment request
var paymentRequest = new Dictionary<string, string>
{
{ “key”, _merchantKey },
{ “txnid”, paymentDetails.TransactionId },
{ “amount”, paymentDetails.Amount.ToString() },
{ “productinfo”, paymentDetails.ProductInfo },
{ “firstname”, paymentDetails.FirstName },
{ “email”, paymentDetails.Email },
{ “phone”, paymentDetails.Phone },
{ “surl”, paymentDetails.SuccessUrl },
{ “furl”, paymentDetails.FailureUrl },
{ “hash”, hash }
};
// Send the payment request to PayUMoney
var response = await SendPaymentRequest(paymentRequest);
return response;
}
private string GenerateHash(PaymentDetails paymentDetails)
{
// Generate the hash value based on the payment details and salt
string hashString = $”{_merchantKey}|{paymentDetails.TransactionId}|{paymentDetails.Amount}|{paymentDetails.ProductInfo}|{paymentDetails.FirstName}|{paymentDetails.Email}|||||||||{_salt}”;
return HashString(hashString);
}
private async Task<string> SendPaymentRequest(Dictionary<string, string> paymentRequest)
{
// Send the payment request to PayUMoney using HttpClient
using (var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(_baseUrl, new FormUrlEncodedContent(paymentRequest));
return await response.Content.ReadAsStringAsync();
}
}
}
Step 4: Create the Payment Details Model
Create a model called PaymentDetails
to hold the payment-related information.
csharp
public class PaymentDetails
{
public string TransactionId { get; set; }
public decimal Amount { get; set; }
public string ProductInfo { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string SuccessUrl { get; set; }
public string FailureUrl { get; set; }
}
Step 5: Create the Payment Controller
Create a new controller called PaymentController
to handle the payment-related actions.
csharp
public class PaymentController : Controller
{
private readonly PayUMoneyIntegration _payUMoneyIntegration;
public PaymentController()
{
_payUMoneyIntegration = new PayUMoneyIntegration(
“YOUR_MERCHANT_KEY”,
“YOUR_SALT”,
“https://secure.payu.in/_payment”
);
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public async Task<ActionResult> InitiatePayment(PaymentDetails paymentDetails)
{
if (ModelState.IsValid)
{
// Initiate the payment with PayUMoney
var response = await _payUMoneyIntegration.InitiatePayment(paymentDetails);
// Redirect the user to the PayUMoney payment page
return new RedirectResult(response);
}
return View(“Index”, paymentDetails);
}
public ActionResult PaymentSuccess()
{
// Handle the payment success scenario
return View();
}
public ActionResult PaymentFailure()
{
// Handle the payment failure scenario
return View();
}
}
Step 6: Create the Views
- Create the
Index.cshtml
view for the payment form.
Create the PaymentSuccess.cshtml
view for the payment success scenario.
xml
@model YourNamespace.Models.PaymentDetails
@using (Html.BeginForm(“InitiatePayment”, “Payment”, FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class=”form-group”>
@Html.LabelFor(model => model.TransactionId)
@Html.EditorFor(model => model.TransactionId)
</div>
<!– Add more form fields for other payment details –>
<button type=”submit” class=”btn btn-primary”>Pay Now</button>
}
2. Create the PaymentSuccess.cshtml
view for the payment success scenario.
xml
<h2>Payment Successful</h2>
<p>Thank you for your payment!</p>
3. Create the PaymentFailure.cshtml
view for the payment failure scenario.
xml
<h2>Payment Successful</h2>
<p>Thank you for your payment!</p>
Step 7: Run the Application
- Start the ASP.NET MVC application.
- Navigate to the payment form at
/Payment
. - Fill in the payment details and click the “Pay Now” button.
- The user will be redirected to the PayUMoney payment page.
- After the payment is completed, the user will be redirected back to the success or failure page based on the payment outcome.
Conclusion:
This guide walks you through the fundamental steps of integrating PayUMoney into an ASP.NET MVC application. You may improve it by adding new functionality, error handling, and security measures according on your needs.