[RUBY] [rails] gem'payjp' implementation procedure

  1. Install gem'payjp'

2. Create and describe order controller

  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

3 Describe validation information in the model

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

[rails] gem'payjp' implementation procedure
[Rails] Comment function implementation procedure memo
Rails Basic CRUD function implementation procedure scaffold
Rails hashtag search implementation
Rails6 countdown timer implementation
Rails Action Text implementation
[Rails 6.0] "Easy login" implementation procedure required for portfolio
Rails Heroku deployment procedure
Rails search function implementation
Rails, RSpec installation procedure
Rails implementation of ajax removal
Rails fuzzy search function implementation
[Rails 6] Implementation of search function
[Rails] Implementation of category function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
[Rails] Implementation of user logic deletion
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[rails] Login screen implementation in devise
[Rails] Implementation of image preview function
[Implementation procedure] Create a user authentication function using sorcery in Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Rails on Docker environment construction procedure
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
[EC2 / Vue / Rails] EC2 deployment procedure for Vue + Rails
[Rails] I will explain the implementation procedure of the follow function using form_with.
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails / Heroku] Error resolution procedure after push
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
[Rails] Implementation procedure when public / private functions are added to the posting function