Skip to content

Instantly share code, notes, and snippets.

@nateflink
Created November 27, 2018 18:43
Show Gist options
  • Save nateflink/4e45a603d74cbeecea1c965b848b45ed to your computer and use it in GitHub Desktop.
Save nateflink/4e45a603d74cbeecea1c965b848b45ed to your computer and use it in GitHub Desktop.
ef save_payment
form_params = params.permit(
:stripeToken,
).deep_stringify_keys
# # Create a Stripe Customer:
customer = nil
begin
customer = Stripe::Customer.create(
:email => current_user.email,
:source => form_params['stripeToken'],
)
rescue => e
Rails.logger.warn { "Encountered an error create Stripe customer for: #{current_user.email}, #{e.message}" }
flash[:alert] = 'Transaction error. Your card has not been charged. Please try again.'
render :action => "payment"
return
end
if customer['email'].to_s != current_user.email
Rails.logger.warn { "Stripe customer email doesn't match #{customer.to_s}" }
flash[:alert] = 'Transaction error. Your card has not been charged. Please try again.'
render :action => "payment"
return
else
current_user.stripe_customer_token = customer.id
current_user.save
end
# # Charge the Customer instead of the card:
charge = nil
begin
charge = Stripe::Charge.create(
:amount => 1000, #$10
:currency => "usd",
:customer => customer.id,
)
rescue => e
Rails.logger.warn { "Encountered an error charging card: #{current_user.email}, #{e.message}" }
flash[:alert] = 'Card error. Your card has not been charged. Please try again.'
render :action => "payment"
return
end
current_user.stripe_charge_id_opencall_2017 = charge.id
current_user.save
flash[:notice] = 'The provided card was charged $10 for this application'
redirect_to dashboard_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment