Skip to content

Files

Latest commit

130c511 · Sep 14, 2024

History

History
This branch is up to date with stripe-samples/accept-a-payment:main.

node-typescript

Accepting a payment with TypeScript

As of 8.0.1, stripe-node provides types for the latest API version. These types offer a beneficial developer experience when working with TypeScript, including enhanced code completion.

To find out more about usage with TypeScript, see the stripe-node README.

Requirements

You’ll need the following:

  • Node.js v20+
  • Stripe account to accept payments (sign up for free).
  • Stripe CLI to tunnel webhook events to your local server.

Setup

Copy the environment variables file from the root of the repository:

cp ../../../.env.example .env

Update .env with your own Stripe API keys.

Optionally - Add Stripe Tax Stripe Tax lets you calculate and collect sales tax, VAT and GST with one line of code. To enable Stripe Tax set up in the dashboard: [Docs - Set up Stripe Tax](https://stripe.com/docs/tax/set-up).

In "server.ts", set calcuateTax to true if you want Stripe to calculate tax for the transactions in this application.

const calculateTax = true;

Install and run

Install dependencies using npm:

npm install

Next, follow these installation steps to install the Stripe CLI which we'll use for webhook forwarding.

After the installation has finished, authenticate the CLI with your Stripe account:

stripe login

To start the webhook forwarding run:

stripe listen --forward-to localhost:4242/webhook

The Stripe CLI will let you know that webhook forwarding is ready and output your webhook signing secret:

> Ready! Your webhook signing secret is whsec_xxx

Copy the webhook signing secret (whsec_xxx) to your .env file.

In a separate terminal window, start the local server:

npm run start # Compiles and runs the TypeScript example.