BarionClient2 1.1.0

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

// Install BarionClient2 as a Cake Tool
#tool nuget:?package=BarionClient2&version=1.1.0

Barion .NET

Barion .Net v2 library originally forked from: Barion .Net what is abandoned. v2 means not just forked, but modernized, made easier to use, and removed old dependencies like NewtonSoft.Json.

The Barion .NET library makes it easy to add Barion payment to your .NET application. It is built upon Barion's Web API.

Build status

Supported operations

  • Immediate payment
  • Reservation payment
  • Refund
  • Finish Reservation

Prerequisites

  • .NET 6 and above

Release Notes

Release Notes

Installation

From package manager:

Install-Package BarionClient2

From dotnet CLI:

dotnet add package BarionClient2

Usage

The heart of the library is the BarionClient class which provides the ExecuteAsync method to execute various operations. Create the operation class you want to use: StartPaymentOperation, GetPaymentStateOperation, RefundOperation or FinishReservationOperation respectively. After setting the operation properties you can use the ExecuteAsync method and pass the opertaion as the parameter.

Note that BarionClient implements IDisposable, use it accordingly.

QuickStart guide

Example

var barionSettings = new BarionSettings
{
    BaseUrl = new Uri("https://api.test.barion.com/"),
    POSKey = Guid.Parse("d1bcff3989885d3a98235c1cd768eba2")
};

using var barionClient = new BarionClient(barionSettings);
var startPayment = new StartPaymentOperation();

// add payment parameters to startPayment

var result = await barionClient.ExecuteAsync<StartPaymentOperationResult>(startPayment);

if(result.IsOperationSuccessful)
{
    // redirect the user to the payment page
}
Registering as a service in ASP.NET Core

Add this section to appsettings.json

"Barion": {
    "BaseUrl": "https://api.test.barion.com/",
    "POSKey": "00000000000000000000000000000000",
    "Payee": "user@example.com",
    "Payer": "user@example.com",
    "PayerPassword": "P@ssW0rd"
}
builder.Services.Configure<BarionSettings>(builder.Configuration.GetSection("Barion"));
builder.Services.AddHttpClient<BarionClient>();

The lifetime of the service is controlled by the framework this way so you don't have to manually dispose the object (i.e. you don't have to use the using statement).

Sample website

You can find a complete sample website under the Samples directory. Check Minimal Web Api example for a detailed example on how to use the client.

Retry Policies

BarionClient comes with built-in retry policies. By default, if a Barion operation fails due to a transient error, it will retry the operation automatically.

You can choose the retry policy to use from the list below:

  • Exponential retry (default): the delay between each retry grows exponentially, e.g. ~3s -> ~7s -> ~15s -> ~31s. The exponential retry policy is well suited for background operations, which are not time sensitive.
  • Linear retry: the delay between each retry is fixed, e.g. 0.5s -> 0.5s -> 0.5s. The linear retry policy should be used in user interactive operations. If a user is waiting for the result, it's usually better to fail fast than letting the user wait for minutes.
  • No retry: will not retry the failed operations. This option should be used if a retry strategy is implemented on a higher level.
barionClient.RetryPolicy = new LinearRetry(TimeSpan.FromMilliseconds(500), 3);

Timeout

The default timeout for every operation is 120s. You can change the timeout by settings the Timeout property of the BarionClient:

barionClient.Timeout = TimeSpan.FromSeconds(15);

You can disable the timeout by using InfiniteTimeSpan:

barionClient.Timeout = System.Threading.Timeout.InfiniteTimeSpan;

Extending the API

You can easily add your own operations by creating a new subclass of BarionOperation. E.g. if you want to support the Reject payment operation you need to create a new class:

public class RejectOperation : BarionOperation
{
    public override HttpMethod Method => HttpMethod.Post;
    public override Uri RelativeUri => new Uri("/v2/Payment/Reject", UriKind.Relative);
    public override Type ResultType => typeof(RejectOperationResult);

    public string UserName { get; set; }
    public string Password { get; set; }
    public Guid PaymentId { get; set; }
}

And the class which represents the result of the operation:

public class RejectOperationResult : BarionOperationResult
{
    public Guid PaymentId { get; set; }
    public bool IsSuccess { get; set; }
}

After this you can use your own operation class the same way as the built in ones.

Contribute

You're welcome to contribute. To build the source code you'll need Visual Studio 2022.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 was computed.  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. 
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
1.1.0 457 12/21/2023
1.0.1 338 11/7/2022
1.0.0 327 11/3/2022