Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions console-ui/src/app/billing/BillingContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
CreditCard,
Building2,
Zap,
AlertCircle,
ArrowDownToLine,
} from "lucide-react";
import { UsageChart } from "@/components/UsageChart";
Expand Down Expand Up @@ -91,6 +90,15 @@
const [withdrawLoading, setWithdrawLoading] = useState(false);
const [selectedStripeCountry, setSelectedStripeCountry] = useState("");

// Once a Stripe Express account exists, its country is locked. Pre-select
// that country so the user sees what will actually be used, and so a
// deliberate change triggers backend creation of a new account.
useEffect(() => {
if (stripeStatus?.stripe_account_country) {
setSelectedStripeCountry(stripeStatus.stripe_account_country);
}
}, [stripeStatus?.stripe_account_country]);

const loadData = useCallback(async () => {
setLoading(true);
try {
Expand Down Expand Up @@ -519,7 +527,7 @@
transition-all flex items-center justify-center gap-2"
>
{actionLoading && <Loader2 size={14} className="animate-spin" />}
{actionLoading ? "Redirecting..." : "Continue"}

Check warning on line 530 in console-ui/src/app/billing/BillingContent.tsx

View workflow job for this annotation

GitHub Actions / Console UI Lint & Build

Define a constant instead of duplicating this literal 3 times
</button>
<p className="mt-4 text-xs text-text-tertiary text-center">
Powered by Stripe. Secure card payment.
Expand Down Expand Up @@ -669,24 +677,36 @@
</>
) : (
<>
<p className="text-sm text-text-secondary mb-4 leading-relaxed flex items-start gap-2">
<AlertCircle size={14} className="text-coral mt-0.5 flex-shrink-0" />
{restricted
? "Stripe needs more information to enable payouts on your account."
: rejected
? "Stripe has disabled payouts on this account. Contact support."
: "Finish linking your account on Stripe to enable withdrawals."}
<p className="text-sm text-text-secondary mb-4 leading-relaxed">
Your Stripe account is locked to{" "}
<span className="font-medium text-text-primary">
{STRIPE_CONNECT_COUNTRIES.find((c) => c.code === status?.stripe_account_country)?.name || status?.stripe_account_country || "your selected country"}
</span>
. If that is not correct, select your country below and we will create a new account.
</p>
{!rejected && (
<button
onClick={onOnboard}
disabled={onboardLoading}
className="flex items-center gap-2 px-5 py-2.5 rounded-lg bg-teal border-2 border-ink text-white text-sm font-bold hover:opacity-90 disabled:opacity-50 transition-all"
>
{onboardLoading ? <Loader2 size={14} className="animate-spin" /> : <Building2 size={14} />}
{onboardLoading ? "Redirecting..." : restricted ? "Provide more info" : "Continue setup"}
</button>
)}
<label className="block text-xs font-mono text-text-tertiary uppercase tracking-wider mb-2">
Country
</label>
<select
value={selectedCountry}
onChange={(e) => onCountryChange(e.target.value)}
className="w-full mb-4 bg-bg-primary border border-border-dim rounded-lg px-4 py-3 text-sm text-text-primary outline-none transition-colors hover:border-teal/40 focus:border-teal"
>
<option value="">Select your country</option>
{STRIPE_CONNECT_COUNTRIES.map((country) => (
<option key={country.code} value={country.code}>
{country.name} ({country.code})
</option>
))}
</select>
<button
onClick={onOnboard}
disabled={onboardLoading || !selectedCountry}
className="flex items-center gap-2 px-5 py-2.5 rounded-lg bg-teal border-2 border-ink text-white text-sm font-bold hover:opacity-90 disabled:opacity-50 transition-all"
>
{onboardLoading ? <Loader2 size={14} className="animate-spin" /> : <Building2 size={14} />}
{onboardLoading ? "Redirecting..." : restricted ? "Provide more info" : "Continue setup"}
</button>
</>
)}

Expand Down
100 changes: 85 additions & 15 deletions console-ui/src/app/providers/earnings/EarningsContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
LogIn,
ArrowDownToLine,
Check,
AlertCircle,
Building2,
CreditCard,
Clock,
Expand Down Expand Up @@ -102,6 +101,15 @@ export default function EarningsContent() {
const [withdrawMethod, setWithdrawMethod] = useState<"standard" | "instant">("standard");
const [withdrawLoading, setWithdrawLoading] = useState(false);
const [selectedCountry, setSelectedCountry] = useState("");

// Once a Stripe Express account exists, its country is locked. Pre-select
// that country so the user sees what will actually be used, and so a
// deliberate change triggers backend creation of a new account.
useEffect(() => {
if (stripeStatus?.stripe_account_country) {
setSelectedCountry(stripeStatus.stripe_account_country);
}
}, [stripeStatus?.stripe_account_country]);
const [countryDropdownOpen, setCountryDropdownOpen] = useState(false);
const [countryFilter, setCountryFilter] = useState("");
const countryDropdownRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -484,24 +492,86 @@ export default function EarningsContent() {
</>
) : (
<>
<p className="text-sm text-text-secondary mb-4 leading-relaxed flex items-start gap-2">
<AlertCircle size={14} className="text-coral mt-0.5 flex-shrink-0" />
{restricted
? "Stripe needs more information to enable payouts on your account."
: rejected
? "Stripe has disabled payouts on this account. Contact support."
: "Finish linking your account on Stripe to enable withdrawals."}
<p className="text-sm text-text-secondary mb-4 leading-relaxed">
Your Stripe account is locked to{" "}
<span className="font-medium text-text-primary">
{STRIPE_CONNECT_COUNTRIES.find(c => c.code === stripeStatus?.stripe_account_country)?.name || stripeStatus?.stripe_account_country || "your selected country"}
</span>
. If that is not correct, select your country below and we will create a new account.
</p>
{!rejected && (
<label className="block text-xs font-mono text-text-tertiary uppercase tracking-wider mb-2">
Country
</label>
<div className="relative mb-4" ref={countryDropdownRef}>
<button
onClick={handleStripeOnboard}
disabled={stripeOnboardLoading}
className="flex items-center gap-2 px-5 py-2.5 rounded-lg bg-teal border-2 border-ink text-white text-sm font-bold hover:opacity-90 disabled:opacity-50 transition-all"
type="button"
onClick={() => { setCountryDropdownOpen(!countryDropdownOpen); setCountryFilter(""); }}
className="w-full flex items-center justify-between gap-2 bg-bg-primary border border-border-dim rounded-lg px-4 py-3 text-sm text-left transition-colors hover:border-teal/40 focus:outline-none focus:border-teal"
>
{stripeOnboardLoading ? <Loader2 size={14} className="animate-spin" /> : <Building2 size={14} />}
{stripeOnboardLoading ? "Redirecting..." : restricted ? "Provide more info" : "Continue setup"}
{selectedCountry ? (
<span className="flex items-center gap-2 text-text-primary">
<span>{STRIPE_CONNECT_COUNTRIES.find(c => c.code === selectedCountry)?.flag}</span>
<span>{STRIPE_CONNECT_COUNTRIES.find(c => c.code === selectedCountry)?.name}</span>
</span>
) : (
<span className="flex items-center gap-2 text-text-tertiary">
<Globe size={14} />
<span>Select your country</span>
</span>
)}
<ChevronDown size={14} className={`text-text-tertiary transition-transform ${countryDropdownOpen ? "rotate-180" : ""}`} />
</button>
)}

{countryDropdownOpen && (
<div className="absolute z-50 mt-1 w-full bg-bg-white border border-border-dim rounded-xl shadow-lg overflow-hidden">
<div className="p-2 border-b border-border-dim">
<div className="relative">
<Search size={14} className="absolute left-3 top-1/2 -translate-y-1/2 text-text-tertiary" />
<input
type="text"
value={countryFilter}
onChange={(e) => setCountryFilter(e.target.value)}
placeholder="Search countries..."
autoFocus
className="w-full bg-bg-primary border border-border-dim rounded-lg pl-9 pr-3 py-2 text-sm text-text-primary placeholder:text-text-tertiary outline-none focus:border-teal"
/>
</div>
</div>
<div className="max-h-64 overflow-y-auto">
{STRIPE_CONNECT_COUNTRIES
.filter(c => {
const q = countryFilter.toLowerCase();
return !q || c.name.toLowerCase().includes(q) || c.code.toLowerCase().includes(q);
})
.map(c => (
<button
key={c.code}
type="button"
onClick={() => { setSelectedCountry(c.code); setCountryDropdownOpen(false); }}
className={`w-full flex items-center gap-3 px-4 py-2.5 text-sm text-left transition-colors ${
selectedCountry === c.code
? "bg-teal/10 text-teal"
: "text-text-secondary hover:bg-bg-hover"
}`}
>
<span className="text-base">{c.flag}</span>
<span className="flex-1">{c.name}</span>
<span className="text-xs font-mono text-text-tertiary">{c.code}</span>
</button>
))
}
</div>
</div>
)}
</div>
<button
onClick={handleStripeOnboard}
disabled={stripeOnboardLoading || !selectedCountry}
className="flex items-center gap-2 px-5 py-2.5 rounded-lg bg-teal border-2 border-ink text-white text-sm font-bold hover:opacity-90 disabled:opacity-50 disabled:cursor-not-allowed transition-all"
>
{stripeOnboardLoading ? <Loader2 size={14} className="animate-spin" /> : <Building2 size={14} />}
{stripeOnboardLoading ? "Redirecting..." : restricted ? "Provide more info" : "Continue setup"}
</button>
</>
Comment on lines +495 to 575

Copy link
Copy Markdown
Contributor

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

)}

Expand Down
1 change: 1 addition & 0 deletions console-ui/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export interface StripeStatus {
configured: boolean;
has_account: boolean;
stripe_account_id?: string;
stripe_account_country?: string;
status: "" | "pending" | "ready" | "restricted" | "rejected";
destination_type?: "" | "bank" | "card";
destination_last4?: string;
Expand Down
37 changes: 25 additions & 12 deletions coordinator/api/stripe_payouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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,
Expand All @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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)
}
}
Expand Down
Loading
Loading