[Java] [Rails] Introduction of PAY.JP

2 minute read

Introduction

We are creating a web application using Ruby on Rails by team development.

The description and code may be confusing because it is still immature. We will update and brush up any changes or changes that could not be described.

For beginners like me, when I repeat searches, I often find that I don’t know “what I want to find out”, 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 saved in PAY.JP instead of being saved in the database of the application. By linking the customer with the credit card and processing it, it becomes an API that can securely settle the credit card.

Regarding the product purchase function in the development of the flea market application, we decided to use PAY.JP to make payments using a credit card.

API: Abbreviation for “Application Programming Interface”, which means “what can be shared by a program specialized for a certain function” or “a mechanism for sharing software functions”.

version information

  • Ruby 2.5.1
  • Rails 5.2.4.3

1. PAY.JP account creation

Create an account on the PAY.JP site.(Advancepreparation)

2. Introducing the PAY.JP gem

Describe the following in Gemfile and execute bundle install.

Gemfile


gem'payjp'

3. Ready for payjp.js

app/views/layouts/application.html.haml


%html
  %head
    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
    %title payjptest
    
    -# Describe script so that payjp.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'
  %body
    = yield

4. Setting environment variables

environment variables: The OS permanently saves the settings, etc., so that the user or the program to be executed can set and refer to them. Some can be explicitly created and modified by user operation. If you do not understand it, you can understand it somehow by executing the printenv command (check if the environment variable is set) in the terminal.

Directly putting the private key or public key on the code is not good for security reasons, so Write the code like this and describe it in .bash_profile. Payjp.api_key = ENV[‘PAYJP_PRIVATE_KEY’]

.bash_profile: Runs once at login. For example, it is loaded when you start Terminal.app. It is good to set an environment variable for what to set specifically in this file.

.bashrc: Once executed at shell startup When you hit bash on the command line, .bashrc is read again. (.bash_profile cannot be read) If you want to set it every time you start the shell, put it in this file.

First, confirm the existence of .bash_profile.

terminal


$ ls -a

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

terminal


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

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

terminal


$ ls -a

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

terminal


$ vim ~/.bash_profile

Describe in .bash_profile.

.bash_profile


// First press "i" to enter insert mode
export PAYJP_ACCESS_KEY='sk_test_*************'
export PAYJP_PUBLIC_KEY='pk_test_*************'
//esc key => :wq to exit .bash_profile

Finally, after setting the environment variables, be sure to enable the settings with the following command.

terminal


$ source ~/.bash_profile

in conclusion

This is the end of PAY.JP introduction. There are various articles on implementing the credit card registration function, so please refer to each one.

Reference article

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