INDIVIRTUAL - TECHNISCH PARTNER IN DIGITALE DIENSTVERLENING

Buckaroo .NET implementation

January 12, 2020

Buckaroo .NET implementation

Buckaroo offers a simple solution for implementing iDeal payments on your website. In order to make use of the Buckaroo implementation, we need to write some code and apply configurations inside the portal (https://plaza.buckaroo.nl/)

The application flow

Before we start programming, let’s get a clear picture of the payment process.
For example, the user has filled in some forms and wants to pay. When the user submits the form, we will redirect the user to the iDeal payment page of Buckaroo. Two events will happen after this step:

  • The user will be redirected back to our website.
  • Buckaroo will notify our WebApp whether the payment has been successful.

Buckaroo(2)

In order to correctly redirect the user to Buckaroo, we need to use the Buckaroo SDK to generate a URL. Because we want the user to be redirected back to the correct page on our website, we need to provide Buckaroo with the correct return URLs.

After the user is redirected from Buckaroo back to our website, Buckaroo calls an endpoint on our website to persist the data of the transaction. Buckaroo will notify us if the payment is successful or not. Another useful feature is that the Buckaroo SDK enables us to provide custom parameters before redirecting the user to Buckaroo, that will be sent back to our endpoint. This is necessary for determining who has completed that particular transaction.

Setting up the portal

Luckily there is not a lot of configuration involved. We need the website key and we need to create a secret key. These two keys are needed in the next section when we implement the code.

Buckaroo website

Buckaroo secret

We can send custom parameters towards Buckaroo, so we can match the payment with the user. In order for Buckaroo to receive these custom parameters, we need to register these inside the portal.

Buckaroo Custom velden

Implementing the code

First, let’s download the BuckarooSDK package via NuGet (https://www.nuget.org/packages/BuckarooSDK/)

The following code will call the Buckaroo API with all the necessary parameters. We provide it with the two keys from the previous step, indicate if it’s a live or test payment and our custom parameters. We also send the URLs where the user will be redirected to after the payment and the URLs for our endpoints, whether the payment was successful or not.

After creating the request, the Buckaroo API will provide us with a unique URL. This way there is no data visible in the URL and the user cannot tamper with the payment process.

using System;
using System.Configuration;
using System.Globalization;
using System.Threading.Tasks;
using System.Web.Mvc;
using BuckarooSdk;
using BuckarooSdk.DataTypes;
using BuckarooSdk.DataTypes.RequestBases;
using BuckarooSdk.DataTypes.Response;
using BuckarooSdk.Logging;
using BuckarooSdk.Services.Ideal.TransactionRequest;
using HttpPostAttribute = System.Web.Mvc.HttpPostAttribute;

public class PaymentController : Controller
{

    [HttpPost]
    public ActionResult Ideal()
    {
        var websiteKey = ConfigurationManager.AppSettings["BuckarooWebsiteKey"];
        var secretKey = ConfigurationManager.AppSettings["BuckarooSecretKey"];
        var isLivePayment = "true".Equals(ConfigurationManager.AppSettings["BuckarooLivePayment"], StringComparison.OrdinalIgnoreCase);
        var ipAddress = Request.UserHostAddress;
    
        var logger = new StandardLogger();
        var client = new SdkClient(() => logger);
        var transactionBase = new TransactionBase
        {
            Currency = "EUR",
            Invoice = "My invoice",
            Description = "My description",
            ReturnUrl = "https://www.yoursite.com/success",
            ReturnUrlReject = "https://www.yoursite.com/rejected",
            ReturnUrlCancel = "https://www.yoursite.com/canceled",
            ReturnUrlError = "https://www.yoursite.com/error",
            PushUrl = "https://www.yoursite.com/buckarooendpoint",
            PushUrlFailure = "https://www.yoursite.com/buckarooendpoint/failed",
            Order = "My order",
            AmountDebit = 12.5m, 
            ClientIp = new IpAddress { Type = 0, Address = ipAddress },
            ContinueOnIncomplete = ContinueOnIncomplete.RedirectToHTML
        };

        transactionBase.AddCustomParameter("city", "Amsterdam");
        transactionBase.AddCustomParameter("zipcode", "1114BC");
        transactionBase.AddCustomParameter("housenumber", "7");
        transactionBase.AddCustomParameter("firstname", "Oscar");
        transactionBase.AddCustomParameter("lastname", "de Vaal");

        var request = client
            .CreateRequest()
            .Authenticate(websiteKey, secretKey, isLivePayment, CultureInfo.GetCultureInfo("nl-nl"))
            .TransactionRequest()
            .SetBasicFields(transactionBase)
            .Ideal()
            .Pay(new IdealPayRequest());

        RequestResponse result = null;
        Task.Run(async () => result = await request.ExecuteAsync()).Wait();

        return Redirect(result.RequiredAction.RedirectURL);
    }
}

After the payment, Buckaroo will call the URL that we have provided, along with a list of parameters. The unique identifier of the payment is the brq_transactions parameter. Our custom parameters are prefixed with “CUST_”.

brq_amount=12.50
brq_currency=EUR
brq_description=My+description
brq_invoicenumber=My+invoice
brq_mutationtype=NotSet
brq_ordernumber=My+order
brq_payment=98D255103FC347ADB92F1C2A248748F2
brq_statuscode=490
brq_statuscode_detail=S072
brq_statusmessage=Failed
brq_test=true
brq_timestamp=2019-12-13+13%3a46%3a37
brq_transaction_method=ideal
brq_transactions=3397E2CAB1DD475F8F200BF273DE16EA
brq_websitekey=********
CUST_city=Amsterdam
CUST_firstname=Oscar
CUST_housenumber=7
CUST_lastname=de+Vaal
CUST_zipcode=1114BC
brq_signature=fc0e0a746cf4ce1f62919fc8e9d76b494e4e447d 

I have created a model class for deserializing the data.

using Newtonsoft.Json;

public class BuckarooDto
{
    [JsonProperty("CUST_city")]
    public string City { get; set; }

    [JsonProperty("brq_transactions")]
    public string PaymentId { get; set; }

    [JsonProperty("CUST_firstname")]
    public string FirstName { get; set; }

    [JsonProperty("CUST_housenumber")]
    public string Housenumber { get; set; }

    [JsonProperty("CUST_lastname")]
    public string LastName { get; set; }

    [JsonProperty("CUST_zipcode")]
    public string Zipcode { get; set; }
}

The following controller is an example of what the Endpoint can look like.

using System.Linq;
using System.Web.Mvc;
using Newtonsoft.Json;

public class BuckarooEndpointController : Controller
{
    public void Index()
    {
        var buckarooDto = GetBuckarooDto();
        //Execute success code
    }

    public void Failed()
    {
        var buckarooDto = GetBuckarooDto();
        //Execute failed code
    }

    private BuckarooDto GetBuckarooDto()
    {
        var parameters = Request.Params
            .Cast<string>()
            .ToDictionary(
                k => k,
                v => Request.Params[v]);

        var json = JsonConvert.SerializeObject(parameters);
        return JsonConvert.DeserializeObject<BuckarooDto>(json);
    } 
}

In case our EndpointController throws an error, Buckaroo will try to call this Endpoint for another 10 times. The interval that Buckaroo will call our endpoint is:

  • 5 minutes
  • 10 minutes
  • 15 minutes
  • 30 minutes
  • 1 hour
  • 2 hours
  • 4 hours
  • 8 hours
  • 8 hours
  • 24 hours
  • 24 hours

It’s also possible to manually initiate this call from the portal.

The result

The user will be redirected to Buckaroo’s iDeal payment page.

Buckaroo iDeal

Oscar de Vaal

Oscar de Vaal

.NET Developer