Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. class PaymentsController < ApplicationController
  2. before_action :authenticate_user!
  3. def create
  4. @product = Product.find(params[:product_id])
  5. @user = current_user
  6. token = params[:stripeToken]
  7. # Create the charge on Stripe's servers - this will charge the user's card
  8. # byebug
  9. begin
  10. charge = Stripe::Charge.create(
  11. :amount => (@product.price * 100).to_i, # amount in cents, again
  12. :currency => 'chf',
  13. :source => token,
  14. :description => params[:stripeEmail]
  15. # :receipt_email => email: @user.email
  16. )
  17. if charge.paid
  18. Order.create(
  19. :product_id => @product.id,
  20. :user_id => @user.id,
  21. :total => @total.to_i
  22. )
  23. end
  24. rescue Stripe::CardError => e
  25. # The card has been declined
  26. body = e.json_body
  27. err = body[:error]
  28. flash[:error] = "Unfortunately, there was an error in processing your payment: #{err[:message]}"
  29. end
  30. redirect_to product_path(@product)
  31. end
  32. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement