#trung_code

1 messages ¡ Page 1 of 1 (latest)

latent juncoBOT
#

👋 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/1359782729994211389

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

fluid stag
#

Can you tell me more about the "add card" step?

blazing briar
#

Below is my add card code
public function addCard($token)
{
try {
$data = [
'source' => $token,
];
$this->user->updateStripeCustomer($data)->toArray();
$paymentMethods = $this->user->paymentMethods()->toArray();
$this->user->update([
'pm_last_four' => $paymentMethods[0]['card']['last4'],
'pm_card_type' => $paymentMethods[0]['card']['brand'],
'pm_type' => 'card',
]);
return true;
} catch (\Stripe\Exception\CardException $e) {
if ($e->getError()->code === trans('stripe.errors.expired_card')) {
throw new InputException('Card expired');
} elseif ($e->getError()->code === trans('stripe.errors.card_declined')) {
if ($e->getError()->decline_code === trans('stripe.errors.insufficient_funds')) {
throw new InputException('Your card has insufficient funds');
}
throw new InputException('Declined by the issuer');
} else {
throw new InputException(trans('stripe.add_card.invalid_request_error'));
}
}
}

fluid stag
#

Ok, I don't recommend you attaching the card to the customer directly.

blazing briar
#

I want to ask a little more about useConfirmCardPayment, with the code like below, will there be asynchrony, in some cases the confirmCardPaymentSuccess() function is not called. I am using nextjs
export const useConfirmCardPayment = () => {
const stripe = useStripe()

const confirmPayment = async ({
clientSecret,
paymentMethod,
pointPackageId,
onSuccess,
confirmCardPaymentSuccess,
}: {
clientSecret: string
paymentMethod: string
pointPackageId: number
onSuccess: () => void
confirmCardPaymentSuccess: (payload: { payment_intent_id: string; point_package_id: number }) => void
}) => {
if (!stripe) return

const { paymentIntent, error } = await stripe.confirmCardPayment(clientSecret, {
  payment_method: paymentMethod,
})

if (error) {
  return toast.error(error.message)
}

if (paymentIntent) {

  confirmCardPaymentSuccess({
    payment_intent_id: paymentIntent.id,
    point_package_id: pointPackageId,
  })

  onSuccess()
}

}

return { confirmPayment }
}

fluid stag
#

I don't quite understand your question, can you rephrase it?

blazing briar
#

I'm using useConfirmCardPayment in a Next.js project, and I have a question about potential asynchronous behavior.

In some cases, the confirmCardPaymentSuccess() function doesn't seem to get called. Here's a simplified version of the code:

export const useConfirmCardPayment = () => {
const stripe = useStripe()

const confirmPayment = async ({
clientSecret,
paymentMethod,
pointPackageId,
onSuccess,
confirmCardPaymentSuccess,
}: {
clientSecret: string
paymentMethod: string
pointPackageId: number
onSuccess: () => void
confirmCardPaymentSuccess: (payload: { payment_intent_id: string; point_package_id: number }) => void
}) => {
if (!stripe) return

const { paymentIntent, error } = await stripe.confirmCardPayment(clientSecret, {
  payment_method: paymentMethod,
})

if (error) {
  return toast.error(error.message)
}

if (paymentIntent) {
  confirmCardPaymentSuccess({
    payment_intent_id: paymentIntent.id,
    point_package_id: pointPackageId,
  })

  onSuccess()
}

}

return { confirmPayment }
}
My question is:
Could there be any asynchronous issues or edge cases where paymentIntent exists but confirmCardPaymentSuccess() still doesn't get called? Or maybe something is preventing it from running? I'd like to understand under what circumstances this might happen.

fluid stag
#

If I understand you correctly, confirmCardPaymentSuccess won't be called when there's a payment error.

#

Is this the thing that you want to confirm?

blazing briar
#

I’d like to confirm whether there could be any cases where paymentIntent exists, but confirmCardPaymentSuccess is still not called. Could this be due to some kind of asynchronous issue?

fluid stag
#

No, there won't be cases when both paymentIntent and error are both non-null.

blazing briar
#

I've noticed that in some cases where the user's card goes through 3DS frictionless, the confirmCardPaymentSuccess function doesn't get called. I'm not quite sure why this happens — could you help me understand it better?

#

Could this be due to an asynchronous issue?

fluid stag
#

Maybe you want to put some logs in your code to troubleshoot?

blazing briar
#

For cards that require 3DS frictionless, is there anything that needs to be adjusted in my code?

fluid stag
#

No, the code looks good to me

blazing briar
#

Are there any test card numbers available that can simulate 3DS frictionless in the test environment?

fluid stag