FB接口之 js调用支付窗口

官方文档: https://developers.facebook.com/docs/reference/dialogs/pay/

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:fb="https://www.facebook.com/2008/fbml">
  <head>
    <title>Facebook Credits Demo</title>
  </head>
  <body>
    <h2>Buy something...</h2>
    <button onclick="buy()">Buy</button>
    <div id="fb-ui-return-data"></div>
    <div id="fb-root"></div>

    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script> 
      FB.init({appId: "YOUR_APP_ID", status: true, cookie: true});    // 这里初始化FB的接口,接入你的应用

      // The dialog only opens if you've implemented the
      // Credits Callback payments_get_items.
      function buy() {
        var obj = {
          method: 'pay',          // FB.UI调用的弹窗类型。这里定义为支付弹窗
          action: 'buy_item',       // 支付的动作。其他为 buy_item(买商品)buy_credits(买平台币)earn_credits(赚取平台币)earn_currency.
          // You can pass any string, but your payments_get_items must
          // be able to process and respond to this data.
          order_info: {'item_id': '1a'},  // 买商品的话是商品的信息
          dev_purchase_params: {'oscif': true}  // 这里不太清楚,好像是商品的价格是否用当地的货币价显示
        };

        FB.ui(obj, js_callback);    // FB的UI调用,obj为参数,js_callback为回调函数
      }

      // This JavaScript callback handles FB.ui's return data and differs
      // from the Credits Callbacks.
      var js_callback = function(data) {
        if (data['order_id']) {      // 当订单成功的时候,会返回商品在FB平台的商品id
          // Facebook only returns an order_id if you've implemented
          // the Credits Callback payments_status_update and settled
          // the user's placed order.

          // Notify the user that the purchased item has been delivered
          // without a complete reload of the game.
          write_callback_data(
                    "<br><b>Transaction Completed!</b> </br></br>"
                    + "Data returned from Facebook: </br>"
                    + "Order ID: " + data['order_id'] + "</br>"
                    + "Status: " + data['status']);
        } else if (data['error_code']) {    // 出现某X非支付成功操作的时候,例如:点击支付窗口的取消按钮
          // Appropriately alert the user.
          write_callback_data(
                    "<br><b>Transaction Failed!</b> </br></br>"
                    + "Error message returned from Facebook:</br>"
                    + data['error_code'] + " - "
                    + data['error_message']);
        } else {            // 其他未知名错误
          // Appropriately alert the user.
          write_callback_data("<br><b>Transaction failed!</b>");
        }
      };

      function write_callback_data(str) {
        document.getElementById('fb-ui-return-data').innerHTML=str;
      }
    </script>
  </body>
</html>

 

posted @ 2013-07-25 19:38  oaijuru  阅读(972)  评论(1编辑  收藏  举报