Skip to content

Commit 6daffdc

Browse files
Add Vite cacheDir for parallel testing, document DOMAIN for React, add billing address element (stripe-samples#3095)
Fixes: Added Vite cacheDir for parallel testing, document DOMAIN for React, and vertically centered html
1 parent 47fa6cc commit 6daffdc

13 files changed

Lines changed: 90 additions & 12 deletions

File tree

custom-payment-flow/client/react-cra/vite.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineConfig({
88
server: {
99
port: 3000,
1010
host: "0.0.0.0",
11+
allowedHosts: ["frontend", "localhost", "web"],
1112
proxy: {
1213
'/api': {
1314
target: 'http://127.0.0.1:4242',

elements-with-checkout-sessions/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ STRIPE_PUBLISHABLE_KEY=<replace-with-your-publishable-key>
22
STRIPE_SECRET_KEY=<replace-with-your-secret-key>
33
STRIPE_WEBHOOK_SECRET=<replace-with-your-webhook-secret>
44
STATIC_DIR=../../client/html
5+
# For React/Vue clients, set to the dev server URL (e.g. http://localhost:3000)
56
DOMAIN=http://localhost:4242
67
PORT=4242

elements-with-checkout-sessions/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,11 @@ The dev server starts on port 3000 and proxies `/api` requests to
158158
`http://127.0.0.1:4242`. Navigate to
159159
[http://localhost:3000](http://localhost:3000) to see the payment form.
160160
Make sure a backend server is running on port 4242 first.
161+
162+
**Important:** Set `DOMAIN` to your Vite dev server URL so Stripe
163+
redirects back to the React app after payment:
164+
165+
```bash
166+
# In your server's .env
167+
DOMAIN=http://localhost:3000
168+
```

elements-with-checkout-sessions/client/html/index.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ body {
99
display: flex;
1010
flex-direction: column;
1111
justify-content: center;
12-
align-content: center;
12+
align-items: center;
13+
height: 100vh;
14+
width: 100vw;
1315
}
1416

1517
form {
@@ -42,6 +44,11 @@ form {
4244
margin-top: 16px;
4345
}
4446

47+
#address-element {
48+
margin-bottom: 24px;
49+
margin-top: 16px;
50+
}
51+
4552
#email {
4653
border-radius: 5px;
4754
box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.03), 0px 3px 6px rgba(0, 0, 0, 0.02);

elements-with-checkout-sessions/client/html/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<input type="email" id="email"
1818
/></label>
1919
<div id="email-errors"></div>
20+
<h4>Billing address</h4>
21+
<div id="address-element">
22+
<!--Stripe.js injects the Address Element-->
23+
</div>
2024
<h4>Payment</h4>
2125
<div id="payment-element">
2226
<!--Stripe.js injects the Payment Element-->

elements-with-checkout-sessions/client/html/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ async function initialize() {
7272
}
7373
});
7474

75+
const addressElement = checkout.createBillingAddressElement();
76+
addressElement.mount("#address-element");
77+
7578
const paymentElement = checkout.createPaymentElement();
7679
paymentElement.mount("#payment-element");
7780
}

elements-with-checkout-sessions/client/react-cra/src/App.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ form {
4040
margin-top: 16px;
4141
}
4242

43+
#address-element {
44+
margin-bottom: 24px;
45+
margin-top: 16px;
46+
}
47+
4348
#email {
4449
border-radius: 5px;
4550
box-shadow: 0px 1px 1px rgba(0, 0, 0, 0.03), 0px 3px 6px rgba(0, 0, 0, 0.02);

elements-with-checkout-sessions/client/react-cra/src/CheckoutForm.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState } from "react";
22
import {
33
PaymentElement,
4+
BillingAddressElement,
45
useCheckout
56
} from '@stripe/react-stripe-js/checkout';
67

@@ -104,6 +105,8 @@ const CheckoutForm = () => {
104105
error={emailError}
105106
setError={setEmailError}
106107
/>
108+
<h4>Billing address</h4>
109+
<BillingAddressElement id="address-element" />
107110
<h4>Payment</h4>
108111
<PaymentElement id="payment-element" />
109112
<button disabled={!checkoutState.checkout.canConfirm || isSubmitting} id="submit">

elements-with-checkout-sessions/client/react-cra/vite.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default defineConfig({
1717
}
1818
}
1919
},
20+
cacheDir: process.env.VITE_CACHE_DIR || 'node_modules/.vite',
2021
build: {
2122
outDir: "build",
2223
},

elements-with-checkout-sessions/testing/manual-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ REACT_DIR="$SAMPLE_DIR/client/react-cra"
134134

135135
for i in $(seq 0 6); do
136136
VITE_SERVER_URL="http://localhost:$((4242 + i))" \
137+
VITE_CACHE_DIR="/tmp/vite-cache-$((3000 + i))" \
137138
PORT=$((3000 + i)) \
138139
npx --prefix "$REACT_DIR" vite --config "$REACT_DIR/vite.config.mjs" "$REACT_DIR" >/dev/null 2>&1 &
139140
PIDS+=($!)

0 commit comments

Comments
 (0)