#Berci

1 messages · Page 1 of 1 (latest)

deep micaBOT
steel canyon
#

Sure, what's the issue?

teal lichen
#

Thank you!

#

So I'm not a very experienced coder and I have a code which creates a paymentintent like this:

#

And I want to change the amount depending on what package the client wants to buy, so I should use paymentintents.update, but don't know where should I implement that. Let's say my goal is to set an amount if I press a button, how could I do that? Thank you

steel canyon
#

You'd first want to create a new route handler in your Express app, like app.post('/update-payment-intent' ...

teal lichen
#

in the same script with the code I sent? Something like this?

steel canyon
#

That will accept the parameters from your front-end application (like the amount and the pi_xxx ID of the payment you're updating), then you can make the call to your API

teal lichen
#

how can I make that call?

steel canyon
#

Which call?

teal lichen
#

so how can I call this code to actually update the price?

steel canyon
#

Your front-end application needs to initiate a network request to that /update-payment-intent endpoint you just added

#

I guess you already have existing front-end code that calls the /create-payment-intent endpoint, so you can just replicate that

teal lichen
#

Okay, I will try to finish it

#

Thank you so much for your help

steel canyon
#

of course, np

teal lichen
#

Have a nice day!

#

just one more question

#

with ctrl + f I found only one reference to create-payment-intent:

#

Here should I implement the update payment intent?

steel canyon
#

Well, that call to create the Payment Intent is firing when the page loads (DOMContentLoaded) which is likely not when you want to update the Payment Intent

#

I guess you want some kind of button that when clicked updates the payment?

teal lichen
#

Yes exactly

steel canyon
#

I'd recommend looking up how to create a button onClick handler then

teal lichen
#

Before I reached you out I created a test.js script like this:

#

And after that I wrote node test:

#

And the amount was updated, but after that, I wrote npm start to start the local server and checkout, but the price didn't changed

steel canyon
#

I suspect that'll be because when you visit the page after npm start you'll get a new Payment Intent for the original amount

teal lichen
#

but if I do npm start and write node test after, I still got the amount not set issue

steel canyon
#

That's because your /create-payment-intent function is passing an amount variable as a parameter, which is seemingly unset

teal lichen
#

So should I set a random value to that?

steel canyon
#

Sure. But in reality your /create-payment-intent endpoint would accept a amount parameter (from your checkout page, for example)

teal lichen
#

Sorry, I don't get what you mean

steel canyon
teal lichen
#

yes

steel canyon
#

In that example, there's a function that returns a value that's passed to the amount parameter:

  // Replace this constant with a calculation of the order's amount
  // Calculate the order total on the server to prevent
  // people from directly manipulating the amount on the client
  return 1400;
};```
#

Looks like you removed that, and now you're essentially sending amount: null to the API (which will error as it expects an integer)

teal lichen
#

I see

#

Where should I paste his code?

steel canyon
#

I'd recommend working through the quickstart guide to understand what's going on

#

The code is annotated so helps provide context

teal lichen
#

okay, I will try to follow that

#

Thank you again

steel canyon
#

np

teal lichen
#

I don't understand this

#

I can't do that?

#

Because the amount doesn't changed

#

If I pay you, can you make a working checkout system?

steel canyon
#

Yeah, in that example it doesn't. But that's just a contrived, over simplified example. You can see in the notes that it says to replace with custom calculation

#

We can't write code for you I'm afraid

#

Have you tried looking at Checkout and/or Payment Links instead? A smaller barrier to entry with those, less code to write

teal lichen
#

Yes, but those look unprofessional, my goal is to make a custom checkout

#

Nevermind, I will keep trying to make it work

#

Thank you, hopefully I won't bother you again 😄

steel canyon
#

Happy to help!