Skip to content

Instantly share code, notes, and snippets.

@ornerymoose
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ornerymoose/10708071 to your computer and use it in GitHub Desktop.
Save ornerymoose/10708071 to your computer and use it in GitHub Desktop.
class ChargesController < ApplicationController
after_filter :destroy_cart, :only => [:show]
def new
@cart = Cart.find(params[:id])
end
def create
begin
@cart = Cart.find(params[:id])
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @cart.total_price * 100,
:description => 'California Love Customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
respond_to do |format|
format.html { redirect_to charge_path(@cart) }
format.json { head :ok }
end
end
def show
@cart = current_cart
@current_cust = Stripe::Customer.retrieve(self.stripe_customer_id).email
end
private
def destroy_cart
@cart = current_cart
@cart.destroy
session[:cart_id] = nil
end
end
<h1>Your Order and Charge</h1>
<p>Hello <%= @current_cust %></p>
Your order was a success!
<br>
<p> Here's what you bought:</p>
<ul>
<% @cart.line_items.each do |item| %>
<li><%= item.quantity %> &times; <%= item.size %> <%= item.color %> <%= item.product.name %><br>
at $<%= item.product.price %> each</li>
<% end %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment