サンプル - index.php

 <?php
 //Enter your APP ID below
 define('APP_ID', 'input app id');
 
 $base='http://api.sample.com';
 $val = $base.'/credits/get_items';
 $val = file_get_contents($val);
 $value = json_decode($val,true);
 $value = $value['CreditItem'];
 ?>
 
 <!-- Copyright 2004-2011 Facebook. All Rights Reserved.-->
 
 <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:fb="http://www.facebook.com/2008/fbml">
 <head>
   <title>Facebook Credits Demo Application</title>
 </head>
 
 <body>
   <h2> Facebook Credits Demo Application</h2>
 
   <p> Create an order by specifying the following attributes:</br>
   <i>ID, Title, price, description, image URL and product URL</i></p>
   
   <!-- Please note that user can change any information in order_info through 
 	javascript. So please make sure you never put price or any other 
 	information you don't want users to modify in order_info. We put everything
 	here only for end-to-end flow testing purpose!! -->
   <form name ="place_order" id="order_form" action="#">
 <?php foreach ($value as $key => $val) { ?>
   <!--ID:-->          <input type="hidden" name="item_id"               
                        value="<?php echo $val['id'] ?>"          
                        id="item_id_el[<?php echo $key ?>]">
   Title:              <input type="text"   name="title"                 
                        value="<?php echo $val['title'] ?>"       
                        id="title_el[<?php echo $key ?>]"> </br>
   Price:              <input type="text"   name="price"                 
                        value="<?php echo $val['price'] ?>"       
                        id="price_el[<?php echo $key ?>]"> </br>
   Description:        <input type="text"   name="description" size="64" 
                        value="<?php echo $val['description'] ?>" 
                        id="desc_el[<?php echo $key ?>]"> </br>
   <!--Image URL:-->   <input type="hidden" name="image_url"   size="64" 
                        value="<?php echo $val['image_url'] ?>"   
                        id="img_el[<?php echo $key ?>]">
   <!--Product URL:--> <input type="hidden" name="product_url" size="64" 
                        value="<?php echo $val['product_url'] ?>" 
                        id="product_el[<?php echo $key ?>]">
   <a onclick="placeOrder(<?php echo $key ?>); return false;">
     <img src="http://www.facebook.com/connect/button.php?app_id=<?php echo APP_ID; ?>&feature=payments&type=light_l">
   </a>
   </br>
 <?php } ?>
   </form>
 
   <?php
   // if $order_id and $status are passed back, we know they were send from the full
   // page view, here we print the same information users see through the normal path
   if ($_GET['order_id'] && $_GET['status']) {
     $html = "Transaction Completed! </br></br>"
       ."Data returned from Facebook: </br>"
       ."<b>Order ID: </b>" . $_GET['order_id'] . "</br>"
       ."<b>Status: </b>" . $_GET['status'];
   } elseif ($_GET['error_code'] && $_GET['error_message']) {
     $html = "Transaction Failed! </br></br>"
       ."Error message returned from Facebook:</br>"
       .$_GET['error_message'];
   }
   ?>
   <div id="output"> <?=$html?> </div> </br></br>
 
   <a href="index.php">Back to home</a>
 
   <div id="fb-root"></div>
   <script src="http://connect.facebook.net/en_US/all.js">
   </script>
 
   <script>
     FB.init({appId: <?php echo APP_ID; ?>, status: true, cookie: true});
 
     function placeOrder(i) {
       var item_id = document.getElementById('item_id_el[' + i + ']').value;
       var title = document.getElementById('title_el[' + i + ']').value;
       var desc = document.getElementById('desc_el[' + i + ']').value;
       var price = document.getElementById('price_el[' + i + ']').value;
       var img_url = document.getElementById('img_el[' + i + ']').value;
       var product_url = document.getElementById('product_el[' + i + ']').value;
 
       var order_info = { "item_id":item_id,
 	                     "title":title,
                          "description":desc,
                          "price":price,
                          "image_url":img_url,
                          "product_url":product_url
                        };
 
       // calling the API ...
       var obj = {
 	    method: 'pay',
 	    order_info: order_info,
 	    purchase_type: 'item'
       };
 
       FB.ui(obj, callback);
     }
     
     var callback = function(data) {
       if (data['order_id']) {
         writeback("Transaction Completed! </br></br>"
         + "Data returned from Facebook: </br>"
         + "<b>Order ID: </b>" + data['order_id'] + "</br>"
         + "<b>Status: </b>" + data['status']);
       } else if (data['error_code']) {
         writeback("Transaction Failed! </br></br>"
         + "Error message returned from Facebook:</br>"
         + data['error_message']);
       } else {
         writeback("Transaction failed!");
       }
     };
 
     function writeback(str) {
       document.getElementById('output').innerHTML=str;
     }
 
   </script>
 
 </body>
 </html>