plugin.web.eSewa.and.Khalti
1.0.6
See the version list below for details.
dotnet add package plugin.web.eSewa.and.Khalti --version 1.0.6
NuGet\Install-Package plugin.web.eSewa.and.Khalti -Version 1.0.6
<PackageReference Include="plugin.web.eSewa.and.Khalti" Version="1.0.6" />
paket add plugin.web.eSewa.and.Khalti --version 1.0.6
#r "nuget: plugin.web.eSewa.and.Khalti, 1.0.6"
// Install plugin.web.eSewa.and.Khalti as a Cake Addin #addin nuget:?package=plugin.web.eSewa.and.Khalti&version=1.0.6 // Install plugin.web.eSewa.and.Khalti as a Cake Tool #tool nuget:?package=plugin.web.eSewa.and.Khalti&version=1.0.6
eSewa and Khalti Payment Gateway Integration for .NET Core Web Applications
This document provides detailed steps to integrate eSewa and Khalti, the leading payment gateways in Nepal, into a .NET Core web application.
Introduction
This package allows you to integrate web payment functionality for eSewa and Khalti. It supports:
- Initiating payments
- Verifying payments
- Sandbox environment for testing
Installation
Install the NuGet package:
> dotnet add package plugin.web.eSewa.and.Khalti
Sandbox Secret Keys
Use the following sandbox keys for testing and development:
eSewa
- Secret Key:
8gBm/:&EnhH.1/q
Khalti
- Secret Key:
live_secret_key_68791341fdd94846a146f0457ff7b455
Note
Set the sandBoxMode
flag to true
for development and testing.
private readonly string Khalti_SecretKey = "live_secret_key_68791341fdd94846a146f0457ff7b455";
private readonly string eSewa_SecretKey = "8gBm/:&EnhH.1/q";
private readonly bool sandBoxMode = true;
Use your own data for live environment.
eSewa Payment Integration
1. Payment Initialization
Use the following code to initialize an eSewa payment:
public async Task<IActionResult> PayWitheSewa()
{
PaymentManager paymentManager = new PaymentManager(
PaymentMethod.eSewa,
PaymentVersion.v2,
PaymentMode.Sandbox,
eSewa_SecretKey
);
string currentUrl = new Uri($"{Request.Scheme}://{Request.Host}").AbsoluteUri;
dynamic request = new
{
Amount = 100,
TaxAmount = 10,
TotalAmount = 110,
TransactionUuid = "bk-" + new Random().Next(10000, 100000).ToString(),
ProductCode = "EPAYTEST",
ProductServiceCharge = 0,
ProductDeliveryCharge = 0,
SuccessUrl = currentUrl,
FailureUrl = currentUrl,
SignedFieldNames = "total_amount,transaction_uuid,product_code",
};
var response = await paymentManager.ProcessPayment<ApiResponse>(request);
return Redirect(response.data);
}
2. Payment Verification
Verify the payment using this method:
public async Task<IActionResult> VerifyEsewaPayment(string data)
{
PaymentManager paymentManager = new PaymentManager(
PaymentMethod.eSewa,
PaymentVersion.v2,
PaymentMode.Sandbox,
string.Empty
);
eSewaResponse response = await paymentManager.VerifyPayment<eSewaResponse>(data);
if (!string.IsNullOrEmpty(nameof(response)) &&
string.Equals(response.status, "complete", StringComparison.OrdinalIgnoreCase))
{
ViewBag.Message = string.Format(
$"Payment with eSewa completed successfully with data: {response.transaction_code} and amount: {response.total_amount}"
);
}
else
{
ViewBag.Message = string.Format("Payment with eSewa failed");
}
return View();
}
Khalti Payment Integration
1. Payment Initialization
Initialize Khalti payment using the code below:
public async Task<ActionResult> PayWithKhalti()
{
string currentUrl = new Uri($"{Request.Scheme}://{Request.Host}").AbsoluteUri;
PaymentManager paymentManager = new PaymentManager(
PaymentMethod.Khalti,
PaymentVersion.v2,
PaymentMode.Sandbox,
Khalti_SecretKey
);
dynamic request = new
{
return_url = currentUrl,
website_url = currentUrl,
amount = 1300,
purchase_order_id = "test12",
purchase_order_name = "test",
customer_info = new KhaltiCustomerInfo()
{
name = "Sushil Shreshta",
email = "shoesheill@gmail.com",
phone = "9846000027"
},
product_details = new List<KhaltiProductDetail>
{
new KhaltiProductDetail()
{
identity = "1234567890",
name = "Khalti logo",
total_price = 1300,
quantity = 1,
unit_price = 1300
}
},
amount_breakdown = new List<KhaltiAmountBreakdown>
{
new KhaltiAmountBreakdown() { label = "Mark Price", amount = 1000 },
new KhaltiAmountBreakdown() { label = "VAT", amount = 300 }
}
};
ApiResponse response = await paymentManager.ProcessPayment<ApiResponse>(request);
KhaltiInitResponse k_Init_Response = JsonConvert.DeserializeObject<KhaltiInitResponse>(JsonConvert.SerializeObject(response.data));
return Redirect(k_Init_Response.payment_url);
}
2. Payment Verification
Verify the Khalti payment using the following method:
private async Task<ActionResult> VerifyPayment(string pidx)
{
PaymentManager paymentManager = new PaymentManager(
PaymentMethod.Khalti,
PaymentVersion.v2,
PaymentMode.Sandbox,
Khalti_SecretKey
);
KhaltiResponse response = await paymentManager.VerifyPayment<KhaltiResponse>(pidx);
if (response != null && string.Equals(response.status, "completed", StringComparison.OrdinalIgnoreCase))
{
ViewBag.Message = string.Format(
$"Payment with Khalti completed successfully with pidx: {response.pidx} and amount: {response.total_amount}"
);
}
else
{
ViewBag.Message = "Payment with Khalti failed";
}
return View();
}
Test Login Credentials
eSewa
- Username:
9806800001/2/3/4/5
- Password:
Nepal@123
- Token:
123456
Khalti
- Mobile Number:
9800000001/2/3/4/5
- Pin:
1111
- OTP:
987654
References
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release.