#ubi

1 messages · Page 1 of 1 (latest)

bold saffronBOT
mint edge
#

Hi there

formal birch
#

I can not use the Checkout/Payment Link/[Anything self-hosted] btw. I tried that and my CTO asked me to remove it

mint edge
formal birch
#

I have that so far, but when you click in that Submit I need to collect the info and send it to my backend

mint edge
#

Okay so what is happening right now?

#

Is there an error?

#

Are you fetching your backend when that button is clicked?

formal birch
#

Well, first, should I remove the payment intent client secret?

#

the link you shared isn't using that, which I rather not do it because technically the Price can change over time

#

You can either pick to do full-payment or payment plan (subscription)

mint edge
#

Yeah you wouldn't pass the client secret to your frontend if you want to confirm from your server

formal birch
#

right right, I was a bit lost because the amount thingy changes over time ...

#

somebody/docs else recommended me to create the payment intent as soon as possible but it is adding more problems than the one that solve

#

I also would like to avoid creating ghost payment intent due to the user clicking awway and coming back as well

#

anyway, let me fix that first

mint edge
#

Sounds good, try changing to the above and let me know after that

formal birch
#
return (
    <Elements
      stripe={stripePromise}
      options={{
        mode: 'payment',
        amount: 1099,
        paymentMethodTypes: ['card'],
        currency: 'usd',
        setupFutureUsage: 'off_session',
        appearance: {},
        paymentMethodCreation: 'manual',
        // passing the client secret obtained from the server
        // clientSecret: data.client_secret,
      }}
    >
      {props.children}
    </Elements>
  );
#

Did that so far!

#

with

<PaymentElement />
<AddressElement options={{ mode: 'billing' }} onChange={onAddressChange} />
mint edge
#

So the only thing to note is that if you are supporting both one-time and Sub payments here then you will want to re-render your Elements component if they swap between those two options

formal birch
#

step two

const handleSubmit = async (event: any) => {
    event.preventDefault();

    if (!stripe || !elements) {
      return;
    }
    
    const { error: submitError } = await elements.submit();
    if (submitError) {
      handleError(submitError);
      return;
    }

    // https://stripe.com/docs/radar/radar-session
    
    const { error, paymentMethod } = await stripe.createPaymentMethod({
      elements,
    });

    if (error) {
      handleError(error);
      return;
    }

    console.log(paymentMethod);
    
    const res = await fetch('http://localhost:44000/api/stripe/create-confirm-intent', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        paymentMethodId: paymentMethod.id,
        address: address,
      }),
    });

    const data = await res.json();

    handleServerResponse(data);
  };
mint edge
#

So that the correct Payment Method types show

formal birch
#

I am moving into the backend portion now!

formal birch
mint edge
#

Oh I see

#

Then you are fine

formal birch
#

So I have to implement that, being said, yes, I can swap stuff based on whatever state

#

alright, I think I am getting it now, so now in the backend I need to create and confirm the payment intent

#

is that correct?

mint edge
#

Yep correct

formal birch
#

looking at the example code in the backend

#
app.post('/create-confirm-intent', async (req, res) => {
  try {
    const intent = await stripe.paymentIntents.create({
      confirm: true,
      amount: 1099,
      currency: 'usd',
      automatic_payment_methods: {enabled: true},
      payment_method: req.body.paymentMethodId, // the PaymentMethod ID sent by your client
      return_url: 'https://example.com/order/123/complete',
      use_stripe_sdk: true,
      mandate_data: {
        customer_acceptance: {
          type: "online",
          online: {
            ip_address: req.ip,
            user_agent: req.get("user-agent"),
          },
        },
      },
    });
    res.json({
      client_secret: intent.client_secret,
      status: intent.status
    });
  } catch (err) {
    res.json({
      error: err
    })
  }
});
#

ahhh never mind! the trick is the confirm: true

#

LOL

#

ignore me!

mint edge
#

👍

formal birch
#

Why should I expose that client_secret in the response btw?

mint edge
#

Because if 3DS is required you will need it to handle 3DS

formal birch
#

I see

#

How could I prevent paying twice for the same course btw?

mint edge
#

Like someone reloading the page and paying again?

formal birch
#

right, or whatever double-click happen by mistake

mint edge
#

Well you want to disable your buy button on click

#

So that you don't have any issue there in terms of double click

#

If you want to prevent duplicate payments from same customer in terms of reloading the page then there are several ways to go about it but my advice would be to use a session/cookie

formal birch
#

Right, just using an extreme example

#

what do you mean by session/cookie?

#

like save an ID for the session that you were type of thing?

#

Not following

formal birch
#

I am familiar with the cookie but I am not sure what do you want me to do with it honestly

mint edge
#

Well you would check on that customers actions

#

And you can check within Stripe if they have already purchased

#

The other thing you can do is use something like email to check if they have already purchased

#

There are a variety of ways to go about preventing duplicates and all depends on how you want to handle

formal birch
#

I like the email idea, do you have a link about it?

mint edge
#

Not really. You would basically want to collect the customer's email and then associate it to a Stripe object (like a Customer potentially or you could use the billing details of a PaymentMethod) and then check whether that object is associated with any past payments based on the email

bold saffronBOT
formal birch
#

How could I attach taxes information to the payment intent?

steep rose
#

Finding the docs for this...

formal birch
steep rose
#

Is this related to the tax question or is it a new question?

formal birch
#

about radar session

#

passing the radar session to my backend, I am doing it already

#

but the example code isnt working for off-session btw

#

nevermind ... it suppose to be in the backend

#
const { error, paymentMethod } = await stripe.createPaymentMethod({
      elements,
      params: {
        radar_options: {
          session: '{{RADAR_SESSION_ID}}',
        },
      },
    });
#

that wouldn't work in the client, but I am not sure how I should do it then, I am calling that function in my FrontEnd before sending the payment info

steep rose
#

Gotcha, will see if we have a doc on how to pass this in in frontend code

#

Can you share the doc that you are looking at here? I am having a bit of trouble picturing the flow that you are currently working with