Skip to content

Commit

Permalink
Add Pro Customer
Browse files Browse the repository at this point in the history
Further simplification of controllers by moving default source handling
into a delegated class.
  • Loading branch information
gbp committed Oct 18, 2024
1 parent 441964c commit 196e483
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
6 changes: 0 additions & 6 deletions app/controllers/alaveteli_pro/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ class AlaveteliPro::SubscriptionsController < AlaveteliPro::BaseController
def index
@customer = current_user.pro_account.try(:stripe_customer)
@subscriptions = current_user.pro_account.subscriptions

if @customer.default_source
@card =
@customer.
sources.select { |card| card.id == @customer.default_source }.first
end
end

# TODO: remove reminder of Stripe params once shipped
Expand Down
12 changes: 12 additions & 0 deletions app/models/alaveteli_pro/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
##
# A wrapper for a Stripe::Customer
#
class AlaveteliPro::Customer < SimpleDelegator
def self.retrieve(id)
new(Stripe::Customer.retrieve(id))
end

def default_source
@default_source ||= sources.find { |c| c.id == __getobj__.default_source }
end
end
5 changes: 1 addition & 4 deletions app/models/pro_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ def update_stripe_customer
def stripe_customer!
return unless stripe_customer_id

Stripe::Customer.retrieve(
id: stripe_customer_id,
expand: ['subscriptions.data.plan.product']
)
AlaveteliPro::Customer.retrieve(stripe_customer_id)
end
end
2 changes: 1 addition & 1 deletion app/views/alaveteli_pro/subscriptions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<div class="settings-section">
<h3><%= _('Your payment details') %></h3>
<% if @customer.default_source %>
<%= render partial: 'card', locals: { card: @card } %>
<%= render partial: 'card', locals: { card: @customer.default_source } %>
<% else %>
<%= render partial: 'add_card' %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,11 +772,6 @@
expect(assigns[:subscriptions].first.id).
to eq(customer.subscriptions.first.id)
end

it 'assigns the default source as card' do
get :index
expect(assigns[:card].id).to eq(customer.default_source)
end
end
end

Expand Down
51 changes: 51 additions & 0 deletions spec/models/alaveteli_pro/customer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'
require 'stripe_mock'

RSpec.describe AlaveteliPro::Customer do
describe '.retrieve' do
it 'returns a new customer instance when successful' do
stripe_customer = double('Stripe::Customer')
allow(Stripe::Customer).to receive(:retrieve).with('cus_123').
and_return(stripe_customer)

customer = described_class.retrieve('cus_123')
expect(customer).to be_an_instance_of(described_class)
expect(customer).to eq(stripe_customer)
end

it 'raises Stripe::InvalidRequestError when customer does not exist' do
StripeMock.start

error = Stripe::InvalidRequestError.new('', '')
StripeMock.prepare_error(error)

expect { described_class.retrieve('cus_123') }.to raise_error(
Stripe::InvalidRequestError
)

StripeMock.stop
end
end

describe '#default_source' do
let(:stripe_customer) do
double('Stripe::Customer', default_source: 'card_123')
end

let(:customer) { described_class.new(stripe_customer) }

it 'returns the default source from the customer sources' do
source1 = double('Stripe::Card', id: 'card_123')
source2 = double('Stripe::Card', id: 'card_456')
allow(customer).to receive(:sources).and_return([source1, source2])

expect(customer.default_source).to eq(source1)
end

it 'memoizes the default source' do
source = double('Stripe::Card', id: 'card_123')
expect(customer).to receive(:sources).once.and_return([source])
2.times { expect(customer.default_source) }
end
end
end

0 comments on commit 196e483

Please sign in to comment.