[JAVA] [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 pay.jp
Introduction of milkode
[Rails 6] cocoon_ introduction
[Ruby on Rails] Introduction of initial data
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Ruby on Rails] Until the introduction of RSpec
Rails Tutorial/Significance of Indexing
[Rails] devise introduction method
Introduction of user authentication
Rails implementation of ajax removal
[Docker] Introduction of basic Docker Instruction
Utilization of Rails Boolean type
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Introduction to RSpec-Everyday Rails Summary-
[Rails] Japanese localization of error messages
Introduction of javaFX in Eclipse (April 2020)
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
Summary of rails validation (for myself)
[Rails] List instances of multiple models
[Rails] Check the contents of the object
[Rails] Implementation of image preview function
Basic knowledge of Ruby on Rails
Rails book review app RSpec introduction
Explanation of the order of rails routes
Kaminari --Added pagination function of Rails
[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
From introduction to use of ActiveHash
Japanese localization of error messages (rails)
[Rails] RSpec Kihon no Ki [Introduction]
[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
[Introduction to Rails] form_with (local: true)
[Rails] Types of associations (one-to-many / many-to-many)
[Introduction to Rails] How to use render
Summary about the introduction of Device
[Specific usage of before_action] Rails refactoring
From pulling docker-image of rails to launching
[Rails] Temporary retention of data by session
Introduction memo of automatic test using Jenkins
The identity of params [: id] in rails
Rails Addition of easy and easy login function
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
[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
Rails [For beginners] Implementation of comment function
Personal application production 2 Introduction of WEB fonts