Card token api and payment api (Json format input file not valid)

Async Function GetTokenPayment(accessToken As String, skey As String) As Task(Of Boolean)

Dim paymentUrl As String = $"https://api.sky.blackbaud.com/payments/v1/cardtokens"

Dim jsonData As String = "{
""card_number"": ""4111111111111111"",
""expiration_date"": ""12/24"",
""cvv"": ""123"",
""cardholder_name"": ""John Doe"",
""billing_address"": {
""line1"": ""123 Main St"",
""city"": ""Anytown"",
""state"": ""NY"",
""postal_code"": ""12345"",
""country"": ""US""
}
}"

'Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonData)


Using client As New HttpClient()

client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken)
'client.DefaultRequestHeaders.Add("Bb-Api-Subscription-Key", skey)


Dim response = Await client.PostAsync(paymentUrl, New StringContent(
New JavaScriptSerializer().Serialize(jsonData), Encoding.UTF8, "application/json"))

'Dim response1 = Await client.PostAsync(paymentUrl, New FormUrlEncodedContent(jsonObject))

Return response.IsSuccessStatusCode
End Using
End Function

We are not getting proper response for the above card token api call , not sure whether the json file which are sending as parameter for this api is invalid or any other authorization header information is missing , Subcritpion key and access token are fine, we have tested those keys in payment configuration .

Please give us the correct json format file for card token and payment api

Comments

  • Chris Rodgers
    Chris Rodgers Blackbaud Employee
    Ninth Anniversary Kudos 2 Name Dropper Participant

    Hi @Sumathi Babu, rather than building the JSON manually, I would suggest that you create a class that contains the properties of the JSON, initialize the class, and set the values as needed. If you intend to use Newtonsoft, you can then simply serialize the object using `Newtonsoft.Json.JsonConvert.SerializeObject`.

    Example:

    Create your class

    Public Class CardTokenAdd


    <Newtonsoft.Json.JsonProperty("exp_month")>
    Public Property Exp_month As Integer

    <Newtonsoft.Json.JsonProperty("exp_year")>
    Public Property Exp_year As Integer

    <Newtonsoft.Json.JsonProperty("is_backoffice")>
    Public Property Is_backoffice As Boolean?

    <Newtonsoft.Json.JsonProperty("name")>
    Public Property Name As String

    <Newtonsoft.Json.JsonProperty("number")>
    Public Property Number As String

    ……Omitted for brevity……….

    End Class

    Then using the class to build your request

    Dim cardToken = New CardTokenAdd()
    cardToken.Number = "4111"

    <omitted>

    Using client As New HttpClient()

    client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken)
    'client.DefaultRequestHeaders.Add("Bb-Api-Subscription-Key", skey)

    Dim json = Newtonsoft.Json.JsonConvert.SerializeObject(addCardToken)
    Dim content = New Net.Http.StringContent(json)
    Dim response = Await client.PostAsync(paymentUrl, New StringContent(content, Encoding.UTF8, "application/json"))

    'Dim response1 = Await client.PostAsync(paymentUrl, New FormUrlEncodedContent(jsonObject))

    Return response.IsSuccessStatusCode
    End Using

    Even better, you can use API Client generators to create most of the code for you. You can pull these generated clients into your code, and outside of setting a few headers (authorization, bb-api-subscription-key), these can take care of most of the work for you.

    To use these, you'll download the OpenAPI definition from our API Reference pages (e.g. Payments API Reference, as illustrated in the screenshot below), and provide this to your client generator.

    Native vb.net support looks pretty sparce for OpenAPI → Client generation, but you could reference a .NET client written in C# or convert the C# to vb.net. Google is a good resource here, but here's one from Microsoft: Get started with NSwag and ASP.NET Core | Microsoft Learn

    75c99fc47abd9b6b55b1dbe0a1129906-huge-im