Skip to content

Commit 1518ff5

Browse files
committed
StripeCheckout get clientSecret
1 parent 60c1e2a commit 1518ff5

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

functions/create-payment-intent.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//domain: .nrtlify/functions/create-payment-intent
2-
const dotenv = require('dotenv')
3-
dotenv.config()
1+
require('dotenv').config()
2+
3+
const stripe=require("stripe")(process.env.REACT_APP_STRIPE_SEC)
44

55

66
exports.handler = async function (event, context) {
@@ -15,10 +15,27 @@ exports.handler = async function (event, context) {
1515

1616
console.log('Received data:', { cart, shipping_fee, total_amount });
1717

18-
return {
19-
statusCode: 200,
20-
body: 'Payment intent created successfully'
18+
const calculateOrderAmount = () => {
19+
return shipping_fee + total_amount;
2120
};
21+
22+
try {
23+
const paymentIntent = await stripe.paymentIntents.create({
24+
amount: calculateOrderAmount(),
25+
currency: "usd"
26+
});
27+
28+
return {
29+
statusCode: 200,
30+
body: JSON.stringify({ clientSecret: paymentIntent.client_secret })
31+
};
32+
} catch (error) {
33+
console.log('Error:', error);
34+
return {
35+
statusCode: 500,
36+
body: 'Error creating payment intent'
37+
};
38+
}
2239
} catch (error) {
2340
console.log('Error:', error);
2441
return {
@@ -27,5 +44,3 @@ exports.handler = async function (event, context) {
2744
};
2845
}
2946
};
30-
31-

package-lock.json

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
"react-icons": "^4.7.1",
1818
"react-router-dom": "^6.10.0",
1919
"react-scripts": "5.0.1",
20+
"stripe": "^12.0.0",
2021
"web-vitals": "^2.1.4"
2122
},
2223
"scripts": {
2324
"start": "react-scripts start",
24-
"build": "CLI= react-scripts build",
25+
"build": "CI= react-scripts build",
2526
"test": "react-scripts test",
2627
"eject": "react-scripts eject",
2728
"netlify": "netlify-dev",

src/components/StripeCheckout.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const CheckoutForm = () => {
4242
{ cart, shipping_fee, total_amount},
4343
{ headers: { 'Content-Type': 'application/json' } }
4444
);
45+
setClientSecret(data.clientSecret);
4546
console.log(data);
4647
return data
4748
} catch (error) {
@@ -52,7 +53,7 @@ const CheckoutForm = () => {
5253

5354
React.useEffect(() => {
5455
createPaymentIntent();
55-
},[createPaymentIntent]);
56+
},[]);
5657

5758
const handleChange = (event) => {};
5859

0 commit comments

Comments
 (0)