Migrer vers les tokens de confirmation
Finaliser les paiements sur le serveur en utilisant un ConfirmationToken au lieu d'un PaymentMethod.
Ce guide vous explique comment finaliser des paiements côté serveur en utilisant un ConfirmationToken plutôt qu’un PaymentMethod pour envoyer à votre serveur les données collectées auprès de votre client.
Un ConfirmationToken
contient un surensemble des données d’un PaymentMethod
(par exemple, les informations de livraison), et active de nouvelles fonctionnalités à mesure que nous les développons.
Créer le Token de confirmationcôté client
Au lieu d’appeler stripe.createPaymentMethod, appelez stripe.createConfirmationToken pour créer un objet ConfirmationToken
. Transmettez ce ConfirmationToken au serveur pour confirmer le PaymentIntent.
La méthode stripe.
accepte les mêmes paramètres que stripe.
(via params.payment_method_data), ainsi que les paramètres shipping et return_url supplémentaires.
// Create the PaymentMethod using the details collected by the Payment Element. const {error, paymentMethod} = await stripe.createPaymentMethod({ elements, params: { billing_details: { name: 'Jenny Rosen', } } }); if (error) { // This point is only reached if there's an immediate error when creating the PaymentMethod. // Show the error to your customer (for example, payment details incomplete) handleError(error); return; } // Create and confirm the PaymentIntent const res = await fetch("/create-confirm-intent", { method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({ paymentMethodId: paymentMethod.id, }), });
// Create the ConfirmationToken using the details collected by the Payment Element and additional shipping information. Provide shipping and return_url if you don't want to provide it when confirming the intent on the server const {error, confirmationToken} = await stripe.createConfirmationToken({ elements, params: { payment_method_data: { billing_details: { name: 'Jenny Rosen', } }, // Remove shipping if you're collecting it using Address Element or don't require it shipping: { name: 'Jenny Rosen', address: { line1: '1234 Main Street', city: 'San Francisco', state: 'CA', country: 'US', postal_code: '94111', }, }, return_url: 'https://example.com/order/123/complete', } }); if (error) { // This point is only reached if there's an immediate error when creating the ConfirmationToken. // Show the error to your customer (for example, payment details incomplete) handleError(error); return; } // Create and confirm the PaymentIntent const res = await fetch("/create-confirm-intent", { method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({ confirmationTokenId: confirmationToken.id, }), });
Créer et envoyer le paiement à Stripecôté serveur
Vous transmettez le ConfirmationToken au serveur pour confirmer le PaymentIntent, plutôt que de transmettre un PaymentMethod comme vous l’avez fait précédemment. Les propriétés stockées sur le ConfirmationToken
sont appliquées à l’Intent lorsque son ID est fourni dans le paramètre confirmation_token au moment de la confirmation.
Remarque
Si vous renseignez les paramètres shipping et return_url du ConfirmationToken, vous n’avez pas besoin de les spécifier à nouveau lors de la confirmation du PaymentIntent.
app.post('/create-confirm-intent', async (req, res) => { try { const intent = await stripe.paymentIntents.create({ confirm: true, amount: 1099, currency: 'usd', // In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default. automatic_payment_methods: {enabled: true}, use_stripe_sdk: true, // the PaymentMethod ID sent by your client payment_method: req.body.paymentMethodId, return_url: 'https://example.com/order/123/complete', mandate_data: { customer_acceptance: { type: "online", online: { ip_address: req.ip, user_agent: req.get("user-agent"), }, }, }, shipping: { name: 'Jenny Rosen', address: { line1: '1234 Main Street', city: 'San Francisco', state: 'CA', country: 'US', postal_code: '94111', }, } }); res.json({ client_secret: intent.client_secret, status: intent.status }); } catch (err) { res.json({ error: err }) } });
app.post('/create-confirm-intent', async (req, res) => { try { const intent = await stripe.paymentIntents.create({ confirm: true, amount: 1099, currency: 'usd', // In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default. automatic_payment_methods: {enabled: true}, use_stripe_sdk: true, // the ConfirmationToken ID sent by your client that already has the shipping, mandate_data, and return_url data confirmation_token: req.body.confirmationTokenId, }); res.json({ client_secret: intent.client_secret, status: intent.status }); } catch (err) { res.json({ error: err }) } });
Tous les paramètres fournis directement au PaymentIntent ou au SetupIntent au moment de la confirmation, tels que shipping
, remplacent les propriétés correspondantes au niveau du ConfirmationToken.