Facebook支付

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}'; //订单的id,必须是唯一的
var callbackurl = '{$callbackurl}'; // 回调地址
window.fbAsyncInit = function() {
	FB.init({
		appId      : appid,
		status     : true,
		cookie     : true,
		xfbml      : true,
		version: 'v3.2' // 使用图形api v3.2版本
	});
	function FacebookCreditsOrder() {
		var obj = {
			method: 'pay',
			action: 'purchaseitem',
			product: product,//产品信息页面的地址
			request_id: request_id,//订单的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();
};

// Load the SDK Asynchronously
(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>

回调

 // facebook 支付回调
    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)) {
                // 处理订单信息
            }
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值