Last active
May 18, 2017 14:39
-
-
Save benbagley/b6314a21c83b44930105dc924efc7eaf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaymentsController < ApplicationController | |
def create | |
@product = Product.find(params[:product_id]) | |
@user = current_user | |
token = params[:stripeToken] | |
# Create the charge on Stripe's servers - this will charge the user's card | |
begin | |
byebug | |
charge = Stripe::Charge.create( | |
amount: (@product.price*100).to_i, # amount in cents, again | |
:currency => "usd", | |
:source => token, | |
:description => params[:stripeEmail], | |
:receipt_email => params[:stripeEmail], | |
) | |
if charge.paid | |
Order.create( | |
:product_id => @product.id, | |
:user_id => current_user, | |
:total => @product.price | |
) | |
end | |
rescue Stripe::CardError => e | |
body = e.json_body | |
err = body[:error] | |
flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}" | |
end | |
redirect_to product_path(@product), notice: 'Thank you for your purchase. Your payment was successfully processed, and a confirmation email has been sent.' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment