Conekta Api wrapper for .NET 6 using the native System.Text.Json
This package is for you if:
- You are implementing the Conekta payment Api on a backend running .NET 6
- You are interested in, or otherwise comfortable with, relying on the native JsonSerializer serializer for all your json serialization and deserialization needs (no more Newtonsoft.Json). JsonSerializer from the System.Text.Json package is the default serializer for .NET going forward.
- You are interested in, or otherwise comfortable with, relying on RestSharp v107+, which is a major upgrade from previous versions. This includes relying on the non-blocking async/await protocol for all your api calls.
- You are interested in, or otherwise comfortable with, factoring your code away from relying on exceptions toward a more functional approach, specifically, with the use of a Result class as the return response from your api calls. I have explicity adopted the helpful approach developed by Vladimir Khorikov, and there is a hard dependency here on his nuget package CSharpFunctionalExtensions. For more on this approach, checkout the following resources:
Available on NuGet
dotnet add package Conekta.Dotnet6
or
PM> Install-Package Conekta.Dotnet6
ConektaRestClient should be registered as a singleton, following RestSharp best practices
builder.Services.AddSingleton<IConektaRestClient>(new ConektaRestClient());
Configure the JsonSerializer to accept trailing commas just in case.
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.AllowTrailingCommas = true;
});
using ConektaDotnet6;
// private readonly IConektaRestClient _conektaRestClient; <==Dependency Injected
var conektaApi = new ConektaApi("en", "your_conekta_private_key", _conektaRestClient);
string conektaCustId = "cus_987afdsf9a87df89";
var customer = await conektaApi.GetCustomerAsync(conektaCustId);
if (customer.IsFailure)
{
// Error will be the ConektaException class
return Content(customer.Error.message);
}
// If the api call is successful, customer.Value will be the Customer class
return Json(customer.Value);
Full examples here
A number of value objects have been created to help manage a few of the peculiarities of the conekta api. If you are not familiar with value obects in .net, you can check out this link.
The important thing to remember is that you do not need to worry about the serialization of these value objects. Custom JsonConverters have been created for each value object to property handle the serialization and deserialization.
Perhaps the most important value object is ConektaAmount. This is a wrapper class to deal with the how the conekta api deals with money amounts, which is that all decimal or double numbers are multiplied by 100 and converted to integer. ConektaAmount will handle this for you.
decimal myMoneyAmount = 125.25;
var conektaAmount = ConektaAmount.Create(myMoneyAmount);
Console.WriteLine(conektaAmount.Value.ToString());
// 12525
// easily convert back to decimal
decimal backToDecimalAmount = conektaAmount.ToDecimal();
It is all too easy to forget this conversion and charge incorrect amounts because you forgot to convert the int to decimal or vice versa. ConektaAmount is your friend.
The conekta api handles datetime stamps with a 32bit int as a unix timestamp. ConektaDatetime wraps this integer. You will now discover that such datetime fields as created_at, updated_at, paid_at etc will be ConektaDatetime values.
ConektaDatetime conektaDtm = ConektaDatetime.Create(DatTime.Now);
DateTime backToDtm = conektaDtm.ToDateTime();
int backToUnixSeconds = conektaDtm.ToUnixTimestamp();
If you need to set a field like expires_at you can do something like the following:
ConektaDatetime expiresAt = ConektaDatetime.Now.AddDays(2);
This will save you time converting back and forth.
Every charge will indicate its payment status which are represented by text values. The PaymentStatus value object wraps these text values so you can benefit from intellisense.
if (charge.Status == PaymentStatus.Refunded) {
// do something
}