-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from goinvo/fermion/better-subscription-manage…
…ment Better subscription management
- Loading branch information
Showing
70 changed files
with
5,246 additions
and
5,518 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Stripe | ||
class CustomerUpdated | ||
def initialize(customer) | ||
@customer = customer | ||
end | ||
|
||
def call | ||
# update the customer's default payment method, email, name, etc. | ||
company = Company.find_by(stripe_id: @customer.id) | ||
|
||
updates = { | ||
customer_email: @customer.email, | ||
customer_name: @customer.name, | ||
} | ||
|
||
if @customer.invoice_settings.default_payment_method.present? | ||
payment_method = Stripe::PaymentMethod.retrieve(@customer.invoice_settings.default_payment_method) | ||
updates = updates.merge( | ||
default_payment_method: @customer.invoice_settings.default_payment_method, | ||
credit_card_brand: payment_method.card.brand, | ||
credit_card_last_four: payment_method.card.last4, | ||
credit_card_exp_month: payment_method.card.exp_month, | ||
credit_card_exp_year: payment_method.card.exp_year | ||
) | ||
end | ||
|
||
company.subscription.update(updates) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Stripe | ||
class SubscriptionUpdated | ||
def initialize(subscription) | ||
@subscription = subscription | ||
end | ||
|
||
def call | ||
company = Company.find_by(stripe_id: @subscription.customer) | ||
previous_quantity = company.subscription.quantity | ||
|
||
canceled_at = @subscription.canceled_at.present? ? Time.at(@subscription.canceled_at) : nil | ||
company.subscription.assign_attributes( | ||
status: @subscription.status, | ||
trial_end: Time.at(@subscription.trial_end), | ||
stripe_id: @subscription.id, | ||
stripe_price_id: @subscription.items.data.first.price.id, | ||
plan_amount: @subscription.items.data.first.price.unit_amount, | ||
quantity: @subscription.quantity, | ||
item_id: @subscription.items.data.first.id, | ||
current_period_start: Time.at(@subscription.current_period_start), | ||
current_period_end: Time.at(@subscription.current_period_end), | ||
canceled_at: canceled_at | ||
) | ||
|
||
company.subscription.save! | ||
|
||
if previous_quantity != company.subscription.quantity | ||
BillingMailer.subscription_updated(company, company.subscription.quantity).deliver_later | ||
end | ||
end | ||
end | ||
end |
9 changes: 0 additions & 9 deletions
9
app/components/settings/billing/needs_subscription_component.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/components/settings/billing/subscription_active_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Settings | ||
module Billing | ||
class SubscriptionActiveComponent < ViewComponent::Base | ||
def initialize(company:) | ||
@company = company | ||
end | ||
|
||
def subscription | ||
@company.subscription | ||
end | ||
|
||
def subscription_percentage | ||
time_today = Time.now.at_beginning_of_day.to_i | ||
start_time = subscription.current_period_start.at_beginning_of_day.to_i | ||
end_time = subscription.current_period_end.at_beginning_of_day.to_i | ||
numerator = time_today - start_time | ||
denominator = end_time - start_time | ||
|
||
"#{((numerator.to_f / denominator.to_f) * 100).round}%" | ||
end | ||
|
||
def credit_card_brand | ||
subscription.credit_card_brand | ||
end | ||
|
||
def credit_card_last_four | ||
subscription.credit_card_last_four | ||
end | ||
|
||
def credit_card_exp_month | ||
subscription.credit_card_exp_month | ||
end | ||
|
||
def credit_card_exp_year | ||
subscription.credit_card_exp_year | ||
end | ||
|
||
def customer_name | ||
subscription.customer_name | ||
end | ||
|
||
def customer_email | ||
subscription.customer_email | ||
end | ||
|
||
def subtotal | ||
amount = Money.new(subscription.plan_amount, "USD") * subscription.quantity | ||
Money.new(amount, "USD") | ||
end | ||
end | ||
end | ||
end |
86 changes: 86 additions & 0 deletions
86
app/components/settings/billing/subscription_canceled_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<%= render(Settings::TabsComponent.new) %> | ||
|
||
<div class="bg-white p-4 mt-4"> | ||
<main class="mx-auto max-w-2xl pb-24 sm:px-6 lg:max-w-7xl lg:px-8"> | ||
<section aria-labelledby="subscription-heading" class="mt-6"> | ||
<h2 id="subscription-heading" class="sr-only">Products purchased</h2> | ||
|
||
<div class="space-y-8"> | ||
<div class="border-b border-t border-gray-200 bg-white shadow-sm sm:rounded-lg sm:border"> | ||
<div class="px-4 py-6 sm:px-6 lg:grid lg:gap-x-8 lg:p-8"> | ||
<div class="sm:flex lg:col-span-7"> | ||
<div class="aspect-h-1 aspect-w-1 w-full flex-shrink-0 overflow-hidden rounded-lg sm:aspect-none sm:h-40 sm:w-40 hidden md:block"> | ||
<%= image_tag "staffplan-logo-notext.png", class: "h-full w-full object-cover object-center sm:h-full sm:w-full" %> | ||
</div> | ||
|
||
<div class="mt-6 sm:ml-6 sm:mt-0"> | ||
<h3 class="text-base font-medium text-gray-900"> | ||
<a href="#">Your StaffPlan subscription has been canceled.</a> | ||
</h3> | ||
<p class="mt-3 text-sm text-gray-500"> | ||
You can continue to use StaffPlan until <%= subscription.current_period_end.iso8601 %>. After that, you will lose access to StaffPlan and your account will be in read-only mode. | ||
</p> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
|
||
<!-- Billing --> | ||
<% if subscription.default_payment_method %> | ||
<section aria-labelledby="billing-heading" class="mt-16"> | ||
<h2 id="billing-heading" class="sr-only">Billing Summary</h2> | ||
|
||
<div class="bg-gray-100 px-4 py-6 sm:rounded-lg sm:px-6 lg:grid lg:grid-cols-12 lg:gap-x-8 lg:px-8 lg:py-8"> | ||
<dl class="grid grid-cols-2 gap-6 text-sm sm:grid-cols-2 md:gap-x-8 lg:col-span-7"> | ||
<div> | ||
<dt class="font-medium text-gray-900">Billing contact</dt> | ||
<dd class="mt-3 text-gray-500"> | ||
<span class="block"><%= customer_name %></span> | ||
<span class="block"><%= customer_email %></span> | ||
</dd> | ||
</div> | ||
<div> | ||
<dt class="font-medium text-gray-900">Payment information</dt> | ||
<dd class="-ml-4 -mt-1 flex flex-wrap"> | ||
<div class="ml-4 mt-4 flex-shrink-0"> | ||
<%= image_tag image_path("credit-cards/#{credit_card_brand}.svg"), style: "height: 24px;", class: "h-6 w-auto" %> | ||
<p class="sr-only"><%= credit_card_brand.capitalize %>></p> | ||
</div> | ||
<div class="ml-4 mt-4"> | ||
<p class="text-gray-900">Ending with <%= credit_card_last_four %></p> | ||
<p class="text-gray-600">Expires <%= credit_card_exp_month %> / <%= credit_card_exp_year %></p> | ||
</div> | ||
</dd> | ||
</div> | ||
</dl> | ||
|
||
<dl class="mt-8 divide-y divide-gray-200 text-sm lg:col-span-5 lg:mt-0"> | ||
<div class="flex items-center justify-between pt-4"> | ||
<dt class="font-medium text-gray-900">Total monthly charges:</dt> | ||
<dd class="font-medium text-indigo-600"> | ||
<%= number_to_currency(subtotal) %> | ||
</dd> | ||
</div> | ||
</dl> | ||
</div> | ||
</section> | ||
<% end %> | ||
|
||
<section aria-labelledby="cancel-subscription" class="mt-16"> | ||
<% if subscription.can_be_resumed? %> | ||
<div class="px-4 py-6 sm:rounded-lg sm:px-6 lg:gap-x-8 lg:px-8 lg:py-8 text-center"> | ||
Changed your mind? You can still manage your StaffPlan subscription until <%= subscription.current_period_end.iso8601 %>. Click the manage button below to renew your subscription. | ||
|
||
<%= link_to "Manage StaffPlan Subscription", Rails.application.credentials.stripe_customer_portal_url, class: "mt-10 block w-full rounded-md bg-indigo-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" %> | ||
</div> | ||
<% else %> | ||
<div class="px-4 py-6 sm:rounded-lg sm:px-6 lg:gap-x-8 lg:px-8 lg:py-8 text-center"> | ||
We're sorry to see you go! If you've changed your mind, please contact us so that we can resume your subscription. | ||
</div> | ||
<% end %> | ||
</section> | ||
|
||
</main> | ||
</div> |
52 changes: 52 additions & 0 deletions
52
app/components/settings/billing/subscription_canceled_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Settings | ||
module Billing | ||
class SubscriptionCanceledComponent < ViewComponent::Base | ||
def initialize(company:) | ||
@company = company | ||
end | ||
|
||
def subscription | ||
@company.subscription | ||
end | ||
|
||
def subscription_percentage | ||
time_today = Time.now.at_beginning_of_day.to_i | ||
start_time = subscription.current_period_start.at_beginning_of_day.to_i | ||
end_time = subscription.current_period_end.at_beginning_of_day.to_i | ||
numerator = time_today - start_time | ||
denominator = end_time - start_time | ||
|
||
"#{((numerator.to_f / denominator.to_f) * 100).round}%" | ||
end | ||
|
||
def credit_card_brand | ||
subscription.credit_card_brand | ||
end | ||
|
||
def credit_card_last_four | ||
subscription.credit_card_last_four | ||
end | ||
|
||
def credit_card_exp_month | ||
subscription.credit_card_exp_month | ||
end | ||
|
||
def credit_card_exp_year | ||
subscription.credit_card_exp_year | ||
end | ||
|
||
def customer_name | ||
subscription.customer_name | ||
end | ||
|
||
def customer_email | ||
subscription.customer_email | ||
end | ||
|
||
def subtotal | ||
amount = Money.new(subscription.plan_amount, "USD") * subscription.quantity | ||
Money.new(amount, "USD") | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.