1
- import React , { useState } from 'react'
1
+ import React , { useState } from 'react' ;
2
2
3
- import CustomDonationInput from '../components/CustomDonationInput'
4
- import PrintObject from '../components/PrintObject'
3
+ import CustomDonationInput from '../components/CustomDonationInput' ;
4
+ import PrintObject from '../components/PrintObject' ;
5
5
6
- import { fetchPostJSON } from '../utils/api-helpers'
7
- import { formatAmountForDisplay } from '../utils/stripe-helpers'
8
- import * as config from '../config'
6
+ import { fetchPostJSON } from '../utils/api-helpers' ;
7
+ import { formatAmountForDisplay } from '../utils/stripe-helpers' ;
8
+ import * as config from '../config' ;
9
9
10
- import { CardElement , useStripe , useElements } from '@stripe/react-stripe-js'
10
+ import { CardElement , useStripe , useElements } from '@stripe/react-stripe-js' ;
11
11
12
12
const CARD_OPTIONS = {
13
+ iconStyle : 'solid' as const ,
13
14
style : {
14
15
base : {
15
16
iconColor : '#6772e5' ,
@@ -19,102 +20,102 @@ const CARD_OPTIONS = {
19
20
fontSize : '16px' ,
20
21
fontSmoothing : 'antialiased' ,
21
22
':-webkit-autofill' : {
22
- color : '#fce883' ,
23
+ color : '#fce883'
23
24
} ,
24
25
'::placeholder' : {
25
- color : '#6772e5' ,
26
- } ,
26
+ color : '#6772e5'
27
+ }
27
28
} ,
28
29
invalid : {
29
30
iconColor : '#ef2961' ,
30
- color : '#ef2961' ,
31
- } ,
32
- } ,
33
- }
31
+ color : '#ef2961'
32
+ }
33
+ }
34
+ } ;
34
35
35
36
const ElementsForm : React . FunctionComponent = ( ) => {
36
37
const [ input , setInput ] = useState ( {
37
38
customDonation : Math . round ( config . MAX_AMOUNT / config . AMOUNT_STEP ) ,
38
- cardholderName : '' ,
39
- } )
40
- const [ payment , setPayment ] = useState ( { status : 'initial' } )
41
- const [ errorMessage , setErrorMessage ] = useState ( '' )
42
- const stripe = useStripe ( )
43
- const elements = useElements ( )
39
+ cardholderName : ''
40
+ } ) ;
41
+ const [ payment , setPayment ] = useState ( { status : 'initial' } ) ;
42
+ const [ errorMessage , setErrorMessage ] = useState ( '' ) ;
43
+ const stripe = useStripe ( ) ;
44
+ const elements = useElements ( ) ;
44
45
45
46
const PaymentStatus = ( { status } : { status : string } ) => {
46
47
switch ( status ) {
47
48
case 'processing' :
48
49
case 'requires_payment_method' :
49
50
case 'requires_confirmation' :
50
- return < h2 > Processing...</ h2 >
51
+ return < h2 > Processing...</ h2 > ;
51
52
52
53
case 'requires_action' :
53
- return < h2 > Authenticating...</ h2 >
54
+ return < h2 > Authenticating...</ h2 > ;
54
55
55
56
case 'succeeded' :
56
- return < h2 > Payment Succeeded 🥳</ h2 >
57
+ return < h2 > Payment Succeeded 🥳</ h2 > ;
57
58
58
59
case 'error' :
59
60
return (
60
61
< >
61
62
< h2 > Error 😭</ h2 >
62
63
< p className = "error-message" > { errorMessage } </ p >
63
64
</ >
64
- )
65
+ ) ;
65
66
66
67
default :
67
- return null
68
+ return null ;
68
69
}
69
- }
70
+ } ;
70
71
71
72
const handleInputChange : React . ChangeEventHandler < HTMLInputElement > = e =>
72
73
setInput ( {
73
74
...input ,
74
- [ e . currentTarget . name ] : e . currentTarget . value ,
75
- } )
75
+ [ e . currentTarget . name ] : e . currentTarget . value
76
+ } ) ;
76
77
77
78
const handleSubmit : React . FormEventHandler < HTMLFormElement > = async e => {
78
- e . preventDefault ( )
79
+ e . preventDefault ( ) ;
79
80
// Abort if form isn't valid
80
- if ( ! e . currentTarget . reportValidity ( ) ) return
81
- setPayment ( { status : 'processing' } )
81
+ if ( ! e . currentTarget . reportValidity ( ) ) return ;
82
+ setPayment ( { status : 'processing' } ) ;
82
83
83
84
// Create a PaymentIntent with the specified amount.
84
85
const response = await fetchPostJSON ( '/api/payment_intents' , {
85
- amount : input . customDonation ,
86
- } )
87
- setPayment ( response )
86
+ amount : input . customDonation
87
+ } ) ;
88
+ setPayment ( response ) ;
88
89
89
90
if ( response . statusCode === 500 ) {
90
- setPayment ( { status : 'error' } )
91
- setErrorMessage ( response . message )
92
- return
91
+ setPayment ( { status : 'error' } ) ;
92
+ setErrorMessage ( response . message ) ;
93
+ return ;
93
94
}
94
95
95
96
// Get a reference to a mounted CardElement. Elements knows how
96
97
// to find your CardElement because there can only ever be one of
97
98
// each type of element.
98
- const cardElement = elements ! . getElement ( CardElement )
99
+ const cardElement = elements ! . getElement ( CardElement ) ;
99
100
100
101
// Use your card Element with other Stripe.js APIs
101
102
const { error, paymentIntent } = await stripe ! . confirmCardPayment (
102
103
response . client_secret ,
103
104
{
104
105
payment_method : {
105
106
card : cardElement ! ,
106
- billing_details : { name : input . cardholderName } ,
107
- } ,
107
+ billing_details : { name : input . cardholderName }
108
+ }
108
109
}
109
- )
110
+ ) ;
110
111
111
112
if ( error ) {
112
- setPayment ( { status : 'error' } )
113
- setErrorMessage ( error . message ?? 'An unknown error occured' )
113
+ setPayment ( { status : 'error' } ) ;
114
+ setErrorMessage ( error . message ?? 'An unknown error occured' ) ;
114
115
} else if ( paymentIntent ) {
115
- setPayment ( paymentIntent )
116
+ setPayment ( paymentIntent ) ;
116
117
}
117
- }
118
+ } ;
118
119
119
120
return (
120
121
< >
@@ -144,8 +145,10 @@ const ElementsForm: React.FunctionComponent = () => {
144
145
options = { CARD_OPTIONS }
145
146
onChange = { e => {
146
147
if ( e . error ) {
147
- setPayment ( { status : 'error' } )
148
- setErrorMessage ( e . error . message ?? 'An unknown error occured' )
148
+ setPayment ( { status : 'error' } ) ;
149
+ setErrorMessage (
150
+ e . error . message ?? 'An unknown error occured'
151
+ ) ;
149
152
}
150
153
} }
151
154
/>
@@ -165,7 +168,7 @@ const ElementsForm: React.FunctionComponent = () => {
165
168
< PaymentStatus status = { payment . status } />
166
169
< PrintObject content = { payment } />
167
170
</ >
168
- )
169
- }
171
+ ) ;
172
+ } ;
170
173
171
- export default ElementsForm
174
+ export default ElementsForm ;
0 commit comments