#_kevinx

1 messages ยท Page 1 of 1 (latest)

solid loomBOT
marsh token
#

Hi, what is the question?

magic wolf
#

but Keep getting error

{"error":"Cannot set properties of undefined (setting 'accountID')"}
#

It seems there is no session in the req obj

#

Here is my API code

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
export default async function handler(req, res) {
  console.log("๐Ÿš€ ~ file: stripeStandardOnboard.js:3 ~ handler ~ req:", req);
  try {
    const account = await stripe.accounts.create({
      type: "standard",
    });

    // Store the ID of the new Standard connected account.
    req.session.accountID = account.id;
    console.log(
      "๐Ÿš€ ~ file: stripeStandardOnboard.js:10 ~ handler ~ account:",
      account
    );

    const origin = `${req.headers.origin}`;
    const accountLink = await stripe.accountLinks.create({
      type: "account_onboarding",
      account: account.id,
      refresh_url: `${origin}/api/stripOnboardRefresh`,
      return_url: `${origin}/success`,
    });

    res.redirect(303, accountLink.url);
  } catch (err) {
    res.status(500).send({
      error: err.message,
    });
  }
}

marsh token
magic wolf
#

req_OlXCu5iCjMyOuh

#

I'm using Next JS btw

marsh token
#

The error code is saying that it cannot identify '(setting 'accountID')', specifically, 'req.session.accountID = account.id;'. You'd want to log this and see what it is and go from there. The request id you shared succeeded for creating account here stripe.accounts.create.

magic wolf
#

I did already, logged req, and like I said, there is no session in the req obj, that's why it says undefined

marsh token
#

The comment line says what it's supposed to be doing. If you do not intend to store it, I think you could move that.

magic wolf
#

not a solution, seems the refresh api needs this too, from the same sample

app.get("/onboard-user/refresh", async (req, res) => {
  if (!req.session.accountID) {
    res.redirect("/");
    return;
  }

  try {
    const { accountID } = req.session;
    const origin = `${req.secure ? "https://" : "http://"}${req.headers.host}`;

    const accountLink = await stripe.accountLinks.create({
      type: "account_onboarding",
      account: accountID,
      refresh_url: `${origin}/onboard-user/refresh`,
      return_url: `${origin}/success.html`,
    });

    res.redirect(303, accountLink.url);
  } catch (err) {
    res.status(500).send({
      error: err.message,
    });
  }
});
#

btw, do you have an example of using Next instead of express? not sure if the session is handled differently between these two

worldly cosmos
#

@magic wolf Just to clarify here this is purely in your own code/app. Stripe has no concept of a "session" and storing thing in a cookie and such. This is purely something you build/own by tracking who your end customer (the account owner) is, which account id acct_123 they are associated with, etc.

#

so the whole req.session part is purely yours and we don't really know much about how you'd do this in a certain framework

magic wolf
#

and it leads me to these questions

worldly cosmos
#

yeah but this is really purely the most basic sample app that you would kind of port to your own entire integration

magic wolf
#

so the whole ideal here is to store the account.id in session and later pull it back to varify user?

worldly cosmos
#

Kind of. You don't really need to store anything in any session. But when someone is sent back to your website/app you should either know who they are and load the account id from somewhere (session or your database) or have them log in first or something

magic wolf
#

Got you, thank you, just a little feedback, as a lot people are using Next JS now, adding a Next JS example would be a lot helpful, again, thanks

#

Have a good weekend.