A light weight library (with respect to zarinpal-dotNet) to help online payment using Zarinpal for .NET core 2.2
web application.
You can download the latest version of Msh.Zarinpal.Api
from Github repository.
To install via nuget
:
Install-Package Msh.Zarinpal.Api -Version 1.0.3
Install from Nuget directly.
Add these codes to startup.cs
file:
using Zarinpal.Api;
using Zarinpal.Api.Models;
...
public void ConfigureServices(IServiceCollection services)
{
services.Configure<ZarinpalConfiguration>(opt => Configuration.GetSection("ZarinpalConfig").Bind(opt));
...
services.AddZarinpal();
}
...
and some where in appsettings.json
"ZarinpalConfig": {
"Token": "Put your api token here",
"UseSandbox": false,
"UseZarinLink": true
}
in controller
:
using Zarinpal.Api;
using Zarinpal.Api.Models;
...
public class HomeController : Controller
{
private readonly IZarinpalProvider _zarinpal;
public HomeController(IZarinpalProvider zarinpal)
{
_zarinpal = zarinpal;
}
public IActionResult Index(string referenceId = null)
{
if (string.IsNullOrWhiteSpace(referenceId))
return View();
return Content(referenceId);
}
public async Task<IActionResult> StartPayment()
{
var model = new ZarinpalPaymentRequestModel
{
Amount = 2000,
Description = "Some description goes here",
CallbackUrl = "https://<domain>.com/PaymentResult",
Email = "Client email",
Mobile = "Client mobile"
};
var t = await _zarinpal.PayAsync(model);
if (t.Succeeded)
return Redirect(t.Result.PaymentUrl);
return BadRequest(t.Errors);
}
public async Task<IActionResult> PaymentResult(string authority, string status)
{
var t = long.Parse(authority);
var result = await _zarinpal.VerifyAsync(new ZarinpalPaymentVerificationModel(2000, authority));
if (result.Succeeded)
return RedirectToAction("Index", new { referenceId = result.Result.ReferenceId });
return BadRequest(result.Errors);
}
}
...