[Rails] Introduction of PAY.JP


I am creating a web application using Ruby on Rails in team development.

Since it is still immature, the description and code may be difficult to understand. We will update and brush up the items and changes that could not be described.

If you are a beginner like me, you tend to lose track of " what you want to look up now " when you search repeatedly, so I will explain technical terms and katakana terms each time.

What is PAY.JP?

cf. https://paymentnavi.com/paymentnews/51558.html

The customer's credit card information is not saved in the database on the application side, but is saved in PAY.JP. By associating the customer with the credit card and processing it, it becomes an API that allows you to make a secure credit card payment.

In the development of the flea market app, we have implemented the product purchase function using this PAY.JP to make payments with a credit card.

API: An abbreviation for "Application Programming Interface", which means "a program that specializes in one function can be shared" or "a mechanism for sharing software functions".

version information

1. PAY.JP account creation

Create an account on the PAY.JP site. (Advance preparation)

2. Introducing the PAY.JP gem

Write the following in the Gemfile and execute ** bundle install **.


gem 'payjp'

3. Make it possible to read payjp.js


    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
    %title payjptest
    -# payjp.Described script so that js can be read
    %script{src: "https://js.pay.jp/", type: "text/javascript"}
    = csrf_meta_tags
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
    = javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
    = yield

4. `Setting environment variables``

Environment variables: The OS permanently saves the setting values so that they can be set and referenced by the user or the program to be executed. Some can be explicitly created or changed by the user's operation. If you are not sure, you can understand it by executing the ** printenv ** command (check if the environment variables are set) in the terminal.

Since it is not good for security reasons to directly attach the private key or public key to the code, Write the code like this and write it in .bash_profile.   Payjp.api_key = ENV['PAYJP_PRIVATE_KEY']

.bash_profile: ** Once at login ** Executed. For example, it is loaded when you start Terminal.app. It is good to set environment variables as to what to set specifically in this file.

.bashrc: ** Executed once when the shell is started **. Hitting bash on the command line will load .bashrc again. (.bash_profile is not read) If you want to set it every time you start the shell, put the setting in this file.

First, check the existence of .bash_profile.


$ ls -a

It wasn't my case. Create an empty file with the ** touch ** command.


$ touch ~/.bash_profile
$ touch ~/.bashrc

Confirm that the file is created. (If it is not displayed, execute the following command as it is)


$ ls -a

.bash_profile	.gitignore	Gemfile		Rakefile	config
.bashrc		public		.ruby-version	Gemfile.lock	app		


$ vim ~/.bash_profile

Described in .bash_profile.


//First, press "i" to enter insert mode.
export PAYJP_ACCESS_KEY='sk_test_*************'
export PAYJP_PUBLIC_KEY='pk_test_*************'
//esc key=> :In the order of wq.bash_Exit profile

When you write the environment variable at the end, be sure to enable the setting with the following command.


$ source ~/.bash_profile

in conclusion

That is all for the introduction of PAY.JP. There are various articles on the implementation of the credit card registration function, so please refer to each one.

Reference article

[Implementing the purchase function using Payjp in Rails] (https://qiita.com/suzy1031/items/7964829086eb929471a6) [Implementing credit card registration and deletion function with Payjp (Rails)] (https://qiita.com/takachan_coding/items/f7e70794b9ca03b559dd) [[Rails] Implement payment function using payjp ① ~ Credit card registration ~] (https://qiita.com/dice9494/items/4aa04da1056d1f15919e) [Summary of .bash_profile and .bashrc] (https://qiita.com/takutoki/items/021b804b9957fe65e093)

Recommended Posts

[Rails] Introduction of PAY.JP
Introduction of milkode
[Rails 6] cocoon_ introduction
[Rails] devise introduction method
Introduction of user authentication
[Docker] Introduction of basic Docker Instruction
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Explanation of the order of rails routes
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] Summary of complicated routing configurations
Check the migration status of rails
[Rails] Implementation of many-to-many category functions
[Rails] Differences and usage of each_with_index and each.with_index
From introduction to usage of byebug
[Rails] Types of associations (one-to-many / many-to-many)
[Introduction to Rails] How to use render
[Specific usage of before_action] Rails refactoring
The identity of params [: id] in rails
Rails Addition of easy and easy login function
[rails] List of actions defined in Controller
[Rails 6.0] About batch saving of multiple records
[In-house study session] Introduction of "Readable Code"
[Rails] Change the label name of f.label
Personal application production 2 Introduction of WEB fonts
[Ruby on Rails] Japanese notation of errors
Rails Basics of creating a new application
Introduction of New Generation Java Programming Guide (Java 10)
Output of the book "Introduction to Java"
Introduction and precautions of gem Friendly Id
Explanation of Ruby on rails for beginners ①
Introduction of IDOM engineer's development environment (physics)
Introduction and usage explanation of Font Awesome
Introduction of New Generation Java Programming Guide (Java 11)
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
Introduction of New Generation Java Programming Guide (Java 12)