Repository navigation
fix(billing): allow changing Stripe Express country by creating new account #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,17 +105,29 @@ func (s *Server) handleStripeOnboard(w http.ResponseWriter, r *http.Request) { | |
| return | ||
| } | ||
|
|
||
| // Reuse the user's existing Stripe account if we have one. If we don't, | ||
| // create a fresh Express account and persist the ID before we ever hand | ||
| // the user a link — otherwise a webhook arriving before our DB write | ||
| // could reference an unknown account. | ||
| // Normalize the requested country up front. Stripe Express country is | ||
| // immutable once the account is created, so we treat the user's selection | ||
| // as the source of truth. | ||
| requestedCountry := strings.ToUpper(strings.TrimSpace(req.Country)) | ||
| if requestedCountry == "" && user.StripeAccountID == "" { | ||
| writeJSON(w, http.StatusBadRequest, errorResponse("invalid_request_error", | ||
| "country is required before creating a Stripe payout account")) | ||
| return | ||
| } | ||
|
|
||
| // Stripe locks the country when the Express account is created. If the | ||
| // user picked a different country than their existing (possibly unfinished) | ||
| // account, create a new account so they can onboard in the right country. | ||
| // See https://docs.stripe.com/connect/accounts — Express country cannot be | ||
| // changed later. | ||
| stripeAcctID := user.StripeAccountID | ||
| if stripeAcctID == "" { | ||
| country := strings.ToUpper(strings.TrimSpace(req.Country)) | ||
| countryChanged := stripeAcctID != "" && requestedCountry != "" && | ||
| (user.StripeAccountCountry == "" || requestedCountry != user.StripeAccountCountry) | ||
|
|
||
| if stripeAcctID == "" || countryChanged { | ||
| country := requestedCountry | ||
| if country == "" { | ||
| writeJSON(w, http.StatusBadRequest, errorResponse("invalid_request_error", | ||
| "country is required before creating a Stripe payout account")) | ||
| return | ||
| country = s.billing.StripeConnect().PlatformCountry() | ||
| } | ||
| acct, err := s.billing.StripeConnect().CreateExpressAccount(billing.CreateExpressAccountParams{ | ||
| Email: user.Email, | ||
|
|
@@ -127,7 +139,7 @@ func (s *Server) handleStripeOnboard(w http.ResponseWriter, r *http.Request) { | |
| return | ||
| } | ||
| stripeAcctID = acct.ID | ||
| if err := s.billing.Store().SetUserStripeAccount(user.AccountID, stripeAcctID, stripeStatusPending, "", "", false); err != nil { | ||
| if err := s.billing.Store().SetUserStripeAccount(user.AccountID, stripeAcctID, stripeStatusPending, country, "", "", false); err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [INFO] 🧩 SetUserStripeAccount now takes 7 parameters, making it harder to call correctly 💡 Suggestion: Consider using a struct parameter or builder pattern to reduce parameter count and improve readability 📊 Score: 2×3 = 6 · Category: over-configuration |
||
| s.logger.Error("stripe connect: persist account id failed", "error", err) | ||
| writeJSON(w, http.StatusInternalServerError, errorResponse("internal_error", "failed to persist Stripe account")) | ||
| return | ||
|
|
@@ -173,6 +185,7 @@ func (s *Server) handleStripeStatus(w http.ResponseWriter, r *http.Request) { | |
| "configured": true, | ||
| "stripe_account_id": user.StripeAccountID, | ||
| "status": user.StripeAccountStatus, | ||
| "stripe_account_country": user.StripeAccountCountry, | ||
| "destination_type": user.StripeDestinationType, | ||
| "destination_last4": user.StripeDestinationLast4, | ||
| "instant_eligible": user.StripeInstantEligible, | ||
|
|
@@ -191,7 +204,7 @@ func (s *Server) handleStripeStatus(w http.ResponseWriter, r *http.Request) { | |
| } else { | ||
| status := stripeStatusForAccount(acct) | ||
| if err := s.billing.Store().SetUserStripeAccount(user.AccountID, user.StripeAccountID, | ||
| status, acct.DestinationType, acct.DestinationLast4, acct.InstantEligible); err != nil { | ||
| status, "", acct.DestinationType, acct.DestinationLast4, acct.InstantEligible); err != nil { | ||
|
Comment on lines
187
to
+207
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 [INFO] 🧩 Status endpoint conditionally updates database state during read operation 💡 Suggestion: Move the SetUserStripeAccount call to a separate sync/refresh endpoint or background job 📊 Score: 2×2 = 4 · Category: misplaced responsibility |
||
| s.logger.Warn("stripe connect: status persist failed", "error", err) | ||
| } else { | ||
| resp["status"] = status | ||
|
|
@@ -523,7 +536,7 @@ func (s *Server) handleAccountUpdated(event *billing.WebhookEvent) { | |
| } | ||
| status := stripeStatusForAccount(acct) | ||
| if err := s.billing.Store().SetUserStripeAccount(user.AccountID, acct.ID, | ||
| status, acct.DestinationType, acct.DestinationLast4, acct.InstantEligible); err != nil { | ||
| status, "", acct.DestinationType, acct.DestinationLast4, acct.InstantEligible); err != nil { | ||
| s.logger.Error("stripe connect webhook: persist account state failed", "error", err) | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 [INFO] 🧩 Custom dropdown implementation duplicates standard select functionality
💡 Suggestion: Use a standard select element like in BillingContent.tsx or extract shared dropdown component
📊 Score: 2×3 = 6 · Category: duplicate logic