This library intend to add a simple interface to your API, if a client of a Software-as-a-Service (SaaS) has their payments in order.
A middleware component is added to the middleware pipeline, and is injected with a instance of an object that implements IPaymentRequiredValidator with your "business rules" validation.
The response of the API will be either the result of the service if everything is OK, or a ProblemDetails with the description of the exception.
public sealed class NoPaymentIsRequiredValidator : IPaymentRequiredValidator
{
private readonly PaymentRequiredResponse _paymentRequiredResponse;
public NoPaymentIsRequiredValidator()
{
this._paymentRequiredResponse = new NoPaymentIsRequiredResponseNullObject();
}
public bool IsPaymentRequired()
{
return _paymentRequiredResponse.IsPaymentRequired;
}
public PaymentRequiredResponse ValidatePaymentRequired(HttpContext context)
{
// implement your own business logic
// ...
return _paymentRequiredResponse;
}
}We need to inject IPaymentRequiredValidator, one of the PaymentRequiredMiddleware dependencies.
// ... previous application configurations and dependencies
builder.Services.AddScoped<IPaymentRequiredValidator, NoPaymentIsRequiredValidator>();
// ... even more application configurations.Add PaymentRequiredMiddleware into the pipeline, between UseAuthentication and UseAuthorization. You need to identify your "client" in order to verify if payment is due or not.
app.UseAuthentication();
app.UseMiddleware<PaymentRequiredMiddleware>();
app.UseAuthorization();- @masterzdran
Payment method icons created by Freepik - Flaticon