html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Loading......</title>
</head>
<body>
<div id="fb-root"></div>
<script>
var appid = '你的应用编号';
var product = '{$product}';
var request_id = '{$request_id}';
var callbackurl = '{$callbackurl}';
window.fbAsyncInit = function() {
FB.init({
appId : appid,
status : true,
cookie : true,
xfbml : true,
version: 'v3.2'
});
function FacebookCreditsOrder() {
var obj = {
method: 'pay',
action: 'purchaseitem',
product: product,
request_id: request_id,
};
FB.ui(obj, function(data) {
console.log(data);
if( data['status'] == 'completed'){
bh_msg_tips('Transaction Completed!')
}else{
bh_msg_tips('Transaction Failed!')
}
setTimeout(function () {
window.location.href = callbackurl;
},1500)
});
}
FacebookCreditsOrder();
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "https://connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function bh_msg_tips(msg){
var oMask = document.createElement("div");
oMask.id = "bh_msg_lay";
oMask.style.position="fixed";
oMask.style.left="0";
oMask.style.top="70%";
oMask.style.zIndex="100";
oMask.style.textAlign="center";
oMask.style.width="100%";
oMask.innerHTML = "<span style='background: rgba(0, 0, 0, 0.65);color: #fff;padding: 10px 15px;border-radius: 3px; font-size: 14px;'>" + msg + "</span>";
document.body.appendChild(oMask);
setTimeout(function(){$("#bh_msg_lay").remove();},2000);
}
</script>
</body>
</html>
产品信息页面的地址
<!DOCTYPE html>
<html>
<head prefix=
"og: http://ogp.me/ns#
fb: http://ogp.me/ns/fb#
product: http://ogp.me/ns/product#">
<meta property="og:type" content="og:product" />
<meta property="og:title" content="100 Coin Pack" />
<meta property="og:image" content="<?= $serverUrl ?>/images/coin_bundle64.png" />
<meta property="og:description" content="A hundred coins!" />
<meta property="og:url" content="<?= $serverUrl ?>/payments/100coins.html" />
<meta property="product:price:amount" content="2.99"/>
<meta property="product:price:currency" content="USD"/>
<meta property="product:price:amount" content="1.99"/>
<meta property="product:price:currency" content="GBP"/>
</head>
</html>
回调
function facebook(){
$verify_token = "ssdfshhngthkouyk";
$hub_challenge = $_GET['hub_challenge'];
$hub_verify_token = _GET['hub_verify_token'];
if ($hub_verify_token === $verify_token) {
return $hub_challenge;
}
$data = file_get_contents("php://input");
$json = json_decode($data,true);
add_error_log('支付结果回调',$data);
if( isset($json["object"]) && $json["object"] && $json["object"] == "payments" ) {
$payment_id = $json["entry"][0]["id"];
$fb = new Facebook([
'app_id' => '你的应用编号',
'app_secret' => '你的秘钥',
'default_graph_version' => 'v3.2',
]);
$result = $fb->get($payment_id . '?fields=user,actions,request_id,items,gift_requests', config('app_token'));
$result = json_decode($result->getBody(), true);
$actions = isset($result['actions'][0]) ? $result['actions'][0] : [];
$request_id = isset($result['request_id']) ? $result['request_id'] : '';
if ($actions['status'] == 'completed' && !empty($request_id)) {
}
}
}
|