admin管理员组文章数量:1357404
As you see my question i am confused because i saw many article and stripe documentation solutions for saving card every time i get something different. My application flow is something like that first user e and he will be given 2 options
- previous saved cards
- enter new card
Enter New card
In doing this i take card details from customer and then i am generating token and sending that token and customerId that will be fetched from db(if old customer) and it means that customer is already generated and at backend i create customer first(if no customerId found) using the stripe token i get after adding payment information and then use the customerId i will charge the customer
stripe.charges.create({
amount:amount,
description: '',
currency: 'usd',
customer: customerid
})
Now here my question starts
I cannot use the token i got after adding payment details because that token is only for one time and I cannot use customerId directly to charge the customer then how I can charge the customer in future and how can i get a list of customers saved payment methods and delete or update them.
As you see my question i am confused because i saw many article and stripe documentation solutions for saving card every time i get something different. My application flow is something like that first user e and he will be given 2 options
- previous saved cards
- enter new card
Enter New card
In doing this i take card details from customer and then i am generating token and sending that token and customerId that will be fetched from db(if old customer) and it means that customer is already generated and at backend i create customer first(if no customerId found) using the stripe token i get after adding payment information and then use the customerId i will charge the customer
stripe.charges.create({
amount:amount,
description: '',
currency: 'usd',
customer: customerid
})
Now here my question starts
I cannot use the token i got after adding payment details because that token is only for one time and I cannot use customerId directly to charge the customer then how I can charge the customer in future and how can i get a list of customers saved payment methods and delete or update them.
Share Improve this question edited Jul 12, 2020 at 18:24 Bilal Yaqoob asked Jul 12, 2020 at 18:03 Bilal YaqoobBilal Yaqoob 1,0073 gold badges13 silver badges30 bronze badges3 Answers
Reset to default 4First, when your customer adds a card on frontend, you can save it as a payment method and use it for later.
Frontend:
const result = await stripe.createPaymentMethod({
type: 'card',
card: cardElement,
billing_details: {
// Some details of your customer like email, name, etc
},
});
And you have to send the result.paymentMethod.id
to backend and create stripe customer with that paymentMethod
.
stripe.customers.create({
email: emailAddress,
invoice_settings: {
default_payment_method: paymentMethodId,
},
payment_method: paymentMethodId,
});
// paymentMethodId is the payment method id passed from frontend
You can check the below documents:
https://stripe./docs/payments/save-and-reuse
https://stripe./docs/payments/save-during-payment
Note: You can only save the id of customer in your database and stripe will handle all of the others. You don't have to try to save something in your database for later usage.
Stripe support few options for storing card details. You can store paymentMethod after initial payment or store card details and then pay.
To store card details before payment:
- Crete StripeCustomer
CustomerCreateParams params = CustomerCreateParams.builder()
.setName("Customer name")
.setEmail("customer@email")
.build();
Customer customer = Customer.create(params);
- Prepare SetupIntent object on backend
SetupIntentCreateParams setupIntentCreateParams = SetupIntentCreateParams.builder().setCustomer(customer.getId()).build();
SetupIntent intent = SetupIntent.create(setupIntentCreateParams);
return new PrepareSetupResponse(intent.getClientSecret());
Send
intent.getClientSecret()
asprepareSetupResponse.data.clientSecret
to frontendSend
stripe.confirmCardSetup
and useresult.setupIntent.payment_method
to createPaymentIntent
on backend.setOffSession(true).setConfirm(true);
is important for offline payments
Builder paramsBuilder = builder()
.setCurrency("USD")
.addPaymentMethodType("card")
.setOffSession(true)
.setConfirm(true);
.setPaymentMethod(confirmSubscriptionRequest.getPaymentMethod())
.setCustomer(customer.getId());
PaymentIntent intent = PaymentIntent.create(paymentIntentParameters.build());
Complete JS side example
axios.post(prepareSetupUrl).then(prepareSetupResponse => {
if (prepareSetupResponse.status === 200) {
const clientSecret = prepareSetupResponse.data.clientSecret;
stripe.confirmCardSetup(clientSecret, {
payment_method: {
card: elements.getElement(CardElement),
billing_details: {
name: "cardholder name"
}
}
}).then(result => {
if (result.setupIntent && result.setupIntent.payment_method) {
axios.post(preparePaymentUrl, {
paymentMethod: result.setupIntent.payment_method
}).then((confirmPaymentResponse) => {
if (confirmPaymentResponse.status === 200) {
this.props.history.push("/checkout/success");
} else {
this.props.history.push("/checkout/failed");
console.log(confirmPaymentResponse.statusText);
}
}).catch(exception => {
console.log(exception);
this.props.history.push("/checkout/failed");
});
} else {
that.props.history.push("/checkout/failed");
}
});
}
When you receive a token from Stripe, that token should then be used in a subsequent call to create a customer with that token. You can do so using the stripe.customers.create
call. This will then convert that token into a source you can repeatedly use. See: https://stripe./docs/api/customers/create#create_customer-source
When you retrieve that customer the next time around, you'll notice that the source persists in the customer object. See: https://stripe./docs/api/customers/retrieve
When creating another charge, use that new source_id found within the customer object instead of the source parameter. If you do not provide one, it'll use the default one set for that customer.
If you need to add additional cards to a customer, use the Card Creation API. This will allow you to create, update, and delete cards for a customer. See: https://stripe./docs/api/cards/create
本文标签: firebaseHow to save card details in stripe (javascript)Stack Overflow
版权声明:本文标题:firebase - How to save card details in stripe (javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743993055a2572501.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论