Skip to content

Commit

Permalink
Merge branch 'add-stripe-token-information' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Sep 29, 2023
2 parents 60cde68 + dd41d8e commit da3e283
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions app/controllers/alaveteli_pro/payment_methods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/alaveteli_pro/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions app/models/pro_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions spec/models/pro_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit da3e283

Please sign in to comment.