#usm-seong_paymentrequestbutton

1 messages ยท Page 1 of 1 (latest)

iron rainBOT
#

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

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

pastel turtleBOT
#

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

fierce coral
#

so the use case is when a user confirms with a credit card with insufficient funds, I'd like to display some meaninful message

#

this is the current state

supple ore
#

Are you integrating with Payment Reques Button or the Express Checkout Element? And can you also share exactly how you're calling complete()?

fierce coral
#

PaymentRequestButtonElement

#

complete('success') for success state
complete('fail') for error state

#

but complete('fail') doesn't close the apple/google pay window

supple ore
#

Can you add it back to your block of code and reshare the full block?

fierce coral
#

import { useEffect, useState } from 'react';
import { useStripe } from '@stripe/react-stripe-js';
import isEqual from 'lodash/isEqual';
import usePrevious from './usePrevious';

export default function usePaymentRequest({ options, onPaymentMethod, setPlatformPaymentOptions }) {
const stripe = useStripe();
const [paymentRequest, setPaymentRequest] = useState(null);
const [canMakePayment, setCanMakePayment] = useState(false);

const prevOptions = usePrevious(options);
const isOptionsChanged = !isEqual(prevOptions, options);

useEffect(() => {
if (stripe && paymentRequest === null) {
const pr = stripe.paymentRequest(options);
setPaymentRequest(pr);
}
}, [stripe, options, isOptionsChanged, paymentRequest]);

useEffect(() => {
let subscribed = true;
if (paymentRequest) {
paymentRequest.canMakePayment().then(res => {
if (res && subscribed) {
setCanMakePayment(true);
setPlatformPaymentOptions(res);
}
});
}

return () => {
  subscribed = false;
};

}, [paymentRequest]);

useEffect(() => {
if (paymentRequest) {
paymentRequest.on('paymentmethod', onPaymentMethod);
}
return () => {
if (paymentRequest) {
paymentRequest.off('paymentmethod', onPaymentMethod);
}
};
}, [paymentRequest, onPaymentMethod]);

return canMakePayment ? paymentRequest : null;
}

#
      const {
        error: stripePIError,
        paymentIntent: responsePI,
        ...rest
      } = await stripe.confirmCardPayment(
        clientSecret,
        {
          payment_method: paymentMethod.id,
        },
        { handleActions: false },
      );
    if (stripeError) {
      if (stripeError.code === 'card_declined') {
        setIsPlatformPayError(true);
      }
      complete('fail');
      return;
    }

  };
supple ore
#

Should if (stripeError) be if (stripePIError)?

fierce coral
#

yeah i made some typos here

#

as I was trimming down the code

#

the question is pretty straighforward. i want to display a custom error message on the apple google pay window when the card was declined

#

or close the window progmatically

supple ore
#

Yeah the way to close the window is through the complete() function, which is why I'm trying to dig down into why that isn't working for you

iron rainBOT
#

usm-seong_paymentrequestbutton

pastel turtleBOT
solid hull
#

Hi ๐Ÿ‘‹

Can you confirm that you have modified your code so the error variable is named consistently?


const {
        error: stripePIError, // <- this name
        paymentIntent: responsePI,
        ...rest
      }
...
if (stripeError) { // <- should be the same here
fierce coral
#

yes

#

like I mentioned before, it's just i made a typo as I was trimming down my existing code

solid hull
#

Okay can you add a console.log(stripeError) just after the if statement to let me know if the error gets logged?

fierce coral
solid hull
#

Okay so code execution is getting down to the if(stripeError) block

fierce coral
#

this is the screenshot of my code

#

i had console logs in if(stripeError)

#

they are printed in the console

#

just like the screenshot above

solid hull
#

I don't see log lines in your code. If you are going to share the code, please share it exactly as you are running it to test behavior without any "cleaning up". It increasese the chances that what we see and what is being run are not the same behavior,

fierce coral
#

i had a console log in if (strtipeError) block, that's what i also said

#

stripeError

#

you already have all the information

#

my code

#

and the error object

solid hull
#

I am trying to be helpful. I am just pointing out that the inconsistency between what you say is running and what you show makes it harder for me to be helpful.

fierce coral
#

i understand this is the code with the console logs

#

like i said, it's just complete('fail') not closing the window

#

so if it's not working i'd at least like to know how to display a meaningful error message in the window

#

*google/apple pay window

solid hull
#

Okay so I see two lines that are logging the error. Can you take out the one in your confirmCardSetup block and make sure the other still outputs the expected results?

fierce coral
#

yes

#

still same output

solid hull
#

Okay. So the Complete function is a function returned on the event object. Are you passing the event object down to this part of the code?

I'm not the most skilled JS dev but, looking at my example code, I have been getting this to run successfully

paymentRequest.on("paymentmethod", (ev) => {
    let confirmFunc = null;
    if (isSetup) {
      confirmFunc = stripe.confirmCardSetup;
    } else {
      confirmFunc = stripe.confirmCardPayment;
    }
    confirmFunc(
      clientSecret,
      { payment_method: ev.paymentMethod.id },
      { handleActions: false }
    ).then((confirmResult) => {
      console.log(confirmResult);
      if (confirmResult.error) {
        ev.complete("fail");  // <- calling the function from the event object.
      } else {
        ev.complete("success");
fierce coral
#

yes that's basically like my code

solid hull
fierce coral
#

event is destructued like this

#

complete and paymentMethod are coming from event object

#

and everything else works just fine it's just complete('fail') not working

solid hull
#

Can you log the complete function just to check that?

fierce coral
#

why is it timed out

solid hull
#

Okay, now we are getting somewhere.

fierce coral
#

why could you check why complete gets ignored and timed out?

#

like what could lead to this

solid hull
#

The issue is the Payment Request is timing out

#

still digging

fierce coral
#

just fyi before confirm card, I fetch payment intent like this

#

kk please let me know if you found something

solid hull
#

Hmmm... that delay could be the problem.

#

Wait... how long does that take?

fierce coral
#

it takes around 1 second

#

but I didn't want to create a payment intent on page load

#

i wanted to have it when the user actually clicks the checkout button

solid hull
#

Oh...

#

Yeah the onClick event is really time sensitive.

#

Just for testing, can you trying to move those calls before the event and see if that works?

#

i.e. create the intent on page load

fierce coral
#

yeah give me a moment

#

still the same. please note that i'm doing
stripeError = { code: 'card_declined } just to simulate error state

solid hull
#

Okay this is a bit more complex than I think we can debug in realtime here. I'm sorry but I'm going to ask you to write in to Support here https://support.stripe.com/contact and please provide the full code for your front-end integration in text so we can spin up a reproduction of your error and trouble shoot from there.