Skip to content

Instantly share code, notes, and snippets.

@benbagley
Last active May 18, 2017 14:39
Show Gist options
  • Save benbagley/b6314a21c83b44930105dc924efc7eaf to your computer and use it in GitHub Desktop.
Save benbagley/b6314a21c83b44930105dc924efc7eaf to your computer and use it in GitHub Desktop.
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