#aabhaskarma_code

1 messages ยท Page 1 of 1 (latest)

near shardBOT
#

๐Ÿ‘‹ 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.

dull ibex
#

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.
      });
random girder
#

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

dull ibex
#

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

random girder
#

You can do that on your create-subscription backend endpoint. Basically you put your validation logic before calling Create Subscription API

dull ibex
#

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

random girder
#

Ah I see, you want to actually trigger a card validation

dull ibex
#

Is there any way to do it in client side?

#

Like i dont want to start free trial on invalid card

random girder
#

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

dull ibex
#

Yes, but I want to do it before creating subscription

random girder
#

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

near shardBOT
vale nimbus
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

#

the deferred flow

dull ibex
#

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?

vale nimbus
#

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

dull ibex
#

So what you suggest? how to validate card and block calling create-subscription api of backend?

vale nimbus
dull ibex