-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-stripe.tsx
146 lines (126 loc) · 4.34 KB
/
react-stripe.tsx
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
import React, { useState, useEffect } from "react";
import {
CardElement,
useStripe,
useElements
} from "@stripe/react-stripe-js";
import {Alert, Spinner} from "react-bootstrap";
import "./_checkout.scss"
export default function CheckoutForm() {
const [succeeded, setSucceeded] = useState(false);
const [error, setError] = useState(null);
const [processing, setProcessing] = useState(false);
const [disabled, setDisabled] = useState(true);
const [clientSecret, setClientSecret] = useState('');
const stripe = useStripe();
const elements = useElements();
const nodeServer = process.env.GATSBY_HANDLE_PAYMENT_INTENT_URL;
useEffect(() => {
// Create PaymentIntent as soon as the page loads
window
.fetch(nodeServer + "/create-payment-intent", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(
{
items: [
{
id: 5,
name: "Example Product"
},
{
id: 6,
name: "Another Test Product"
}
]
}
)
})
.then(res => {
return res.json();
})
.then(data => {
setClientSecret(data.clientSecret);
});
}, []);
const cardStyle = {
style: {
base: {
color: "#32325d",
fontFamily: 'Arial, sans-serif',
fontSmoothing: "antialiased",
fontSize: "16px",
"::placeholder": {
color: "#32325d"
}
},
invalid: {
color: "#fa755a",
iconColor: "#fa755a"
}
}
};
const handleChange = async (event) => {
// Listen for changes in the CardElement
// and display any errors as the customer types their card details
setDisabled(event.empty);
setError(event.error ? event.error.message : "");
};
const handleSubmit = async ev => {
// Ensure we don't submit the form
ev.preventDefault();
setProcessing(true);
const payload = await stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: elements.getElement(CardElement),
billing_details: {
name: ev.target.name.value
}
}
});
if (payload.error) {
setError(`Payment failed: ${payload.error.message}`);
setProcessing(false);
} else {
// All is well, let's move forward and update the UI
setError(null);
setProcessing(false);
setSucceeded(true);
}
};
return (
<form id="payment-form" onSubmit={handleSubmit}>
<CardElement id="card-element" options={cardStyle} onChange={handleChange} />
<button
disabled={processing || disabled || succeeded}
id="submit"
>
<span id="button-text">
{processing ? (
<Spinner animation="border" role="status">
<span className="sr-only">Loading...</span>
</Spinner>
) : (
"Pay Now"
)}
</span>
</button>
{/* Show any error that happens when processing the payment */}
{error && (
<div className="result-message card-error" role="alert">
<Alert variant="warning">
{error}
</Alert>
</div>
)}
{/* Show a success message upon completion */}
<div className={succeeded ? "result-message" : "result-message hidden"} role="alert">
<Alert variant="success">
Payment processed successfully!
</Alert>
</div>
</form>
);
}