How to Use PayPal Subscriptions API with Node.js in easy steps

Hey, friends today I will teach you How to Use PayPal Subscriptions API with Node.js. Our Google add-on store processes recurring payments with PayPal Subscriptions with Digital Goods, and invoices are sent to customers via Document Studio.

How to Use PayPal Subscriptions API with Node.js in easy steps

so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to Use PayPal Subscriptions API with Node.js in easy steps.

There are two steps involved.

  • Customers pay for and complete their orders on our website.
  • A BILLING is sent by PayPal.
  • SUBSCRIPTION.
  • WEBHOOK TO A SERVERLESS FUNCTION ACTIVATED
  • The function (which runs on Firebase and Google Cloud) verifies the subscription and determines whether or not the status is active.
  • To complete the order, it calls the Apps Script API.

The cloud function used to use the official PayPal SDK for Node.js, but it has recently been deprecated and no longer supports the new PayPal subscriptions API endpoints. The process of migrating from the PayPal Node SDK to your own solution is relatively simple and consists of two steps:

You might also like our trending code snippets

How to use the PayPal Subscriptions API with Node.js to manage recurring payments on serverless cloud functions.

1. Get the PayPal Access Token

const { default: axios } = require("axios");

const getPayPalAccessToken = async () => {
  const client_id = "PayPal Client ID goes here";
  const client_secret = "PayPal Client Secret goes here";
  const options = {
    url: "https://api-m.paypal.com/v1/oauth2/token",
    method: "POST",
    headers: {
      Accept: "application/json",
      "Accept-Language": "en_US",
      "Content-Type": "application/x-www-form-urlencoded",
    },
    auth: {
      username: client_id,
      password: client_secret,
    },
    params: {
      grant_type: "client_credentials",
    },
  };
  const { status, data } = await axios(options);
  return data.access_token;
};

If you want to test your integration with your PayPal sandbox account rather than the production version, replace api-m.paypal.com with api-m.sandbox.paypal.com in the requests and use the sandbox client secret credentials.

2. Verify PayPal Subscription

A successful request returns the HTTP 200 OK status code and a JSON response body.

const { default: axios } = require("axios");

const verifyPayPalSubscription = async (subscription_id) => {
  const token = await getPayPalAccessToken();
  const options = {
    method: "GET",
    url: `https://api-m.paypal.com/v1/billing/subscriptions/${subscription_id}`,
    headers: {
      Authorization: `Bearer ${token}`,
      Accept: "application/json",
    },
  };
  const { status, data = {} } = await axios(options);
  if (status === 200) {
    const { subscriber: { email_address } = {}, status } = data;
    return status === "ACTIVE";
  }
  return false;
};

When the PayPal Subscription is discovered to be active, an HTTP request is sent to the Google Apps Script API, which sends the invoice and licence to the customer.. Learn more.

Leave a Comment