0.0.20 • Published 7 months ago

@team-gpt/paddle-billing-sdk v0.0.20

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Paddle Billing SDK

Unofficial Paddle Billing API SDK for Node.js runtime

See https://developer.paddle.com/api-reference/overview

Getting Started

Setup

npm i @team-gpt/paddle-billing-sdk
yarn add @team-gpt/paddle-billing-sdk
bun add @team-gpt/paddle-billing-sdk

Peer dependencies

Axios - https://axios-http.com/

npm i axios
yarn add axios

Endpoints

  • Prices
  • Products
  • Customer
  • Discounts
  • Addresses
  • Businesses
  • Transactions
  • Subscriptions
  • Adjustments
  • Event Types
  • Events
  • Notifications

Usage

Authentication

Create an apikey from the Authentication page in Paddle platform:

PADDLE_AUTH_SECRET=
NEXT_PUBLIC_PADDLE_VENDOR_ID=
NEXT_PUBLIC_PADDLE_SANDBOX=

Paddle Client

import { PaddleClient } from '@team-gpt/paddle-billing-sdk'

/**
 * @see https://developer.paddle.com/api-reference/overview
 */
export const paddleClient = new PaddleClient({
  authToken: process.env.PADDLE_AUTH_SECRET || 'MISSING',
  vendorId: Number(process.env.NEXT_PUBLIC_PADDLE_VENDOR_ID),
  sandbox: Boolean(process.env.NEXT_PUBLIC_PADDLE_SANDBOX),
})

Webhooks

Usage with Next.js API handlers

// /pages/api/webhooks/paddle-events.ts

import { WebhookEvents, signatureHeader } from 'paddle-billing-sdk'
import { NextApiRequest, NextApiResponse } from 'next'

const authSecret = process.env.PADDLE_AUTH_SECRET
const webhookSecret = process.env.PADDLE_WEBHOOK_SECRET

if (!authSecret) throw new Error('No Paddle auth secret set!')

export const config = {
  api: {
    bodyParser: false,
  },
}

const handler = async function (req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    res.setHeader('Allow', 'POST')
    res.status(405).end('Method Not Allowed')
    return
  }

  if (!req.headers[signatureHeader] || typeof req.headers[signatureHeader] !== 'string') {
    res.status(400).end('Invalid signature')
    return
  }

  try {
    const sig = req.headers[signatureHeader]
    const events = new WebhookEvents(sig, webhookSecret)
    const buf = await WebhookEvents.buffer(req)
    const event = events.constructEvent(buf)
    console.log(event)
  } catch (error) {
    console.log(error)
    if (error instanceof Error) {
      res.status(400).json({ error: error.message })
    }
  }
}

Receive webhooks

  1. Create an account in https://ngrok.com/
  2. Expose your local server

    ngrok http 3000
  3. Add the exposed server to Paddle Notifications at https://sandbox-vendors.paddle.com/notifications

    https://xxx-xx-xxx-xxx-xx.ngrok-free.app/api/webhooks/paddle-events
  4. Send a request

Extend custom data

To extend the default custom data interfaces add the following to your codebase

// Custom interfaces for metadata
declare module '@team-gpt/paddle-billing-sdk' {
  export interface PriceMetadata {
    myKey: string
  }
  export interface ProductMetadata {
    myKey: string
  }
  export interface CustomerMetadata {
    myKey: string
  }
  export interface TransactionMetadata {
    myKey: string
  }
  export interface SubscriptionMetadata {
    myKey: string
  }
}

Testing

bun test

License

MIT

0.0.20

7 months ago

0.0.18

7 months ago

0.0.17

7 months ago

0.0.16

7 months ago

0.0.15

7 months ago

0.0.13

7 months ago

0.0.12

7 months ago

0.0.11

7 months ago

0.0.10

7 months ago

0.0.9

7 months ago

0.0.7

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.0

7 months ago