Coder Social home page Coder Social logo

jannikwempe / nodejs-paddle-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from invertase/nodejs-paddle-sdk

0.0 0.0 0.0 146 KB

A fully typed Node.js library for integration with Paddle.

License: Apache License 2.0

JavaScript 3.49% TypeScript 96.51%

nodejs-paddle-sdk's Introduction

Node Paddle SDK

A NodeJS library for integrating with Paddle.

The library provides full TypeScript definitions for the Paddle API & Webhook events.

Installation

npm install @invertase/node-paddle-sdk --save

Usage

import { PaddleSDK } from '@invertase/node-paddle-sdk';

const paddle = new PaddleSDK(
  12345, // Required: Vendor ID
  'xxxx', // Required: Vendor Auth Code
  'xxxx', // Optional: Public Key
);

The Public Key is optional, but required if using the verifyWebhook method.

API

The library provides integration with the Paddle API endpoints.

The vendor_id and vendor_auth_code parameters are not required and are automatically provided by the library.

Example usage:

try {
  const coupons = await paddle.listCoupons({
    product_id: 123,
  });
} catch (e) {
  console.error(e.code, e.message);
}

Product API

Coupons

Products

Licenses

Pay Links

Transactions

Payments

Subscription API

Plans

Users

Modifiers

Payments

One-off Charges

Alert API

Webhooks

Webhooks

The library provides useful helpers for handling Webhooks, along with type definitions for events for TypeScript users.

Verifying a webhook

To verify a webhook, you must have provided a public key whilst creating a PaddleSDK instance otherwise an error will be thrown. If the provided body does not conform to a Paddle Webhook request (JSON response containing both a alert_name and p_signature), an error will also be thrown.

Pass the request body to the verifyWebhook method to verify the signature:

// Express example

// Ensure you accept a JSON request body.
app.use(express.json());

app.post('/your-webhook-endpoint', (req, res) => {
  const verified = paddle.verifyWebhook(req.body);

  if (!verified) {
    return res.status(403).send('Invalid webhook request.');
  }

  ...
});

TypeScript

The library provides type definitions for the various Paddle Webhook events. Using the alert_name, you can discover the type of the event:

import { PaddleWebhook } from '@invertase/node-paddle-sdk';

app.post('/your-webhook-endpoint', (req, res) => {
  const verified = paddle.verifyWebhook(req.body);

  if (!verified) {
    return res.status(403).send('Invalid webhook request.');
  }

  const event = req.body as PaddleWebhook;

  if (event.alert_name === 'subscription_created') {
    // `event` is now cast as a `SubscriptionCreatedWebhook`
    console.log(event.subscription_id);
  }
});

Sandbox Environment

If using the Paddle Sandbox Environment, you can set the library to use a custom server endpoint:

import { PaddleSDK } from '@invertase/node-paddle-sdk';

const paddle = new PaddleSDK(
  12345, // Required: Vendor ID
  'xxxx', // Required: Vendor Auth Code
  'xxxx', // Optional: Public Key
  'https://sandbox-vendors.paddle.com/api/2.0', // Optional: Custom Server Endpoint
);

License


Built and maintained by Invertase.

nodejs-paddle-sdk's People

Contributors

alex4671 avatar ehesp avatar jannikwempe avatar ma-jaeseong avatar salakar avatar walelito avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.