Services_Amazon
[ class tree: Services_Amazon ] [ index: Services_Amazon ] [ all elements ]

Source for file amazon_ecs4_cart.php

Documentation is available at amazon_ecs4_cart.php

  1. <?php
  2. //
  3. // Example of usage for Services_AmazonECS4
  4. //
  5. // This example uses the following functions:
  6. // - CartAdd
  7. // - CartClear
  8. // - CartCreate
  9. // - CartGet
  10. // - CartModify
  11. //
  12.  
  13. require_once 'config.php';
  14. require_once 'PEAR.php';
  15. require_once 'Services/AmazonECS4.php';
  16.  
  17. function report_error($msg)
  18. {
  19.     echo "<p><i>{$msg}</i><p></body></html>";
  20.     exit();
  21. }
  22.  
  23. function existsCart()
  24. {
  25.     return isset($_COOKIE['CartId']? true : false;
  26. }
  27.  
  28. $php_self htmlspecialchars($_SERVER['PHP_SELF']);
  29.  
  30.  
  31. $action = isset($_GET['action']$_GET['action''';
  32.  
  33. switch ($action{
  34. case 'add':
  35.     $item = array('ASIN' => $_GET['ASIN'],
  36.                   'Quantity' => $_GET['Quantity']);
  37.     if (!existsCart()) {
  38.         $result $amazon->CartCreate($item);
  39.         if (PEAR::isError($result)) {
  40.             report_error($result->message);
  41.         }
  42.         setcookie('CartId'$result['CartId']time(+ 60*60*24);
  43.         setcookie('HMAC'$result['HMAC']time(+ 60*60*24);
  44.     else {
  45.         $result $amazon->CartAdd($_COOKIE['CartId']$_COOKIE['HMAC']$item);
  46.         if (PEAR::isError($result)) {
  47.             report_error($result->message);
  48.         }
  49.     }
  50.     break;
  51.  
  52. case 'modify':
  53.     if (!existsCart()) {
  54.         report_error('Invalid action');
  55.     }
  56.     $item = array('CartItemId' => $_GET['CartItemId']);
  57.     if (isset($_GET['SaveForLater'])) {
  58.         $item += array('Action' => 'SaveForLater');
  59.     else if (isset($_GET['MoveToCart'])) {
  60.         $item += array('Action' => 'MoveToCart');
  61.     else {
  62.         $item += array('Quantity' => $_GET['Quantity']);
  63.     }
  64.     $result $amazon->CartModify($_COOKIE['CartId']$_COOKIE['HMAC']$item);
  65.     if (PEAR::isError($result)) {
  66.         die($result->message);
  67.     }
  68.     
  69.     break;
  70.  
  71. case 'clear':
  72.     if (!existsCart()) {
  73.         report_error('Invalid action');
  74.     }
  75.     $result $amazon->CartClear($_COOKIE['CartId']$_COOKIE['HMAC']);
  76.     break;
  77.  
  78. default:
  79.     if (existsCart()) {
  80.         $result $amazon->CartGet($_COOKIE['CartId']$_COOKIE['HMAC']);
  81.         if (PEAR::isError($result)) {
  82.             setcookie('CartId'null0);
  83.             setcookie('HMAC'null0);
  84.             report_error($result->message);
  85.         }
  86.     }
  87.     break;
  88. }
  89.  
  90. echo <<< EOT
  91. <html>
  92. <head>
  93.     <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
  94.     <title>Services_AmazonECS4 example - Cart Operations</title>
  95. </head>
  96. <body>
  97. <h1>Services_AmazonECS4 example - Cart Operations</h1>
  98. <p>
  99. <a href="http://docs.amazonwebservices.com/AWSEcommerceService/2006-03-08/PgUsingShoppingCartArticle.html" target="_blank">Using the Amazon E-Commerce Service Shopping Cart</a>
  100. </p>
  101. <form action="{$php_self}" method="get">
  102. <table border="0">
  103. <tr>
  104.     <td>ASIN <input type="text" name="ASIN" size="20" /></td>
  105.     <td>Quantity <input type="text" name="Quantity" size="3" value="1" /></td>
  106.     <td><input type="submit" value="Add to cart" /></td>
  107. </tr>
  108. </table>
  109. <input type="hidden" name="action" value="add" />
  110. </form>
  111. EOT;
  112.  
  113. // CartItems
  114. echo <<< EOT
  115. CartItems :<br />
  116. <table border="1">
  117. <tr><th>ASIN</th><th>Title</th><th>Price</th><th>Qty</th><th>Save</th><th></th></tr>
  118. EOT;
  119. $items = array();
  120. if (isset($result['CartItems'])) {
  121.     if (isset($result['CartItems']['CartItem']['CartItemId'])) {
  122.         $items = array($result['CartItems']['CartItem']);
  123.     else {
  124.         $items $result['CartItems']['CartItem'];
  125.     }
  126. }
  127. foreach ($items as $v{
  128. echo <<< EOT
  129.     <tr>
  130.         <form action="{$php_self}" method="get">
  131.         <td>{$v['ASIN']}</td>
  132.         <td>{$v['Title']}</td>
  133.         <td>{$v['Price']['FormattedPrice']}</td>
  134.         <td>
  135.             <input type="text" size="3" name="Quantity" value="{$v['Quantity']}" />
  136.             <input type="hidden" name="CartItemId" value="{$v['CartItemId']}" />
  137.         </td>
  138.         <td>
  139.             <input type="checkbox" name="SaveForLater" value="save" />
  140.         </td>
  141.         <td>
  142.             <input type="submit" value="Update" />
  143.         </td>
  144.         <input type="hidden" name="action" value="modify" />
  145.         </form>
  146.     </tr>
  147. EOT;
  148. }
  149. echo <<< EOT
  150. </table><br />
  151. EOT;
  152.  
  153. // Saved Items
  154. echo <<< EOT
  155. Saved Items to buy later :<br />
  156. <table border="1">
  157. <tr><th>ASIN</th><th>Title</th><th>Price</th><th>Qty</th><th>Move</th><th></th></tr>
  158. EOT;
  159. $items = array();
  160. if (isset($result['SavedForLaterItems'])) {
  161.     if (isset($result['SavedForLaterItems']['SavedForLaterItem']['CartItemId'])) {
  162.         $items = array($result['SavedForLaterItems']['SavedForLaterItem']);
  163.     else {
  164.         $items $result['SavedForLaterItems']['SavedForLaterItem'];
  165.     }
  166. }
  167. foreach ($items as $v{
  168. echo <<< EOT
  169.     <tr>
  170.         <form action="{$php_self}" method="get">
  171.         <td>{$v['ASIN']}</td>
  172.         <td>{$v['Title']}</td>
  173.         <td>{$v['Price']['FormattedPrice']}</td>
  174.         <td>
  175.             <input type="text" size="3" name="Quantity" value="{$v['Quantity']}" />
  176.             <input type="hidden" name="CartItemId" value="{$v['CartItemId']}" />
  177.         </td>
  178.         <td>
  179.             <input type="checkbox" name="MoveToCart" value="move" />
  180.         </td>
  181.         <td>
  182.             <input type="submit" value="Update" />
  183.         </td>
  184.         <input type="hidden" name="action" value="modify" />
  185.         </form>
  186.     </tr>
  187. EOT;
  188. }
  189. echo <<< EOT
  190. </table><br />
  191. EOT;
  192.  
  193. // Clear
  194. if (isset($result['CartItems']|| isset($result['SavedForLaterItems'])) {
  195. echo <<< EOT
  196. <p><a href="{$php_self}?action=clear">Clear</a></p>
  197. EOT;
  198. }
  199.     
  200. // Purchase
  201. if (isset($result['CartItems']|| isset($result['SavedForLaterItems'])) {
  202. echo <<< EOT
  203. <p><a href="{$result['PurchaseURL']}">Purchase</a></p>
  204. EOT;
  205. }
  206.  
  207. // Processing Time
  208. echo '<p>Processing Time : ' $amazon->getProcessingTime('sec</p>';
  209.  
  210. // Result
  211. echo '<p>Result :</p>';
  212. if (isset($result)) {
  213.     echo '<pre>';
  214.     var_dump($result);
  215.     echo '</pre>';
  216. }
  217.  
  218. echo <<< EOT
  219. </body>
  220. </html>
  221. EOT;
  222.  
  223. ?>

Documentation generated on Mon, 11 Mar 2019 15:46:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.