Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Created November 7, 2018 04:05
Show Gist options
  • Save staycreativedesign/5b54fa338c007e1246db28f8fa7303d0 to your computer and use it in GitHub Desktop.
Save staycreativedesign/5b54fa338c007e1246db28f8fa7303d0 to your computer and use it in GitHub Desktop.
def create
@amount = params[:amount]
@amount = @amount.gsub('$', '').gsub(',', '')
begin
@amount = Float(@amount).round(2)
rescue
flash[:error] = 'Charge not completed. Please enter a valid amount in USD ($).'
redirect_to new_charge_path
return
end
@amount = (@amount * 100).to_i # Must be an integer!
if @amount < 500
flash[:error] = 'Charge not completed. Donation amount must be at least $5.'
redirect_to new_charge_path
return
end
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
if params[:subscription] == 'no'
Stripe::Charge.create(
:amount => @amount,
:currency => 'usd',
:source => params[:stripeToken],
:description => 'Donation'
)
elsif params[:subscription] == 'yes'
Stripe::Subscription.create(
:customer => customer.id,
items: [{plan: params[:plan]}]
)
end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment