#cgourmelon

1 messages · Page 1 of 1 (latest)

edgy grottoBOT
livid leaf
#

Hi! What's the question?

primal lichen
#

Hi Vanya, I'm tryong to post some metadata coming from my checkout page

#

I'm following this tutorial

#

Everything works fine except for my custom inputs on my checkout page which I try to pass using stripe.confirmPayment

#

Here's my code :

#

// Confirm the PaymentIntent using the details collected by the Payment Element
const {error} = await stripe.confirmPayment({
elements,
clientSecret,
confirmParams: {
return_url: 'https://boutique.pompesfunebresdefrance.com/merci-2',
payment_method_data : {
billing_details : {
name : form.pff_fleurs_commandes_client_nom.value,
email : form.pff_fleurs_commandes_client_email.value,
phone : form.pff_fleurs_commandes_client_telephone.value,
address: {
line1: form.pff_fleurs_commandes_client_adresse_1.value,
line2: form.pff_fleurs_commandes_client_adresse_2.value,
city: form.pff_fleurs_commandes_client_ville.value,
postal_code: form.pff_fleurs_commandes_client_cp.value
}
},
metadata : {
'email' : form.pff_fleurs_commandes_client_email.value,
'nom' : form.pff_fleurs_commandes_client_nom.value,
'prenom' : form.pff_fleurs_commandes_client_prenom.value,
'address1' : form.pff_fleurs_commandes_client_adresse_1.value,
'address2' : form.pff_fleurs_commandes_client_adresse_2.value,
'city' : form.pff_fleurs_commandes_client_ville.value,
'postal_code' : form.pff_fleurs_commandes_client_cp.value
}
}
},
});

#

The billing details are coming through properly but not my metadata

#

Am I doing something wrong ?

livid leaf
#

Could you please share an example PaymentIntent ID? pi_xxx

#

Also, where do you expect the metadata to be available?

primal lichen
#

pi_3Mr1RpBCoEFG98lq1e7uMssB

#

I'm hoping to have it in the response of a payment_intent.succeeded

#

"latest_charge": "ch_3Mr1RpBCoEFG98lq1Xc4J3wE",
"livemode": false,
"metadata": {
},

#

in the metadata field

#

So I can retrieve the info on a webhook using for instance : $pff_fleurs_commandes_client_prenom = $event->data->object->metadata->prenom;

livid leaf
#

You added the metadata to the payment_method_data so it lives on your Payment Method object.

primal lichen
#

Should I put it somewhere else then ?

livid leaf
#

You can send the metadata to your backend and add it when you create your PaymentIntent.

primal lichen
#

Do you have a piece of code I can study to see how it works ?

livid leaf
#

Maybe you could share your current implementation and I will suggest where the metadata could live?

primal lichen
#

I'm also tryong to push my metadata to the create.php file that creates the PaymentIntent and obtain clientSecret but I can't find a way to make it work

edgy grottoBOT
livid leaf
#

So when you do:

const res = await fetch("/create.php", {
You can send your metadata along.

#

How does your backend look?

primal lichen
#

Ok but how 😅

#

There is no backend.

livid leaf
#

So where's this request going? /create.php

primal lichen
#

The data is suppose to go to Stripe, then I'm using a webhook to create an order with all the info needed

#

(There is no CMS, it's just pure code)

#

Honestly implementing our current bank system and Paypal was a piece of cake, but implementing Stripe is really difficult 😅

livid leaf
#

That's your backend 😉
You can put your metadata in:
$intent = $stripe->paymentIntents->create(, along with your order_id

primal lichen
#

In the create.php file you mean ?

livid leaf
#

Yes

primal lichen
#

But how do I push my input values of the form to this file ?

#

This is not working :

#

var metadata = [{
email: document.getElementById("pff_fleurs_commandes_client_email").value,
name: document.getElementById("pff_fleurs_commandes_client_nom").value,
address: document.getElementById("pff_fleurs_commandes_client_adresse_1").value,
city: document.getElementById("pff_fleurs_commandes_client_ville").value,
postalCode: document.getElementById("pff_fleurs_commandes_client_cp").value
}];

const data = new FormData(event.target);
const formJSON = Object.fromEntries(data.entries());

// Create the PaymentIntent and obtain clientSecret
const res = await fetch("/create.php", {
method: "POST",
body: JSON.stringify({ metadata }),
});

livid leaf
#

What's not working?

primal lichen
#

The Json file created this way only contains the clientSecret, and not any metadada

livid leaf
# primal lichen But how do I push my input values of the form to this file ?

The response contains the clientSecret. But you can put whatever you want in the request body of the /create.php request. This part is not related to Stripe in any way so you can do whatever you like here.
You can send all the necessary data from the form to /create.php endpint, and add it to the metadata when you create your PaymentIntent on the backend.

primal lichen
#

I don't know how to send the data from the form to this file, this is where I struggle

livid leaf
#

I see you're already doing this:

var metadata = [{
    email: document.getElementById("pff_fleurs_commandes_client_email").value,
    name: document.getElementById("pff_fleurs_commandes_client_nom").value,
    address: document.getElementById("pff_fleurs_commandes_client_adresse_1").value,
    city: document.getElementById("pff_fleurs_commandes_client_ville").value,
    postalCode: document.getElementById("pff_fleurs_commandes_client_cp").value
    }];
const res = await fetch("/create.php", {
    method: "POST",
    body: JSON.stringify({ metadata }),
  });
primal lichen
#

Ok I'm getting close. I've manage to push my data to the payload, now I need to push it back in response.

#

If you can bear with me a little longer while I'm doing this it would be great

deft cargo
#

Sure happy to help. Vanya had to step out but I am here. Do you have current questions or are you saying that you are actively testing this now and will come back with further questions if they come up?

livid leaf
primal lichen
#

To put it in a variable $metadata

#

So I can do this

#

'amount' => $total_panier,
'currency' => 'eur',
'automatic_payment_methods' => ['enabled' => true],
'metadata' => $metadata

livid leaf
#

Sure, sorry, I misunderstood. That's right.

#

You meant you want to read it from the request body in the PHP file and send it to Stripe when creating the PaymentIntent, correct?

primal lichen
#

Yes exactely

primal lichen
#

Ok, I think I've done it !

#

Can you check the payment pi_3Mso7GBCoEFG98lq0cwrxXeL

livid leaf
#

Nice. I see 2 fields in the metadata field on this PaymentIntent. Is this what you expected?

primal lichen
#

Yep 🥹

#

Ok now that this is settle I have some follow up questions if you don't mind

#
  • Can I push as many data in this metadata field or is it limitated
#
  • What I am buidling, is this the right way to go, that is to say that the order is created thax to the webhook after the success payment or it would be wiser to creted it before, and to update it with the webhook
primal lichen
#

(hope I make sense 😅 )

livid leaf
#
  1. Either way works. Just make sure to clearly mark the order as incomplete (in your DB) until you get the payment_intent.succeeded webhook.
primal lichen
#

Do I need to redirect a user if the payment was unsuccesful or he will stay in the checkout page with an error message ?

#

(redirect to a page that says "sorry try again")

livid leaf
primal lichen
#

Perfect thx. I've just simulated an error and there's an error message indeed which appeard. Any way to custom these messages ?

livid leaf
#

It just depends on what you do in the handleError(error) function, and it just depends on you at this point, not related to Stripe.

primal lichen
#

Ok thx. Well Vanya you've been of great help, thank you for everything !