Skip to content

WebApplication

MohammadReza edited this page Jan 7, 2022 · 1 revision

To use this library in a asp dotnet app you can install ZarinpalSharp.Asp from nuget.

This is simple extension package and you don't really need that. just take a look at AspExtensions.cs so you can see!

How to?

After installation you should add ZarinClient to IServicesCollection in Startup.cs file, like below:

  1. Add reference to ZarinpalSharp.Asp.
using ZarinSharp.Asp;
  1. Add ZarinClient with configurations.
// Add Zarinpal client with Configurations specified in appsettings.json
services.AddZarinClient(Configuration.GetZarinpalConfiguration());
  1. Add configurations to appsettings.json
  • (GetZarinpalConfiguration() Reads configs from appsettings.json)
"ZarinpalConfiguration": {
    "Token": "1344b5d4-0048-11e8-94db-005056a205be",
    "CallbackUrl": "http://yoursite.com/verify"
}
  1. Use client in your Controller or anywhere else using DI.
// Get ZarinClient from services collection via DI.
private readonly IZarinClient _zarinClient;

public PaymentController(IZarinClient zarinClient)
{
    _zarinClient = zarinClient;
}

#region Request Payment
[HttpGet]
public async Task<ZarinpalPaymentRequestRespond> GetAsync()
{
    // Request a payment.
    return await _zarinClient.PaymentRequestAsync(20000, "This is my payment!");
}
#endregion
Clone this wiki locally