Add button to buy extra-storage subscriptions

This commit is contained in:
Jorge Manrubia
2025-12-17 08:46:27 +01:00
parent a1a67c65f5
commit 8205823588
3 changed files with 22 additions and 3 deletions
@@ -9,10 +9,10 @@ class Account::SubscriptionsController < ApplicationController
session = Stripe::Checkout::Session.create \
customer: find_or_create_stripe_customer,
mode: "subscription",
line_items: [ { price: Plan.paid.stripe_price_id, quantity: 1 } ],
line_items: [ { price: plan_param.stripe_price_id, quantity: 1 } ],
success_url: account_subscription_url + "?session_id={CHECKOUT_SESSION_ID}",
cancel_url: account_subscription_url,
metadata: { account_id: Current.account.id, plan_key: Plan.paid.key },
metadata: { account_id: Current.account.id, plan_key: plan_param.key },
automatic_tax: { enabled: true },
tax_id_collection: { enabled: true },
billing_address_collection: "required",
@@ -22,6 +22,10 @@ class Account::SubscriptionsController < ApplicationController
end
private
def plan_param
@plan_param ||= Plan[params[:plan_key]] || Plan.paid
end
def set_stripe_session
@stripe_session = Stripe::Checkout::Session.retrieve(params[:session_id]) if params[:session_id]
end
@@ -36,7 +40,7 @@ class Account::SubscriptionsController < ApplicationController
def create_stripe_customer
Stripe::Customer.create(email: Current.user.identity.email_address, name: Current.account.name, metadata: { account_id: Current.account.id }).tap do |customer|
Current.account.create_subscription!(stripe_customer_id: customer.id, plan_key: Plan.paid.key, status: "incomplete")
Current.account.create_subscription!(stripe_customer_id: customer.id, plan_key: plan_param.key, status: "incomplete")
end
end
end
@@ -7,6 +7,7 @@
<p class="margin-none-block-start">If you'd like to keep using Fizzy past <%= Plan.free.card_limit %> cards, it's only <strong>$<%= Plan.paid.price %>/month</strong> for <strong>unlimited cards</strong> + <strong>unlimited users</strong>. You'll also get <strong><%= storage_to_human_size(Plan.paid.storage_limit) %></strong> of storage. </p>
<%= button_to "Upgrade to #{Plan.paid.name} for $#{ Plan.paid.price }/month", account_subscription_path, class: "btn settings-subscription__button txt-medium", form: { data: { turbo: false } } %>
<%= button_to "Upgrade to #{Plan.paid_with_extra_storage.name} for $#{ Plan.paid_with_extra_storage.price }/month", account_subscription_path(plan_key: Plan.paid_with_extra_storage.key), class: "btn settings-subscription__button txt-medium", form: { data: { turbo: false } } %>
<p>Cancel anytime, no contracts, take your data with you whenever.</p>
<p class="settings-subscription__footer txt-small margin-none-block-end">Right now you're on the <strong><%= Current.account.plan.name %></strong> plan which includes <%= Plan.free.card_limit %> cards, unlimited users, and <%= storage_to_human_size(Plan.free.storage_limit) %> of storage.</p>
@@ -43,4 +43,18 @@ class Account::SubscriptionsControllerTest < ActionDispatch::IntegrationTest
post account_subscription_path
assert_response :forbidden
end
test "create with custom plan_key redirects to stripe checkout" do
customer = OpenStruct.new(id: "cus_test_37signals")
session = OpenStruct.new(url: "https://checkout.stripe.com/session123")
Stripe::Customer.stubs(:retrieve).returns(customer)
Stripe::Checkout::Session.stubs(:create).with do |params|
params[:metadata][:plan_key] == :monthly_extra_storage_v1
end.returns(session)
post account_subscription_path(plan_key: :monthly_extra_storage_v1)
assert_redirected_to "https://checkout.stripe.com/session123"
end
end