Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor payment metadata storage #95

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions app/commands/stripe/customer_updated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ def call
# update the customer's default payment method, email, name, etc.
company = Company.find_by(stripe_id: @customer.id)

if company.blank?
Rollbar.report_message("Customer not found for Stripe ID: #{@customer.id}", 'warning')
return
end

updates = {
customer_email: @customer.email,
customer_name: @customer.name,
Expand All @@ -16,12 +21,26 @@ def call
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
default_payment_method: @customer.invoice_settings.default_payment_method
)

case payment_method.type
when "card"
updates = updates.merge(
payment_metadata: {
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
}
)
when "link"
updates = updates.merge(
payment_metadata: {
email: payment_method.link.email
}
)
end
end

company.subscription.update(updates)
Expand Down
5 changes: 5 additions & 0 deletions app/commands/stripe/subscription_updated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def initialize(subscription)

def call
company = Company.find_by(stripe_id: @subscription.customer)
if company.blank?
Rollbar.report_message("Customer not found for Stripe ID: #{@customer.id}", 'warning')
return
end

previous_quantity = company.subscription.quantity

canceled_at = @subscription.canceled_at.present? ? Time.at(@subscription.canceled_at) : nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,26 @@
<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>
<% if subscription.card_payment_method? %>
<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>
<% elsif subscription.link_payment_method? %>
<div class="ml-4 mt-4">
<p class="text-gray-900">
<svg class="LinkLogo__svg" viewBox="0 0 52 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.32711 2.08111C7.32711 0.930578 8.27753 0 9.45259 0C10.6276 0 11.5781 0.930578 11.5781 2.08111C11.5781 3.23164 10.6276 4.16222 9.45259 4.16222C8.27753 4.16222 7.32711 3.23164 7.32711 2.08111ZM0 0.218434H3.80167V22.7553H0V0.218434ZM51.4297 6.68172C49.2352 11.2669 46.7813 14.6001 46.7813 14.6001L52 22.7384H47.5071L44.293 17.7133C41.0788 21.3172 37.882 23.0768 34.8061 23.0768C31.0563 23.0768 29.5183 20.4543 29.5183 17.4764C29.5183 17.1436 29.523 16.7337 29.5274 16.3383V16.3381L29.5274 16.3378C29.5316 15.9695 29.5356 15.6138 29.5356 15.3445C29.5356 11.4023 29.1209 10.2856 27.7557 10.4717C25.1464 10.827 21.1892 16.6304 18.6145 22.7553H15.0374V6.68172H18.8391V14.6847C21.0164 11.1147 23.0037 8.03528 26.2005 6.85091C28.0495 6.15721 29.622 6.46176 30.4342 6.81707C33.3891 8.08604 33.3718 11.1992 33.3373 15.3615C33.32 15.9198 33.32 16.512 33.32 17.138C33.32 18.6608 33.752 19.3207 34.8061 19.4222C35.8256 19.5237 36.6033 19.033 36.6033 19.033V0.218434H40.4049V16.3597C40.4049 16.3597 43.7055 13.4157 47.1961 6.68172H51.4297ZM11.351 6.6833H7.54938V22.7569H11.351V6.6833Z" fill="currentColor"></path>
<title>Link</title>
</svg>
</p>
<p class="text-gray-600"><%= subscription.link_email %></p>
</div>
<% end %>
</dd>
</div>
</dl>
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/webhooks/stripe_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def handle_customer_subscription_deleted(event)
# when a trial runs out? cancel the subscription.
subscription = event.data.object
company = Company.find_by(stripe_id: subscription.customer)
if company.blank?
Rollbar.report_message("Customer not found for Stripe ID: #{@customer.id}", 'warning')
return
end

company.subscription.update(
status: subscription.status,
Expand All @@ -61,6 +65,10 @@ def handle_customer_subscription_created(event)
# the relevant subscription data so we have it
subscription = event.data.object
company = Company.find_by(stripe_id: subscription.customer)
if company.blank?
Rollbar.report_message("Customer not found for Stripe ID: #{@customer.id}", 'warning')
return
end

company.subscription.update(
status: subscription.status,
Expand Down
39 changes: 38 additions & 1 deletion app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ class Subscription < ApplicationRecord
CANCELED = "canceled".freeze
UNPAID = "unpaid".freeze

CARD = "card".freeze
LINK = "link".freeze

belongs_to :company

validates :status, presence: true, inclusion: { in: [INCOMPLETE, INCOMPLETE_EXPIRED, TRIALING, ACTIVE, PAST_DUE, CANCELED, UNPAID] }
validates :payment_method_type, inclusion: { in: [CARD, LINK] }, if: -> { default_payment_method.present? }
validates :status, inclusion: { in: [INCOMPLETE, INCOMPLETE_EXPIRED, TRIALING, ACTIVE, PAST_DUE, CANCELED, UNPAID] }

def active?
status == ACTIVE
Expand All @@ -31,4 +35,37 @@ def trialing_with_payment_method?
def can_be_resumed?
canceled? && current_period_end > Time.now.utc
end

def credit_card_brand
return if default_payment_method.blank?
payment_metadata["credit_card_brand"]
end

def credit_card_last_four
return if default_payment_method.blank?
payment_metadata["credit_card_last_four"]
end

def credit_card_exp_month
return if default_payment_method.blank?
payment_metadata["credit_card_exp_month"]
end

def credit_card_exp_year
return if default_payment_method.blank?
payment_metadata["credit_card_exp_year"]
end

def link_email
return if default_payment_method.blank?
payment_metadata["email"]
end

def card_payment_method?
payment_method_type == CARD
end

def link_payment_method?
payment_method_type == LINK
end
end
10 changes: 10 additions & 0 deletions db/migrate/20240227134950_add_payment_metadata_to_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddPaymentMetadataToSubscriptions < ActiveRecord::Migration[7.1]
def change
add_column :subscriptions, :payment_method_type, :string
add_column :subscriptions, :payment_metadata, :jsonb, default: {}
remove_column :subscriptions, :credit_card_brand, :string
remove_column :subscriptions, :credit_card_last_four, :string
remove_column :subscriptions, :credit_card_exp_month, :string
remove_column :subscriptions, :credit_card_exp_year, :string
end
end
8 changes: 3 additions & 5 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading