From 0b5e0ab292c257e82cb84649b268c9c468414c5e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 11 Oct 2021 18:24:17 -0600 Subject: [PATCH] feature: add Product.list and Plan.list --- paypal-checkout.js | 19 ++++++++++++++++++- tests/list.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/list.js diff --git a/paypal-checkout.js b/paypal-checkout.js index 245e509..ced8d8c 100644 --- a/paypal-checkout.js +++ b/paypal-checkout.js @@ -84,7 +84,7 @@ Product.categories = { }; */ -Product.create = async function _createSubscription({ +Product.create = async function _createProduct({ request_id, name, description, @@ -127,6 +127,14 @@ Product.create = async function _createSubscription({ .then(must201or200) .then(justBody); }; +Product.list = async function _listProducts() { + return await PayPal.request({ + url: "/v1/catalogs/products", + json: true, + }) + .then(must201or200) + .then(justBody); +}; let Plan = {}; Plan.intervals = { @@ -201,6 +209,15 @@ Plan.create = async function _createPlan({ .then(justBody); }; +Plan.list = async function _listPlans() { + return await PayPal.request({ + url: "/v1/billing/plans", + json: true, + }) + .then(must201or200) + .then(justBody); +}; + let Subscription = {}; Subscription.actions = { CONTINUE: "CONTINUE", diff --git a/tests/list.js b/tests/list.js new file mode 100644 index 0000000..22a26d9 --- /dev/null +++ b/tests/list.js @@ -0,0 +1,36 @@ +"use strict"; + +require("dotenv").config({ path: ".env" }); +require("dotenv").config({ path: ".env.secret" }); + +if (!process.env.PAYPAL_CLIENT_ID) { + console.error( + "Please copy example.env to .env and update the values from the PayPal API Dashboard at https://developer.paypal.com/developer/applications" + ); + process.exit(1); +} + +let PayPal = require("../"); +let { Plan, Product, Subscription } = PayPal; + +async function test() { + let products = await Plan.list(); + console.info(); + console.info("Products:"); + console.info(JSON.stringify(products, null, 2)); + + let plans = await Plan.list(); + console.info(); + console.info("Plans:"); + console.info(JSON.stringify(plans, null, 2)); + + console.info(); +} + +if (require.main === module) { + PayPal.init(process.env.PAYPAL_CLIENT_ID, process.env.PAYPAL_CLIENT_SECRET); + test().catch(function (err) { + console.error("Something bad happened:"); + console.error(JSON.stringify(err, null, 2)); + }); +}