#aabhaskarma_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1303633704941256819
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Sample code
const stripe = Stripe('<%=process.env.STRIPE_PUBLIC_KEY%>');
const options = {clientSecret: clientSecretStripe};
const elements = stripe.elements(options);
let form = document.getElementById('subscription-form');
form.addEventListener('submit', async (ev) => {
ev.preventDefault();
const { error: submitError } = await elements.submit();
if (submitError) {
return;
}
// create subsbscription
// confirm payment or confirm setup using create subscription response.
});
That sounds like a Defer Intent flow https://docs.stripe.com/payments/accept-a-payment-deferred
Should be possible
Following this Doc, after doing your validation logic, instead of creating the PaymentIntent, you create the Subscription and return its invoice.payment_intent.client_secret
I am currently doing this
https://docs.stripe.com/payments/accept-a-payment-deferred?platform=web&type=subscription
const form = document.getElementById('payment-form');
const submitBtn = document.getElementById('submit');
const handleError = (error) => {
const messageContainer = document.querySelector('#error-message');
messageContainer.textContent = error.message;
submitBtn.disabled = false;
}
form.addEventListener('submit', async (event) => {
// We don't want to let default form submission happen here,
// which would refresh the page.
event.preventDefault();
// Prevent multiple form submissions
if (submitBtn.disabled) {
return;
}
// Disable form submission while loading
submitBtn.disabled = true;
// Trigger form validation and wallet collection
const {error: submitError} = await elements.submit();
if (submitError) {
handleError(submitError);
return;
}
// Create the subscription
const res = await fetch('/create-subscription', {
method: "POST",
});
const {type, clientSecret} = await res.json();
const confirmIntent = type === "setup" ? stripe.confirmSetup : stripe.confirmPayment;
const {error} = await stripe.confirmPayment({
elements,
clientSecret,
confirmParams: {
return_url: 'https://example.com/order/123/complete',
},
});
if (error) {
// This point is only reached if there's an immediate error when confirming the Intent.
// Show the error to your customer (for example, "payment details incomplete").
handleError(error);
} else {
// Your customer is redirected to your return_url. For some payment
// methods like iDEAL, your customer is redirected to an intermediate
// site first to authorize the payment, then redirected to the return_url.
}
});
i want to validate invalid card between elements.submit(); and await fetch('/create-subscription', {
stripe.confirmPayment is below "await fetch('/create-subscription', {"
I dont want to create subscription if card have Insufficient Funds
Also we are using free trial
doing above active free trial even if card is invalid
You can do that on your create-subscription backend endpoint. Basically you put your validation logic before calling Create Subscription API
any exmaple for validation logic?
I want to validate all these card
Insufficient Funds: 4000000000009995
Lost Card: 4000000000009987
Stolen Card: 4000000000009979
Expired Card: 4000000000000069
Incorrect CVC: 4000000000000127
Processing Error: 4000000000000119
Incorrect Number: 4242424242424241
Exceeding Velocity Limit: 4000000000006975
Ah I see, you want to actually trigger a card validation
Subscription will perform this validation when you try to create it with a collected card, if you use payment_behavior = allow_incomplete. See https://docs.stripe.com/api/subscriptions/create#create_subscription-payment_behavior
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Is there any way to do it in client side?
Like i dont want to start free trial on invalid card
It can be validated via the client side confirmation call stripe.confirmPayment too. ie. if you use payment_behavior = default_incomplete and let it go back to client
Yes, but I want to do it before creating subscription
Hm okie. Then you may want to change the approach and use the SetupIntent first. That should validate the card. Only after you got a confirmed SetupIntent, you can go ahead with a Subscription
๐ taking over for my colleague. Let me catch up.
I think another option would be this https://docs.stripe.com/payments/accept-a-payment-deferred?platform=web&type=subscription
the deferred flow
I think SetupIntent first will work. Now I need example where we do both confirmSetup and confirmPayment or we don't need to do that both at same time?
the problem with this approach is that sometimes (could be very often) that your customers would have to go through SCA twice since you're confirming the Setup first and then confirming the Payment
So what you suggest? how to validate card and block calling create-subscription api of backend?
if the Payment doesn't go through the subscription won't be in active or in trial mode
Here is test customer I just created https://dashboard.stripe.com/test/customers/cus_RAQe0SJ6KRXoBf
Using Insufficient Funds: 4000000000009995
The trial is active
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.