- Introduction
- Contributing
- Minimum Requirements
- Dependency
- Quick Start
- Configuration
- Client Creation
- Request ID, Correlation ID and Idempotency Key
- Full Examples
- Individual API Call Examples
This SDK allows merchants with .NET 8-based e-commerce sites to seamlessly integrate with Blink PayNow and Blink AutoPay in order to accept digital payments.
This SDK is written in C# 12.
We welcome contributions from the community. Your pull request will be reviewed by our team.
This project is licensed under the MIT License.
- .NET 8 or higher
- If via your IDE, look for
BlinkDebitApiClientin the NuGet tool - If via .NET command line interface, run
dotnet add package BlinkDebitApiClient --version - If via
.csprojfile, add<PackageReference Include="BlinkDebitApiClient" Version="<LATEST_VERSION>"/>
var logger = LoggerFactory
.Create(builder => builder
.SetMinimumLevel(LogLevel.Information)
.AddConsole()
.AddDebug())
.CreateLogger<MyProgram>();
var blinkpayUrl = "https://sandbox.debit.blinkpay.co.nz";
var clientId = "";
var clientSecret = "";
var timeout = 10000;
var client = new BlinkDebitClient(logger, blinkpayUrl, clientId, clientSecret, timeout);
var gatewayFlow = new GatewayFlow("https://www.blinkpay.co.nz/sample-merchant-return-page");
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr("particulars", "code", "reference");
var amount = new Amount("0.01", Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
try {
var qpCreateResponse = await client.CreateQuickPaymentAsync(request);
logger.LogInformation("Redirect URL: {}", qpCreateResponse.RedirectUri); // Redirect the consumer to this URL
var qpId = qpCreateResponse.QuickPaymentId;
var qpResponse = await client.AwaitSuccessfulQuickPaymentAsync(qpId, 300); // Will throw an exception if the payment was not successful after 5min
} catch (BlinkServiceException e) {
logger.LogError("Encountered an error: " + e.Message);
}- Customise/supply the required properties in your
appsettings.jsonand/orProperties/launchSettings.json. This file should be available in your project folder. - The BlinkPay Sandbox debit URL is
https://sandbox.debit.blinkpay.co.nzand the production debit URL ishttps://debit.blinkpay.co.nz. - The client credentials will be provided to you by BlinkPay as part of your on-boarding process.
- Properties can be supplied using environment variables.
Warning Take care not to check in your client ID and secret to your source control.
Configuration will be detected and loaded according to the hierarchy -
- As provided directly to client constructor
- Environment variables e.g.
export BLINKPAY_CLIENT_SECRET=... Properties/launchSettings.jsonappsettings.json- Default values
The following values are recommended to be supplied using environment variables.
export BLINKPAY_DEBIT_URL=<BLINKPAY_DEBIT_URL>
export BLINKPAY_CLIENT_ID=<BLINKPAY_CLIENT_ID>
export BLINKPAY_CLIENT_SECRET=<BLINKPAY_CLIENT_SECRET>If you want to use your launchSettings file locally, substitute the correct values to your Properties/launchSettings.json file. Do not commit this file into your repository.
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"Demo": {
"commandName": "Project",
"environmentVariables": {
"BLINKPAY_DEBIT_URL": "<BLINKPAY_DEBIT_URL>",
"BLINKPAY_CLIENT_ID": "<BLINKPAY_CLIENT_ID>",
"BLINKPAY_CLIENT_SECRET": "<BLINKPAY_CLIENT_SECRET>",
"BLINKPAY_TIMEOUT": "10000",
"BLINKPAY_RETRY_ENABLED": "true"
}
}
}
}To use your appsettings file, you can pass environment variables from command line or CI/CD for the placeholders into your appsettings.json file.
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"BlinkPay": {
"DebitUrl": "{BLINKPAY_DEBIT_URL}",
"ClientId": "{BLINKPAY_CLIENT_ID}",
"ClientSecret": "{BLINKPAY_CLIENT_SECRET}",
"Timeout": 10000,
"RetryEnabled": true
}
}The recommended approach for ASP.NET Core applications is to use the BlinkDebitApiClient.Extensions.DependencyInjection NuGet package:
dotnet add package BlinkDebitApiClient.Extensions.DependencyInjectionConfigure via appsettings.json and register in Program.cs:
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);
// Register BlinkDebitClient from configuration
builder.Services.AddBlinkDebitClient(builder.Configuration);
var app = builder.Build();Alternatively, configure options directly in code:
using BlinkDebitApiClient.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
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 = 15;
options.RetryEnabled = true;
});
var app = builder.Build();Inject IBlinkDebitClient into your controllers or services:
public class PaymentController : ControllerBase
{
private readonly IBlinkDebitClient _blinkClient;
private readonly ILogger<PaymentController> _logger;
public PaymentController(IBlinkDebitClient blinkClient, ILogger<PaymentController> logger)
{
_blinkClient = blinkClient;
_logger = logger;
}
[HttpPost("quick-payment")]
public async Task<IActionResult> CreateQuickPayment([FromBody] QuickPaymentDto dto)
{
try
{
var gatewayFlow = new GatewayFlow(dto.RedirectUri);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(dto.Particulars, dto.Code, dto.Reference);
var amount = new Amount(dto.Amount, Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
var response = await _blinkClient.CreateQuickPaymentAsync(request);
return Ok(new { redirectUri = response.RedirectUri, quickPaymentId = response.QuickPaymentId });
}
catch (BlinkServiceException ex)
{
_logger.LogError(ex, "Failed to create quick payment");
return StatusCode(500, new { error = ex.Message });
}
}
}Benefits:
- âś… Automatic singleton lifetime management (follows HTTP client best practices)
- âś… Configuration validation on startup (fail fast)
- âś… Seamless integration with ASP.NET Core logging and configuration
- âś… Interface-based dependency injection (
IBlinkDebitClient) - âś… Supports both
appsettings.jsonand programmatic configuration
The client code can use .NET dependency injection manually:
// configure dependency injection
var serviceCollection = new ServiceCollection();
// configure path to appsettings.json
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..");
var config = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json")
.Build();
// configure BlinkPayProperties
serviceCollection.Configure<BlinkPayProperties>(config.GetSection("BlinkPay"));
serviceCollection.AddSingleton(resolver => resolver.GetRequiredService<IOptions<BlinkPayProperties>>().Value);
// configure logger
serviceCollection.AddLogging(builder =>
{
builder
.AddConsole() // use file logging
.AddDebug(); // use information
});
var serviceProvider = serviceCollection.BuildServiceProvider();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger("BlinkDebitClient");
serviceCollection.AddSingleton(logger);
// create BlinkDebitClient
serviceCollection.AddSingleton<BlinkDebitClient>();
serviceProvider = serviceCollection.BuildServiceProvider();
// retrieve BlinkDebitClient
var client = serviceProvider.GetService<BlinkDebitClient>();Another way is to supply the required values during object creation:
// configure logger
var logger = LoggerFactory
.Create(builder => builder
.AddConsole() // use file logging
.AddDebug()) // use information
.CreateLogger<MyProgram>();
// configure path to appsettings.json
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..");
var config = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json")
.Build();
// bind BlinkPay settings section to BlinkPayProperties
var blinkPayProperties = new BlinkPayProperties();
config.GetSection("BlinkPay").Bind(blinkPayProperties);
// create BlinkDebitClient
var client = new BlinkDebitClient(logger, blinkPayProperties);
// or
// var client = new BlinkDebitClient(logger, blinkPayProperties.DebitUrl, blinkPayProperties.ClientId, blinkPayProperties.ClientSecret);An optional request ID, correlation ID and idempotency key can be added as arguments to API calls. They will be generated for you automatically if they are not provided.
A request can have one request ID and one idempotency key but multiple correlation IDs in case of retries.
Note: For error handling, a BlinkServiceException can be caught.
A quick payment is a one-off payment that combines the API calls needed for both the consent and the payment.
var gatewayFlow = new GatewayFlow("https://www.blinkpay.co.nz/sample-merchant-return-page");
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr("particulars", "code", "reference");
var amount = new Amount("0.01", Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
var qpCreateResponse = await client.CreateQuickPaymentAsync(request);
_logger.LogInformation("Redirect URL: {}", qpCreateResponse.RedirectUri); // Redirect the consumer to this URL
var qpId = qpCreateResponse.QuickPaymentId;
var qpResponse = await client.AwaitSuccessfulQuickPaymentAsync(qpId, 300); // Will throw an exception if the payment was not successful after 5minvar redirectFlow = new RedirectFlow("https://www.blinkpay.co.nz/sample-merchant-return-page", Bank.BNZ);
var authFlowDetail = new AuthFlowDetail(redirectFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr("particulars");
var amount = new Amount("0.01", Amount.CurrencyEnum.NZD);
var request = new SingleConsentRequest(authFlow, pcr, amount);
var createConsentResponse = await client.CreateSingleConsentAsync(request);
var redirectUri = createConsentResponse.RedirectUri; // Redirect the consumer to this URL
var paymentRequest = new PaymentRequest
{
ConsentId = createConsentResponse.ConsentId
};
var paymentResponse = await client.CreatePaymentAsync(paymentRequest);
_logger.LogInformation("Payment Status: {}", (await client.GetPaymentAsync(paymentResponse.PaymentId)).Status);
// TODO inspect the payment result statusThe SDK provides helper methods to wait for consent authorization and payment completion. Understanding the auto-revoke behavior is critical for proper implementation.
| Method | Auto-Revokes on Timeout? | Reason |
|---|---|---|
AwaitSuccessfulQuickPaymentAsync |
âś… YES | Quick payments combine consent + payment - should complete immediately or be cancelled |
AwaitAuthorisedSingleConsentAsync |
❌ NO | Single consents require separate payment step - no funds processed if abandoned |
AwaitAuthorisedEnduringConsentAsync |
âś… YES | Enduring consents grant ongoing access - clean up if abandoned for security |
AwaitSuccessfulPaymentAsync |
❌ N/A | Payments cannot be revoked once initiated |
Best Practices:
- Manually revoke single or enduring consents if you determine the customer has permanently abandoned the authorization flow (before timeout expires)
- Enduring consents will auto-revoke on timeout, but earlier manual revocation improves security
Important: Payment settlement is asynchronous. Payments transition through these states:
Settlement Statuses:
Pending- Payment initiated, not yet settledAcceptedSettlementInProcess- Settlement in progressAcceptedSettlementCompleted- âś… ONLY THIS STATUS means money has been sent from the payer's bankRejected- Payment failed
Wash-up Implementation:
// Poll payment status until settlement completes
public async Task<Payment> WaitForSettlement(Guid paymentId, int maxAttempts = 60)
{
for (int i = 0; i < maxAttempts; i++)
{
var payment = await client.GetPaymentAsync(paymentId);
if (payment.Status == Payment.StatusEnum.AcceptedSettlementCompleted)
{
return payment; // SUCCESS - funds sent from payer's bank
}
if (payment.Status == Payment.StatusEnum.Rejected)
{
throw new Exception("Payment rejected");
}
await Task.Delay(5000); // Wait 5 seconds between checks
}
throw new Exception("Payment settlement timeout");
}Only AcceptedSettlementCompleted confirms funds have been sent from the payer's bank. In rare cases, payments may remain in AcceptedSettlementInProcess for extended periods.
Supplies the supported banks and supported flows on your account.
var bankMetadataList = await client.GetMetaAsync();var gatewayFlow = new GatewayFlow(redirectUri);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
var createQuickPaymentResponse = await client.CreateQuickPaymentAsync(request);var redirectFlowHint = new RedirectFlowHint(bank);
var flowHint = new GatewayFlowAllOfFlowHint(redirectFlowHint);
var gatewayFlow = new GatewayFlow(redirectUri, flowHint);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
var createQuickPaymentResponse = await client.CreateQuickPaymentAsync(request);var decoupledFlowHint = new DecoupledFlowHint(bank, identifierType, identifierValue);
var flowHint = new GatewayFlowAllOfFlowHint(decoupledFlowHint);
var gatewayFlow = new GatewayFlow(redirectUri, flowHint);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
var createQuickPaymentResponse = await client.CreateQuickPaymentAsync(request);var redirectFlow = new RedirectFlow(redirectUri, bank);
var authFlowDetail = new AuthFlowDetail(redirectFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
var createQuickPaymentResponse = await client.CreateQuickPaymentAsync(request);var decoupledFlow = new DecoupledFlow(bank, identifierType, identifierValue, callbackUrl);
var authFlowDetail = new AuthFlowDetail(decoupledFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new QuickPaymentRequest(authFlow, pcr, amount);
var createQuickPaymentResponse = await client.CreateQuickPaymentAsync(request);var quickPaymentResponse = await client.GetQuickPaymentAsync(quickPaymentId);await client.RevokeQuickPaymentAsync(quickPaymentId);var gatewayFlow = new GatewayFlow(redirectUri);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new SingleConsentRequest(authFlow, pcr, amount);
var createConsentResponse = await client.CreateSingleConsentAsync(request);var redirectFlowHint = new RedirectFlowHint(bank);
var flowHint = new GatewayFlowAllOfFlowHint(redirectFlowHint);
var gatewayFlow = new GatewayFlow(redirectUri, flowHint);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new SingleConsentRequest(authFlow, pcr, amount);
var createConsentResponse = await client.CreateSingleConsentAsync(request);var decoupledFlowHint = new DecoupledFlowHint(bank, identifierType, identifierValue);
var flowHint = new GatewayFlowAllOfFlowHint(decoupledFlowHint);
var gatewayFlow = new GatewayFlow(redirectUri, flowHint);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new SingleConsentRequest(authFlow, pcr, amount);
CreateConsentResponse createConsentResponse = await client.CreateSingleConsentAsync(request);Suitable for most consents.
var redirectFlow = new RedirectFlow(redirectUri, bank);
var authFlowDetail = new AuthFlowDetail(redirectFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new SingleConsentRequest(authFlow, pcr, amount);
var createConsentResponse = await client.CreateSingleConsentAsync(request);This flow type allows better support for mobile by allowing the supply of a mobile number or previous consent ID to identify the customer with their bank.
The customer will receive the consent request directly to their online banking app. This flow does not send the user through a web redirect flow.
var decoupledFlow = new DecoupledFlow(bank, identifierType, identifierValue, callbackUrl);
var authFlowDetail = new AuthFlowDetail(decoupledFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new SingleConsentRequest(authFlow, pcr, amount);
var createConsentResponse = await client.CreateSingleConsentAsync(request);Get the consent including its status
var consent = await client.GetSingleConsentAsync(consentId);await client.RevokeSingleConsentAsync(consentId);Request an ongoing authorisation from the customer to debit their account on a recurring basis.
Note that such an authorisation can be revoked by the customer in their mobile banking app.
var gatewayFlow = new GatewayFlow(redirectUri);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var maximumAmountPeriod = new Amount(total, Amount.CurrencyEnum.NZD);
var maximumAmountPayment = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new EnduringConsentRequest(authFlow, startDate, endDate, period, maximumAmountPeriod, maximumAmountPayment, hashedCustomerIdentifier);
var createConsentResponse = await client.CreateEnduringConsentAsync(request);var redirectFlowHint = new RedirectFlowHint(bank);
var flowHint = new GatewayFlowAllOfFlowHint(redirectFlowHint);
var gatewayFlow = new GatewayFlow(redirectUri, flowHint);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var maximumAmountPeriod = new Amount(total, Amount.CurrencyEnum.NZD);
var maximumAmountPayment = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new EnduringConsentRequest(authFlow, startDate, endDate, period, maximumAmountPeriod, maximumAmountPayment, hashedCustomerIdentifier);
var createConsentResponse = await client.CreateEnduringConsentAsync(request);var decoupledFlowHint = new DecoupledFlowHint(bank, identifierType, identifierValue);
var flowHint = new GatewayFlowAllOfFlowHint(decoupledFlowHint);
var gatewayFlow = new GatewayFlow(redirectUri, flowHint);
var authFlowDetail = new AuthFlowDetail(gatewayFlow);
var authFlow = new AuthFlow(authFlowDetail);
var pcr = new Pcr(particulars, code, reference);
var maximumAmountPeriod = new Amount(total, Amount.CurrencyEnum.NZD);
var maximumAmountPayment = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new EnduringConsentRequest(authFlow, startDate, endDate, period, maximumAmountPeriod, maximumAmountPayment, hashedCustomerIdentifier);
var createConsentResponse = await client.CreateEnduringConsentAsync(request);var redirectFlow = new RedirectFlow(redirectUri, bank);
var authFlowDetail = new AuthFlowDetail(redirectFlow);
var authFlow = new AuthFlow(authFlowDetail);
var maximumAmountPeriod = new Amount(total, Amount.CurrencyEnum.NZD);
var maximumAmountPayment = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new EnduringConsentRequest(authFlow, startDate, endDate, period, maximumAmountPeriod, maximumAmountPayment, hashedCustomerIdentifier);
var createConsentResponse = await client.CreateEnduringConsentAsync(request);var decoupledFlow = new DecoupledFlow(bank, identifierType, identifierValue, callbackUrl);
var authFlowDetail = new AuthFlowDetail(decoupledFlow);
var authFlow = new AuthFlow(authFlowDetail);
var maximumAmountPeriod = new Amount(total, Amount.CurrencyEnum.NZD);
var maximumAmountPayment = new Amount(total, Amount.CurrencyEnum.NZD);
var request = new EnduringConsentRequest(authFlow, startDate, endDate, period, maximumAmountPeriod, maximumAmountPayment, hashedCustomerIdentifier);
var createConsentResponse = await client.CreateEnduringConsentAsync(request);var consent = await client.GetEnduringConsentAsync(consentId);await client.RevokeEnduringConsentAsync(consentId);The completion of a payment requires a consent to be in the Authorised status.
var paymentRequest = new PaymentRequest
{
ConsentId = consentId
};
var paymentResponse = await client.CreatePaymentAsync(request);If you already have an approved consent, you can run a Payment against that consent at the frequency as authorised in the consent.
var pcr = new Pcr(particulars, code, reference);
var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var paymentRequest = new PaymentRequest(consentId, pcr, amount);
var paymentResponse = await client.CreatePaymentAsync(request);var payment = await client.GetPaymentAsync(paymentId);var request = new AccountNumberRefundRequest(paymentId);
var refundResponse = await client.CreateRefundAsync(request);var pcr = new Pcr(particulars, code, reference);
var request = new FullRefundRequest(paymentId, pcr, redirectUri);
var refundResponse = await client.CreateRefundAsync(request);var amount = new Amount(total, Amount.CurrencyEnum.NZD);
var pcr = new Pcr(particulars, code, reference);
var request = new PartialRefundRequest(paymentId, amount pcr, redirectUri);
var refundResponse = await client.CreateRefundAsync(request);var refund = await client.GetRefundAsync(refundId);