Skip to content

Commit

Permalink
Upgrade Stripe API version
Browse files Browse the repository at this point in the history
Bumps the API version to the latest version support by stripe-ruby-mock.

Also remove gem lock allowing us to upgrade both stripe-ruby-mock and
the main stripe gem.

Fixes broken tests and other changes which aren't testable, such as:
1. Invoice#date being rename Invoice#created
2. Plan#name being moved to Plan#product & Product#name and not loaded/
   expanded automatically.
  • Loading branch information
gbp committed Oct 10, 2024
1 parent 8e05eb2 commit 6eda678
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 54 deletions.
5 changes: 2 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ gem 'sidekiq', '~> 6.5.12'
gem 'sidekiq-limit_fetch', '~> 4.4.1'
gem 'statistics2', '~> 0.54'
gem 'strip_attributes', git: 'https://github.com/mysociety/strip_attributes.git', branch: 'globalize3-rails7'
gem 'stripe', '~> 5.55.0'
gem 'stripe', '~> 11.7.0'
gem 'syck', '~> 1.4.1', require: false
gem 'syslog_protocol', '~> 0.9.0'
gem 'vpim', '~> 24.2.20'
Expand Down Expand Up @@ -181,8 +181,7 @@ group :test do
gem 'simplecov', '~> 0.22.0'
gem 'simplecov-lcov', '~> 0.7.0'
gem 'capybara', '~> 3.40.0'
gem 'stripe-ruby-mock', git: 'https://github.com/stripe-ruby-mock/stripe-ruby-mock',
ref: '6ceea96'
gem 'stripe-ruby-mock', '~> 4.0.0'
gem 'rails-controller-testing'
end

Expand Down
20 changes: 7 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ GIT
concurrent-ruby (~> 1.0)
rack (> 1, < 3)

GIT
remote: https://github.com/stripe-ruby-mock/stripe-ruby-mock
revision: 6ceea9679bb573cb8bc6830f1bdf670b220a9859
ref: 6ceea96
specs:
stripe-ruby-mock (3.1.0.rc3)
dante (>= 0.2.0)
multi_json (~> 1.0)
stripe (> 5, < 6)

PATH
remote: gems/alaveteli_features
specs:
Expand Down Expand Up @@ -532,7 +522,11 @@ GEM
statistics2 (0.54)
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stripe (5.55.0)
stripe (11.7.0)
stripe-ruby-mock (4.0.0)
dante (>= 0.2.0)
multi_json (~> 1.0)
stripe (> 5, < 12)
syck (1.4.1)
syslog_protocol (0.9.2)
text (1.3.1)
Expand Down Expand Up @@ -668,8 +662,8 @@ DEPENDENCIES
statistics2 (~> 0.54)
stimulus-rails (~> 1.3.4)
strip_attributes!
stripe (~> 5.55.0)
stripe-ruby-mock!
stripe (~> 11.7.0)
stripe-ruby-mock (~> 4.0.0)
syck (~> 1.4.1)
syslog_protocol (~> 0.9.0)
turbo-rails (~> 2.0.10)
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/alaveteli_pro/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def index
end

def show
stripe_plan = Stripe::Plan.retrieve(plan_name)
stripe_plan = Stripe::Plan.retrieve(
id: plan_name, expand: ['product']
)
@plan = AlaveteliPro::WithTax.new(stripe_plan)
rescue Stripe::InvalidRequestError
raise ActiveRecord::RecordNotFound
Expand Down
10 changes: 3 additions & 7 deletions app/controllers/alaveteli_pro/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,9 @@ def destroy
@customer = current_user.pro_account.try(:stripe_customer)
raise ActiveRecord::RecordNotFound unless @customer

@subscription = Stripe::Subscription.retrieve(params[:id])

unless @subscription.customer == @customer.id
raise ActiveRecord::RecordNotFound
end

@subscription.delete(at_period_end: true)
@subscription = current_user.pro_account.subscriptions.
retrieve(params[:id])
@subscription.update(cancel_at_period_end: true)

flash[:notice] = _('You have successfully cancelled your subscription ' \
'to {{pro_site_name}}',
Expand Down
2 changes: 1 addition & 1 deletion app/models/alaveteli_pro/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def paid?
end

# attributes
def date
def created
Time.at(super).to_date
end

Expand Down
8 changes: 8 additions & 0 deletions app/models/alaveteli_pro/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def require_authorisation?
].include?(payment_intent.status)
end

def update(attributes)
__setobj__(Stripe::Subscription.update(id, attributes))
end

def delete
Stripe::Subscription.cancel(id)
end

private

def method_missing(*args)
Expand Down
7 changes: 6 additions & 1 deletion app/models/pro_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def update_source
end

def stripe_customer!
Stripe::Customer.retrieve(stripe_customer_id) if stripe_customer_id
return unless stripe_customer_id

Stripe::Customer.retrieve(
id: stripe_customer_id,
expand: ['subscriptions.data.plan.product']
)
end
end
2 changes: 1 addition & 1 deletion app/views/alaveteli_pro/invoices/_invoice.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<li>
<strong><%= invoice.date.to_fs(:long) %></strong>
<strong><%= invoice.created.to_fs(:long) %></strong>

<%= _('Invoice {{invoice_number}} for {{invoice_amount}}',
invoice_number: invoice.number,
Expand Down
2 changes: 1 addition & 1 deletion app/views/alaveteli_pro/plans/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<h3><%= _('Selected plan') %></h3>
<div class="plan-overview">
<div class="plan-overview__desc">
<%= @plan.name %>
<%= @plan.product.name %>
</div>
<div class="plan-overview__details">
<%= billing_frequency(@plan.interval) %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div class="plan-overview">
<div class="plan-overview__desc">
<%= subscription.plan.name %>
<%= subscription.plan.product.name %>
</div>

<div class="plan-overview__details">
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/stripe.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Stripe.api_key = AlaveteliConfiguration.stripe_secret_key
Stripe.api_version = '2017-01-27'
Stripe.api_version = '2020-03-02'
Stripe.enable_telemetry = false

module Stripe
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/alaveteli_pro/plans_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
subscription =
Stripe::Subscription.create(customer: customer, plan: 'pro')

subscription.delete
Stripe::Subscription.cancel(subscription.id)
user.create_pro_account(stripe_customer_id: customer.id)
get :show, params: { id: 'pro' }
end
Expand Down
29 changes: 20 additions & 9 deletions spec/controllers/alaveteli_pro/subscriptions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -898,18 +898,29 @@
Stripe::Subscription.create(customer: customer, plan: plan.id)
end

it 'raises an error' do
before do
sign_in user
expect {
delete :destroy, params: { id: other_subscription.id }
}.to raise_error ActiveRecord::RecordNotFound
delete :destroy, params: { id: other_subscription.id }
end

it 'sends an exception email' do
mail = ActionMailer::Base.deliveries.first
expect(mail.subject).to match(/Stripe::InvalidRequestError/)
end

it 'renders an error message' do
expect(flash[:error]).to match(/There was a problem/)
end

it 'redirects to the subscriptions page' do
expect(response).to redirect_to(subscriptions_path)
end
end

context 'when we are rate limited' do
before do
error = Stripe::RateLimitError.new
StripeMock.prepare_error(error, :cancel_subscription)
StripeMock.prepare_error(error, :update_subscription)
delete :destroy, params: { id: subscription.id }
end

Expand All @@ -930,7 +941,7 @@
context 'when Stripe receives an invalid request' do
before do
error = Stripe::InvalidRequestError.new('message', 'param')
StripeMock.prepare_error(error, :cancel_subscription)
StripeMock.prepare_error(error, :update_subscription)
delete :destroy, params: { id: subscription.id }
end

Expand All @@ -951,7 +962,7 @@
context 'when we cannot authenticate with Stripe' do
before do
error = Stripe::AuthenticationError.new
StripeMock.prepare_error(error, :cancel_subscription)
StripeMock.prepare_error(error, :update_subscription)
delete :destroy, params: { id: subscription.id }
end

Expand All @@ -972,7 +983,7 @@
context 'when we cannot connect to Stripe' do
before do
error = Stripe::APIConnectionError.new
StripeMock.prepare_error(error, :cancel_subscription)
StripeMock.prepare_error(error, :update_subscription)
delete :destroy, params: { id: subscription.id }
end

Expand All @@ -993,7 +1004,7 @@
context 'when Stripe returns a generic error' do
before do
error = Stripe::StripeError.new
StripeMock.prepare_error(error, :cancel_subscription)
StripeMock.prepare_error(error, :update_subscription)
delete :destroy, params: { id: subscription.id }
end

Expand Down
8 changes: 1 addition & 7 deletions spec/lib/alaveteli_pro/metrics_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,7 @@
let!(:pending_cancel_sub) do
subscription = Stripe::Subscription.create(customer: customer,
plan: pro_plan.id)

# NOTE: - in later API versions, at_period_end is no longer
# available for delete so we'd have to call something like
# this instead:
# Stripe::Subscription.update(subscription.id,
# cancel_at_period_end: true)
subscription.delete(at_period_end: true)
Stripe::Subscription.update(subscription.id, cancel_at_period_end: true)
subscription
end

Expand Down
6 changes: 3 additions & 3 deletions spec/models/alaveteli_pro/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
id: 'in_123',
status: 'open',
charge: 'ch_123',
date: 1722211200,
created: 1722211200,
amount_paid: 0
)
end
Expand Down Expand Up @@ -53,10 +53,10 @@
end
end

describe '#date' do
describe '#created' do
it 'returns a date object for the invoice' do
with_env_tz 'UTC' do
expect(invoice.date).to eq(Date.new(2024, 7, 29))
expect(invoice.created).to eq(Date.new(2024, 7, 29))
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions spec/models/pro_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
end

let(:past_due_sub) do
subscription.save
subscription
StripeMock.mark_subscription_as_past_due(subscription)
Stripe::Subscription.retrieve(subscription.id)
end
Expand Down Expand Up @@ -158,7 +158,7 @@
subject { pro_account.subscription? }

context 'when there is an active subscription' do
before { subscription.save }
before { subscription }
it { is_expected.to eq true }
end

Expand All @@ -168,12 +168,18 @@
end

context 'when there is an expiring subscription' do
before { subscription.delete(at_period_end: true) }
before do
Stripe::Subscription.update(subscription.id, cancel_at_period_end: true)
end

it { is_expected.to eq true }
end

context 'when an existing subscription is cancelled' do
before { subscription.delete }
before do
Stripe::Subscription.cancel(subscription.id)
end

it { is_expected.to eq false }
end

Expand Down

0 comments on commit 6eda678

Please sign in to comment.