作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的 Facebook 应用程序中实现 Facebook Credits。有谁知道 Ruby on Rails 中可用的 Facebook Credits 示例应用程序版本?有人为此制作了 gem 吗?如果我找到一个,我会在下面链接...
最佳答案
这是我的代码,采用 FB 示例并对其进行 RORing:
require 'facebook_signed_request.rb'
class FacebookCreditsController < ApplicationController
skip_before_filter :verify_authenticity_token
layout nil
def index
facebook_request = FacebookSignedRequest.new(request.params['signed_request'])
data = { "content" => {} }
# handle invalid request
if !facebook_request.valid?
return
end
signed_request = facebook_request.signed_request
# handle invalid signed request
if signed_request.nil?
return
end
method = request.params['method']
order_id = request.params['order_id']
# second state response from facebook
if method == 'payments_status_update'
status = request.params['status']
if status == 'placed'
next_state = 'settled'
data['content']['status'] = next_state
elsif status == 'settled'
redirect_to '/lots'
return
end
# compose returning data array_change_key_case
data['content']['order_id'] = order_id
# first stage response from facebook
elsif method == 'payments_get_items'
order_info = request.params['order_info']
item = JSON.parse(order_info)
item['price'] = item['price'].to_i
# for url fields, if not prefixed by http://, prefix them
url_key = [ 'product_url', 'image_url' ]
url_key.each do |key|
if item[key][0..6] != 'http://'
item[key] = "http://#{item[key]}"
end
end
# if payload['test_mode']
if request.params['test_mode']
update_keys = ['title', 'description']
update_keys.each do |key|
item[key] = '[Test Mode] ' + item[key]
end
end
data['content'] = [item]
end
data['method'] = method
render :json => data
end
end
那么除此之外还有:
require 'base64'
require 'json'
require 'openssl'
class FacebookSignedRequest
attr_reader :signed_request
def initialize(signed_request)
@signed_request = signed_request
end
def base64_url_decode str
encoded_str = str.gsub('-','+').gsub('_','/')
encoded_str += '=' while !(encoded_str.size % 4).zero?
Base64.decode64(encoded_str)
end
def valid?
# TODO: move it to some configuration
secret = " << my secret is here >>"
# decode data
encoded_sig, payload = signed_request.split('.')
sig = base64_url_decode(encoded_sig).unpack("H*")[0]
data = JSON.parse base64_url_decode(payload)
if data['algorithm'].to_s.upcase != 'HMAC-SHA256'
# Rails.logger.error 'Unknown algorithm. Expected HMAC-SHA256'
return false
end
#check sig
expected_sig = OpenSSL::HMAC.hexdigest('sha256', secret, payload)
if expected_sig != sig
# Rails.logger.error 'Bad Signed JSON signature!'
return false
end
data
end
我不知道这是否对其他人有帮助,但它对我有用。抱歉花了这么长时间才记得回来发布我的工作代码...
按要求查看相关代码:
#view
<%= javascript_include_tag "premium_signup" %>
<script type="text/javascript">
$(document).ready(function() {
$('#premium_signup_button').click(function() {
signupAsPremiumMember('Premium Membership', 'Create unlimited auctions with no extra charge at all for 1 year.', "1", '', '');
});
});
</script>
...
<button id="premium_signup_button">Signup as a premium member</button>
#premium_signup.js
function signupAsPremiumMember(title, desc, price, imageURL, productURL) {
var order_info = {
"title": title,
"description": desc,
"price": price,
"image_url": imageURL,
"product_url": productURL
};
var payload = {
"method": 'pay',
"order_info": order_info,
"purchase_type": 'item'
};
console.log(FB.ui(payload, facebookPurchaseCompletionCallback));
}
function facebookPurchaseCompletionCallback(data) {
if (data['order_id']) {
console.log(data['order_id']);
}
else if (data['error_code']) {
console.log(data['error_code']);
}
else {
console.log("failed");
}
}
#in the layout
#in head
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<%= javascript_include_tag 'application' %>
#in body
<div id="fb-root"></div>
<script>
FB.init({
appId : '############',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
现在所有这一切都来自一个人,他坐下来学习用 Ruby On Rails 编程,我尽可能少地掌握 Javascript 支持知识,在去年大约 12 周的时间里,今年又花了几周时间一年的好措施。我这样说是出于谨慎——我发布的代码可能是垃圾……但它确实有效:-)
如果有人真的发现任何有用的答案,我们将不胜感激 - 只是说:-)
关于ruby-on-rails-3 - 那里有 Facebook Credits Ruby On Rails gem 吗?或者他们的 Facebook Credits 示例应用程序的 ruby 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4824035/
我是一名优秀的程序员,十分优秀!