-
Notifications
You must be signed in to change notification settings - Fork 0
/
file39.rb
58 lines (48 loc) · 1.26 KB
/
file39.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class OrdersController < ApplicationController
before_action :nologin_user, only: [:index]
before_action :limit_user, only: [:index]
before_action :no_item, only: [:index]
before_action :item_set, only: [:index, :create]
def index
@order = OrderAddress.new
end
def create
@order = OrderAddress.new(order_params)
if @order.valid?
pay_item
@order.save
redirect_to root_path
else
render :index
end
end
private
def order_params
params.require(:order_address).permit(:prefecture_id, :postal_code, :city, :house_number, :building, :phone).merge(user_id: current_user.id, item_id: params[:item_id], token: params[:token])
end
def pay_item
Payjp.api_key = "C5510T5CXYKWYCY00RHHMM2HCY0411M200XCEUE4H4WED0Y5TXT22DEKD1H55HK"
Payjp::Charge.create(
amount: @item.price,
card: order_params[:token],
currency:'jpy'
)
end
def limit_user
@item = Item.find(params[:item_id])
if current_user.id == @item.user_id
redirect_to root_path
end
end
def nologin_user
redirect_to user_session_path unless user_signed_in?
end
def no_item
if @item.order != nil
redirect_to root_path
end
end
def item_set
@item = Item.find(params[:item_id])
end
end