#yen6305

1 messages · Page 1 of 1 (latest)

runic kiteBOT
rain flicker
#

Hello

blazing urchin
#

Hii

rain flicker
#

You are using Payment Element here?

blazing urchin
#

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

rain flicker
#

Which Event type are you listening for?

blazing urchin
#

charge.succeeded

rain flicker
#

Can you provide me an example Event ID?

blazing urchin
#

Eh, where can I find the event id?

rain flicker
#

You can provide the Charge ID that is within the object JSON if that is easier

blazing urchin
#

pi_3NiJYgD7Shm9DtKO05UtxfHF

#

Is the most recent one

rain flicker
#

Thanks looking

blazing urchin
#

Thankyou

rain flicker
runic kiteBOT
blazing urchin
#

Oh

#

Is it also possible to pass the metadata in stripe.confirmPayment?

rain flicker
#

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.

blazing urchin
#

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?

rain flicker
#

Yep

blazing urchin
#

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

blazing fossil
#

👋 hopping in here since bismarck had to head out

blazing urchin
#
// 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

blazing fossil
#

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

blazing urchin
#

I don't have a stripe.paymentIntents.create

#

And I use the Payment element

#

Oh wait, i didnt use that guide excuse me

blazing fossil
#

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,
      },
    });
blazing urchin
#

Ohh this

#

Im sorry