def index
@order = Order.new
end
def create
@order = Order.new(price: order_params[:price])
#@price to order="1000"Price enters
if @order.valid?
#Conditions for saving(Not blank)If you clear, payment and data will be saved
pay_item#Make a payment
@order.save#Save the data
return redirect_to root_path
else
render 'index'
end
end
private
def order_params
params.permit(:price, :token)#Columns that allow data storage
end
def pay_item
Payjp.api_key = ENV["PAYJP_SECRET_KEY"] # PAY.JP test private key
Payjp::Charge.create(
amount: order_params[:price], #Product price
card: order_params[:token], #Card token
currency:'jpy' #Currency type(Japanese yen)
)
end
end
-Token (card information) is not saved in the order table. So for @order, use price: order_params [: price] The price price information entered by the user is substituted.
・ Payment is completed by pay_item Price: 1000 at @ order.save, save price information in table
class Order < ApplicationRecord
validates :price, presence: true
end
・ What is validation? Conditions for saving data in a table
・ This time, if you send with the price input field empty I try not to save the data.
・ Since token (credit card information) is not saved in the table, validation Do not describe the storage conditions.
Recommended Posts