如何使用 ASP .NET MVC 集成 PayPal 订阅 API

问题描述

我按照此 PayPal 文档 https://developer.paypal.com/docs/api/subscriptions/v1 获取订阅 API。我想使用 ASP .NET MVC 调用这个 API https://api.sandbox.paypal.com/v1/billing/subscriptions/${subscriberId}。此 API 需要使用 PayPal clientID 和 secretID 的授权令牌,我已成功创建令牌,但在调用订阅 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"

另外,我刚刚使用 react JS 调用了这个 API,我得到了想要的结果。但是在 ASP .NET MVC 中如何调用这个订阅 API 呢? 反应 JS 代码

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 代码

[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
                };
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)