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

[pro#622] Update Pro subscription sign up #7925

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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: 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
Loading