#DaanVDH-Onboarding

1 messages ยท Page 1 of 1 (latest)

shadow atlas
#

๐Ÿ‘‹ happy to help

safe fog
#

Thanks

hoary marten
#

Hello ๐Ÿ‘‹
Apologies for the delay here

#

Taking over for my colleague as they need to step away

#

Is there a guide/tutorial you're following for this?

safe fog
#

Yes, I'm using this example for Go:

#

But i've changed some things here in there because i'm using Gin instead of the standard http package

#

This is my code:

#
1/2
func (h *Handler)onboardUser(c *gin.Context) {

    authUser := c.MustGet("user").(*user.User)

    u := &user.User{
        UserId:    authUser.UserId,
        UserEmail: authUser.UserEmail,
    }

    u, err := h.UserService.GetUserById(u.UserId)
    if err != nil {
        log.Printf("Faild to get user with id: %v\n", u.UserId)
        c.JSON(apperrors.Status(err), gin.H{
            "error": apperrors.NewNotFound("User", u.UserId.String()),
        })
        return
    }
    if len(u.StripeAccountId) > 0 {
        log.Printf("Failed to onboard user. Stripe account with ID: %s already exists", u.StripeAccountId)
        c.JSON(apperrors.Status(err), gin.H{
            "error": apperrors.NewConflict("Stripe Account", u.StripeAccountId),
        })
        return
    }
    // Create account
    accountParams := &stripe.AccountParams{
        Params:                stripe.Params{Metadata: map[string]string{"userId": u.UserId.String() }},
        Type:                  stripe.String(string(stripe.AccountTypeStandard)),
    }
    accountDetails, _ := account.New(accountParams)
    accountID := accountDetails.ID

#
2/2
// Store the accountID in the session
    session, _ := store.Get(c.Request, "account-link-session")
    session.Values["accountID"] = accountID
    err = session.Save(c.Request, c.Writer)


    if err != nil {
        log.Printf(err.Error())
        c.JSON(apperrors.Status(err), gin.H{
            "error": apperrors.NewInternal(),
        })
        return
    }


    //origin := c.Request.Header.Get("Origin")
    refreshURL := "http://localhost:3000/onboarding/refresh"
    returnURL := "http://localhost:3000/onboarding/success"
    // Create account link
    accountLinkParams := &stripe.AccountLinkParams{
        Account:    stripe.String(accountID),
        RefreshURL: stripe.String(refreshURL),
        ReturnURL:  stripe.String(returnURL),
        Type:       stripe.String("account_onboarding"),
    }
    result, err := accountlink.New(accountLinkParams)

    if err != nil {
        c.JSON(apperrors.Status(err), gin.H{
            "error": apperrors.NewInternal(),
        })
        return
    }
    u.StripeAccountId = accountID
    _, err = h.UserService.SetStripeAccountId(*u)
    if err != nil {
        c.JSON(apperrors.Status(err), gin.H{
            "error": apperrors.NewInternal(),
        })
        return
    }
    
    http.Redirect(c.Writer, c.Request, result.URL, 303)
    return
}
#
//router

c.R.POST("/stripe/onboarding",middleware.AuthUser(h.TokenService), h.onboardUser)

hoary marten
#

that's a lot of code
I'm assuming that the CORs is showing up when you redirect?

#

I'm not super familiar with Gin vs http as I've not worked with Go as much but do you think switching back to http might help?

#

also doesn't look like the CORS is coming from Stripe's side.
Its most likely thrown by your server

safe fog
#

Yeah, The problem I think is that the front end isn't expecting to be redirected before getting some data back.

hoary marten
#

I see. Yeah, not sure to be honest since it isn't API related.

safe fog
#

I fixed it by sending the URL to the browser and doing a simple window.location.replace there

#

but i'm not sure if that is considered good practice