简体   繁体   中英

Paypal saved card rest API through 400 Bad Request error in C#

I am developing application using .net MVC C#. I tried to call rest API of PayPal for save credit card details, my code were working fine but suddenly it starting through 400 Bad Request exception. here is my code,

private static async Task<string> StorePaymentCards(string accessToken, PaymentInfoModel cardDetails)
    {
        try
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.sandbox.paypal.com/v1/vault/credit-card");
            var result = "";
            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method = "POST";
            httpWebRequest.Accept = "application/json; charset=utf-8";
            httpWebRequest.Headers.Add("Authorization", "Bearer " + accessToken);

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                var loginjson = new JavaScriptSerializer().Serialize(new
                {
                    payer_id = cardDetails.PayerId.Trim(),
                    type = cardDetails.CardType.Trim(),
                    number = cardDetails.CardNumber.Trim(),
                    expire_month = cardDetails.ExpireMonth.Trim(),
                    expire_year = cardDetails.ExpireYear.Trim(),
                    first_name = cardDetails.FirstName.Trim()
                });

                streamWriter.Write(loginjson);
                streamWriter.Flush();
                streamWriter.Close();

                //The code fails when creating the Response here, and go into catch block
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
                return result;
            }

        }

        catch (Exception ex)
        {
            return ex.GetBaseException().Message;
        }
    }
}

Can anyone help me out from this error? Thank you in advance.

 private static async Task<string> StorePaymentCards(string accessToken, PaymentInfoModel cardDetails)
    {
        try
        {
          //my stuff
        }
        catch (WebException ex)
        {
            string text = null;
            using (WebResponse response = ex.Response)
            {
                HttpWebResponse httpResponse = (HttpWebResponse)response;
                Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                using (Stream data = response.GetResponseStream())
                using (var reader = new StreamReader(data))
                {
                    text = reader.ReadToEnd();
                    Console.WriteLine(text);
                }
            }
            return text; //ex.GetBaseException().Message;
        }
    }

This is changed I have done in my catch block to trace proper error, so it return me that error which I was facing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM