Dependency Injection extensions for BlinkDebitApiClient to enable seamless integration with ASP.NET Core and other .NET applications using Microsoft.Extensions.DependencyInjection
$ dotnet add package BlinkDebitApiClient.Extensions.DependencyInjectionDependency Injection extensions for BlinkDebitApiClient to enable seamless integration with ASP.NET Core and other .NET applications using Microsoft.Extensions.DependencyInjection.
dotnet add package BlinkDebitApiClient
dotnet add package BlinkDebitApiClient.Extensions.DependencyInjection
using BlinkDebitApiClient.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Add Blink Debit Client with inline configuration
builder.Services.AddBlinkDebitClient(options =>
{
options.DebitUrl = "https://sandbox.debit.blinkpay.co.nz";
options.ClientId = builder.Configuration["BlinkPay:ClientId"]!;
options.ClientSecret = builder.Configuration["BlinkPay:ClientSecret"]!;
options.TimeoutSeconds = 10; // Optional, default is 10
options.RetryEnabled = true; // Optional, default is true
});
var app = builder.Build();
appsettings.json:
{
"BlinkPay": {
"DebitUrl": "https://sandbox.debit.blinkpay.co.nz",
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret",
"TimeoutSeconds": 10,
"RetryEnabled": true
}
}
Program.cs:
using BlinkDebitApiClient.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Add Blink Debit Client from configuration
builder.Services.AddBlinkDebitClient(builder.Configuration);
var app = builder.Build();
Here's a complete example showing how to create a quick payment with the DI extension:
using BlinkDebitApiClient.Api.V1;
using BlinkDebitApiClient.Exceptions;
using BlinkDebitApiClient.Extensions.DependencyInjection;
using BlinkDebitApiClient.Model.V1;
var builder = WebApplication.CreateBuilder(args);
// Add services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Add Blink Debit Client from configuration
builder.Services.AddBlinkDebitClient(builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Quick payment endpoint
app.MapPost("/api/quick-payment", async (IBlinkDebitClient blinkClient, ILogger<Program> logger) =>
{
try
{
// Create a quick payment request
var gatewayFlow = new GatewayFlow("https://www.example.com/return");
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr("Payment"); // Particulars only (max 12 chars)
var amount = new Amount("1.25", Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
// Create the payment
var response = await blinkClient.CreateQuickPaymentAsync(request);
return Results.Ok(new
{
quickPaymentId = response.QuickPaymentId,
redirectUri = response.RedirectUri
});
}
catch (BlinkServiceException ex)
{
logger.LogError(ex, "Failed to create quick payment");
return Results.Problem(ex.Message, statusCode: 500);
}
})
.WithName("CreateQuickPayment")
.WithOpenApi();
app.Run();
using BlinkDebitApiClient.Api.V1;
using BlinkDebitApiClient.Exceptions;
using BlinkDebitApiClient.Model.V1;
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class PaymentsController : ControllerBase
{
private readonly IBlinkDebitClient _blinkClient;
private readonly ILogger<PaymentsController> _logger;
public PaymentsController(IBlinkDebitClient blinkClient, ILogger<PaymentsController> logger)
{
_blinkClient = blinkClient;
_logger = logger;
}
[HttpPost("quick-payment")]
public async Task<IActionResult> CreateQuickPayment()
{
try
{
// Create the payment request
var gatewayFlow = new GatewayFlow("https://www.example.com/return");
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr("Payment"); // Particulars only (max 12 chars)
var amount = new Amount("1.25", Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
// Create the payment
var payment = await _blinkClient.CreateQuickPaymentAsync(request);
return Ok(new
{
quickPaymentId = payment.QuickPaymentId,
redirectUri = payment.RedirectUri
});
}
catch (BlinkServiceException ex)
{
_logger.LogError(ex, "Failed to create quick payment");
return StatusCode(500, "Payment creation failed");
}
}
[HttpGet("{id}")]
public async Task<IActionResult> GetPayment(Guid id)
{
try
{
var payment = await _blinkClient.GetQuickPaymentAsync(id);
return Ok(new
{
quickPaymentId = payment.QuickPaymentId,
status = payment.Consent.Status.ToString()
});
}
catch (BlinkServiceException ex)
{
_logger.LogError(ex, "Failed to retrieve payment {PaymentId}", id);
return NotFound();
}
}
}
using BlinkDebitApiClient.Api.V1;
public class PaymentProcessorService : BackgroundService
{
private readonly IBlinkDebitClient _blinkClient;
private readonly ILogger<PaymentProcessorService> _logger;
public PaymentProcessorService(IBlinkDebitClient blinkClient, ILogger<PaymentProcessorService> logger)
{
_blinkClient = blinkClient;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
// Process payments...
var banks = await _blinkClient.GetMetaAsync();
_logger.LogInformation("Retrieved {Count} banks", banks.Count);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing payments");
}
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
}
}
}
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
DebitUrl | string | Yes | - | Base URL for the Blink Debit API (e.g., https://sandbox.debit.blinkpay.co.nz for sandbox or https://debit.blinkpay.co.nz for production) |
ClientId | string | Yes | - | OAuth2 client ID for authentication |
ClientSecret | string | Yes | - | OAuth2 client secret for authentication |
TimeoutSeconds | int | No | 10 | Timeout in seconds for API requests |
RetryEnabled | bool | No | true | Enable retry policy with exponential backoff |
Use different appsettings.{Environment}.json files:
appsettings.Development.json:
{
"BlinkPay": {
"DebitUrl": "https://sandbox.debit.blinkpay.co.nz",
"ClientId": "dev-client-id",
"ClientSecret": "dev-client-secret"
}
}
appsettings.Production.json:
{
"BlinkPay": {
"DebitUrl": "https://debit.blinkpay.co.nz",
"ClientId": "prod-client-id",
"ClientSecret": "prod-client-secret"
}
}
// Azure Key Vault
builder.Configuration.AddAzureKeyVault(
new Uri($"https://{builder.Configuration["KeyVaultName"]}.vault.azure.net/"),
new DefaultAzureCredential());
// Then access secrets
builder.Services.AddBlinkDebitClient(options =>
{
options.DebitUrl = builder.Configuration["BlinkPay-DebitUrl"]!;
options.ClientId = builder.Configuration["BlinkPay-ClientId"]!;
options.ClientSecret = builder.Configuration["BlinkPay-ClientSecret"]!;
});
dotnet user-secrets init
dotnet user-secrets set "BlinkPay:ClientId" "your-client-id"
dotnet user-secrets set "BlinkPay:ClientSecret" "your-client-secret"
MIT License - see LICENSE for details
For issues and questions, please visit: