Skip to content

Instantly share code, notes, and snippets.

@ornerymoose
Created April 17, 2014 04:44
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/10953400 to your computer and use it in GitHub Desktop.
Save ornerymoose/10953400 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
email = params.delete(:stripeEmail)
@customer = Customer.new(email: email)
@cart = Cart.find(params[:id])
@current_cart = current_cart
stripe_customer = Stripe::Customer.create(
:email => email,
:card => params[:stripeToken]
)
@customer.stripe_customer_id = stripe_customer.id
@customer.save!
charge = Stripe::Charge.create(
:customer => stripe_customer.id,
:amount => @cart.total_price * 100,
:description => 'Customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
respond_to do |format|
CustomerMailer.registration_confirmation(@customer.email).deliver
format.html { redirect_to charge_path(@cart, {customer_id: @customer.id}) }
format.json { head :ok }
end
end
def show
@cart = current_cart
@customer_id = Customer.find(params[:customer_id])
end
private
def destroy_cart
@cart = current_cart
@cart.destroy
session[:cart_id] = nil
end
end
class CustomerMailer < ActionMailer::Base
default from: "emailsender@gmail.com"
def registration_confirmation(user)
@customer = user
@cart = current_cart
#attachments["rails.png"] = File.read("#{Rails.root}/public/images/rails.png")
mail(:to => "#{user}", :subject => "Your Order Love Order")
end
end
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<p>
Dear <%= @customer %>,
<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>
</p>
<p>Have a great day!</p>
</body>
</html>
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "mygmailaddress",
:password => "mygmailpassword",
:authentication => "plain",
:enable_starttls_auto => true
}
ActionMailer::Base.default_url_options[:host] = "localhost:3000"
#Mail.register_interceptor(DevelopmentMailInterceptor) if Rails.env.development?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment