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

Documentation generated on Mon, 11 Mar 2019 14:12:17 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.