Stripe subscription flow test
if not String.contains?(System.get_env("DATABASE_URL"), "rds-stage") do
raise("You need to be connected to stage RDS to run this test")
end
if not String.starts_with?(System.get_env("STRIPE_SECRET_KEY"), "sk_test") do
raise("You need to be connected to test Stripe to run this tests")
end
Setup: Aliases and Plans
alias Sanbase.Repo
alias Sanbase.Billing.{Subscription, Plan}
alias Sanbase.Accounts.User
sanbase_pro_monthly_plan_id = 201
sanbase_pro_yearly_plan_id = 202
sanbase_pro_monthly_plan = Plan.by_id(sanbase_pro_monthly_plan_id)
sanbase_pro_yearly_plan = Plan.by_id(sanbase_pro_yearly_plan_id)
bussiness_max_yearly_plan = Plan.by_id(110)
Setup: Fetch test user and delete everything in Stripe for this user.
{:ok, user} = User.by_email("tsvetozar.penov+stripetestuserstage@gmail.com")
if user.stripe_customer_id do
Sanbase.StripeApi.delete_customer(user)
else
{:ok, user}
end
Subscribe user to Sanbase PRO
{:ok, user} = User.by_email("tsvetozar.penov+stripetestuserstage@gmail.com")
{:ok, subscription} = Sanbase.Billing.Subscription.subscribe2(user, sanbase_pro_monthly_plan, "pm_card_visa")
Upgrade to Bussiness MAX
{:ok, user} = User.by_email("tsvetozar.penov+stripetestuserstage@gmail.com")
{:ok, subscription} = Sanbase.Billing.Subscription.subscribe2(user, bussiness_max_yearly_plan, "pm_card_visa")
Downgrade to Sanbase PRO
{:ok, user} = User.by_email("tsvetozar.penov+stripetestuserstage@gmail.com")
{:ok, subscription} = Sanbase.Billing.Subscription.subscribe2(user, sanbase_pro_monthly_plan, "pm_card_visa")
Upgrade user subscription to yearly
{:ok, subscription} = Subscription.update_subscription(subscription, sanbase_pro_yearly_plan)
Cancel user subscription (at the end of the billing period)
Subscription.cancel_subscription_at_period_end(subscription)
Teardown: Delete everything for this user in Stripe
{:ok, user} = User.by_email("tsvetozar.penov+stripetestuserstage@gmail.com")
if user.stripe_customer_id do
Sanbase.StripeApi.delete_customer(user)
end