#Rahim Dobani
1 messages · Page 1 of 1 (latest)
Hi! Let me help you with this.
You have to listen to webhook events, specifically, setup_intent.succeeded: https://stripe.com/docs/webhooks
updateCustomer: async (req, res) => {
// console.log(req.body)
let token = req.headers['x-access-token'] || req.headers['authorization'];
// Remove Bearer from string
token = token ? token.replace(/^Bearer\s+/, "") : "";
if (token === process.env.BEARER_TOKEN) {
const customerID = req && req.body && req.body.customerID
const pmID = req && req.body && req.body.pmID
try {
const customer = await stripe.customers.update(
customerID,
{
'invoice_settings': {
'default_payment_method': pmID
}
});
// console.log(customer);
res.status(200).send({ customer, success: true });
} catch (err) {
// response.status(400).send(Webhook Error: ${err.message});
// console.log(err);
res.status(500).send({ message: "Please try again", error: err, success: false });
return;
}
}
else {
res.status(200).send({ success: false });
}
},
const confirmPayment = await stripe.confirmSetup({
//Elements instance that was used to create the Payment Element
elements,
confirmParams: {
return_url: 'https://www.google.com/',
},
}); another question is what if dont want to pass redirect url in this api?
is there any other api to add card to customer? i am using payment element
What you need to do, as I mentioned earlier, is to listen to setup_intent.succeeded webhook event, and then you can perform the necessary actions with your new PaymentMethod object.
If you have added a customer when creating a SetupIntent it will be automatically attached: https://stripe.com/docs/api/setup_intents/create#create_setup_intent-customer
const createCustomer = await axios({
url: "http://localhost:3070/api/createCustomer",
method: 'post',
headers: {
"Content-Type": "application/json",
},
data: {
name,
email,
// priceId
}
})
const customerID = createCustomer && createCustomer.data && createCustomer.data.customer && createCustomer.data.customer.id
// console.log(customerID);
const createSetupIntent = await axios({
url: "http://localhost:3070/api/createSetupIntent",
method: 'post',
headers: {
"Content-Type": "application/json",
},
data: {
customerID,
}
})
setClientSecret(createSetupIntent && createSetupIntent.data && createSetupIntent.data.setupIntent.client_secret)
// console.log(createSetupIntent);
}
// console.log(clientSecret);
const options = {
// passing the SetupIntent's client secret
clientSecret: clientSecret,
// Fully customizable with appearance API.
appearance: {/.../},
};
i am using this api to add customer
then passing options to render payment element.
I would suggest doing this in the same API call to your backend.
I would suggest you follow the standard example for SetupIntents: https://stripe.com/docs/payments/save-and-reuse?platform=web&ui=elements