Introduction
This package is NOT supported by PayPal. The current PayPal Node SDK does not support the newest Javascript features. This package is intended to support the most cutting edge Javascript features.
Main Features
- Written in Typescript and provide api types externally
- Native Promise support using the request retry library
- Retry failed api calls automatically using request retry library and paypal idempotency
- Store access token expiration date and check before sending request. Currently the paypal sdk only updates the token if the request fails. This is more efficient.
- Api Pre Validation using Joi Schemas. This improves efficiency by preventing invalid api calls from being submitted.
- High Unit test coverage.
- Provide request function to submit any URL. Future proofs in case helper method is not available.
- Mocks for testing.
Installation
npm install --save paypal-rest-api
Configuration
The most up to date configuration options can be found on the IConfigureOptions interface
import { PayPalRestApi } from "../src";
const paypal = new PayPalRestApi({
client_id: "", // Your paypal client id
client_secret": "", // Your paypal client secret
mode: "sandbox", // "production" or "sandbox"
requestOptions: {
maxRetries: 2, // Sets the number of retries for 500 or Network timeout. Set to 0 to disable.
retryDelay: 5000, // Microseconds to wait until next retry. 5000 = 5 seconds
// Any options from the following
// https://github.com/FGRibreau/node-request-retry
// https://github.com/request/request
},
validate: true, // Turns on prevalidation. set to false if your validations are false negative. Only available in execute method.
});
Usage
There are 2 different methods to make API Calls. For full examples refer to the examples folder.
Run an example
// "examples/ANY_FILE_IN_EXAMPLES_FOLDER"
npm run example -- examples/request
Execute Method
The execute method can be executed for any api call that has a helper method.
import { PayPalRestApi } from "../src";
const paypal = new PayPalRestApi({
client_id: "YOUR_CLIENT_ID",
client_secret": "YOUR_CLIENT_SECRET",
mode: "sandbox",
});
paypal.execute("createInvoice", {
body: {
// https://developer.paypal.com/docs/api/invoicing/#invoices_create
merchant_info: {
business_name: "testy",
},
},
})
.then((response) => console.log)
.catch((err) => console.error);
Request Method
If a helper method does not exist you can always use the request method to directly execute an API call to an endpoint. You must specify the path and method.
import { PayPalRestApi } from "../src";
const paypal = new PayPalRestApi({
client_id: "YOUR_CLIENT_ID",
client_secret": "YOUR_CLIENT_SECRET",
mode: "sandbox",
});
paypal.request("v1/invoicing/invoices/", {
body: {
merchant_info: {
business_name: "testy",
},
},
method: "POST",
})
.then((response) => console.log)
.catch((err) => console.error);