forked from stripe-samples/accept-a-payment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
218 lines (199 loc) · 6.42 KB
/
server.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const express = require('express');
const cors = require('cors');
const app = express();
const { resolve } = require('path');
// Replace if using a different env file or config
const env = require('dotenv').config({ path: './.env' });
const calculateTax = false;
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY, {
apiVersion: "2023-10-16",
appInfo: { // For sample support and debugging, not required for production:
name: "stripe-samples/accept-a-payment/custom-payment-flow",
version: "0.0.2",
url: "https://github.com/stripe-samples"
}
});
app.use(express.static(process.env.STATIC_DIR));
app.use(
express.json({
// We need the raw body to verify webhook signatures.
// Let's compute it only when hitting the Stripe webhook endpoint.
verify: function (req, res, buf) {
if (req.originalUrl.startsWith('/webhook')) {
req.rawBody = buf.toString();
}
},
})
);
app.use(cors({
origin: 'http://localhost:3000'
}));
app.get('/', (req, res) => {
const path = resolve(process.env.STATIC_DIR + '/index.html');
res.sendFile(path);
});
app.get('/config', (req, res) => {
res.send({
publishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
});
});
const calculate_tax = async (orderAmount, currency) => {
const taxCalculation = await stripe.tax.calculations.create({
currency,
customer_details: {
address: {
line1: "10709 Cleary Blvd",
city: "Plantation",
state: "FL",
postal_code: "33322",
country: "US",
},
address_source: "shipping",
},
line_items: [
{
amount: orderAmount,
reference: "ProductRef",
tax_behavior: "exclusive",
tax_code: "txcd_30011000"
}
],
});
return taxCalculation;
};
app.post('/create-payment-intent', async (req, res) => {
const { paymentMethodType, currency, paymentMethodOptions } = req.body;
// Each payment method type has support for different currencies. In order to
// support many payment method types and several currencies, this server
// endpoint accepts both the payment method type and the currency as
// parameters. To get compatible payment method types, pass
// `automatic_payment_methods[enabled]=true` and enable types in your dashboard
// at https://dashboard.stripe.com/settings/payment_methods.
//
// Some example payment method types include `card`, `ideal`, and `link`.
let orderAmount = 1400;
let params = {};
if (calculateTax) {
let taxCalculation = await calculate_tax(orderAmount, currency)
params = {
payment_method_types: paymentMethodType === 'link' ? ['link', 'card'] : [paymentMethodType],
amount: taxCalculation.amount_total,
currency: currency,
metadata: { tax_calculation: taxCalculation.id }
}
}
else {
params = {
payment_method_types: paymentMethodType === 'link' ? ['link', 'card'] : [paymentMethodType],
amount: orderAmount,
currency: currency,
}
}
// If this is for an ACSS payment, we add payment_method_options to create
// the Mandate.
if (paymentMethodType === 'acss_debit') {
params.payment_method_options = {
acss_debit: {
mandate_options: {
payment_schedule: 'sporadic',
transaction_type: 'personal',
},
},
}
} else if (paymentMethodType === 'konbini') {
/**
* Default value of the payment_method_options
*/
params.payment_method_options = {
konbini: {
product_description: 'Tシャツ',
expires_after_days: 3,
},
}
} else if (paymentMethodType === 'customer_balance') {
params.payment_method_data = {
type: 'customer_balance',
}
params.confirm = true
params.customer = req.body.customerId || await stripe.customers.create().then(data => data.id)
}
/**
* If API given this data, we can overwride it
*/
if (paymentMethodOptions) {
params.payment_method_options = paymentMethodOptions
}
// Create a PaymentIntent with the amount, currency, and a payment method type.
//
// See the documentation [0] for the full list of supported parameters.
//
// [0] https://stripe.com/docs/api/payment_intents/create
try {
const paymentIntent = await stripe.paymentIntents.create(params);
// Send publishable key and PaymentIntent details to client
res.send({
clientSecret: paymentIntent.client_secret,
nextAction: paymentIntent.next_action,
});
} catch (e) {
return res.status(400).send({
error: {
message: e.message,
},
});
}
});
app.get('/payment/next', async (req, res) => {
const intent = await stripe.paymentIntents.retrieve(
req.query.payment_intent,
{
expand: ['payment_method'],
}
);
res.redirect(`/success?payment_intent_client_secret=${intent.client_secret}`);
});
app.get('/success', async (req, res) => {
const path = resolve(process.env.STATIC_DIR + '/success.html');
res.sendFile(path);
});
// Expose a endpoint as a webhook handler for asynchronous events.
// Configure your webhook in the stripe developer dashboard
// https://dashboard.stripe.com/test/webhooks
app.post('/webhook', async (req, res) => {
let data, eventType;
// Check if webhook signing is configured.
if (process.env.STRIPE_WEBHOOK_SECRET) {
// Retrieve the event by verifying the signature using the raw body and secret.
let event;
let signature = req.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
req.rawBody,
signature,
process.env.STRIPE_WEBHOOK_SECRET
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`);
return res.sendStatus(400);
}
data = event.data;
eventType = event.type;
} else {
// Webhook signing is recommended, but if the secret is not configured in `config.js`,
// we can retrieve the event data directly from the request body.
data = req.body.data;
eventType = req.body.type;
}
if (eventType === 'payment_intent.succeeded') {
// Funds have been captured
// Fulfill any orders, e-mail receipts, etc
// To cancel the payment after capture you will need to issue a Refund (https://stripe.com/docs/api/refunds)
console.log('💰 Payment captured!');
} else if (eventType === 'payment_intent.payment_failed') {
console.log('❌ Payment failed.');
}
res.sendStatus(200);
});
app.listen(4242, () =>
console.log(`Node server listening at http://localhost:4242`)
);