Skip to main content

Add Subscription

Description

A webhook event subscription is added per checkout merchant, via Payment Admin external API. To add a new subscription, use the add callback subscription endpoint specifying the webhook endpoint URL together with the events for which the webhook should be invoked.

Endpoint

Method: POST
/api/v2/callbacks/subscriptions

Sample Code

Add Subscription Example
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json;
using System.Text;

internal static class AddSubscription
{
internal static async Task AddSubscriptionAsync()
{
var services = new ServiceCollection();
services.AddHttpClient("MyApiClient", client =>
{
client.BaseAddress = new Uri("http://paymentadminapistage.svea.com/api/");
});

var serviceProvider = services.BuildServiceProvider();
var httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("MyApiClient");

var apiUrl = "v2/callbacks/subscriptions";

var subscriptionRequest = new SubscriptionRequest("webhookurl", new string[] { "CheckoutOrder.Created" });
var jsonString = JsonSerializer.Serialize(subscriptionRequest);
var subscriptionRequestJson = new StringContent(
jsonString,
Encoding.UTF8,
"application/json");

// Add authorization header
Authentication.CreateAuthenticationToken(out string token, out string timestamp, jsonString);
httpClient.DefaultRequestHeaders.Add("Authorization", token);
httpClient.DefaultRequestHeaders.Add("Timestamp", timestamp);

try
{
HttpResponseMessage response = await httpClient.PostAsync(apiUrl, subscriptionRequestJson);

// Check if the request was successful
if (response.IsSuccessStatusCode)
{
string responseData = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response: " + responseData);
}
else
{
Console.WriteLine("Failed to retrieve data. Status code: " + response.StatusCode);
}
}
catch (HttpRequestException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}

internal record SubscriptionRequest(string CallbackUri, string[] Events);