#Berci
1 messages · Page 1 of 1 (latest)
Sure, what's the issue?
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
You'd first want to create a new route handler in your Express app, like app.post('/update-payment-intent' ...
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
how can I make that call?
Which call?
so how can I call this code to actually update the price?
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
of course, np
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?
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?
Yes exactly
I'd recommend looking up how to create a button onClick handler then
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
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
but if I do npm start and write node test after, I still got the amount not set issue
That's because your /create-payment-intent function is passing an amount variable as a parameter, which is seemingly unset
So should I set a random value to that?
Sure. But in reality your /create-payment-intent endpoint would accept a amount parameter (from your checkout page, for example)
Sorry, I don't get what you mean
Looks like you downloaded the example from here, right? https://stripe.com/docs/payments/quickstart
yes
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)
I'd recommend working through the quickstart guide to understand what's going on
The code is annotated so helps provide context
np
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?
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
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 😄
Happy to help!