diff --git a/.gitignore b/.gitignore index 3d0a5a46..c8fc2c7e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ /tmp .DS_Store public/uploads +config/config.yml diff --git a/Gemfile b/Gemfile index f2312016..4ef68647 100644 --- a/Gemfile +++ b/Gemfile @@ -39,6 +39,10 @@ gem "stripe" gem "aasm" +gem "settingslogic" +gem "will_paginate" +gem "ransack" + group :development do gem "annotate" diff --git a/Gemfile.lock b/Gemfile.lock index 8953a06a..430116f9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -156,6 +165,7 @@ GEM json (>= 1.8.0) warden (1.2.3) rack (>= 1.0) + will_paginate (3.0.4) PLATFORMS ruby @@ -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 diff --git a/app/controllers/card_charges_controller.rb b/app/controllers/card_charges_controller.rb index 3c612013..51cd0914 100644 --- a/app/controllers/card_charges_controller.rb +++ b/app/controllers/card_charges_controller.rb @@ -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 diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index a332486d..407dcc20 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -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 diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index e2ed10ec..70af446c 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,5 +1,7 @@ class ProductsController < ApplicationController + before_filter :validate_search_key, :only => [:search] + def index @products = Product.order("id DESC") end @@ -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 diff --git a/app/models/setting.rb b/app/models/setting.rb new file mode 100644 index 00000000..ab37808e --- /dev/null +++ b/app/models/setting.rb @@ -0,0 +1,4 @@ +class Setting < Settingslogic + source "#{Rails.root}/config/config.yml" + namespace Rails.env +end \ No newline at end of file diff --git a/app/services/stripe_charge.rb b/app/services/stripe_charge.rb new file mode 100644 index 00000000..cdf489d9 --- /dev/null +++ b/app/services/stripe_charge.rb @@ -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 \ No newline at end of file diff --git a/app/views/common/_navbar.html.erb b/app/views/common/_navbar.html.erb index 41356da3..1655302c 100644 --- a/app/views/common/_navbar.html.erb +++ b/app/views/common/_navbar.html.erb @@ -14,6 +14,17 @@ + + <%= form_tag search_products_path, :class => "navbar-form navbar-left" do %> +