From dd41d8e2b6a68ba3146ab39b5df49fff5d95f33f Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 28 Sep 2023 22:12:16 +0100 Subject: [PATCH] Update Pro subscription sign up Pass the whole Stripe token through to `ProAccount`, this allow the token to by checked prior to Pro sign up. --- .../alaveteli_pro/payment_methods_controller.rb | 5 +++-- app/controllers/alaveteli_pro/subscriptions_controller.rb | 5 +++-- app/models/pro_account.rb | 8 +++++--- spec/models/pro_account_spec.rb | 4 ++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/controllers/alaveteli_pro/payment_methods_controller.rb b/app/controllers/alaveteli_pro/payment_methods_controller.rb index 770bd3cf75..9d310042ea 100644 --- a/app/controllers/alaveteli_pro/payment_methods_controller.rb +++ b/app/controllers/alaveteli_pro/payment_methods_controller.rb @@ -6,12 +6,13 @@ def update @token = Stripe::Token.retrieve(params[:stripe_token]) @pro_account = current_user.pro_account ||= current_user.build_pro_account - @pro_account.source = @token.id + @pro_account.token = @token @pro_account.update_stripe_customer flash[:notice] = _('Your payment details have been updated') - rescue Stripe::CardError => e + rescue ProAccount::CardError, + Stripe::CardError => e flash[:error] = e.message rescue Stripe::RateLimitError, diff --git a/app/controllers/alaveteli_pro/subscriptions_controller.rb b/app/controllers/alaveteli_pro/subscriptions_controller.rb index 927f527318..cf40483e1e 100644 --- a/app/controllers/alaveteli_pro/subscriptions_controller.rb +++ b/app/controllers/alaveteli_pro/subscriptions_controller.rb @@ -46,7 +46,7 @@ def create @token = Stripe::Token.retrieve(params[:stripe_token]) - @pro_account.source = @token.id + @pro_account.token = @token @pro_account.update_stripe_customer @subscription = @pro_account.subscriptions.build @@ -60,7 +60,8 @@ def create @subscription.save - rescue Stripe::CardError => e + rescue ProAccount::CardError, + Stripe::CardError => e flash[:error] = e.message rescue Stripe::RateLimitError, diff --git a/app/models/pro_account.rb b/app/models/pro_account.rb index 58a4a427e4..d668a17847 100644 --- a/app/models/pro_account.rb +++ b/app/models/pro_account.rb @@ -14,7 +14,9 @@ class ProAccount < ApplicationRecord include AlaveteliFeatures::Helpers - attr_writer :source + CardError = Class.new(StandardError) + + attr_writer :token belongs_to :user, inverse_of: :pro_account @@ -65,9 +67,9 @@ def update_email end def update_source - return unless @source + return unless @token - stripe_customer.source = @source + stripe_customer.source = @token.id end def stripe_customer! diff --git a/spec/models/pro_account_spec.rb b/spec/models/pro_account_spec.rb index 5c1b7e3f07..3f452b1e90 100644 --- a/spec/models/pro_account_spec.rb +++ b/spec/models/pro_account_spec.rb @@ -84,7 +84,7 @@ it 'sets Stripe customer default source' do old_source = customer.default_source - pro_account.source = stripe_helper.generate_card_token + pro_account.token = double(id: stripe_helper.generate_card_token) expect { pro_account.update_stripe_customer }.to change( customer, :default_source @@ -120,7 +120,7 @@ it 'does not set new Stripe customer default source' do with_feature_disabled(:alaveteli_pro) do - pro_account.source = stripe_helper.generate_card_token + pro_account.token = double(id: stripe_helper.generate_card_token) expect { pro_account.update_stripe_customer }.to_not change( customer, :default_source )