Zarinpal.AspNetCore
Zarinpal payment gateway for asp.net core
Install / Use
/learn @MehdiMst00/Zarinpal.AspNetCoreREADME
Zarinpal.AspNetCore
Zarinpal payment gateway for Asp.Net Core
Installation
PM> Install-Package Zarinpal.AspNetCore -Version 4.0.0
- Use
AddZarinpalto add needed services to service container.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddZarinpal(options =>
{
builder.Configuration.GetSection("Zarinpal").Bind(options);
});
// Or
builder.Services.AddZarinpal(options =>
{
options.MerchantId = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx";
options.ZarinpalMode = ZarinpalMode.Sandbox;
options.Currency = ZarinpalCurrency.IRT;
});
Note: If you bind options from appsettings.json See sample
- Inject
IZarinpalServiceto your controller
public class MyController : Controller
{
private readonly IZarinpalService _zarinpalService;
public MyController(IZarinpalService zarinpalService)
{
_zarinpalService = zarinpalService;
}
}
- Finish setup :)
Request Payment
int rial = 50000;
/*
* Pay atttention: Currency is important, default is IRR (Rial)
*
* Here we set it to Toman (IRT)
"Zarinpal": {
...
"Currency": "IRT", // IRR - IRT
...
}
*/
var request = new ZarinpalRequestDTO(toman, "خرید",
"https://localhost:7219/Home/VerifyPayment",
"test@test.com", "09123456789");
var result = await _zarinpalService.RequestAsync(request);
if (result.Data != null)
{
// You can store or log zarinpal data in database
string authority = result.Data.Authority;
int code = result.Data.Code;
int fee = result.Data.Fee;
string feeType = result.Data.FeeType;
string message = result.Data.Message;
}
if (result.IsSuccessStatusCode)
return Redirect(result.RedirectUrl);
Verify Payment
[HttpGet]
public async Task<IActionResult> VerifyPayment()
{
// Check 'Status' and 'Authority' query param so zarinpal sent for us
if (HttpContext.IsValidZarinpalVerifyQueries())
{
int rial = 50000;
/*
* Pay atttention: Currency is important, default is IRR (Rial)
*
* Here we set it to toman (IRT)
"Zarinpal": {
...
"Currency": "IRT", // IRR - IRT
...
}
*/
var verify = new ZarinpalVerifyDTO(toman,
HttpContext.GetZarinpalAuthorityQuery()!);
var response = await _zarinpalService.VerifyAsync(verify);
if (response.Data != null)
{
// You can store or log zarinpal data in database
ulong refId = response.Data.RefId;
int fee = response.Data.Fee;
string feeType = response.Data.FeeType;
int code = response.Data.Code;
string cardHash = response.Data.CardHash;
string cardPan = response.Data.CardPan;
}
if (response.IsSuccessStatusCode)
{
// Do Somethings...
var refId = response.RefId;
var statusCode = response.StatusCode;
}
return View(response.IsSuccessStatusCode);
}
return View(false);
}
What is IAdvancedZarinpalService?
- You can use
IAdvancedZarinpalServiceIf you wanna see 'UnVerified' method:
private readonly IAdvancedZarinpalService _advancedZarinpalService;
public MyController(IAdvancedZarinpalService advancedZarinpalService)
{
_advancedZarinpalService = advancedZarinpalService;
}
public async Task<IActionResult> UnVerifiedPayments()
{
var result = await _advancedZarinpalService.UnVerifiedAsync();
return Ok(result);
}
Extensions maybe you need in your project
// For verify payment
bool isValidZarinpalVerifyQueries = HttpContext.IsValidZarinpalVerifyQueries();
// Get authority from query params
string authority = HttpContext.GetZarinpalAuthorityQuery();
// Get status message
var message = ZarinpalStatusCode.St100.GetStatusCodeMessage();
Support
For support, click here.
Docs
V4 Zarinpal Docs: click here.
Give a star ⭐️ !!!
If you liked the project, please give a star :)
License
Related Skills
node-connect
348.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
348.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
348.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
