This repository was archived by the owner on Mar 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathapi.js
64 lines (60 loc) · 1.59 KB
/
api.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
// @flow
const createPaymentIntent = (options: {}): Promise<string> => {
// To use this demo with your own Stripe account, clone this Runkit backend:
// https://runkit.com/stripe/create-intents
return window
.fetch(`https://create-intents-35aylzrcx0ej.runkit.sh/payment_intents`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({options}),
})
.then((res) => {
if (res.status === 200) {
return res.json();
} else {
return null;
}
})
.then((data) => {
if (!data || data.error) {
console.log('API error:', {data});
throw new Error('PaymentIntent API Error');
} else {
return data.client_secret;
}
});
};
const createSetupIntent = (options: {}): Promise<string> => {
// To use this demo with your own Stripe account, clone this Runkit backend:
// https://runkit.com/stripe/create-intents
return window
.fetch(`https://create-intents-35aylzrcx0ej.runkit.sh/setup_intents`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({options}),
})
.then((res) => {
if (res.status === 200) {
return res.json();
} else {
return null;
}
})
.then((data) => {
if (!data || data.error) {
console.log('API error:', {data});
throw new Error('SetupIntents API Error');
} else {
return data.client_secret;
}
});
};
const api = {
createPaymentIntent,
createSetupIntent,
};
export default api;