paypal-client
A PayPal HTTP API client, for internal use in ModCord.
Installing
To install the library, use the command below in your project directory.
npm install @modcord/paypal-client
Library Usage
-
Since PayPal works with basic authentication issuing short-lived tokens, the library requires the user to trigger the token exchange loop before making any requests to the REST API.
-
The paypal.identify()
will return a promise that will resolve with true
when the client has obtained the authorization and also a ready
event will be emitted.
-
It is recommended to check if the paypal.ready
property is strictly true
before trying to fetch or make any requests.
-
To get your app's client id and secret go to https://developer.paypal.com/developer/applications/.
const PayPalClient = require("@modcord/paypal-client");
const paypal = new PayPalClient({
client_id: "AT5...3sR",
secret: "EDv...lcj",
environment: "sandbox",
keep_cache: false
});
paypal.on("ready", () => {
console.log("The authorization has been resolved, and the library is ready to make requests..");
});
paypal.identify();
Important!!
- All of the examples below assume you set up the library the way it is shwon above, if you did it in another way you will need to tweak the examples for your own use case.
- The examples below show the full list of parameters you can use to call a method or do a specific thing, when the comment next to it says is optional, you may ommit to include that in your code.
1. Catalog Products API
1.1 Products
1.1.1 Creating a product
const { ProductTemplate } = require("@modcord/modcord-client");
...
paypal.on("ready", async () => {
const newProduct = new ProductTemplate()
.setName("My New Product")
.setDescription("A description for my new product.")
.setType("DIGITAL")
.setCategory("SOFTWARE")
.setId("MY-VERY-OWN-ID")
.setImageUrl("https://image.shutterstock.com/image-vector/new-item-sign-stamp-on-600w-1773071672.jpg")
.setHomeUrl("https://google.com");
const myNewProduct = await paypalClient.products.create(newProduct);
console.log(myNewPorduct);
});
1.1.2 Fetching products in bulk
paypal.on("ready", async () => {
const myProducts = await paypal.products.fetchBulk({
page_count: 1,
all: false
});
console.log(myProducts);
});
1.1.3 Fetching product by id
paypal.on("ready", async () => {
const myProduct = await paypal.products.fetch(
"PROD-6BF74421N29171330",
false
);
console.log(myProduct);
}
});
1.1.4 Updating a product
- This method can be applied to any product instance.
paypal.on("ready", async () => {
const myProduct = await paypal.products.fetch(
"PROD-6BF74421N29171330",
false
);
console.log(myProduct.name);
console.log(myProduct.description);
console.log(myProduct.imageUrl);
console.log(myProduct.homeUrl);
const operationResult = await myProduct.update({
name: "ModCord Pro",
description: "The re-branded pro plan.",
imageUrl: "https://image.shutterstock.com/image-vector/new-item-sign-stamp-on-600w-1773071672.jpg",
homeUrl: "https://modcord.com"
});
console.log(operationResult);
console.log(myProduct.name);
console.log(myProduct.description);
console.log(myProduct.imageUrl);
console.log(myProduct.homeUrl);
});
2. Subscriptions API
2.1 Plans
2.1.1 Fetching plans in bulk
paypal.on("ready", async () => {
const myPlans = await paypal.plans.fetchBulk({
product_id: "PROD-2435DY8H5G84454455V",
page_count: 1,
all: false
});
console.log(myPlans);
});
2.1.2 Fetching plans by id
paypal.on("ready", async () => {
const plan = await paypal.plans.fetch(
"P-5D525680HN2867820MBNPCQI",
false
);
console.log(plan);
});
2.1.3 Deactivating a plan
- You can apply this method on every plan instance.
paypal.on("ready", async () => {
const plan = await paypal.plans.fetch(
"P-5D525680HN2867820MBNPCQI",
false
);
console.log(plan.status);
const operationResult = await plan.deactivate();
console.log(operationResult);
console.log(plan.status);
2.1.3 Activating a plan
- You can activate plans with statuses
CREATED
and INACTIVE
- You can apply this method on every plan instance.
paypal.on("ready", async () => {
const plan = await paypal.plans.fetch(
"P-5D525680HN2867820MBNPCQI",
false
);
console.log(plan.status);
const operationResult = await plan.activate();
console.log(operationResult);
console.log(plan.status);
3.1.4 Updating a plan
Fetching multiple plans.
paypal.on("ready", async () => {
const plan = await paypal.plans.fetch(
"P-5D525680HN2867820MBNPCQI",
false
);
console.log(plan.description);
console.log(plan.taxes.percentage);
console.log(plan.paymentPreferences);
const operationResult = await plan.update({
description: "The shiny new description!",
taxes: {
percentage: 10.5
},
paymentPreferences: {
autoBillOutstanding: false,
paymentFailureThreshold: 4,
setupFee: {
currencyCode: "USD",
value: 4.69
},
setupFeeFailureAction: "CANCEL"
}
});
console.log(operationResult);
console.log(plan.description);
console.log(plan.taxes.percentage);
console.log(plan.paymentPreferences);
});
3.1.5 Creating a plan
paypal.on("ready", async () => {
const myProduct = await paypal.products.fetch(
"PROD-6BF74421N29171330",
true
);
const myNewPlan = new PlanTemplate()
.setProductId(myProduct.id)
.setName("My Nice Plan")
.setDescription("A very nice plan to create.")
.setStatus("ACTIVE")
.setPaymentPreferences({
autoBillOutstanding: true,
setupFailureAction: "CANCEL",
paymentFailureThreshold: 3,
setupFee: {
currencyCode: "USD",
value: 2.44
}
})
.setTaxes(20, true)
.addBillingCycle({
fixedPrice: {
currencyCode: "USD",
value: 0
},
pricingModel: "VOLUME"
}, {
intervalUnit: "WEEK",
intervalCount: 2
}, "TRIAL", 1, 1)
.addBillingCycle({
fixedPrice: {
currencyCode: "USD",
value: 4.99
},
pricingModel: "VOLUME"
}, {
intervalUnit: "MONTH",
intervalCount: 1
}, "REGULAR", 2, 0)
.setQuantitySupported(false);
const myPlan = await paypal.plans.create(myNewPlan);
console.log(myPlan);
});