Skip to content

Commit

Permalink
detach the default payment method only id exists
Browse files Browse the repository at this point in the history
  • Loading branch information
tspenov committed Oct 2, 2024
1 parent 48c8f7c commit 246f5cc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/sanbase/discord_bot/ai_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Sanbase.DiscordBot.AiServer do
{:error, _, _} -> ["twitter"]
end

messages = AiContext.fetch_history_context(discord_metadata, 5)
messages = AiContext.fetch_history_context(discord_metadata, 10)

ai_server_params = %{
question: question,
Expand Down
7 changes: 5 additions & 2 deletions lib/sanbase/stripe/stripe_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,14 @@ defmodule Sanbase.StripeApi do
end
end

def detach_payment_method(stripe_customer_id) do
# Detach payment method if it exists
def maybe_detach_payment_method(stripe_customer_id) do
with {:ok, customer} <- Stripe.Customer.retrieve(stripe_customer_id),
default_payment_method when is_binary(default_payment_method) <-
customer.invoice_settings.default_payment_method,
{:ok, %Stripe.PaymentMethod{}} <-
Stripe.PaymentMethod.detach(%{
payment_method: customer.invoice_settings.default_payment_method
payment_method: default_payment_method
}) do
:ok
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase_web/graphql/resolvers/billing_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ defmodule SanbaseWeb.Graphql.Resolvers.BillingResolver do
context: %{auth: %{current_user: current_user}}
}) do
if current_user.stripe_customer_id do
StripeApi.detach_payment_method(current_user.stripe_customer_id)
StripeApi.maybe_detach_payment_method(current_user.stripe_customer_id)
end

Billing.create_or_update_stripe_customer(current_user, card_token)
Expand Down

0 comments on commit 246f5cc

Please sign in to comment.