Skip to content

Instantly share code, notes, and snippets.

@rocodev-tech
rocodev-tech / card_charges_controller.rb
Created June 22, 2014 19:51
card_charges_controller.rb
class CardChargesController < ApplicationController
before_action :authenticate_user!
def create
@order = current_user.orders.find_by_token(params[:order_id])
@amount = @order.total * 100 # in cents
Stripe.api_key = 'sk_test_iszGpQ7DUU1QsF6oBGnH8bf2'
jQuery ($) ->
$("#payment-form").submit (event) ->
$form = $(this)
# Disable the submit button to prevent repeated clicks
$form.find("button").prop "disabled", true
Stripe.createToken $form, stripeResponseHandler
# Prevent the form from submitting with the default action
false
@rocodev-tech
rocodev-tech / pay_with_credit_card.html.erb
Last active August 29, 2015 14:02
app/views/orders/pay_with_credit_card.html.erb
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_OLKKfmfN1N9YOAKx0OQZsTqQuGvie');
</script>
<%= form_tag order_card_charges_path(@order.token), :id => "payment-form", :class => "form form-horizontal" do %>
@rocodev-tech
rocodev-tech / order_mailer.rb
Created June 13, 2014 09:32
app/mailers/order_mailer.rb
class OrderMailer < ActionMailer::Base
default from: "[email protected]"
def notify_order_placed(order)
@order = order
@user = order.user
@order_items = @order.items
@order_info = @order.info
@rocodev-tech
rocodev-tech / notify_order_placed.html.erb
Last active August 29, 2015 14:02
app/views/order_mailer/notify_order_placed.html.erb
<link rel="stylesheet" type="text/css" href="/assets/application.css" />
<div class="row">
<div class="col-md-12">
<h2> 訂單明細 </h2>
<table class="table table-borderd">
<thead>
<tr>
@rocodev-tech
rocodev-tech / orders_controller.rb
Created June 13, 2014 06:14
app/controllers/orders_controller.rb
def show
@order = current_user.orders.find_by_token(params[:id])
@order_info = @order.info
@order_items = @order.items
end
@rocodev-tech
rocodev-tech / show.html.erb
Last active August 29, 2015 14:02
app/views/orders/show.html.erb
<div class="row">
<div class="col-md-12">
<h2> 訂單明細 </h2>
<table class="table table-bordered">
<thead>
<tr>
<th width="80%">商品明細</th>
<th>單價</th>
@rocodev-tech
rocodev-tech / order_info.rb
Created June 13, 2014 04:30
app/models/order_info.rb
class OrderInfo < ActiveRecord::Base
belongs_to :order
validates :billing_name , :presence => true
validates :billing_address , :presence => true
validates :shipping_name , :presence => true
validates :shipping_address , :presence => true
end
@rocodev-tech
rocodev-tech / order.rb
Created June 13, 2014 04:09
app/models/order.rb
class Order < ActiveRecord::Base
belongs_to :user
has_many :items, :class_name => "OrderItem", :dependent => :destroy
has_one :info, :class_name => "OrderInfo", :dependent => :destroy
accepts_nested_attributes_for :info
def build_item_cache_from_cart(cart)
@rocodev-tech
rocodev-tech / orders_controller.rb
Last active August 29, 2015 14:02
app/controllers/orders_controller.rb
class OrdersController < ApplicationController
before_action :authenticate_user!
def create
@order = current_user.orders.build(order_params)
if @order.save
@order.build_item_cache_from_cart(current_cart)
@order.calculate_total!(current_cart)