Reputation: 1541
I am following the official tutorial to implement the FB credits but its not working.
I have added alert statements to make sure the code is executing, from the alert messages, I am sure that there are no js errors and FB.ui is being called. I have alert messages in the callback function also but no response received.
I am breaking my head since 5 hours to figure out what is wrong in the code. Can some one please help me.
Additional info on the app:
Here is the buy.php
<?php
include_once '/Config.php';
include_once '/fb-sdk/facebook.php';
?>
<html>
<head>
<title>My Facebook Credits Page</title>
</head>
<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : '<?php echo Config::$appId?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
channelUrl : 'http://localhost/buy.php', // channel.html file
oauth : true // enable OAuth 2.0
});
var callback = function(data) {
if (data['order_id']) {
alert('called back');
return true;
} else {
//handle errors here
alert('some error');
return false;
}
};
function placeOrder(){
alert('in placeOrder()');
var order_info = 'myorderinfo';
alert('creating obj');
var obj = {
method: 'pay',
order_info: order_info,
action: 'buy_item',
dev_purchase_params: {'oscif': true}
};
alert('calling ui');
FB.ui(obj, callback);
}
</script>
<input type="button" value="post" onclick="postFeed()" />
<input type="button" value="Buy" onclick="placeOrder()" />
</body>
</html>
If you notice the alert calls, I am getting alert messages in the order
there are alert messages in the callback function too but they are not being called
To make sure the fb is inited properly I have implemented feed post function and called from the "postFeedback"'s click event
function postFeed(){
alert('in postFeed()');
FB.ui(
{
method: 'feed',
name: 'Facebook Dialogs',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
caption: 'Reference Documentation',
description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
}
This is working fine and posting feed on my wall
I have also implemented the callback.php using the example given in the https://developers.facebook.com/docs/authentication/signed_request/
And yes, I have configured the app settings properly
callback.php
<?php
include_once 'Config.php';
mysql_connect('localhost','root','');
mysql_select_db("precious_world");
//var_dump($_REQUEST);
//dump the request into the db
$request = join(':', $_REQUEST);
$request = mysql_real_escape_string($request);
$query = "insert into fbcredits_callback(data)values('$request')";
$result = mysql_query($query);
$fb_signed_req = $_REQUEST['signed_request'];
echo parse_signed_request($signed_request, Config::$appSecret);
function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
?>
I have some extra code in this file to dump the entire request to trace the request
Upvotes: 0
Views: 435
Reputation: 43816
I see 'localhost' in there in one of your channel URLs, but if you're also using localhost as your credits callback URL there's no way Facebook will be able to reach it (and thus, there's no way for the credits order to proceed)
Upvotes: 1