Search code examples
.netxamarin.formscybersource

Cybersource post request always getting session timeout


I know that Cybersource isn't that much of familiar here, but I'm really stuck with something and maybe someone can help me.

I've tried Cybersource Support Center but apparently they don't support testing accounts.

I have the following code in which I'm trying to execute a post request to Cybersource:

private async void Confirm_Payment(object sender, EventArgs e)
{
    var randomGuid = Guid.NewGuid().ToString();
    var currentDateTime = GetUTCDateTime();
    var referenceNumber = "1594706148080";

    using (var client = new HttpClient())
    {
        using (var content = new MultipartFormDataContent())
        {
            var values = new Dictionary<string, string>
            {
                {"access_key", "ACCESS_KEY"},
                {"profile_id", "PROFILE_ID"},
                {"transaction_uuid", randomGuid},
                {"signed_field_names", "access_key,profile_id,transaction_uuid,signed_field_names,unsigned_field_names,signed_date_time,locale,transaction_type,reference_number,amount,currency"},
                {"unsigned_field_names", ""},
                {"signed_date_time", currentDateTime},
                {"locale", "en"},
                {"transaction_type", "authorization"},
                {"reference_number", referenceNumber},
                {"amount", "100.00"},
                {"currency", "USD"},
                {"submit", "Submit"}
            };
            var signature = Security.Sign(values);
            foreach (var keyValuePair in values)
            {
                content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
            }

            content.Add(new StringContent(signature), "signature");
            var requestUri = "https://testsecureacceptance.cybersource.com/pay";
            var result = await client.PostAsync(requestUri, content);
            if (result.IsSuccessStatusCode)
            {
                var resp = await result.Content.ReadAsStringAsync();
                await Navigation.PushAsync(new WebScreen(requestUri));
            }
        }
    }
}

result.IsSuccessStatusCode is 200 OK but whenever I getawait result.Content.ReadAsStringAsync(); I got a Session Timeout Page

Note that the same code in .NET is working fine:

<form id="payment_confirmation" action="https://testsecureacceptance.cybersource.com/pay" method="post">
    <fieldset id="confirmation">
        <legend>Review Payment Details</legend>
        <div>
            <%
                foreach (var key in Request.Form.AllKeys)
                {
                    Response.Write("<div>");
                    Response.Write("<span class=\"fieldName\">" + key + ":</span><span class=\"fieldValue\">" + Request.Params[key] + "</span>");
                    Response.Write("</div>");
                }
    %>
        </div>
    </fieldset>
    <%
        IDictionary<string, string> parameters = new Dictionary<string, string>();
        foreach (var key in Request.Form.AllKeys)
        {
            Response.Write("<input type=\"hidden\" id=\"" + key + "\" name=\"" + key + "\" value=\"" + Request.Params[key] + "\"/>\n");
            parameters.Add(key, Request.Params[key]);
        }
        Response.Write("<input type=\"hidden\" id=\"signature\" name=\"signature\" value=\"" + secureacceptance.Security.Sign(parameters) + "\"/>\n");
%>
    <input type="submit" id="submit" value="Confirm" />
</form>

I tried to take the same code and execute it in Xamarin Forms but it didn't work.

I think the issue is that we should add specific Headers to let the Cybersource Gateway don't give session timeout issue. Anyone know what could be the problem?


Solution

  • I don't know if someone will ever have this issue, but I solved it by adding a cookie to the Webview

    Even though navigating in the Webview is throwing the same error (Session Timeout). But this question is resolved by adding the following code:

    var domain = new Uri(webSource.BaseUrl).Host;
    var cookie = new Cookie
    {
        Secure = true,
        Expired = false,
        HttpOnly = false,
        Name = "cookie",
        Expires = DateTime.Now.AddDays(10),
        Domain = domain,
        Path = "/"
    };
    cookies.Add(cookie);
    paymentWebView.Cookies = cookies;