Zarinpal.AspNetCore 4.0.0

dotnet add package Zarinpal.AspNetCore --version 4.0.0
                    
NuGet\Install-Package Zarinpal.AspNetCore -Version 4.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Zarinpal.AspNetCore" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zarinpal.AspNetCore" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Zarinpal.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Zarinpal.AspNetCore --version 4.0.0
                    
#r "nuget: Zarinpal.AspNetCore, 4.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Zarinpal.AspNetCore@4.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Zarinpal.AspNetCore&version=4.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Zarinpal.AspNetCore&version=4.0.0
                    
Install as a Cake Tool

Zarinpal.AspNetCore

Zarinpal payment gateway for Asp.Net Core

Installation

  1. Download and install package from NuGet or GitHub
PM> Install-Package Zarinpal.AspNetCore -Version 4.0.0
  1. Use AddZarinpal to 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

  1. Inject IZarinpalService to your controller
public class MyController : Controller
{
    private readonly IZarinpalService _zarinpalService;

    public MyController(IZarinpalService zarinpalService)
    {
         _zarinpalService = zarinpalService;
    }
}
  1. 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 IAdvancedZarinpalService If 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

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 is compatible.  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.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.0 1,360 12/18/2025