#Aimer - Fronted + Backend
1 messages · Page 1 of 1 (latest)
i watch a youtube videos
from your channel
which doesnt really use that much of the node js
i also once did a payment method which use node backend
my code is like this
where the payment is done from express server
is there any different
The image you shared is focused on the front-end part
for the one in the video
its this
But the payment intent will still need to be generated on the server
he only have this in the server
just the price
import Stripe from "stripe";
const stripe = new Stripe(process.env.SECRET_KEY);
export default async (req, res) => {
if (req.method === "POST") {
try {
const { amount } = req.body;
// Psst. For production-ready applications we recommend not using the
// amount directly from the client without verifying it first. This is to
// prevent bad actors from changing the total amount on the client before
// it gets sent to the server. A good approach is to send the quantity of
// a uniquely identifiable product and calculate the total price server-side.
// Then, you would only fulfill orders using the quantity you charged for.
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency: "usd"
});
res.status(200).send(paymentIntent.client_secret);
} catch (err) {
res.status(500).json({ statusCode: 500, message: err.message });
}
} else {
res.setHeader("Allow", "POST");
res.status(405).end("Method Not Allowed");
}
};
© 2022 GitHub, Inc.
Terms
This is the front end part that
const handleFormSubmit = async ev => {
ev.preventDefault();
const billingDetails = {
name: ev.target.name.value,
email: ev.target.email.value,
address: {
city: ev.target.city.value,
line1: ev.target.address.value,
state: ev.target.state.value,
postal_code: ev.target.zip.value
}
};
setProcessingTo(true);
const cardElement = elements.getElement("card");
try {
const { data: clientSecret } = await axios.post("/api/payment_intents", {
amount: price * 100
});
const paymentMethodReq = await stripe.createPaymentMethod({
type: "card",
card: cardElement,
billing_details: billingDetails
});
if (paymentMethodReq.error) {
setCheckoutError(paymentMethodReq.error.message);
setProcessingTo(false);
return;
}
const { error } = await stripe.confirmCardPayment(clientSecret, {
payment_method: paymentMethodReq.paymentMethod.id
});
if (error) {
setCheckoutError(error.message);
setProcessingTo(false);
return;
}
onSuccessfulCheckout();
} catch (err) {
setCheckoutError(err.message);
}
};
he only use the backend to generate a client_secret which will be process on the front end
i use other npm package called for react which is called react-stripe-checkout
it has different style of handling payment
Okay so what's the question?
The backend snippet generates a charge using a Payment Intent. It sounds like your code uses the Stripe Checkout Session
whats the different
You can review both approaches (with examples of the front-end and back-end code) here: https://stripe.com/docs/payments/accept-a-payment
👍
The price ID is an ID of a price record in your Stripe account. For example:
You can either find them in your dashboard or via the API:
https://stripe.com/docs/api/prices
ty