Listing payment options
This article provides a quick guide to show the list of valid payment options
available to the user.
Code sample
To show a list of valid payment options, you should depend on the
IPaymentOptionService
interface, as below.
| public record SampleContext(int SegmentationId) : PaymentContext;
public class CheckoutController : Controller
{
...
public async Task<IActionResult> Index(<YOUR_PARAMETERS>)
{
// Simple example here, normally you could be able
//to get more information from parameters passed
// to the controller method
var sampleContext = new SampleContext(1);
var paymentOptions = await _paymentOptionService
.ListValidPaymentOptionsAsync(sampleContext);
if (!paymentOptions.Any())
{
throw new InvalidOperationException("No payment options found");
}
var checkoutViewModel = new CheckoutViewModel
{
PaymentOptions = paymentOptions.ToList()
};
return View(checkoutViewModel);
}
}
|
Notice the SampleContext
which you get to define yourself. This contextual object is
important if you require special rules for deciding if a given payment option is
enabled or not.
Next step
With the options presented to the user, the next step is to initiate a payment with
the given payment identifier. You can read more about that here.