ショッピングカートのサンプルコード(C#)

Amazon.co.jpのリモートショッピングカートを操作する(商品をショッピングカートに入れる、出す、すべて出す、カート内を表示)サンプルです。ショッピングカートに商品を入れるには、TextBoxに商品のASINかOfferListingIdを入力します。ショッピングカートから出すには、CartItemIdを入力します。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<%@ Page Language="C#" %>
<%@ Import Namespace="Amazon.jp.v20060913" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    
    private const string AWS_ACCESS_KEY_ID = "(あなたのAccessKeyID)";
    private const string ASSOCIATE_TAG = "dobonnet-22";
 
    private const string CART_ID_SESSION = "CartID";
    private const string CART_HMAC_SESSION = "Cart_HMAC";
    
    //カートに商品を追加
    protected void Button1_Click(object sender, EventArgs e)
    {
        AddToCart();
    }
 
    //カートから商品を削除
    protected void Button2_Click(object sender, EventArgs e)
    {
        RemoveFromCart();
    }
 
    //カーとの中身を確認
    protected void Button3_Click(object sender, EventArgs e)
    {
        GetCart();
    }
 
    //カートをクリア
    protected void Button4_Click(object sender, EventArgs e)
    {
        ClearCart();
    }
 
    //カートを作成し、商品を追加
    protected void CreateCart()
    {
        CartCreateRequestItem ri = new CartCreateRequestItem();
        //カートに追加する商品のASINを指定
        ri.ASIN = TextBox1.Text;
        if (IdTypeList.SelectedValue == "OfferListingId")
        {
            //カートに追加する商品のOfferListingIdを指定
            ri.OfferListingId = TextBox1.Text;
        }
        else
        {
            //カートに追加する商品のASINを指定
            ri.ASIN = TextBox1.Text;
        }
        //カートに入れる個数を指定
        ri.Quantity = "1";
 
        CartCreateRequest req = new CartCreateRequest();
        req.Items = new CartCreateRequestItem[] { ri };
 
        CartCreate cart = new CartCreate();
        //あなたのAccess Key ID
        cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID;
        //あなたのAssociate ID
        cart.AssociateTag = ASSOCIATE_TAG;
        cart.Request = new CartCreateRequest[] { req };
 
        AWSECommerceService aws = new AWSECommerceService();
        CartCreateResponse res;
        try
        {
            //カートを作成し、商品を追加
            res = aws.CartCreate(cart);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }
 
        //失敗した時
        if (res == null || res.Cart == null)
        {
            Label1.Text = "結果を取得できませんでした。";
            return;
        }
        if (res.Cart[0].Request.Errors != null)
        {
            Label1.Text = "エラー:" +
                res.Cart[0].Request.Errors[0].Message;
            return;
            
        }
 
        //カートの中身を表示
        Label1.Text = GetCartItemsList(res.Cart[0]);
 
        //CartIDとHMACを保存する
        Session[CART_ID_SESSION] = res.Cart[0].CartId;
        Session[CART_HMAC_SESSION] = res.Cart[0].HMAC;
    }
 
    //カートに商品を追加する
    protected void AddToCart()
    {
        //カートがなければ、作成する
        if (Session[CART_ID_SESSION] == null ||
            Session[CART_HMAC_SESSION] == null)
        {
            CreateCart();
            return;
        }
 
        CartAddRequestItem ri = new CartAddRequestItem();
        if (IdTypeList.SelectedValue == "OfferListingId")
        {
            //カートに追加する商品のOfferListingIdを指定
            ri.OfferListingId = TextBox1.Text;
        }
        else
        {
            //カートに追加する商品のASINを指定
            ri.ASIN = TextBox1.Text;
        }
        //カートに入れる個数を指定
        ri.Quantity = "1";
 
        CartAddRequest req = new CartAddRequest();
        //CartIDとHMACを指定
        req.CartId = (string)Session[CART_ID_SESSION];
        req.HMAC = (string)Session[CART_HMAC_SESSION];
        req.Items = new CartAddRequestItem[] { ri };
 
        CartAdd cart = new CartAdd();
        //あなたのAccess Key ID
        cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID;
        //あなたのAssociate ID
        cart.AssociateTag = ASSOCIATE_TAG;
        cart.Request = new CartAddRequest[] { req };
 
        AWSECommerceService aws = new AWSECommerceService();
        CartAddResponse res;
        try
        {
            //カートに追加
            res = aws.CartAdd(cart);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }
 
        //失敗した時
        if (res == null || res.Cart == null)
        {
            Label1.Text = "結果を取得できませんでした。";
            return;
        }
        if (res.Cart[0].Request.Errors != null)
        {
            Label1.Text = "エラー:" +
                res.Cart[0].Request.Errors[0].Message;
            return;
        }
 
        //カートの中身を表示
        Label1.Text = GetCartItemsList(res.Cart[0]);
    }
 
    //カートから商品を削除
    protected void RemoveFromCart()
    {
        //カートがあるか
        if (Session[CART_ID_SESSION] == null ||
            Session[CART_HMAC_SESSION] == null)
        {
            Label1.Text = "ショッピングカートがありません。";
            return;
        }
 
        CartModifyRequestItem ri = new CartModifyRequestItem();
        //削除する商品のCartItemIdを指定
        ri.CartItemId = TextBox2.Text;
        //Quantityを0にする
        ri.Quantity = "0";
 
        CartModifyRequest req = new CartModifyRequest();
        //CartIDとHMACを指定
        req.CartId = (string)Session[CART_ID_SESSION];
        req.HMAC = (string)Session[CART_HMAC_SESSION];
        req.Items = new CartModifyRequestItem[] { ri };
 
        CartModify cart = new CartModify();
        //あなたのAccess Key ID
        cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID;
        //あなたのAssociate ID
        cart.AssociateTag = ASSOCIATE_TAG;
        cart.Request = new CartModifyRequest[] { req };
 
        AWSECommerceService aws = new AWSECommerceService();
        CartModifyResponse res;
        try
        {
            //カートから商品を削除
            res = aws.CartModify(cart);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }
 
        //失敗した時
        if (res == null || res.Cart == null)
        {
            Label1.Text = "結果を取得できませんでした。";
            return;
        }
        if (res.Cart[0].Request.Errors != null)
        {
            Label1.Text = "エラー:" +
                res.Cart[0].Request.Errors[0].Message;
            return;
 
        }
 
        //カートの中身を表示
        Label1.Text = GetCartItemsList(res.Cart[0]);
    }
 
    //カートの中身を見る
    protected void GetCart()
    {
        //カートがあるか
        if (Session[CART_ID_SESSION] == null ||
            Session[CART_HMAC_SESSION] == null)
        {
            Label1.Text = "ショッピングカートがありません。";
            return;
        }
 
        CartGetRequest req = new CartGetRequest();
        //CartIDとHMACを指定
        req.CartId = (string)Session[CART_ID_SESSION];
        req.HMAC = (string)Session[CART_HMAC_SESSION];
 
        CartGet cart = new CartGet();
        //あなたのAccess Key ID
        cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID;
        //あなたのAssociate ID
        cart.AssociateTag = ASSOCIATE_TAG;
        cart.Request = new CartGetRequest[] { req };
 
        AWSECommerceService aws = new AWSECommerceService();
        CartGetResponse res;
        try
        {
            //カートの中身を取得
            res = aws.CartGet(cart);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }
 
        //失敗した時
        if (res == null || res.Cart == null)
        {
            Label1.Text = "結果を取得できませんでした。";
            return;
        }
        if (res.Cart[0].Request.Errors != null)
        {
            Label1.Text = "エラー:" +
                res.Cart[0].Request.Errors[0].Message;
            return;
 
        }
 
        //カートの中身を表示
        Label1.Text = GetCartItemsList(res.Cart[0]);
    }
 
    //カートの中身をクリアする
    protected void ClearCart()
    {
        //カートがあるか
        if (Session[CART_ID_SESSION] == null ||
            Session[CART_HMAC_SESSION] == null)
        {
            Label1.Text = "ショッピングカートがありません。";
            return;
        }
        
        CartClearRequest req = new CartClearRequest();
        //CartIDとHMACを指定
        req.CartId = (string)Session[CART_ID_SESSION];
        req.HMAC = (string)Session[CART_HMAC_SESSION];
 
        CartClear cart = new CartClear();
        //あなたのAccess Key ID
        cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID;
        //あなたのAssociate ID
        cart.AssociateTag = ASSOCIATE_TAG;
        cart.Request = new CartClearRequest[] { req };
 
        AWSECommerceService aws = new AWSECommerceService();
        CartClearResponse res;
        try
        {
            //カートをクリアする
            res = aws.CartClear(cart);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }
 
        if (res == null || res.Cart == null)
        {
            Label1.Text = "結果を取得できませんでした。";
            return;
        }
        if (res.Cart[0].Request.Errors != null)
        {
            Label1.Text = "エラー:" +
                res.Cart[0].Request.Errors[0].Message;
            return;
 
        }
 
        //カートの中身を表示
        Label1.Text = GetCartItemsList(res.Cart[0]);
    }
 
    //カート内の商品を表示
    protected string GetCartItemsList(Cart cart)
    {
        string ret = "";
 
        ret = "ショッピングカート内の商品 :<br />\n";
        if (cart.CartItems != null)
        {
            ret += "<ul>\n";
            foreach (CartItem i in cart.CartItems.CartItem)
            {
                ret += "<li><b>" + i.Title + "</b><br />\n";
                ret += "数量 : " + i.Quantity + "<br />\n";
                ret += "価格 : " + i.ItemTotal.FormattedPrice
                    + "<br />\n";
                ret += "CartItemId : " + i.CartItemId + "<br />\n";
            }
            ret += "</ul>\n";
 
            ret += "合計 : " + cart.SubTotal.FormattedPrice + "<br />\n";
            ret += "<a href=\"" + cart.PurchaseURL + "\">購入する</a><br />\n";
        }
        else
        {
            ret += "カートは空です。<br />\n";
        }
 
        return ret;
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Amazonショッピングカート</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="IdTypeList" runat="server">
            <asp:ListItem>ASIN</asp:ListItem>
            <asp:ListItem>OfferListingId</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server" EnableViewState="False">
        </asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="カートに入れる"
         OnClick="Button1_Click" />
        <br />
        <label for="TextBox2">CartItemId : </label>
        <asp:TextBox ID="TextBox2" runat="server" EnableViewState="False">
        </asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="カートから出す"
         OnClick="Button2_Click" /><br />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click"
         Text="カートの中身を見る" />
        <asp:Button ID="Button4" runat="server" Text="カートをクリア"
         OnClick="Button4_Click" /><br />
    </div>
    </form>
    <br />
    <asp:Label ID="Label1" runat="server" EnableViewState="False">
    </asp:Label>
</body>
</html>

ページ情報
  • 作成日 : 2006-10-20 (金) 01:25:28
  • 作成者 : DOBON!
  • 最終編集日 : 2006-10-20 (金) 01:25:28
  • 最終編集者 : DOBON!
[ トップ ]   [ 新規 | 子ページ作成 | 一覧 | 単語検索 | 最終更新 | ヘルプ ]