我如何获得 ruby 到 return json 响应以用于 rails 控制器
How to i get ruby to return json response to be used in rails controller
我正在使用 Api,到目前为止一切顺利,我能够发送请求和接收响应,但我希望 ruby 代码发送 json 响应和可以从我的 rails 控制器访问的消息,以便我可以获得 simple_form 来呈现错误或成功消息,并处理来自 rails 控制器的重定向,我还想保存事务来自模型响应的详细信息,这是我的库代码
class SimplePay
require 'net/https'
require 'uri'
require 'json'
def self.pay(api_key, token, amount, amount_currency, status)
uri = URI.parse('https://checkout.simplepay.ng/v2/payments/card/charge/')
header = {'Content-Type': 'text/json'}
data = {
'token': token,
'amount': amount,
'amount_currency': amount_currency
}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, header)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri, header)
request.basic_auth(api_key, '')
request.set_form_data(data)
response = http.request(request)
res = JSON.parse(response.read_body)
response_code = res['response_code']
if response.kind_of? Net::HTTPSuccess
if response_code.eql? 20000
puts 'success'
else
puts 'Failed'
end
elsif status.eql? true
puts 'Success But Not Charged'
elsif status.eql? false
raise error 'Failed To Charge Card'
elsif response.kind_of? Net::HTTPBadRequest
raise error 'Failed 400'
elsif response.kind_of? Net::HTTPError
raise error 'Failed 404'
else
raise error 'Unknown Error'
end
end
end
我该如何实现?
我会说发送一个 Hash
到控制器,然后当你发送它时,只要做一个 .to_json
就会变成 json
我正在使用 Api,到目前为止一切顺利,我能够发送请求和接收响应,但我希望 ruby 代码发送 json 响应和可以从我的 rails 控制器访问的消息,以便我可以获得 simple_form 来呈现错误或成功消息,并处理来自 rails 控制器的重定向,我还想保存事务来自模型响应的详细信息,这是我的库代码
class SimplePay
require 'net/https'
require 'uri'
require 'json'
def self.pay(api_key, token, amount, amount_currency, status)
uri = URI.parse('https://checkout.simplepay.ng/v2/payments/card/charge/')
header = {'Content-Type': 'text/json'}
data = {
'token': token,
'amount': amount,
'amount_currency': amount_currency
}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, header)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri, header)
request.basic_auth(api_key, '')
request.set_form_data(data)
response = http.request(request)
res = JSON.parse(response.read_body)
response_code = res['response_code']
if response.kind_of? Net::HTTPSuccess
if response_code.eql? 20000
puts 'success'
else
puts 'Failed'
end
elsif status.eql? true
puts 'Success But Not Charged'
elsif status.eql? false
raise error 'Failed To Charge Card'
elsif response.kind_of? Net::HTTPBadRequest
raise error 'Failed 400'
elsif response.kind_of? Net::HTTPError
raise error 'Failed 404'
else
raise error 'Unknown Error'
end
end
end
我该如何实现?
我会说发送一个 Hash
到控制器,然后当你发送它时,只要做一个 .to_json
就会变成 json