#TeamSeeds-CustomerPortal
1 messages Β· Page 1 of 1 (latest)
Is /create-customer-portal-session your endpoint? You would want to check your server log for the error then
yes i believe so, and also just realized i can share the replit:
https://replit.com/@playseeds/stripe-customer-portal-1#index.js
i'm sure it's something simple, like app.post is in the wrong place or something
Ah I see
Customer Portal creation is a backend method, you will need to call it server-side
it can't be called from js like that
thanks! any sample code/docs/demo somewhere to show me how to call it server-side? prob obvious that i'm a new coder
i guess that just means within a server.js file eh
No worries, our Doc should be the best resource: https://stripe.com/docs/billing/subscriptions/integrating-customer-portal
on language, choose "Node"
nah i've looked at that for house and it doesn't explain how to call something server-side for someone with my knowledge level
hours*
it's assuming that that's known
I can help a bit on that. First you would need to install Stripe libraries for your backend
are you familiar with replit? i think the other thing may be that replit spins up node.js (or whatever) for you
i used it after seeing cj avilla use it in a stripe demo that enabled me to successfully set up variable payments for checkout yesterday
Then, inside your server side code, include the call and return the url
app.post('/create-customer-portal-session', async (req, res) => {
// Authenticate your user.
const session = await stripe.billingPortal.sessions.create({
customer: '{{CUSTOMER_ID}}',
return_url: 'https://example.com/account',
});
res.redirect(session.url);
});
Not so familiar π Will need time to play around with it. But I believe the way it work is similar
How about testing locally? Setting up node on your local. You will need it anyway
replit just does that for you
it's an awesome tool actually
web IDE
but i have node on my machine
what's the code to install stripe libraries for backend? that's the piece i'm missing
i've included 'const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);'
i thought 'stripe' was what was used to install stripe dependencies and that's there
Hold on, still registering lol
Okie registered but don't have write permission for that project
In a MTG, brb
ok
okay this link should give you write permissions:
and yeah what's notable is that when you create a replit project it asks you this, so i'd already selected node.js within replit for this project
can see that you're back
are you able to help or should i look elsewhere?
Sorry was stretched out a little bit
looking now
Did you see my changes?
Mostly working. Need to figure it out how to insert your secret key there
Okie
put your secret key here and try again!
ok will check it out now
yep i know that where the secret key goes. got checkout working here as mentioned
not quite working
it's not throwing the 'Cannot POST' error anymore, but isn't rerouting to /create-customer-portal-session
instead it goes here:
console logs say it's throwing a 400
StripeInvalidRequestError: You canβt create a portal session in test mode until you save your customer portal settings in test mode at https://dashboard.stripe.com/test/settings/billing/portal.
Here is the error
you would want to go to that link and setup some configuration to make it work
ah okay!
that's super helpful, thanks
where did you see this error?
nvm - i see it in the logs now
π
it's working! hooray!
π
just tested it though and it isn't showing a customer portal that's associated with a specific user, nor is it retaining information when it's added
for example, when i visited it just now, i added a credit card
when i went back a couple minutes later, the credit card info had disappeared
any idea what the issue may be? thanks!
Because in the code I am creating new Customer every time
This part
const customer = await stripe.customers.create({});
To make your usecase work, you would want to somehow load the same customer id into the request
I was creating new customer because it is just for demonstration to make this work in the first place
okay i see this in the code and how it differs from the code in the docs
i replaced the app.post you created with the code straight from the stripe node docs:
app.post('/create-customer-portal-session', async (req, res) => {
// Authenticate your user.
const session = await stripe.billingPortal.sessions.create({
customer: '{{CUSTOMER_ID}}',
return_url: 'https://example.com/account',
});
res.redirect(session.url);
});
and it's breaking again
what will successfully load the customer ID?
because it needs a customer id to replace in '{{CUSTOMER_ID}}'
yep i understand that
My code was actively creating a new Customer, and takes its Id to supply into
Well it's a broad question and relates to how you want to design your whole application
Generally you need to authenticate your user someway, behind a login page/signup process
the customer portal needs to allow each individual to manage their own subscriptions
it's the same use case for everyone
have you looked at the stripe node docs?
after you authenticated your user, you can tie them to one Customer object within Stripe API. When your customer comes to your page, you should have known the Customer Id already
Yeah
the comments say that authenticating the user is what the code you removed will do
Wait, which part are you referring to?
literally the comments in the code
from the stripe node customer portal integration docs
//Authenticate your user on line 4
and the code below that is what you'd swapped out, but it's breaking when i try to add it back in
i do see this line about tho: "Make sure to authenticate customers on your site before creating sessions for them"
so i get what you're saying, but it means this step is left out of the docs
It's just a comment π
yeah a comment that speaks directly to what we're discussing
Because it's a broad question and really depends on how you would design your system, we just put it there as a comment
it's not a broad question. it's the very use case that is the reason customer portal exists
Um sorry if it's confusing to you, but the Doc really can't outline how you would authenticate your user, because it's a very different process for each of merchant
I would say, let's build your user authentication first, then go back to continue integrating this Customer Portal, because it has a dependency on your user authentication process