#cgourmelon
1 messages · Page 1 of 1 (latest)
Hi! What's the question?
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
}
}
},
});
Service de livraison de Fleurs en cérémonie - Remise en mains propres au maître de cérémonie - Fleurs Frâiches et de saisons - POMPES FUNÈBRES DE FRANCE
The billing details are coming through properly but not my metadata
Am I doing something wrong ?
Could you please share an example PaymentIntent ID? pi_xxx
Also, where do you expect the metadata to be available?
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;
You added the metadata to the payment_method_data so it lives on your Payment Method object.
Should I put it somewhere else then ?
You can send the metadata to your backend and add it when you create your PaymentIntent.
Do you have a piece of code I can study to see how it works ?
Maybe you could share your current implementation and I will suggest where the metadata could live?
This is my checkout.js file which handles the form submit
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
So when you do:
const res = await fetch("/create.php", {
You can send your metadata along.
How does your backend look?
So where's this request going? /create.php
It creates the PaymentIntent and obtain clientSecret, taht is all
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 😅
That's your backend 😉
You can put your metadata in:
$intent = $stripe->paymentIntents->create(, along with your order_id
In the create.php file you mean ?
Yes
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 }),
});
What's not working?
The Json file created this way only contains the clientSecret, and not any metadada
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.
I don't know how to send the data from the form to this file, this is where I struggle
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 }),
});
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
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?
Why do you want to push it back in response?
To put it in a variable $metadata
So I can do this
'amount' => $total_panier,
'currency' => 'eur',
'automatic_payment_methods' => ['enabled' => true],
'metadata' => $metadata
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?
Yes exactely
Nice. I see 2 fields in the metadata field on this PaymentIntent. Is this what you expected?
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
- You can put up to 50 keys, with values of 500 characters long: https://stripe.com/docs/api/metadata#:~:text=You can specify up to 50 keys%2C with key names up to 40 characters long and values up to 500 characters long.
(hope I make sense 😅 )
perfect !
- Either way works. Just make sure to clearly mark the order as incomplete (in your DB) until you get the payment_intent.succeeded webhook.
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")
It depends how it is implemented, but in the standard implementation they will stay on the page.
To make sure, please test your integration with Test cards: https://stripe.com/docs/testing
https://stripe.com/docs/testing#declined-payments
Perfect thx. I've just simulated an error and there's an error message indeed which appeard. Any way to custom these messages ?
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.
Ok thx. Well Vanya you've been of great help, thank you for everything !