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