Riviera.ZarinPal 3.0.0-preview.1

This is a prerelease version of Riviera.ZarinPal.
dotnet add package Riviera.ZarinPal --version 3.0.0-preview.1                
NuGet\Install-Package Riviera.ZarinPal -Version 3.0.0-preview.1                
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="Riviera.ZarinPal" Version="3.0.0-preview.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Riviera.ZarinPal --version 3.0.0-preview.1                
#r "nuget: Riviera.ZarinPal, 3.0.0-preview.1"                
#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.
// Install Riviera.ZarinPal as a Cake Addin
#addin nuget:?package=Riviera.ZarinPal&version=3.0.0-preview.1&prerelease

// Install Riviera.ZarinPal as a Cake Tool
#tool nuget:?package=Riviera.ZarinPal&version=3.0.0-preview.1&prerelease                

Riviera.ZarinPal

Unofficial implementation of the ZarinPal API for .NET

NuGet Version NuGet Downloads

Installation

You can install this package via the Package Manager Console in Visual Studio.

Install-Package Riviera.ZarinPal -PreRelease

Choosing the version

Currently there are two ZarinPal REST API versions to choose from:

Version 4

To use the ZarinPalService (v4) you need to register it via the IServiceCollection.

builder.Services.Configure<Riviera.ZarinPal.V4.ZarinPalOptions>(options => builder.Configuration.GetSection("ZarinPal").Bind(options));
builder.Services.AddHttpClient<Riviera.ZarinPal.V4.ZarinPalService>();

Then, add the following options to your appsettings.Development.json file.

"ZarinPal": {
  "MerchantId": "your-merchant-id",
  "IsDevelopment": true
}

After registering the service, you need to add it to your controller.

public class HomeController : Controller
{
    private readonly Riviera.ZarinPal.V4.ZarinPalService _zarinpal;

    public HomeController(Riviera.ZarinPal.V4.ZarinPalService zarinpal)
    {
        _zarinpal = zarinpal;
    }
}

Requesting a payment

You can request a payment via the RequestPaymentAsync method.

  • Amount is the transaction amount.
  • Description is a short description about the transaction.
  • CallbackUri is the url to redirect to after the transaction.
// using Riviera.ZarinPal.V4.Models;

[Route("send")]
public async Task<IActionResult> Send()
{
    var payment = new NewPayment
    {
        Amount = 1000,
        Description = "This is a test payment",
        CallbackUri = new Uri("https://localhost:5001/get"),
        Currency = "IRR",
    };

    var result = await _zarinpal.RequestPaymentAsync(payment);

    if (result?.Data?.PaymentUri != null && result.Data.Code == 100)
    {
        return Redirect(result.Data.PaymentUri.AbsoluteUri);
    }

    return Content($"Failed, Error {result.Error.Code}: {result.Error.Message}");
}

Verifying the transaction

You can verify the transaction via the VerifyPaymentAsync method.

This action was our CallbackUri in the RequestPaymentAsync method.

After the transaction is completed, the payment provider will redirect to this action.

// using Riviera.ZarinPal.V4.Models;

[Route("get")]
public async Task<IActionResult> Get()
{
    // Get Status and Authority and show error if not available.
    if (!Request.Query.TryGetValue("Status", out var status) ||
        !Request.Query.TryGetValue("Authority", out var authority))
    {
        return Content("Bad request.");
    }

    // Check if query string status is valid.
    if (_zarinpal.IsStatusValid(status) == false)
    {
        return Content("Failed.");
    }

    long amount = 1000;
    var result = await _zarinpal.VerifyPaymentAsync(amount, authority);

    // Check if transaction was successful.
    if (result?.Data != null && (result.Data.Code == 100 || result.Data.Code == 101))
    {
        return Content($"Success, RefId: {result.Data.RefId}");
    }

    // Show unsuccessful transaction with code.
    return Content($"Failed, Error {result.Data.Code}: {result.Error.Message}");
}

Pending Payments

You can get the transactions that has not been verified via the GetPendingPaymentsAsync method.

var result = await _zarinpal.GetPendingPaymentsAsync();

Refund

You can refund a transaction via the RefundAsync method.

  • Authority is the transaction unique id.
  • AccessToken is a personal access token that can be generated in your user account page.
var result = await _zarinpal.RefundAsync("authority", "accessToken");

Version 1

To use ZarinPalService (v1) you need to register it via the IServiceCollection.

builder.Services.Configure<Riviera.ZarinPal.V1.ZarinPalOptions>(options => builder.Configuration.GetSection("ZarinPal").Bind(options));
builder.Services.AddHttpClient<Riviera.ZarinPal.V1.ZarinPalService>();

Then, add the following options to your appsettings.Development.json file.

"ZarinPal": {
  "MerchantId": "your-merchant-id",
  "IsDevelopment": true
}

After registering the service, you need to add it to your controller.

public class HomeController : Controller
{
    private readonly Riviera.ZarinPal.V1.ZarinPalService _zarinpal;

    public HomeController(Riviera.ZarinPal.V1.ZarinPalService zarinpal)
    {
        _zarinpal = zarinpal;
    }
}

Requesting a payment

You can request a payment via the RequestPaymentAsync method.

  • Amount is the transaction amount.
  • Description is a short description about the transaction.
  • Callback Url is the url to redirect to after the transaction.
[Route("send")]
public async Task<IActionResult> Send()
{
    long amount = 1000;
    string description = "This is a test payment";
    string callbackUrl = "https://localhost:5001/get";

    var request = await _zarinpal.RequestPaymentAsync(amount, description, new Uri(callbackUrl));

    if (request.IsSuccess())
    {
        return Redirect(request.PaymentUri.AbsoluteUri);
    }

    return Content($"Failed, Error code: {request.Status}");
}

Verifying a transaction

You can verify the transaction via the VerifyPaymentAsync method.

This action was our Callback Url in the RequestPayment method.

After the transaction is completed, the payment provider will redirect to this action.

[Route("get")]
public async Task<IActionResult> Get()
{
    // Get Status and Authority and show error if not available.
    if (!Request.Query.TryGetValue("Status", out var status) ||
        !Request.Query.TryGetValue("Authority", out var authority))
    {
        return Content("Bad request.");
    }

    // Check if query string status is valid.
    if (_zarinpal.IsStatusValid(status) == false)
    {
        return Content("Failed.");
    }

    long amount = 1000;
    var response = await _zarinpal.VerifyPaymentAsync(amount, authority);

    // Check if transaction was successful.
    if (response.IsSuccess())
    {
        return Content($"Success, RefId: {response.RefId}");
    }

    // Show unsuccessful transaction with code.
    return Content($"Failed, Error code: {response.Status}");
}

License

This project is licensed under the MIT License.

Product 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 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 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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
3.0.0-preview.1 164 3/7/2025
2.0.0-preview.1 153 2/5/2023
1.0.0-preview.9 4,890 11/24/2021
1.0.0-preview.8 248 7/31/2021
1.0.0-preview.7 194 3/9/2021
1.0.0-preview.6 289 11/12/2020
1.0.0-preview.5 235 11/11/2020
1.0.0-preview.4 230 10/19/2020
1.0.0-preview.3 279 10/7/2020
1.0.0-preview.2 261 8/27/2020
1.0.0-preview.1 298 5/20/2020