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 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' |
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
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 |
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
<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 %> |
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 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 |
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
<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> |
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
def show | |
@order = current_user.orders.find_by_token(params[:id]) | |
@order_info = @order.info | |
@order_items = @order.items | |
end |
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
<div class="row"> | |
<div class="col-md-12"> | |
<h2> 訂單明細 </h2> | |
<table class="table table-bordered"> | |
<thead> | |
<tr> | |
<th width="80%">商品明細</th> | |
<th>單價</th> |
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 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 |
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 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) |
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 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) |