Git Product home page Git Product logo

softmax.zarinpal's Introduction

Softmax.ZarinPal

Unofficial implementation of ZarinPal API for .NET 6

Nuget Version Nuget Downloads

Installing the NuGet Package

You can install this package by entering the following command into your Package Manager Console:

Install-Package Softmax.ZarinPal

Note: This package requires .NET 6.0.

Use in ASP.NET Core 6

Startup Configuration

Register ZarinPalService in your Program.cs class:

// using Softmax.ZarinPal;
// using Softmax.ZarinPal.Enums;

builder.Services.AddZarinPal(options =>
{
    // Required
    options.MerchantId = "your-merchant-id";
    
    // Optional (defualt: IRR)
    options.CurrencyType = CurrencyType.IRR;
    
    // Optional
    options.DefaultCallbackUri = new Uri("your-callback-uri");
});

Note: IRR is Iran Rial and IRT is Iran Toman.

Controller Configuration

Then, you need to add ZarinPalService to your controller:

// using Softmax.ZarinPal;

public class HomeController : Controller
{
    private readonly IZarinPalService _zarinpal;

    public HomeController(IZarinPalService zarinpal)
    {
        _zarinpal = zarinpal;
    }
}

Payment

You can request a new payment by the PaymentAsync method.

  • Amount is the transaction amount. (Required)
  • Description description of the transaction. (Required)
  • Callback Url the url to which the transaction should be redirected after completion. (Optional)
[Route("send")]
public async Task<IActionResult> Send()
{
      // Defualt CurrencyType is IRR (Rial) , you can change it in service options
      long amount = 1000; // Required   
      string description = "This is a test payment"; // Required
      
      string callbackUrl = new Uri("https://localhost:5001/verify"); // Optional 
      string email = "your-email"; // Optional 
      string mobile = "your-mobie"; // Optional 
  
      var result = await _zarinPal.PaymentAsync(new PaymentRequest
      {
          CallbackUrl = callbackUrl,
          Description = description,
          Amount = amount,
          Email = email,
          Mobile = mobile,
      });
  
      if (result.IsSuccess())
      {
          return Redirect(result.Data.PaymentUri.AbsoluteUri);
      }
  
      return Ok($"Failed, Error code: {result.Error.Code}");
}

Verify Payment

You can verify the payment transaction using the VerifyAsync method.

This action is called when the Callback Url we defined earlier is called (when the transaction is completed)

[Route("verify")]
public async Task<IActionResult> Verify()
{
    // 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 BadRequest();
    }
  
    long amount = 1000;
    var result = await _zarinPal.VerifyAsync(authority: authority, amount: amount);
  
    // Check if transaction was successful.
    if (result.IsSuccess())
    {
      return Ok($"Success, RefId: {result.Data.RefId}");
    }
  
    // Show unsuccessful transaction with code.
    return BadRequest($"Failed, Error code: {result.Error.Code}");
}

Another .NET Platforms

Payment

You can request a payment via PaymentAsync method.

  • Amount is the transaction amount. (Required)
  • Description description of the transaction. (Required)
  • Callback Url the url to which the transaction should be redirected after completion. (Optional)
using Softmax.ZarinPal;
using Softmax.ZarinPal.Enums;

namespace ZarinPal
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IZarinPalService zarinPal = 
              new ZarinPalService(merchantId: "your-merchant-id", defaultCallbackUri: new Uri("your-callback-uri"), currencyType: CurrencyType.IRR);
              
            // Defualt CurrencyType is IRR (Rial) , you can change it in service options
            long amount = 1000; // Required   
            string description = "This is a test payment"; // Required
        
            string callbackUrl = new Uri("https://localhost:5001/verify"); // Optional 
            string email = "your-email"; // Optional 
            string mobile = "your-mobie"; // Optional 
        
            var result = await zarinPal.PaymentAsync(new PaymentRequest
            {
                CallbackUrl = callbackUrl,
                Description = description,
                Amount = amount,
                Email = email,
                Mobile = mobile,
            });
        
            if (result.IsSuccess())
            {
                Console.WriteLine(result.Data.PaymentUri.AbsoluteUri);
            }
        
            Console.WriteLine($"Failed, Error code: {result.Error.Code}");
        }
    }
}

Verify Payment

You can verify the payment transaction using the VerifyAsync method.

using Softmax.ZarinPal;
using Softmax.ZarinPal.Enums;

namespace ZarinPal
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IZarinPalService zarinPal = 
              new ZarinPalService(merchantId: "your-merchant-id", defaultCallbackUri: new Uri("your-callback-uri"), currencyType: CurrencyType.IRR);
                  
            long amount = 1000;
            var result = await zarinPal.VerifyAsync(authority: "your-authority", amount: 1000);
          
            // Check if transaction was successful.
            if (result.IsSuccess())
            {
              Console.WriteLine($"Success, RefId: {result.Data.RefId}");
            }
          
            // Show unsuccessful transaction with code.
            Console.WriteLine($"Failed, Error code: {result.Error.Code}");
        }
    }
}

Contact with me

License

This project is licensed under the MIT License.

softmax.zarinpal's People

Contributors

mortezamir81 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.