ショッピングカートのサンプルコード(VB.NET) †
ここで紹介しているコードはこちらのプロキシクラスを使用しています。
ここで紹介しているコードはこちらのC#のコードをVB.NETに変換したものです。
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
| | <%@ Page Language="VB" %>
<%@ 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 AWS_ACCESS_KEY_ID As String = "(あなたのAccessKeyID)"
Private Const ASSOCIATE_TAG As String = "dobonnet-22"
Private Const CART_ID_SESSION As String = "CartID"
Private Const CART_HMAC_SESSION As String = "Cart_HMAC"
Protected Sub Button1_Click( _
ByVal sender As Object, ByVal e As EventArgs)
AddToCart()
End Sub
Protected Sub Button2_Click( _
ByVal sender As Object, ByVal e As EventArgs)
RemoveFromCart()
End Sub
Protected Sub Button3_Click( _
ByVal sender As Object, ByVal e As EventArgs)
GetCart()
End Sub
Protected Sub Button4_Click( _
ByVal sender As Object, ByVal e As EventArgs)
ClearCart()
End Sub
Protected Sub CreateCart()
Dim ri As New CartCreateRequestItem()
ri.ASIN = TextBox1.Text
If IdTypeList.SelectedValue = "OfferListingId" Then
ri.OfferListingId = TextBox1.Text
Else
ri.ASIN = TextBox1.Text
End If
ri.Quantity = "1"
Dim req As New CartCreateRequest()
req.Items = New CartCreateRequestItem() {ri}
Dim cart As New CartCreate()
cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID
cart.AssociateTag = ASSOCIATE_TAG
cart.Request = New CartCreateRequest() {req}
Dim aws As New AWSECommerceService()
Dim res As CartCreateResponse
Try
res = aws.CartCreate(cart)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
End Try
If res Is Nothing OrElse res.Cart Is Nothing Then
Label1.Text = "結果を取得できませんでした。"
Return
End If
If Not (res.Cart(0).Request.Errors Is Nothing) Then
Label1.Text = "エラー:" + _
res.Cart(0).Request.Errors(0).Message
Return
End If
Label1.Text = GetCartItemsList(res.Cart(0))
Session(CART_ID_SESSION) = res.Cart(0).CartId
Session(CART_HMAC_SESSION) = res.Cart(0).HMAC
End Sub
Protected Sub AddToCart()
If Session(CART_ID_SESSION) Is Nothing OrElse _
Session(CART_HMAC_SESSION) Is Nothing Then
CreateCart()
Return
End If
Dim ri As New CartAddRequestItem()
If IdTypeList.SelectedValue = "OfferListingId" Then
ri.OfferListingId = TextBox1.Text
Else
ri.ASIN = TextBox1.Text
End If
ri.Quantity = "1"
Dim req As New CartAddRequest()
req.CartId = CStr(Session(CART_ID_SESSION))
req.HMAC = CStr(Session(CART_HMAC_SESSION))
req.Items = New CartAddRequestItem() {ri}
Dim cart As New CartAdd()
cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID
cart.AssociateTag = ASSOCIATE_TAG
cart.Request = New CartAddRequest() {req}
Dim aws As New AWSECommerceService()
Dim res As CartAddResponse
Try
res = aws.CartAdd(cart)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
End Try
If res Is Nothing OrElse res.Cart Is Nothing Then
Label1.Text = "結果を取得できませんでした。"
Return
End If
If Not (res.Cart(0).Request.Errors Is Nothing) Then
Label1.Text = "エラー:" + res.Cart(0).Request.Errors(0).Message
Return
End If
Label1.Text = GetCartItemsList(res.Cart(0))
End Sub
Protected Sub RemoveFromCart()
If Session(CART_ID_SESSION) Is Nothing OrElse _
Session(CART_HMAC_SESSION) Is Nothing Then
Label1.Text = "ショッピングカートがありません。"
Return
End If
Dim ri As New CartModifyRequestItem()
ri.CartItemId = TextBox2.Text
ri.Quantity = "0"
Dim req As New CartModifyRequest()
req.CartId = CStr(Session(CART_ID_SESSION))
req.HMAC = CStr(Session(CART_HMAC_SESSION))
req.Items = New CartModifyRequestItem() {ri}
Dim cart As New CartModify()
cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID
cart.AssociateTag = ASSOCIATE_TAG
cart.Request = New CartModifyRequest() {req}
Dim aws As New AWSECommerceService()
Dim res As CartModifyResponse
Try
res = aws.CartModify(cart)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
End Try
If res Is Nothing OrElse res.Cart Is Nothing Then
Label1.Text = "結果を取得できませんでした。"
Return
End If
If Not (res.Cart(0).Request.Errors Is Nothing) Then
Label1.Text = "エラー:" + _
res.Cart(0).Request.Errors(0).Message
Return
End If
Label1.Text = GetCartItemsList(res.Cart(0))
End Sub
Protected Sub GetCart()
If Session(CART_ID_SESSION) Is Nothing OrElse _
Session(CART_HMAC_SESSION) Is Nothing Then
Label1.Text = "ショッピングカートがありません。"
Return
End If
Dim req As New CartGetRequest()
req.CartId = CStr(Session(CART_ID_SESSION))
req.HMAC = CStr(Session(CART_HMAC_SESSION))
Dim cart As New CartGet()
cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID
cart.AssociateTag = ASSOCIATE_TAG
cart.Request = New CartGetRequest() {req}
Dim aws As New AWSECommerceService()
Dim res As CartGetResponse
Try
res = aws.CartGet(cart)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
End Try
If res Is Nothing OrElse res.Cart Is Nothing Then
Label1.Text = "結果を取得できませんでした。"
Return
End If
If Not (res.Cart(0).Request.Errors Is Nothing) Then
Label1.Text = "エラー:" + _
res.Cart(0).Request.Errors(0).Message
Return
End If
Label1.Text = GetCartItemsList(res.Cart(0))
End Sub
Protected Sub ClearCart()
If Session(CART_ID_SESSION) Is Nothing OrElse _
Session(CART_HMAC_SESSION) Is Nothing Then
Label1.Text = "ショッピングカートがありません。"
Return
End If
Dim req As New CartClearRequest()
req.CartId = CStr(Session(CART_ID_SESSION))
req.HMAC = CStr(Session(CART_HMAC_SESSION))
Dim cart As New CartClear()
cart.AWSAccessKeyId = AWS_ACCESS_KEY_ID
cart.AssociateTag = ASSOCIATE_TAG
cart.Request = New CartClearRequest() {req}
Dim aws As New AWSECommerceService()
Dim res As CartClearResponse
Try
res = aws.CartClear(cart)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
End Try
If res Is Nothing OrElse res.Cart Is Nothing Then
Label1.Text = "結果を取得できませんでした。"
Return
End If
If Not (res.Cart(0).Request.Errors Is Nothing) Then
Label1.Text = "エラー:" + _
res.Cart(0).Request.Errors(0).Message
Return
End If
Label1.Text = GetCartItemsList(res.Cart(0))
End Sub
Protected Function GetCartItemsList(ByVal cart As Cart) As String
Dim ret As String = ""
ret = "ショッピングカート内の商品 :<br />" + vbLf
If Not (cart.CartItems Is Nothing) Then
ret += "<ul>" + vbLf
Dim i As CartItem
For Each i In cart.CartItems.CartItem
ret += "<li><b>" + i.Title + "</b><br />" + vbLf
ret += "数量 : " + i.Quantity + "<br />" + vbLf
ret += "価格 : " + i.ItemTotal.FormattedPrice + _
"<br />" + vbLf
ret += "CartItemId : " + i.CartItemId + "<br />" + _
vbLf
Next i
ret += "</ul>" + vbLf
ret += "合計 : " + cart.SubTotal.FormattedPrice + _
"<br />" + vbLf
ret += "<a href=""" + cart.PurchaseURL + _
""">購入する</a><br />" + vbLf
Else
ret += "カートは空です。<br />" + vbLf
End If
Return ret
End Function
</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>
|