Skip to content
This repository was archived by the owner on Feb 3, 2022. It is now read-only.

Commit f0a87fa

Browse files
Update stripe-node & next to latest versions
1 parent d722bb4 commit f0a87fa

File tree

16 files changed

+152
-7659
lines changed

16 files changed

+152
-7659
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Stripe keys
2-
STRIPE_PUBLISHABLE_KEY=pk_12345
2+
# https://dashboard.stripe.com/apikeys
3+
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_12345
34
STRIPE_SECRET_KEY=sk_12345
5+
# https://stripe.com/docs/webhooks/signatures
46
STRIPE_WEBHOOK_SECRET=whsec_1234

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
# Node files
66
node_modules/
7+
package-lock.json
8+
yarn.lock
79

810
# Typescript
911
dist

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ Read more about testing on Stripe at https://stripe.com/docs/testing.
3333

3434
### Included functionality
3535

36+
- [Global CSS styles](https://nextjs.org/blog/next-9-2#built-in-css-support-for-global-stylesheets)
3637
- Making `.env` variables available to next: [next.config.js](next.config.js)
37-
- **Note**: When deploying with Now you need to [add your secrets](https://zeit.co/docs/v2/serverless-functions/env-and-secrets) and specify a [now.json](/now.json) file.
38+
- **Note**: When deploying with Now you need to [add your secrets](https://vercel.com/docs/v2/serverless-functions/env-and-secrets) and specify a [now.json](/now.json) file.
3839
- Implementation of a Layout component that loads and sets up Stripe.js and Elements for usage with SSR via `loadStripe` helper: [components/Layout.tsx](components/Layout.tsx).
3940
- Stripe Checkout
4041
- Custom Amount Donation with redirect to Stripe Checkout:
@@ -58,7 +59,7 @@ Read more about testing on Stripe at https://stripe.com/docs/testing.
5859

5960
### Using `create-next-app`
6061

61-
Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
62+
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
6263

6364
```bash
6465
npm init next-app --example with-stripe-typescript with-stripe-typescript-app
@@ -71,7 +72,7 @@ yarn create next-app --example with-stripe-typescript with-stripe-typescript-app
7172
Download the example:
7273

7374
```bash
74-
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-stripe-typescript
75+
curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-stripe-typescript
7576
cd with-stripe-typescript
7677
```
7778

@@ -86,7 +87,7 @@ cp .env.example .env
8687
You will need a Stripe account ([register](https://dashboard.stripe.com/register)) to run this sample. Go to the Stripe [developer dashboard](https://stripe.com/docs/development#api-keys) to find your API keys and replace them in the `.env` file.
8788

8889
```bash
89-
STRIPE_PUBLISHABLE_KEY=<replace-with-your-publishable-key>
90+
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=<replace-with-your-publishable-key>
9091
STRIPE_SECRET_KEY=<replace-with-your-secret-key>
9192
```
9293

@@ -114,9 +115,9 @@ The CLI will print a webhook secret key to the console. Set `STRIPE_WEBHOOK_SECR
114115

115116
### Deploy
116117

117-
Deploy it to the cloud with [ZEIT Now](https://zeit.co/new?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
118+
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
118119

119-
**Note**: You must add your Stripe secrets using the ZEIT Now CLI ([Download here](https://zeit.co/download)):
120+
**Note**: You must add your Stripe secrets using the Vercel CLI ([Download here](https://vercel.com/download)):
120121

121122
```bash
122123
now secrets add stripe_publishable_key pk_***

components/CheckoutForm.tsx

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
import React, { useState } from 'react'
1+
import React, { useState } from 'react';
22

3-
import CustomDonationInput from '../components/CustomDonationInput'
3+
import CustomDonationInput from '../components/CustomDonationInput';
44

5-
import { fetchPostJSON } from '../utils/api-helpers'
6-
import { formatAmountForDisplay } from '../utils/stripe-helpers'
7-
import * as config from '../config'
5+
import { fetchPostJSON } from '../utils/api-helpers';
6+
import { formatAmountForDisplay } from '../utils/stripe-helpers';
7+
import * as config from '../config';
88

9-
import { useStripe } from '@stripe/react-stripe-js'
9+
import { useStripe } from '@stripe/react-stripe-js';
1010

1111
const CheckoutForm: React.FunctionComponent = () => {
12+
const [loading, setLoading] = useState(false);
1213
const [input, setInput] = useState({
1314
customDonation: Math.round(config.MAX_AMOUNT / config.AMOUNT_STEP),
14-
})
15-
const stripe = useStripe()
15+
});
16+
const stripe = useStripe();
1617

17-
const handleInputChange: React.ChangeEventHandler<HTMLInputElement> = e =>
18+
const handleInputChange: React.ChangeEventHandler<HTMLInputElement> = (e) =>
1819
setInput({
1920
...input,
2021
[e.currentTarget.name]: e.currentTarget.value,
21-
})
22+
});
2223

23-
const handleSubmit: React.FormEventHandler<HTMLFormElement> = async e => {
24-
e.preventDefault()
24+
const handleSubmit: React.FormEventHandler<HTMLFormElement> = async (e) => {
25+
e.preventDefault();
26+
setLoading(true);
2527
// Create a Checkout Session.
2628
const response = await fetchPostJSON('/api/checkout_sessions', {
2729
amount: input.customDonation,
28-
})
30+
});
2931

3032
if (response.statusCode === 500) {
31-
console.error(response.message)
32-
return
33+
console.error(response.message);
34+
return;
3335
}
3436

3537
// Redirect to Checkout.
@@ -38,12 +40,13 @@ const CheckoutForm: React.FunctionComponent = () => {
3840
// available to this file, so you can provide it as parameter here
3941
// instead of the {{CHECKOUT_SESSION_ID}} placeholder.
4042
sessionId: response.id,
41-
})
43+
});
4244
// If `redirectToCheckout` fails due to a browser or network
4345
// error, display the localized error message to your customer
4446
// using `error.message`.
45-
console.warn(error.message)
46-
}
47+
console.warn(error.message);
48+
setLoading(false);
49+
};
4750

4851
return (
4952
<form onSubmit={handleSubmit}>
@@ -60,12 +63,12 @@ const CheckoutForm: React.FunctionComponent = () => {
6063
<button
6164
className="checkout-style-background"
6265
type="submit"
63-
disabled={!stripe}
66+
disabled={!stripe || loading}
6467
>
6568
Donate {formatAmountForDisplay(input.customDonation, config.CURRENCY)}
6669
</button>
6770
</form>
68-
)
69-
}
71+
);
72+
};
7073

71-
export default CheckoutForm
74+
export default CheckoutForm;

components/ElementsForm.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ const CARD_OPTIONS = {
2020
fontSize: '16px',
2121
fontSmoothing: 'antialiased',
2222
':-webkit-autofill': {
23-
color: '#fce883'
23+
color: '#fce883',
2424
},
2525
'::placeholder': {
26-
color: '#6772e5'
27-
}
26+
color: '#6772e5',
27+
},
2828
},
2929
invalid: {
3030
iconColor: '#ef2961',
31-
color: '#ef2961'
32-
}
33-
}
31+
color: '#ef2961',
32+
},
33+
},
3434
};
3535

3636
const ElementsForm: React.FunctionComponent = () => {
3737
const [input, setInput] = useState({
3838
customDonation: Math.round(config.MAX_AMOUNT / config.AMOUNT_STEP),
39-
cardholderName: ''
39+
cardholderName: '',
4040
});
4141
const [payment, setPayment] = useState({ status: 'initial' });
4242
const [errorMessage, setErrorMessage] = useState('');
@@ -69,21 +69,21 @@ const ElementsForm: React.FunctionComponent = () => {
6969
}
7070
};
7171

72-
const handleInputChange: React.ChangeEventHandler<HTMLInputElement> = e =>
72+
const handleInputChange: React.ChangeEventHandler<HTMLInputElement> = (e) =>
7373
setInput({
7474
...input,
75-
[e.currentTarget.name]: e.currentTarget.value
75+
[e.currentTarget.name]: e.currentTarget.value,
7676
});
7777

78-
const handleSubmit: React.FormEventHandler<HTMLFormElement> = async e => {
78+
const handleSubmit: React.FormEventHandler<HTMLFormElement> = async (e) => {
7979
e.preventDefault();
8080
// Abort if form isn't valid
8181
if (!e.currentTarget.reportValidity()) return;
8282
setPayment({ status: 'processing' });
8383

8484
// Create a PaymentIntent with the specified amount.
8585
const response = await fetchPostJSON('/api/payment_intents', {
86-
amount: input.customDonation
86+
amount: input.customDonation,
8787
});
8888
setPayment(response);
8989

@@ -104,8 +104,8 @@ const ElementsForm: React.FunctionComponent = () => {
104104
{
105105
payment_method: {
106106
card: cardElement!,
107-
billing_details: { name: input.cardholderName }
108-
}
107+
billing_details: { name: input.cardholderName },
108+
},
109109
}
110110
);
111111

@@ -143,7 +143,7 @@ const ElementsForm: React.FunctionComponent = () => {
143143
<div className="FormRow elements-style">
144144
<CardElement
145145
options={CARD_OPTIONS}
146-
onChange={e => {
146+
onChange={(e) => {
147147
if (e.error) {
148148
setPayment({ status: 'error' });
149149
setErrorMessage(

components/Layout.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ type Props = {
88
title?: string;
99
};
1010

11-
const stripePromise = loadStripe(process.env.STRIPE_PUBLISHABLE_KEY!);
11+
const stripePromise = loadStripe(
12+
process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!
13+
);
1214

1315
const Layout: React.FunctionComponent<Props> = ({
1416
children,
15-
title = 'TypeScript Next.js Stripe Example'
17+
title = 'TypeScript Next.js Stripe Example',
1618
}) => (
1719
<Elements stripe={stripePromise}>
1820
<Head>
@@ -60,7 +62,7 @@ const Layout: React.FunctionComponent<Props> = ({
6062
</a>
6163
.{' View code on '}
6264
<a
63-
href="https://github.com/zeit/next.js/tree/canary/examples/with-stripe-typescript"
65+
href="https://github.com/vercel/next.js/tree/canary/examples/with-stripe-typescript"
6466
target="_blank"
6567
rel="noopener noreferrer"
6668
>

next.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

now.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"build": {
77
"env": {
8-
"STRIPE_PUBLISHABLE_KEY": "@stripe_publishable_key"
8+
"NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY": "@stripe_publishable_key"
99
}
1010
}
1111
}

0 commit comments

Comments
 (0)