<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Unified Checkout: Capture Context API 400 Bad Request in cybersource APIs</title>
    <link>https://community.developer.cybersource.com/t5/cybersource-APIs/Unified-Checkout-Capture-Context-API-400-Bad-Request/m-p/89726#M1605</link>
    <description>&lt;P&gt;Would just like to provide an update on this. Wish to say thank you to Justin from Cybersource support for the help with this one.&lt;/P&gt;&lt;P&gt;If anyone encounters the same issue please follow these steps.&lt;/P&gt;&lt;P&gt;1. There is no need to encode the request body as a JWT. A standard JSON body is all that's required.&lt;/P&gt;&lt;P&gt;2. You still need to send your merchant-id, api key and secret as part of the request for authentication.&lt;/P&gt;&lt;P&gt;3. The response back to will be in JWT format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We found these sources very helpful for us to generate the Capture Context JWT successfully.&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/CyberSource/cybersource-rest-samples-csharp/tree/46b7872a3e1493177b5b178946aadae6c2e45955" target="_blank"&gt;https://github.com/CyberSource/cybersource-rest-samples-csharp/tree/46b7872a3e1493177b5b178946aadae6c2e45955&lt;/A&gt;&lt;BR /&gt;The specific sample for generating the Capture Context can be found here:&lt;BR /&gt;&lt;A href="https://github.com/CyberSource/cybersource-rest-samples-csharp/blob/46b7872a3e1493177b5b178946aadae6c2e45955/Source/Samples/UnifiedCheckout/GenerateUnifiedCheckout.cs" target="_blank"&gt;https://github.com/CyberSource/cybersource-rest-samples-csharp/blob/46b7872a3e1493177b5b178946aadae6c2e45955/Source/Samples/UnifiedCheckout/GenerateUnifiedCheckout.cs&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jul 2024 06:41:30 GMT</pubDate>
    <dc:creator>customerminds</dc:creator>
    <dc:date>2024-07-19T06:41:30Z</dc:date>
    <item>
      <title>Unified Checkout: Capture Context API 400 Bad Request</title>
      <link>https://community.developer.cybersource.com/t5/cybersource-APIs/Unified-Checkout-Capture-Context-API-400-Bad-Request/m-p/89709#M1596</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;We are looking to build an integration using Unified Checkout with C# .NET. We have hit a bit of snag with the CaptureContext. When we make a request to the CaptureContext API we are receiving a 400 Bad Request response. We can't identify which parameters are incorrect. We believe that we are constructing, encrypting and signing the payload correctly? Please see example of our C# code below:&lt;BR /&gt;&lt;BR /&gt;Any help at all is very much appreciated as we cannot move forwards until we can figure out this step and receive our capturecontext token&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;//Here is our Controller which makes the request to captureContext
[HttpPost]
    [Route("getcapturecontext")]
    public async Task&amp;lt;string&amp;gt; GetCaptureContextAsync()
    {
        var jwt = CreateJWT();
        var apiKey = ApiKey;
        var apiSecret = ApiSecret;
        var date = DateTime.UtcNow.ToString("R");

        var signature = GenerateSignature(jwt, apiKey, apiSecret, date);

        // Set the headers and request body
        using (var client = new HttpClient())
        {
            var signatureHeader = $"keyid=\"{apiKey}\", algorithm=\"HmacSHA256\", signature=\"{signature}\"";
            client.DefaultRequestHeaders.Add("v-c-merchant-id", "cmw50sandbox_1719243295");
            client.DefaultRequestHeaders.Add("date", date);
            client.DefaultRequestHeaders.Add("signature", signatureHeader);

            var requestContent = new StringContent(jwt, Encoding.UTF8, "application/jwt");

            // Send the request to CaptureContext
            var response = await client.PostAsync("https://apitest.cybersource.com/up/v1/capture-contexts", requestContent);
            var responseContent = response.Content;
            return await response.Content.ReadAsStringAsync();
        }        
    }
   
    //Create the JWT with sample payload and Base64 encoding
    private string CreateJWT()
    {
        var header = new { alg = "HS256", typ = "JWT" };

        var payload = new
        {
            targetOrigins = new List&amp;lt;string&amp;gt; { "https://yourCheckoutPage.com" },
            clientVersion = "0.19",
            allowedCardNetworks = new List&amp;lt;string&amp;gt; { "VISA", "MASTERCARD", "AMEX" },
            allowedPaymentTypes = new List&amp;lt;string&amp;gt; { "PANENTRY", "SRC", "GOOGLEPAY" },
            country = "IE",
            locale = "en_IE",
            captureMandate = new
            {
                billingType = "PARTIAL",
                requestEmail = false,
                requestPhone = false,
                requestShipping = false,
                showAcceptedNetworkIcons = true
            },
            orderInformation = new
            {
                amountDetails = new
                {
                    totalAmount = "21.00",
                    currency = "EUR"
                }
            }
        };

        var headerBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header));
        var payloadBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload));

        var headerBase64 = Base64UrlEncode(headerBytes);
        var payloadBase64 = Base64UrlEncode(payloadBytes);

        return $"{headerBase64}.{payloadBase64}";
    }
    
    //Base64 encoding service for header and payload
    private string Base64UrlEncode(byte[] input)
    {
        var base64 = Convert.ToBase64String(input);
        return base64.Replace("+", "-").Replace("/", "_").TrimEnd('=');
    }

    //Sign the data using secret key. SignData encodes data using HmacSha256 encryption
    private string GenerateSignature(string jwt, string apiKey, string apiSecret, string date)
    {
        var dataToSign = $"date: {date}\n" +
                         $"digest: SHA-256={ComputeSha256Hash(jwt)}\n" +
                         $"v-c-merchant-id: cmw50sandbox_1719243295";

        return SignData(dataToSign, apiSecret);
    }

    private string ComputeSha256Hash(string rawData)
    {
        using (SHA256 sha256Hash = SHA256.Create())
        {
            var bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
            return Convert.ToBase64String(bytes);
        }
    }

    private string SignData(string data, string key)
    {
        var keyBytes = Convert.FromBase64String(key);
        using (var hmacsha256 = new HMACSHA256(keyBytes))
        {
            var messageBytes = Encoding.UTF8.GetBytes(data);
            var hashMessage = hmacsha256.ComputeHash(messageBytes);
            return Convert.ToBase64String(hashMessage);
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone provide assistance or feedback to help us move forwards with our integration. We are not sure how to move forwards.&lt;BR /&gt;Here is the response we receive back from the CaptureContext API&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;"{\"response\":{\"rmsg\":\"Bad Request\"}}"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Any assistance is greatly appreciated&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Many thanks,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Tim Murray&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CustomerMinds&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 13:15:21 GMT</pubDate>
      <guid>https://community.developer.cybersource.com/t5/cybersource-APIs/Unified-Checkout-Capture-Context-API-400-Bad-Request/m-p/89709#M1596</guid>
      <dc:creator>customerminds</dc:creator>
      <dc:date>2024-07-16T13:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: Unified Checkout: Capture Context API 400 Bad Request</title>
      <link>https://community.developer.cybersource.com/t5/cybersource-APIs/Unified-Checkout-Capture-Context-API-400-Bad-Request/m-p/89726#M1605</link>
      <description>&lt;P&gt;Would just like to provide an update on this. Wish to say thank you to Justin from Cybersource support for the help with this one.&lt;/P&gt;&lt;P&gt;If anyone encounters the same issue please follow these steps.&lt;/P&gt;&lt;P&gt;1. There is no need to encode the request body as a JWT. A standard JSON body is all that's required.&lt;/P&gt;&lt;P&gt;2. You still need to send your merchant-id, api key and secret as part of the request for authentication.&lt;/P&gt;&lt;P&gt;3. The response back to will be in JWT format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We found these sources very helpful for us to generate the Capture Context JWT successfully.&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/CyberSource/cybersource-rest-samples-csharp/tree/46b7872a3e1493177b5b178946aadae6c2e45955" target="_blank"&gt;https://github.com/CyberSource/cybersource-rest-samples-csharp/tree/46b7872a3e1493177b5b178946aadae6c2e45955&lt;/A&gt;&lt;BR /&gt;The specific sample for generating the Capture Context can be found here:&lt;BR /&gt;&lt;A href="https://github.com/CyberSource/cybersource-rest-samples-csharp/blob/46b7872a3e1493177b5b178946aadae6c2e45955/Source/Samples/UnifiedCheckout/GenerateUnifiedCheckout.cs" target="_blank"&gt;https://github.com/CyberSource/cybersource-rest-samples-csharp/blob/46b7872a3e1493177b5b178946aadae6c2e45955/Source/Samples/UnifiedCheckout/GenerateUnifiedCheckout.cs&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2024 06:41:30 GMT</pubDate>
      <guid>https://community.developer.cybersource.com/t5/cybersource-APIs/Unified-Checkout-Capture-Context-API-400-Bad-Request/m-p/89726#M1605</guid>
      <dc:creator>customerminds</dc:creator>
      <dc:date>2024-07-19T06:41:30Z</dc:date>
    </item>
  </channel>
</rss>

