/examples/amazon_ecs4_cart.php

https://github.com/ttsuruoka/Services_Amazon · PHP · 224 lines · 188 code · 20 blank · 16 comment · 25 complexity · 85906d1e4cf3bb5d40dae826fc9d4027 MD5 · raw file

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