How to integrate PayPal subscription API using ASP .NET MVC

Viewed 400

I followed this PayPal document https://developer.paypal.com/docs/api/subscriptions/v1 for subscription API. I want to call this API https://api.sandbox.paypal.com/v1/billing/subscriptions/${subscriberId} using ASP .NET MVC. This API needs authorization token using PayPal clientID and secretID which I've successfully created the token but I've problem in calling subscription API.

curl -v -X GET 
https://api.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token"

Also I've just call this API using react JS and I'm getting desire result. But in ASP .NET MVC how to call this subscription API ? React JS Code:

async function checkPayPalPaymentStatus(subscriberId, access_token) {
  var token = `Bearer ${access_token}`;
  return await fetch(
    `https://api.sandbox.paypal.com/v1/billing/subscriptions/${subscriberId}`,
    {
      method: "GET",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: token,
      },
    }
  ).then((response) => response.json());
}

ASP .NET MVC Code:

[HttpGet]
        [Route("api/paypal/checkPayPalPaymentStatus")]
        public async Task<object> CheckPayPalPaymentStatus(string accessToken)
        {
            try
            {
                //
                string url = "https://api.sandbox.paypal.com/v1/billing/subscriptions/" + "I-BW452GLLEP1G";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Headers["Authorization"] = "Bearer " + Convert.ToBase64String(Encoding.Default.GetBytes(accessToken));
                request.Accept = "application/json";
                request.Method = "GET";
                request.ContentType = "application/json";

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                string responseString = streamRead.ReadToEnd();
                JObject jsonConvertedResponseString = JObject.Parse(responseString);
                return new
                {
                    Success = true,
                    Message = "",
                    Data = jsonConvertedResponseString
                };
            }
            catch (Exception ex)
            {
                return new
                {
                    Success = false,
                    Message = ex.Message,
                    Data = DBNull.Value
                };
            }
        }
0 Answers
Related