Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store v3 官方解答 #46

Open
wants to merge 5 commits into
base: store-v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
/tmp
.DS_Store
public/uploads
config/config.yml
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ gem "stripe"

gem "aasm"

gem "settingslogic"
gem "will_paginate"
gem "ransack"


group :development do
gem "annotate"
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ GEM
nokogiri (1.6.2.1)
mini_portile (= 0.6.0)
orm_adapter (0.5.0)
polyamorous (1.0.0)
activerecord (>= 3.0)
polyglot (0.3.5)
rack (1.5.2)
rack-test (0.6.2)
Expand All @@ -104,6 +106,12 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.3.2)
ransack (1.2.3)
actionpack (>= 3.0)
activerecord (>= 3.0)
activesupport (>= 3.0)
i18n
polyamorous (~> 1.0.0)
rdoc (4.1.1)
json (~> 1.4)
rest-client (1.6.7)
Expand All @@ -122,6 +130,7 @@ GEM
sdoc (0.4.0)
json (~> 1.8)
rdoc (~> 4.0, < 5.0)
settingslogic (2.0.9)
simple_form (3.1.0.rc1)
actionpack (~> 4.0)
activemodel (~> 4.0)
Expand Down Expand Up @@ -156,6 +165,7 @@ GEM
json (>= 1.8.0)
warden (1.2.3)
rack (>= 1.0)
will_paginate (3.0.4)

PLATFORMS
ruby
Expand All @@ -172,12 +182,15 @@ DEPENDENCIES
letter_opener
mini_magick
rails (= 4.1.0)
ransack
roadie
sass-rails (~> 4.0.3)
sdoc (~> 0.4.0)
settingslogic
simple_form (= 3.1.0rc1)
spring
sqlite3
stripe
turbolinks
uglifier (>= 1.3.0)
will_paginate
28 changes: 10 additions & 18 deletions app/controllers/card_charges_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,21 @@ def create
@order = current_user.orders.find_by_token(params[:order_id])
@amount = @order.total * 100 # in cents

Stripe.api_key = 'sk_test_iszGpQ7DUU1QsF6oBGnH8bf2'

customer = Stripe::Customer.create(
:email => current_user.email,
:card => params[:stripeToken]
)


charge = Stripe::Charge.create(
:customer => customer.id,
charge = StripeCharge.create(
:amount => @amount,
:card => params[:stripeToken],
:description => @order.token ,
:currency => 'usd'
)

@order.set_payment_with!("credit_card")
@order.make_payment!

redirect_to order_path(@order.token), :notice => "成功完成付款"

rescue Stripe::CardError => e
flash[:error] = e.message
if charge.successful?
@order.set_payment_with!("credit_card")
@order.make_payment!
redirect_to order_path(@order.token), :notice => "成功完成付款"
else
flash[:error] = charge.error_message
render "orders/pay_with_credit_card"
end

end

end
3 changes: 1 addition & 2 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def create

if @order.save

OrderPlacingService.new(current_cart, order).place_order!

OrderPlacingService.new(current_cart, @order).place_order!

redirect_to order_path(@order.token)
else
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class ProductsController < ApplicationController

before_filter :validate_search_key, :only => [:search]

def index
@products = Product.order("id DESC")
end
Expand Down Expand Up @@ -30,5 +32,23 @@ def add_to_cart

end

def search
if @query_string.present?
search_result = Product.ransack(@search_criteria).result(:distinct => true)
@products = search_result.paginate(:page => params[:page], :per_page => 20 )
end
end


protected

def validate_search_key
@query_string = params[:q].gsub(/\\|\'|\/|\?/, "") if params[:q].present?
@search_criteria = search_criteria(@query_string)
end


def search_criteria(query_string)
{ :title_cont => query_string }
end
end
4 changes: 4 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Setting < Settingslogic
source "#{Rails.root}/config/config.yml"
namespace Rails.env
end
31 changes: 31 additions & 0 deletions app/services/stripe_charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class StripeCharge

attr_reader :error_message, :response

def initialize(options={})
@response = options[:response]
@error_message = options[:error_message]
end

def self.create(options={})

Stripe.api_key = Setting.stripe.secret_key

begin
response = Stripe::Charge.create(
amount: options[:amount],
currency: "usd",
card: options[:card],
description: options[:description]
)
new(:response => response)
rescue Stripe::CardError => e
new(:error_message => e.message)
end
end

def successful?
@response.present?
end

end
11 changes: 11 additions & 0 deletions app/views/common/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@

</ul>


<%= form_tag search_products_path, :class => "navbar-form navbar-left" do %>
<div class="form-group">

<input type="text" name="q" class="form-control" value="<%= params[:q] %>" placeholder="搜尋商品名稱" />

</div>
<button type="submit" class="btn btn-default">Submit</button>
<% end %>


<ul class="nav navbar-nav navbar-right">

<li>
Expand Down
2 changes: 1 addition & 1 deletion app/views/orders/pay_with_credit_card.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script type="text/javascript">
Stripe.setPublishableKey('pk_test_Ng4DPkOCTPNdLNiZ884ltPEq');
Stripe.setPublishableKey('<%= Setting.stripe.publishable_key %>');
</script>


Expand Down
9 changes: 9 additions & 0 deletions app/views/products/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="row">

<h2> 搜尋字串: <%= @query_string %> </h2>

<hr>

<%= render :partial => "product_item", :collection => @products , :as => :product %>

</div>
17 changes: 17 additions & 0 deletions config/config.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defaults: &defaults
app_name: "ArtStore"
stripe:
secret_key:
publishable_key:
mailgun:
username:
password:
development:
<<: *defaults

test:
<<: *defaults

production:
<<: *defaults

10 changes: 10 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mailgun.org',
port: 587,
domain: 'sandboxd321bed1624b4744b456caca384107f8.mailgun.org',
user_name: Setting.mailgun.username,
password: Setting.mailgun.password,
authentication: 'plain'
}
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
member do
post :add_to_cart
end
collection do
post :search
end
end

resources :carts do
Expand Down