2012-12-05 58 views
1

我有一個簡單的Rails應用程序,它向外部網站發出請求,並返回一個帶有回調到我的網站的json url響應,以通知我的Rails應用程序關於響應。 JSON的URL樣本是https://voguepay.com/?v_transaction_id=demo-1345109950&type=json和響應主體下方是:如何在rails中解析json url響應

{"merchant_id":"demo","transaction_id":"demo-1345109950","email":"[email protected]","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"} 

我想這個響應轉換成一個Rails方法不是可以簡單地給我我需要讓我的查詢的屬性。例如,從響應體我需要做一個動作,如:

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 

上面的代碼只是一個樣本來解釋我想達到的目標。任何幫助都會很棒。

我都試過typhoeusyajl-ruby寶石,但是當請求時,被刪除我的所有身份驗證方法,我不斷收到錯誤信息無法驗證CSRF元令牌。即使我跳過它,當前用戶也會自動註銷(使用設計認證)。使用的代碼樣品低於:

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 

了,我不想用一塊寶石,我想這樣做手工,看是否CSRF元令牌將不會受到影響。所以,關於如何實現這一點的任何想法都會很棒。我正在使用rails 3.2.9。

謝謝!

回答

5

你可以這樣做:

def notify 
    response = JSON('https://voguepay.com/?v_transaction_id=demo-1345109950&type=json').body 
response["status"] 
response["date"] 
response["merchant_id"] 
response["total"] 
end  
+0

,這將很好地工作?真棒,我會試試這個。但是,我會得到csfr令牌錯誤。 – Uchenna

+0

檢查http://stackoverflow.com/questions/5669322/turn-off-csrf-token-in-rails-3以查看如何禁用特定操作的csrf保護。這應該解決這個問題 – rorra