Search code examples
webhooksadyen

Adyen and .NET Standard .. Webhook


Are there any samples out there for Adyen with ASP.NET Standard.. Looking specifically for the WebHook part. I am not using .Net Core and I would like to avoid it if possible.

I have managed to get the client side working based off the php version of this sample: https://github.com/Adyen/adyen-components-js-sample-code . (I am happy to share this with anyone who is interested.)

What I need now is a concrete answer on : Can the webhook be made to work with ASP.NET .NET Standard?

.Net Core is so simple with this line:

// Return a 202 status with an empty response body
return Accepted();

How do I do the same in .Net Standard ?

I have tried this :

protected void Page_Load(object sender, EventArgs e)
{
   Response.ContentType = ""; // not sure what = its not json
   Response.Write ( HttpStatusCode.Accepted);
   // this doesnt work because I want to return the code not write it as body.. not sure how to do this. 
}

The above result in postman says this : enter image description here

What I really want is how .Net Core does it like this :

enter image description here


Solution

  • You can leave the body empty and use the following line:

    Response.StatusCode = 202;
    

    Can the webhook be made to work with ASP.NET .NET Standard?

    Adyen sends webhooks to your server-url. This means that your backend needs a way to deserialize the json-payload and verify the HMAC signature using the ADYEN_HMAC_KEY, see Adyen Docs on Webhooks.

    For receiving Adyen webhooks, I recommend to look into ASP .NET Core to expose an API-endpoint that can receive this POST-request securely. Assuming you have a different way to accept these payloads in your application, you need to do two steps:

    1. Deserialize the payload using the Adyen .NET Library WebhookHandler class, this will return a NotificationRequest

    Here's an example if you prefer to deserialize it yourself:

    var jsonSettings = new JsonSerializerSettings();
    NotificationRequest result = JsonConvert.DeserializeObject<NotificationRequest>(payload, jsonSettings);
    

    You can then access:

    List<NotificationRequestItemContainer> list = result.NotificationItemContainers; // Take the first item and verify its HMAC signature in step 2
    
    1. Verify the HMAC signature using the HmacValidator class
    new HMACValidator().IsValidHmac(list[0], hmacKey); // hmacKey is your secret `ADYEN_HMAC_KEY` (never expose this on the client-side)
    

    Let me know if this helped