rsnext/examples/with-stripe-typescript
Todor Totev b12f5a2669
[Examples] Remove React.FC from with-stripe-typescript (#13803)
Related to [this](https://github.com/facebook/create-react-app/pull/8177).

Let me know if you want me to change something.
2020-06-05 16:17:36 +00:00
..
components [Examples] Remove React.FC from with-stripe-typescript (#13803) 2020-06-05 16:17:36 +00:00
config Add Stripe TypeScript Example (#10482) 2020-02-10 22:24:38 -05:00
pages Stripe example updates (#13581) 2020-05-30 17:32:36 +00:00
public Add global CSS styles to example/with-stripe-typescript (#10520) 2020-02-13 13:08:18 -05:00
utils Upgrade to Prettier 2 (#13061) 2020-05-18 15:24:37 -04:00
.env.local.example Stripe example updates (#13581) 2020-05-30 17:32:36 +00:00
next-env.d.ts Add Stripe TypeScript Example (#10482) 2020-02-10 22:24:38 -05:00
package.json Stripe example updates (#13581) 2020-05-30 17:32:36 +00:00
README.md Suggest npx over npm init (#13637) 2020-06-01 17:36:57 +00:00
styles.css Stripe example updates (#13581) 2020-05-30 17:32:36 +00:00
tsconfig.json Add Stripe TypeScript Example (#10482) 2020-02-10 22:24:38 -05:00
vercel.json Stripe example updates (#13581) 2020-05-30 17:32:36 +00:00

Example using Stripe with TypeScript and react-stripe-js

This is a full-stack TypeScript example using:

Demo

See the sample live or fork on CodeSandbox.

The demo is running in test mode -- use 4242424242424242 as a test card number with any CVC + future expiration date.

Use the 4000000000003220 test card number to trigger a 3D Secure challenge flow.

Read more about testing on Stripe at https://stripe.com/docs/testing.

Checkout Demo A gif of the Checkout payment page.
Elements Demo A gif of the custom Elements checkout page.

Included functionality

How to use

Using create-next-app

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example with-stripe-typescript with-stripe-typescript-app
# or
yarn create next-app --example with-stripe-typescript with-stripe-typescript-app

Download manually

Download the example:

curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-stripe-typescript
cd with-stripe-typescript

Required configuration

Copy the .env.local.example file into a file named .env.local in the root directory of this project:

cp .env.local.example .env.local

You will need a Stripe account (register) to run this sample. Go to the Stripe developer dashboard to find your API keys and replace them in the .env.local file.

NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=<replace-with-your-publishable-key>
STRIPE_SECRET_KEY=<replace-with-your-secret-key>

Now install the dependencies and start the development server.

npm install
npm run dev
# or
yarn
yarn dev

Forward webhooks to your local dev server

First install the CLI and link your Stripe account.

Next, start the webhook forwarding:

stripe listen --forward-to localhost:3000/api/webhooks

The CLI will print a webhook secret key to the console. Set STRIPE_WEBHOOK_SECRET to this value in your .env.local file.

Deploy

Deploy to Vercel

Deploy it to the cloud with Vercel (Documentation).

Note: You must add your Stripe secrets using the Vercel CLI (Download here):

vercel secrets add stripe_publishable_key pk_***
vercel secrets add stripe_secret_key sk_***
vercel secrets add stripe_webhook_secret whsec_***

After deploying, copy the deployment URL with the webhook path (https://your-url.now.sh/api/webhooks) and create a live webhook endpoint in your Stripe dashboard.

Note: Your live webhook will have a different secret. To update it in your deployed application you will need to first remove the existing secret and then add the new secret:

vercel secrets rm stripe_webhook_secret
vercel secrets add stripe_webhook_secret whsec_***

As the secrets are set as env vars in the project at deploy time, we will need to redeploy our app after we made changes to the secrets.

Authors