mirror of
https://github.com/neocities/neocities.git
synced 2025-08-06 09:34:57 +02:00
Ability to change stored credit card
This commit is contained in:
parent
b6686531c7
commit
ec306f9059
6 changed files with 86 additions and 4 deletions
|
@ -35,6 +35,9 @@ post '/plan/update' do
|
|||
end
|
||||
end
|
||||
|
||||
# Do not create customer if there is already a customer. Is this necessary?
|
||||
redirect request.referrer if current_site.stripe_subscription_id
|
||||
|
||||
site ||= parent_site
|
||||
|
||||
DB.transaction do
|
||||
|
|
|
@ -334,3 +334,29 @@ get '/settings/unsubscribe_email/?' do
|
|||
end
|
||||
erb :'settings/account/unsubscribe'
|
||||
end
|
||||
|
||||
post '/settings/update_card' do
|
||||
require_login
|
||||
|
||||
customer = Stripe::Customer.retrieve current_site.stripe_customer_id
|
||||
|
||||
old_card_ids = customer.sources.collect {|s| s.id}
|
||||
|
||||
begin
|
||||
customer.sources.create source: params[:stripe_token]
|
||||
rescue Stripe::InvalidRequestError => e
|
||||
if e.message.match /cannot use a.+token more than once/
|
||||
flash[:error] = 'Card is already being used.'
|
||||
redirect '/settings#billing'
|
||||
else
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
old_card_ids.each do |card_id|
|
||||
customer.sources.retrieve(card_id).delete
|
||||
end
|
||||
|
||||
flash[:success] = 'Card information updated.'
|
||||
redirect '/settings#billing'
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue