#TeamSeeds-CustomerPortal

1 messages Β· Page 1 of 1 (latest)

ashen dew
#

Is /create-customer-portal-session your endpoint? You would want to check your server log for the error then

white latch
#

i'm sure it's something simple, like app.post is in the wrong place or something

ashen dew
#

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

white latch
#

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

ashen dew
#

on language, choose "Node"

white latch
#

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

ashen dew
#

I can help a bit on that. First you would need to install Stripe libraries for your backend

white latch
#

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

ashen dew
#

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

white latch
#

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

ashen dew
#

Hold on, still registering lol

#

Okie registered but don't have write permission for that project

#

In a MTG, brb

white latch
#

ok

#

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

white latch
#

can see that you're back

white latch
#

are you able to help or should i look elsewhere?

ashen dew
#

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

#

put your secret key here and try again!

white latch
#

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

ashen dew
#
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

white latch
#

ah okay!

#

that's super helpful, thanks

#

where did you see this error?

#

nvm - i see it in the logs now

ashen dew
#

πŸ˜„

white latch
#

it's working! hooray!

ashen dew
#

πŸ‘

white latch
#

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!

ashen dew
#

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

white latch
#

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?

ashen dew
#

because it needs a customer id to replace in '{{CUSTOMER_ID}}'

white latch
#

yep i understand that

ashen dew
#

My code was actively creating a new Customer, and takes its Id to supply into

white latch
#

where's the code to retrieve multiple customer IDs?

#

yep i know

ashen dew
#

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

white latch
#

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?

ashen dew
#

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

white latch
#

the comments say that authenticating the user is what the code you removed will do

ashen dew
#

Wait, which part are you referring to?

white latch
#

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

ashen dew
#

It's just a comment πŸ™‚

white latch
#

yeah a comment that speaks directly to what we're discussing

ashen dew
#

Because it's a broad question and really depends on how you would design your system, we just put it there as a comment

white latch
#

it's not a broad question. it's the very use case that is the reason customer portal exists

ashen dew
#

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

ashen dew
#

πŸ‘‹ I hope this was helpful! I will step down for the day, and if you have other questions, feel free to open new thread and my colleague would be able to help!

#

Have a nice day!