测试 Paypal 订阅

问题描述 投票:0回答:1

我正在尝试使用 REST API 开发 PayPal 订阅,在实施过程中,我遇到了几个问题。我面临的挑战之一是弄清楚如何测试订阅功能,而不必等待一整天过去。 在 PayPal 沙盒环境中,是否有类似于 Stripe 中的时钟模拟的功能,可以让我加速订阅间隔的测试过程?这将极大地有助于加快订阅系统的测试和开发。

rest paypal paypal-subscriptions
1个回答
0
投票

您可以使用这样的节点验证 Paypal 订阅的状态:

const fetch = require('node-fetch');
const clientIdAndSecret = "CLIENTID:SECRET";

async function verifySubscription(subscriptionID) { 
   const authUrl = "https://api-m.paypal.com/v1/oauth2/token";
   const subscriptionsUrl = "https://api.paypal.com/v1/billing/subscriptions/" + subscriptionID;
   const base64 = Buffer.from(clientIdAndSecret).toString('base64');
   try {
      let response = await fetch(authUrl, { 
         method: 'POST',
         headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
            'Authorization': `Basic ${base64}`,
         },
         body: 'grant_type=client_credentials'
       });
     let data = await response.json();
     let subResponse = await fetch(subscriptionsUrl, { 
        method: 'get', 
        headers: {
          'Authorization': 'Bearer '+data.access_token, 
          'Content-Type': 'application/json'
        }, 
    });
    let subscriptionData = await subResponse.json();
    if (!!subscriptionData) {
        if (subscriptionData.status === "ACTIVE") {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
  } catch (error) {
    console.log(error);
    return false;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.