#yen6305
1 messages · Page 1 of 1 (latest)
Hello
Hii
You are using Payment Element here?
Yes
I use the payment element
I have my webhook file all set up and tested the incoming event.types and it works as expected
But my event.data.object contains data but the metadata object is empty
Which Event type are you listening for?
charge.succeeded
Can you provide me an example Event ID?
Eh, where can I find the event id?
You can provide the Charge ID that is within the object JSON if that is easier
Otherwise you can find the Event ID via https://dashboard.stripe.com/test/events
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Thanks looking
Thankyou
Okay so you didn't actually pass any metadata when you created that PaymentIntent: https://dashboard.stripe.com/test/logs/req_BdYgAWwUe1lIZz
No since metadata is sensitive we don't support passing it client-side as that would be a security risk.
You would want to update your PaymentIntent server-side just before confirmation if you aren't going to know your metadata until that point.
Well I am already doing this in my code:
Checkout.js
let paymentIntent = await createPaymentIntentApi(cart.key).catch(
(error) => {
setMessage(error.message);
setIsLoading(false);
return;
}
);
functions.js
export const createPaymentIntentApi = async (cartKey) => {
try {
const response = await fetch("/api/stripe/create-payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ cartKey: cartKey }),
});
if (!response.ok) {
throw new Error("Failed to create payment intent.");
}
const responseData = await response.json();
return responseData;
} catch (error) {
throw new Error(err);
}
}
and /api/stripe/create-payment-intent code is handled on the server side (im using Next.js, so everything in the api folder is server-side)
So is it okay to pass the metadata in createPaymentIntentApi?
Yep
I've tried this:
export const createPaymentIntentApi = async (cartKey) => {
try {
const response = await fetch("/api/stripe/create-payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ cartKey: cartKey, metadata: { orderId: 404040} }),
});
Is this correct? Because metadata is still empty
This is the create-payment-intent.js
👋 hopping in here since bismarck had to head out
// The Payment intent API tracks a payment from creation through checkout, and triggers additional authentication steps when required.
// // This is your test secret API key.
import axios from 'axios'; // Import Axios
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const fetchCartData = async (cartKey) => {
try {
const response = await axios.get(
`${process.env.NEXT_PUBLIC_WP_API_URL}/wp-json/cocart/v2/cart?cart_key=${cartKey}`
);
return response.data;
} catch (error) {
throw new Error("Error fetching cart data: " + error.message);
}
};
export default async function handler(req, res) {
const { cartKey } = req.body;
if (!cartKey) {
throw new Error("Cart key is missing.");
}
// Fetch the cart data using the CoCart Lite API
// const cartData = await fetchCartData(cartKey);
// Fetch the cart data using the CoCart Lite API
const cartData = await fetchCartData(cartKey);
// Calculate the total amount based on line items in the cart data (items is seen as a line item)
// Line item is an item that has been placed in a cart. In this case only the SSG - Striking Strings Glissando is in the cart with a quantity.
const totalAmount = cartData.items.reduce((accumulator, item) => {
const quantity = item.quantity.value;
const price = parseFloat(item.price);
return accumulator + quantity * price;
}, 0);
const paymentIntent = await stripe.paymentIntents.create({
amount: totalAmount,
currency: "eur",
// payment_method_types: ['card', 'ideal'],
// capture_method: 'manual',
automatic_payment_methods: {
enabled: true,
},
});
res.send({
clientSecret: paymentIntent.client_secret,
paymentIntentId: paymentIntent.id,
cartKey: cartKey,
});
};
That's okay
Your server-side code isn't actually using the metadata - you need to nmake sure to pass it to your call to await stripe.paymentIntents.create
I don't have a stripe.paymentIntents.create
And I use the Payment element
Oh wait, i didnt use that guide excuse me
It's right there in the code you sent over-
const paymentIntent = await stripe.paymentIntents.create({
amount: totalAmount,
currency: "eur",
// payment_method_types: ['card', 'ideal'],
// capture_method: 'manual',
automatic_payment_methods: {
enabled: true,
},
});