I've a simple Rails app that makes a request to an external website and returns a json url response with a callback to my site to notify my Rails app about the response. The json url sample is https://voguepay.com/?v_transaction_id=demo-1345109950&type=json
and the response body is below:
{"merchant_id":"demo","transaction_id":"demo-1345109950","email":"testuser@buyerdomain.com","total":"10","total_paid_by_buyer":"10.00","total_credited_to_merchant":"9.90","extra_charges_by_merchant":"0.00","merchant_ref":"","memo":"Donation of N10 to Test User","status":"Approved","date":"2012-01-01 11:39:11","referrer":"http:\/\/sellerdomain.com\/buy_now.html","method":"VoguePay","fund_maturity":"2012-01-03"}
I'd like to convert this response into a Rails method than can simply give me the attribute I need to make my query. For example, from the response body I need to make an action like:
def notify
response = Json.parse('https://voguepay.com/?v_transaction_id=demo-1345109950&type=json').body
response.status
response.date
response.merchant_id
response.total
end
The code above is just a sample to explain what I want to achieve. Any help will be great.
I've tried both typhoeus and yajl-ruby gems but when the request comes in, all my authentication methods are removed and I keep getting the error message cannot authenticate csrf meta token. Even if I skip it the current user will be signed out automatically ( with devise authentication). The code sample used is below:
class NotificationsController < ApplicationController
skip_before_filter :verify_authenticity_token
def notify
@transaction_id = params[:transaction_id]
do_notify
end
private
def do_notify
hydra = Typhoeus::Hydra.new
request = Typhoeus::Request.new("https://voguepay.com/?v_transaction_id=#{@transaction_id}&type=json")
request.on_complete do |response|
logger.info("#{response.request.url} in #{response.time} seconds") #remove in production to avoid huge logs
transaction = Yajl::Parser.parse(response.body) #or Yajl::Parser.parse(response.body)
#Now we have the following keys in our transaction hash you can do whatever
transaction[:merchant_id]
transaction[:transaction_id]
transaction[:email]
transaction[:total]
transaction[:merchant_ref]
transaction[:memo]
transaction[:status]
transaction[:date]
transaction[:referrer]
transaction[:method]
@plan = Plan.find_by_id(transaction[:merchant_ref])
if(transaction[:total] == 0)
logger.error "Invalid total for transaction:#{@transaction_id}"
#do not subscribe the user or generate invoice, notify user of error n cancel order
elsif(transaction[:status] != 'Approved')
logger.error "Failed transaction for transaction:#{@transaction_id}"
#do not subscribe the user or generate invoice, notify user of error n cancel order
elsif(transaction[:total] >= @plan.naira_price.to_s)
current_user.award_user_credits(@plan.hjc.to_i)
end
end
hydra.queue(request)
hydra.run
end
end
Anymore, I don't want to use a gem I want to do it manually to see if the csrf meta token will not be affected. So any idea on how I can achieve this will be great. I am using rails 3.2.9.
Thank you!