Skip to content

Creating payments

This article provides a quick guide to create payments.

Code sample

Once the user has selected their given payment option, you can begin the process of creating a payment.For this step, you need to depend on IPaymentCreationService (_paymentCreationService below) and your persistence layer (_orderStorageService below).

public class PaymentController : Controller
{
    ...

    [HttpPost]
    public async Task<IActionResult> Index(<YOUR_PARAMETERS>)
    {  
        // Prepare the request
        var paymentIdentifier = ...; 
        var shippingAddress = ...;
        var billingAddress = ...;
        var order = ...;
        var cultureCode = ...;

        // Create the order in your own persistence
        _orderStorageService.Create(paymentOrder);

        // Simple example here, normally you could be able to 
        // get more information from the http method, http context etc.
        var sampleContext = new SampleContext(1);

        var request = new PaymentCreationRequest(
            paymentIdentifier, 
            shippingAddress, 
            billingAddress, 
            order, 
            cultureCode);

        try
        {
            var response = await _paymentCreationService
                .HandlePaymentAsync(sampleContext, request);

            return response switch
            {
                PaymentCreationRedirectResponse redirectResponse 
                    => Redirect(redirectResponse.AbsoluteUrl),
                _ => BadRequest(),
            };
        }
        catch (PaymentException ex)
        {
            return BadRequest(ex.Message);
        }
    }
}

The AbsoluteUrl returned in the redirect response will redirect the user to the given payment provider's payment window.

Next step

Once the payment has been succesfully initiated, the user will be redirected to the given payment window, finish their payment, and again be redirected back to your webshop. In order to ensure payments are correctly authorized by a payment provider, you must setup a controller which accepts callbacks. This is covered in the next step here.