How to set environment variables when using Payjp with Rails

environment

macOS Ruby 2.6.5 Rails 6.0.3.2

Overview

I used Payjp in my Rails application. At that time, I didn't know how to set the environment variables, so I'll keep it as a memorandum.

Goal you want to achieve

Being able to handle environment variables through Payjp.

The problem you want to solve

It is not good for security to put the private key or public key directly in the code. Therefore, I would like to handle private and public keys using environment variables.

Environment variable

The value of the environment variable can be used from Rails by writing ENV [''].

Local environment

After Catalina

Terminal


Application name% vim ~/.zshrc

Before Catalina

Terminal


Application name% vim ~/.bash_profile

Do the above in the terminal.

.zshrc

//First press "i" to enter input mode
export PAYJP_ACCESS_KEY='sk_test_*************'
export PAYJP_PUBLIC_KEY='pk_test_*************'
//Then press esc:with wq.Exit from zshrc

Application name% source ~/.zshrc

The source command is a command that executes the command written in the file in the current shell. It is mainly used to reflect the shell configuration file.

If you just change the environment variable, rails will only read the environment variable before the change. Therefore, if the application is running, it will be closed once. Then log in again with rails s in the terminal with the modified environment variables.

In order to handle the set environment variables, describe them in the controller in the following format.

creditcards_controller.rb


#Credit card storage
  def create
    Payjp.api_key = ENV["PAYJP_PRIVATE_KEY"]
    if params['payjp-token'].blank?
      render :new
    else
      customer = Payjp::Customer.create(
        card: params['payjp-token'],
        metadata: {user_id: current_user.id}
      )
      @card = Creditcard.new(
        user_id: current_user.id,
        customer_id: customer.id,
        card_id: customer.default_card
      )
      if @card.save
        redirect_to root_path, notice: "Your credit card has been registered"
      else
        render :new, notice: "Credit card registration failed"
      end
    end

The above example sentence is a process to tokenize and save the credit card information entered in the form.

Production environment

Also, the files in which environment variables are set differ between the local environment and the production environment.

Terminal


[ec2-user ~]$ sudo vim /etc/environment

/etc/environment

//First press "i" to enter input mode
PAYJP_ACCESS_KEY='sk_test_*************'
PAYJP_PUBLIC_KEY='pk_test_*************'
//Then press esc:with wq/etc/Exit the environment
exit
//Log in to ec2 with ssh again

As with the local environment, you will need to restart or redeploy unicorn when you set or modify environment variables.

At the end

I hope this article is of some help to you.

reference: https://wa3.i-3-i.info/word11788.html https://qiita.com/Richelieu/items/b872dfce81124084b199#%E7%92%B0%E5%A2%83%E5%A4%89%E6%95%B0%E3%81%AB%E6%9B%B8%E3%81%8D%E8%BE%BC%E3%82%80 https://qiita.com/suzy1031/items/7964829086eb929471a6

Recommended Posts

How to set environment variables when using Payjp with Rails
How to set and describe environment variables using Rails zsh
How to build Rails 6 environment with Docker
[Rails] How to build an environment with Docker
How to automatically generate ER diagram when migrating with Rails6
How to specify db when creating an app with rails
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to get along with Rails
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
Things to keep in mind when using Sidekiq with Rails
How to install Pry after building Rails development environment with Docker
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
[Rails 5] How to display the password change screen when using devise
[Rails] How to upload images using Carrierwave
[Rails] How to use rails console with docker
How to resolve errors when installing Rails 5.1.3
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to use environment variables in RubyOnRails
How to implement image posting using rails
[Rails] How to handle data using enum
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Node.js express Docker] How to define Docker environment variables and load them with node.js
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to apply Rails environment variables to Docker container (aws :: Sigv4 :: Errors solution)
[Rails] How to write when making a subquery
[Rails] How to create a graph using lazy_high_charts
How to link Rails6 Vue (from environment construction)
[Rails] Error resolution when generating tokens with PAYJP
[Rails] How to upload multiple images using Carrierwave
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
How to create member variables with JPA Model
Introduce dotenv to Docker + Rails to manage environment variables
[Rails] How to use PostgreSQL in Vagrant environment
How to get the query string to actually issue when using PreparedStatement with JDBC
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
How to set environment variables in the properties file of Spring boot application
How to write Rails
How to uninstall Rails
How to embed JavaScript variables in HTML with Thymeleaf
How to make batch processing with Rails + Heroku configuration
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
How to push an app developed with Rails to Github
Whether to enable SSL when using JDBC with MySQL.
How to build docker environment with Gradle for intelliJ
How to unit test with JVM with source using RxAndroid
How to delete a new_record object built with Rails
How to add characters to display when using link_to method
How to make an almost static page with rails
How to manually generate a JWT with Rails Knock
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
[Java] Refer to and set private variables with reflection
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
Using PAY.JP API with Rails ~ Implementation Preparation ~ (payjp.js v2)
How to build Java development environment with VS Code
How to query Array in jsonb with Rails + postgres