-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartToOrderSample.cs
More file actions
52 lines (45 loc) · 1.74 KB
/
CartToOrderSample.cs
File metadata and controls
52 lines (45 loc) · 1.74 KB
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
using CSharpAmazonBusinessAPI;
using CSharpAmazonBusinessAPI.Model.Cart;
using CSharpAmazonBusinessAPI.Model.Ordering;
namespace CSharpAmazonBusinessAPI.SampleCode;
// End-to-end REST workflow: search → add to cart → place order. Country defaults from the
// connection's marketplace; pass it explicitly only to override per call.
public class CartToOrderSample
{
private readonly AmazonBusinessConnection _connection;
public CartToOrderSample(AmazonBusinessConnection connection)
{
_connection = connection;
}
public async Task<PlaceOrderResult?> SearchAddAndOrderAsync(
string customerEmail,
string keywords,
string cartId,
int quantity = 1,
CancellationToken cancellationToken = default)
{
var search = await _connection.ProductSearch.SearchProductsAsync(
keywords: keywords,
customerEmail: customerEmail,
pageSize: 1,
cancellationToken: cancellationToken);
var firstAsin = search.Products?.FirstOrDefault()?.Asin;
if (string.IsNullOrEmpty(firstAsin)) return null;
await _connection.Cart.AddItemsAsync(
cartId,
new AddItemsRequest
{
Items = new List<AddItemRequest>
{
new AddItemRequest { ProductIdentifier = firstAsin, Quantity = quantity },
},
},
cancellationToken: cancellationToken);
var order = new PlaceOrderRequest
{
ExternalId = Guid.NewGuid().ToString(),
// LineItems, ShippingAddress, PaymentInfo, OrderRequestProperties etc. go here.
};
return await _connection.Ordering.PlaceOrderAsync(order, cancellationToken);
}
}