*ショッピングカートのサンプルコード(C#) [#yd149ee2] ここで紹介しているコードは[[こちらのプロキシクラス>../../../ProxyClass]]を使用しています。 Amazon.co.jpのリモートショッピングカートを操作する(商品をショッピングカートに入れる、出す、すべて出す、カート内を表示)サンプルです。ショッピングカートに商品を入れるには、TextBoxに商品のASINかOfferListingIdを入力します。ショッピングカートから出すには、CartItemIdを入力します。 #code(csharp){{ <%@ 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> }} //これより下は編集しないでください #pageinfo(,2006-10-20 (金) 01:25:28,DOBON!,2006-10-20 (金) 02:13:26,DOBON!) |