CodeLearner
CodeLearner

Reputation: 50

How to prefill email field in Stripe Express onboarding?

In these lines of code for a booking website from some online course , I'm creating a stripe express account for sellers and an accountLink object containing the url that sends user (seller) into the flow.

I'm having trouble passing the user email in the account link url (LINK in code),I'm getting my personal email pre-filled in the form instead of the user's email I'm using for testing.

  import User from "../models/user"
  import Stripe from "stripe"
  import queryString from "query-string"

   const stripe = Stripe(process.env.STRIPE_SECRET)

   export const createConnectAccount = async (req, res) => {
  // 1. find user from db
  const user = await User.findById(req.user._id).exec()
  //2. if user don't have stripe_account_id yet, create now
  if (!user.stripe_account_id) {
    const account = await stripe.accounts.create({type: 'express');
    user.stripe_account_id = account.id
    user.save()
}
// 3. create login link based on account id (for frontend to complete onboarding)
let accountLink = await stripe.accountLinks.create({ 
    account: user.stripe_account_id,
    refresh_url: process.env.STRIPE_REDIRECT_URL ,  
    return_url: process.env.STRIPE_REDIRECT_URL , 
    type: 'account_onboarding'
}) 

// prefill any info such as email
accountLink = Object.assign(accountLink, {
  'stripe_user[email]': user.email,
});

const LINK = `${accountLink.url}?${queryString.stringify(accountLink)}`
res.send(LINK)
}

I've also tried this syntax but nothing changed :

const account = await stripe.accounts.create({type: 'express' , email: user.email});

What is the correct syntax to prefill user email in the link that is being sent?

Upvotes: 0

Views: 1049

Answers (2)

PalMaxone
PalMaxone

Reputation: 52

The way to prefill information is to give your email at the account creation time.

const account = await stripe.accounts.create({
  type: 'express',
  email: '[email protected]',
});

I dont know why it didnt prefill in the incognito window but i recommend to use another browser. You can also prefill other information too while connecting to stripe.

const account = await stripe.accounts.create({
        type: 'express',
        email: '[email protected]',
        country: 'yourcountry',
        business_type: 'individual',
        business_profile: { url: 'https://your_url' },
        individual: {
          first_name: 'first_name',
          last_name: 'last_name,
          phone: 'phone_number',
          address: {
            city: 'city',
            line1: 'address1',
            line2: 'address2'
            postal_code: 'zip_code'
          }
        }
      )

You can check for other params: https://stripe.com/docs/api/accounts/create in the [individual option]. Stripe also throws standard error if you have invalid params. Error details: https://stripe.com/docs/error-handling?lang=ruby#catch-exceptions

Upvotes: 1

toby
toby

Reputation: 622

The correct way to prefill the email for the connected account is to provide it when creating the account:

const account = await stripe.accounts.create({
  type: 'express',
  email: user.email,
});

I'd also recommend opening the account onboarding links in an incognito window as they'll clash with any active Stripe sessions you have (such as being logged into the dashboard/docs) and display the wrong email.

Sources:
https://stripe.com/docs/connect/express-accounts#create-account https://stripe.com/docs/api/accounts/create?lang=node

Upvotes: 0

Related Questions