Skip to content

Commit

Permalink
fix: add logic to put credit card if user doesn't have yet (#647)
Browse files Browse the repository at this point in the history
* fix: add logic to put credit card if user doesn't have yet

* refactor: payment method helpers

* refactor

* fix: return error instead of nil
  • Loading branch information
gris authored Sep 22, 2023
1 parent 34d071e commit 0052383
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 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 fmt.Errorf("failed to add payment method")
}
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,22 @@ 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 hasPaymentMethodCheck(client *turso.Client, stripeId string) (bool, error) {
if stripeId != "" {
return client.Billing.HasPaymentMethodWithStripeId(stripeId)
}
return client.Billing.HasPaymentMethod()
}

func checkPaymentMethod(client *turso.Client, stripeId string) (bool, error) {
errsInARoW := 0
var hasPaymentMethod bool
var err error
for {
hasPaymentMethod, err := client.Billing.HasPaymentMethod()
hasPaymentMethod, err = hasPaymentMethodCheck(client, stripeId)
if err != nil {
errsInARoW += 1
}
Expand All @@ -315,6 +343,28 @@ func PaymentMethodHelper(client *turso.Client, selected string) (bool, error) {
}
}

func PaymentMethodHelperOverages(client *turso.Client) (bool, error) {
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 +384,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

0 comments on commit 0052383

Please sign in to comment.