Skip to content

Commit

Permalink
add payment_method function
Browse files Browse the repository at this point in the history
  • Loading branch information
anndoko committed May 17, 2017
1 parent 015f1b4 commit 6ded4df
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
19 changes: 19 additions & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ def show
@product_lists = @order.product_lists
end


def pay_with_alipay
@order = Order.find_by_token(params[:id])
@order.set_payment_with!("alipay")
@order.pay!

redirect_to order_path(@order.token), notice: "使用支付宝成功完成付款"
end

def pay_with_wechat
@order = Order.find_by_token(params[:id])
@order.set_payment_with!("wechat")
@order.pay!

redirect_to order_path(@order.token), notice: "使用微信成功完成付款"
end



private

def order_params
Expand Down
8 changes: 8 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ def generate_token
self.token = SecureRandom.uuid
end

def set_payment_with!(method)
self.update_columns(payment_method: method)
end

def pay!
self.update_columns(is_paid: true )
end

validates :billing_name, presence: true
validates :billing_address, presence: true
validates :shipping_name, presence: true
Expand Down
12 changes: 12 additions & 0 deletions app/views/orders/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,17 @@
</tr>
</tbody>
</table>


<% if !@order.is_paid? %>
<div class="group pull-right">
<%= link_to("支付寶", pay_with_alipay_order_path(@order.token), :method => :post, :class => "btn btn-danger") %>
<%= link_to("以微信支付", pay_with_wechat_order_path(@order.token), :method => :post, :class => "btn btn-danger") %>
</div>
<% else %>
<p class="text-center">此訂單已完成付款</p>
<% end %>


</div>
</div>
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
end

resources :cart_items
resources :orders

resources :orders do
member do
post :pay_with_alipay
post :pay_with_wechat
end
end

end
5 changes: 5 additions & 0 deletions db/migrate/20170517033551_add_is_paid_to_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIsPaidToOrder < ActiveRecord::Migration[5.0]
def change
add_column :orders, :is_paid, :boolean, default: false
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170517033724_add_payment_method_to_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPaymentMethodToOrder < ActiveRecord::Migration[5.0]
def change
add_column :orders, :payment_method, :string
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170516115526) do
ActiveRecord::Schema.define(version: 20170517033724) do

create_table "cart_items", force: :cascade do |t|
t.integer "cart_id"
Expand All @@ -32,9 +32,11 @@
t.string "billing_address"
t.string "shipping_name"
t.string "shipping_address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "token"
t.boolean "is_paid", default: false
t.string "payment_method"
end

create_table "product_lists", force: :cascade do |t|
Expand Down

0 comments on commit 6ded4df

Please sign in to comment.