Skip to content
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

fix: add logic to put credit card if user doesn't have yet #647

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Changes from 2 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
67 changes: 49 additions & 18 deletions internal/cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ var planEnableOverages = &cobra.Command{
return err
}

hasPaymentMethod, err := client.Billing.HasPaymentMethod()
if err != nil {
return err
}
if !hasPaymentMethod {
ok, err := PaymentMethodHelperOverages(client)
if err != nil {
return fmt.Errorf("failed to check payment method: %w", err)
}
if !ok {
return nil
gris marked this conversation as resolved.
Show resolved Hide resolved
}
fmt.Println("Payment method added successfully.")
fmt.Printf("You can manage your payment methods with %s.\n\n", internal.Emph("turso org billing"))
}
return client.Organizations.SetOverages(org, true)
},
}
Expand Down Expand Up @@ -296,9 +311,19 @@ func PaymentMethodHelper(client *turso.Client, selected string) (bool, error) {
spinner := prompt.Spinner("Waiting for you to add a payment method")
defer spinner.Stop()

return checkPaymentMethod(client, "")
}

func checkPaymentMethod(client *turso.Client, stripeId string) (bool, error) {
errsInARoW := 0
var hasPaymentMethod bool
var err error
for {
hasPaymentMethod, err := client.Billing.HasPaymentMethod()
if stripeId != "" {
hasPaymentMethod, err = client.Billing.HasPaymentMethodWithStripeId(stripeId)
} else {
hasPaymentMethod, err = client.Billing.HasPaymentMethod()
}
gris marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
errsInARoW += 1
}
Expand All @@ -315,6 +340,28 @@ func PaymentMethodHelper(client *turso.Client, selected string) (bool, error) {
}
}

func PaymentMethodHelperOverages(client *turso.Client) (bool, error) {
athoscouto marked this conversation as resolved.
Show resolved Hide resolved
fmt.Print("You need to add a payment method before you can enable overages.\n")
printPricingInfoDisclaimer()

ok, _ := promptConfirmation("Want to add a payment method right now?")
if !ok {
fmt.Printf("When you're ready, you can use %s to manage your payment methods.\n", internal.Emph("turso org billing"))
return false, nil
}

fmt.Println()
if err := billingPortal(client); err != nil {
return false, err
}
fmt.Println()

spinner := prompt.Spinner("Waiting for you to add a payment method")
defer spinner.Stop()

return checkPaymentMethod(client, "")
}

func PaymentMethodHelperWithStripeId(client *turso.Client, stripeId, orgName string) (bool, error) {
fmt.Printf("You need to add a payment method before you can create organization %s on the %s plan.\n", internal.Emph(orgName), internal.Emph("scaler"))
printPricingInfoDisclaimer()
Expand All @@ -334,23 +381,7 @@ func PaymentMethodHelperWithStripeId(client *turso.Client, stripeId, orgName str
spinner := prompt.Spinner("Waiting for you to add a payment method")
defer spinner.Stop()

errsInARoW := 0
for {
hasPaymentMethod, err := client.Billing.HasPaymentMethodWithStripeId(stripeId)
if err != nil {
errsInARoW += 1
}
if errsInARoW > 5 {
return false, err
}
if err == nil {
errsInARoW = 0
}
if hasPaymentMethod {
return true, nil
}
time.Sleep(1 * time.Second)
}
return checkPaymentMethod(client, stripeId)
}

func GetSelectPlanInfo(client *turso.Client) (plans []turso.Plan, current string, hasPaymentMethod bool, err error) {
Expand Down
Loading