-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
52 lines (40 loc) · 1.35 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
import Stripe from 'stripe'
import express from 'express'
const stripe = new Stripe('sk_test_09l3shTSTKHYCzzZZsiLl2vA', { apiVersion: '2020-08-27' })
const app = express()
app.use(express.static('public'))
app.use(express.json())
// オーダの金額を計算するコードを書く
const calculateOrderAmount = (items: Items) => {
const amount = items
// ここで計算する処理を書く
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400
}
type Items = {
id: string[]
}
type ItemsBody = {
items: Items
}
interface CustomRequest<T> extends express.Request {
body: T
}
app.post('/create-payment-intent', async (req: CustomRequest<ItemsBody>, res: express.Response) => {
const { items }: ItemsBody = req.body
// Create a PaymentIntent with the order amount and currency
const paymentIntent: Stripe.PaymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: 'usd',
})
res.send({
clientSecret: paymentIntent.client_secret,
})
})
app.listen(4242, () => {
console.log('Running on port 4242')
})