I'm trying to connect the payment via liqpay to the site, but I get this error:

public_key is a required field

She points to the line

<%=liqpay_button @liqpay_request %> 

In show file

 <% if @place.status == false %> <p>If you have paid already, the response didn't come in yet from LiqPAY. Refresh this page later.</p> <%=liqpay_button @liqpay_request %> <% elsif @place.status == true %> <p>Payment succeeded.</p> <% else %> <p>Payment failed.</p> <% end %> 

Controller code places_controller.rb

  if @place.status == false @liqpay_request = Liqpay::Request.new( :amount => @searched_film_session_price, :currency => 'UAH', :name => @place_title, :order_id => @place.id, :result_url => place_url(@place) # :server_url => liqpay_payment_place_url(@place) ) 

In the config / initializers / liqpay.rb file I wrote:

 Liqpay.default_options = { public_key: ENV['публичный ключ полученный при регистрации'], private_key: ENV['приватный ключ полученный при регистрации'], currency: 'UAH' } 

Code payments_controller.rb

 class PaymentsController < ApplicationController protect_from_forgery :except => :liqpay_payment def liqpay_payment @liqpay_response = Liqpay::Response.new(params) @place = Place.find(@liqpay_response.order_id) @place.data = {} (Liqpay::Response::ATTRIBUTES - %w(public_key sender_phone transaction_id)).each do |attribute| @place.data[attribute] = @liqpay_response.send(attribute) end if @liqpay_response.success? @place.update_attributes!(:status => true) else @place.update_attributes!(:status => false) end redirect_to @place rescue Liqpay::InvalidResponse render text: 'Payment error', status: 500 end end 
  • Maxim, please transfer your decision from the question in response. - Sasha Chernykh
  • one
    @ Sasha Black Did. - Maxim Cherevatov

2 answers 2

I hope you do not have time to commit.

Never, never store confidential data (passwords, keys, logins to third-party services) directly in the application code.

ENV is an object for accessing environment variables . They are the traditional means of storing configurations on 12-factor platforms like Heroku. Not perfect , but bearable.

The environment variable is the name-value pair ENV[название] #=> значение , think up the name yourself so that the expression ENV[...] looks meaningful. The same rules as when naming variables: call it so that at a glance it is clear what the meaning of the expression is.

It should be clear why ENV[ключ] did not work: there was no variable in the environment with this name and this expression returned nil .


How to set environment variables depends on the platform. Heroku has a separate section in the application settings, and locally ... there’s a whole story, storing environment variables for several applications on the same developer’s machine is not very fun, so they usually use wrappers over ENV configuration files as a "backup plan" (if not configuration file).

The path to the configuration file is .gitignore in .gitignore , so it never gets outside, but is completely absent in production, so ENV used.

I prefer figaro ( use case ), but there is also a config .

  • I understood this even before I wrote the answer to the question, I just did not correctly formulate the idea) - Maxim Cherevatov
  • @MaximCherevatov you have in mind that random people will come here and read what we have written here. This management is quite dangerous information, with the wording you need to be careful .-. - D-side
  • I understand you, I will be more careful. - Maxim Cherevatov

The problem was in env. I have registered the received keys directly in this field.

Update

I was misunderstood, I did not advise writing values ​​there directly, but clarified that I did it from the very beginning and it turned out to be a mistake !! I fixed it by connecting the 'figaro' gem!

  • To supplement your question, please use the edit option. The “Post Reply” button should only be used for comprehensive answers to questions. - From the queue of checks - torokhkun
  • @totorro is an exhaustive answer to the question, although this is a rubric #вредные_советы . - D-side