A typical BankID payment flow involves initiating the payment and then handling the callback to resolve the result.
1. Initiate Payment
Create a BankIdPaymentProperties object containing the transaction details (amount, currency, recipient, etc.). Use the BankIdInitiatePayment helper to redirect the user to the provider.
2. Resolve Result
In your callback endpoint, use IBankIdPaymentService.GetPaymentResultAsync(provider) to retrieve the outcome. Check result.Succeeded and access result.BankIdCompletionData to get user information (Personal Identity Number, Name) and device data (IP Address).
// Inside a Controller
[HttpPost("Payment")]
public IActionResult Payment([FromQuery] string provider, [FromForm] PaymentRequestModel model)
{
var props = new BankIdPaymentProperties(TransactionType.card, "Demo Merchant Name")
{
Money = new("100,00", "SEK"),
UserVisibleData = "Demo of Payment with Active Login.",
Items = {
{"scheme", provider},
{"transactionType", nameof(TransactionType.card)},
{"recipientName", "Demo Merchant Name"},
{"amount", "100,00"},
{"currency", "SEK"}
},
};
var returnPath = $"{Url.Action(nameof(Callback))}?provider={provider}";
return this.BankIdInitiatePayment(props, returnPath, provider);
}
[HttpPost]
public async Task<IActionResult> Callback(string provider)
{
var result = await _bankIdPaymentService.GetPaymentResultAsync(provider);
if (result?.Succeeded != true || result.BankIdCompletionData == null)
{
throw new Exception("Payment error");
}
// Process result.BankIdCompletionData...
}