1
+ // This is your test publishable API key.
2
+ const stripe = Stripe ( "pk_test_51Q3BCQC06XYmpb7G4NfpCp7RfBau4A65hH9mPn5FRpBL0aXd2Fx9PggDxE3zBouWte7hyFViwLXMcOzsHOpydKEC00LRpAdUHQ" ) ;
3
+
4
+ // The items the customer wants to buy
5
+ const items = [ { id : "xl-tshirt" , amount : 1000 } ] ;
6
+
7
+ let elements ;
8
+
9
+ initialize ( ) ;
10
+
11
+ document
12
+ . querySelector ( "#payment-form" )
13
+ . addEventListener ( "submit" , handleSubmit ) ;
14
+
15
+ // Fetches a payment intent and captures the client secret
16
+ async function initialize ( ) {
17
+ const response = await fetch ( "/create-payment-intent" , {
18
+ method : "POST" ,
19
+ headers : { "Content-Type" : "application/json" } ,
20
+ body : JSON . stringify ( { items } ) ,
21
+ } ) ;
22
+ const { clientSecret, dpmCheckerLink } = await response . json ( ) ;
23
+
24
+ const appearance = {
25
+ theme : 'stripe' ,
26
+ } ;
27
+ elements = stripe . elements ( { appearance, clientSecret } ) ;
28
+
29
+ const paymentElementOptions = {
30
+ layout : "tabs" ,
31
+ } ;
32
+
33
+ const paymentElement = elements . create ( "payment" , paymentElementOptions ) ;
34
+ paymentElement . mount ( "#payment-element" ) ;
35
+
36
+ // [DEV] For demo purposes only
37
+ setDpmCheckerLink ( dpmCheckerLink ) ;
38
+ }
39
+
40
+ async function handleSubmit ( e ) {
41
+ e . preventDefault ( ) ;
42
+ setLoading ( true ) ;
43
+
44
+ const { error } = await stripe . confirmPayment ( {
45
+ elements,
46
+ confirmParams : {
47
+ // Make sure to change this to your payment completion page
48
+ return_url : "http://localhost:4242/complete.html" ,
49
+ receipt_email : document . getElementById ( "email" ) . value ,
50
+ } ,
51
+ } ) ;
52
+
53
+ // This point will only be reached if there is an immediate error when
54
+ // confirming the payment. Otherwise, your customer will be redirected to
55
+ // your `return_url`. For some payment methods like iDEAL, your customer will
56
+ // be redirected to an intermediate site first to authorize the payment, then
57
+ // redirected to the `return_url`.
58
+ if ( error . type === "card_error" || error . type === "validation_error" ) {
59
+ showMessage ( error . message ) ;
60
+ } else {
61
+ showMessage ( "An unexpected error occurred." ) ;
62
+ }
63
+
64
+ setLoading ( false ) ;
65
+ }
66
+
67
+ // ------- UI helpers -------
68
+
69
+ function showMessage ( messageText ) {
70
+ const messageContainer = document . querySelector ( "#payment-message" ) ;
71
+
72
+ messageContainer . classList . remove ( "hidden" ) ;
73
+ messageContainer . textContent = messageText ;
74
+
75
+ setTimeout ( function ( ) {
76
+ messageContainer . classList . add ( "hidden" ) ;
77
+ messageContainer . textContent = "" ;
78
+ } , 4000 ) ;
79
+ }
80
+
81
+ // Show a spinner on payment submission
82
+ function setLoading ( isLoading ) {
83
+ if ( isLoading ) {
84
+ // Disable the button and show a spinner
85
+ document . querySelector ( "#submit" ) . disabled = true ;
86
+ document . querySelector ( "#spinner" ) . classList . remove ( "hidden" ) ;
87
+ document . querySelector ( "#button-text" ) . classList . add ( "hidden" ) ;
88
+ } else {
89
+ document . querySelector ( "#submit" ) . disabled = false ;
90
+ document . querySelector ( "#spinner" ) . classList . add ( "hidden" ) ;
91
+ document . querySelector ( "#button-text" ) . classList . remove ( "hidden" ) ;
92
+ }
93
+ }
94
+
95
+ function setDpmCheckerLink ( url ) {
96
+ document . querySelector ( "#dpm-integration-checker" ) . href = url ;
97
+ }
0 commit comments