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

Stripe API upgrade #8405

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
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
7 changes: 3 additions & 4 deletions app/controllers/alaveteli_pro/stripe_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ def invoice_payment_succeeded
subscription = Stripe::Subscription.retrieve(subscription_id)
plan_name = subscription.plan.name

charge.description =
"#{ pro_site_name }: #{ plan_name }"

charge.save
Stripe::Charge.update(
charge.id, description: "#{pro_site_name}: #{plan_name}"
)
end
end

Expand Down
20 changes: 7 additions & 13 deletions app/controllers/alaveteli_pro/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ def create
@pro_account.token = @token
@pro_account.update_stripe_customer

@subscription = @pro_account.subscriptions.build
@subscription.update_attributes(
attributes = {
plan: params.require(:plan_id),
tax_percent: tax_percent,
payment_behavior: 'allow_incomplete'
)

@subscription.coupon = coupon_code if coupon_code?
}
attributes[:coupon] = coupon_code if coupon_code?

@subscription.save
@subscription = @pro_account.subscriptions.create(attributes)

rescue ProAccount::CardError,
Stripe::CardError => e
Expand Down Expand Up @@ -151,13 +149,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: 2 additions & 5 deletions app/models/alaveteli_pro/subscription_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ def initialize(customer)
@customer = customer
end

def build
def create(attributes = {})
AlaveteliPro::Subscription.new(
Stripe::Subscription.new.tap do |subscription|
params = { customer: @customer }
subscription.update_attributes(params)
end
Stripe::Subscription.create(attributes.merge(customer: @customer.id))
)
end

Expand Down
38 changes: 20 additions & 18 deletions app/models/pro_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,32 @@ def update_stripe_customer
return unless feature_enabled?(:pro_pricing)

@subscriptions = nil unless stripe_customer
@stripe_customer = stripe_customer || Stripe::Customer.new

update_email
update_source
attributes = {}
attributes[:email] = user.email if stripe_customer.try(:email) != user.email
attributes[:source] = @token.id if @token

@stripe_customer = (
if attributes.empty?
stripe_customer
elsif stripe_customer
Stripe::Customer.update(stripe_customer.id, attributes)
else
Stripe::Customer.create(attributes)
end
)

stripe_customer.save
update(stripe_customer_id: stripe_customer.id)
update(stripe_customer_id: @stripe_customer.id)
end

private

def update_email
return unless stripe_customer.try(:email) != user.email

stripe_customer.email = user.email
end

def update_source
return unless @token

stripe_customer.source = @token.id
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
5 changes: 5 additions & 0 deletions doc/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Highlighted Features

* Upgrade Stripe API version (Graeme Porteous)
* Fix script/mailin when multiple EXCEPTION_NOTIFICATIONS_TO addresses are
specified (Graeme Porteous)
* Add example logrotate configuration (Graeme Porteous)
Expand Down Expand Up @@ -127,6 +128,10 @@
`config/nginx-ssl.conf.example` and update your production configuration if
needed.

* _Note:_ If you have Pro pricing enabled, this release changes the Stripe API
version from `2017-01-27` to `2020-03-02`. No changes should be necessary to
your Stripe account.

# 0.44.0.1

## Highlighted Features
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
Loading
Loading