#johnl_code

1 messages ยท Page 1 of 1 (latest)

lofty bladeBOT
north solsticeBOT
#

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

lofty bladeBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1260018382741438576

๐Ÿ“ Have more to share? Add details, code, screenshots, videos, etc. below.

limpid fjord
#

I am trying to send info to the server after the successful payment (name and email).

I heard that I can insert this:

         method: 'POST',
         headers: {
           'Content-Type': 'application/json',
         },
         body: JSON.stringify({
           paymentIntentId: paymentIntent.id,
           additionalData: 'Any additional data you want to send',
         }),
       });```


However, this code cannot be reached because "payment_intent_client_secret" is not appended to the URL like it was supposed to? ( console.log(clientSecret) prints null)
#

Most of my code is pre-made btw, I am just trying to modify it for my needs

hot viper
#

The name and email are your own web form?

limpid fjord
hot viper
#

And you're asking how to send the info from it to the back-end?

limpid fjord
#

correct

#

I need to fund their account by the amount

hot viper
#

When you submit the Payment Element, are you also making sure the name/email is included in the same HTML form element? So that it also gets submitted?

hot viper
#

Okay, and in your submit handler, are you able to console.log() the values for paymentIntentId and additionalData?

lofty bladeBOT
limpid fjord
# hot viper Okay, and in your submit handler, are you able to console.log() the values for `...

This is the handler:

  e.preventDefault();
  setLoading(true);


  const response = await fetch("/create-payment-intent", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      cost
    }),
  });

  const { clientSecret } = await response.json();

  const { error } = await stripe.confirmPayment({
    elements,

    redirect: 'if_required',
  });

  if (error) {
    if (error.type === "card_error" || error.type === "validation_error") {
      showMessage(error.message);
    } else {
      showMessage("An unexpected error occurred.");
    }
    setLoading(false);
  }
}
#

There is no paymentIntentId or additionalData

#

This is commented out btw, I want to make sure the code is reachable first

#

(it isn't reachable yet)

little swan
#

๐Ÿ‘‹ Taking over this thread, catching up now

#

In confirmPayment() function, the redirect is set to if_required. If the payment method used isn't a redirect payment method, you won't be redirected to the return_url and payment_intent_client_secret won't be available

limpid fjord
#

So you mean do this?

#

Or like, something like this

little swan
limpid fjord
#

I ended up creating a new page fo redirect

#

And I passed the client secret into the url params

#

And now I get this

#

I now have the clientsecret key, but retreivePaymentIntent does not work

little swan
#

stripe.retrievePaymentIntent() is a promise. You either use await or use promise to access the payment intent

#

For example,

// Retrieve the PaymentIntent
stripe
  .retrievePaymentIntent(clientSecret)
  .then(({paymentIntent}) => {
    switch (paymentIntent.status) {
      ....
    });
  });
limpid fjord
#

Got it, I made it a promise and now it works!

#

Just one thing...

#

On the server side, can I get the cost from the payment intent id?

#

Obvioudly I don't want to pass the cost through the additional information because then the user can just edit it

little swan
#

You can get the amount information from there

limpid fjord
#

Wait

#

Sure, but how do I get a PaymentIntent object in the first place

#

From the id

#

I got it

#

Okay, thanks for all your help!!!