-
Notifications
You must be signed in to change notification settings - Fork 14
/
Task04b_checkout.ts
69 lines (52 loc) · 2.3 KB
/
Task04b_checkout.ts
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
import * as checkout from "./handson/order";
import { createPayment } from "./handson/payment";
import { log } from "./utils/logger";
const customerKey = "tt-customer";
const cartId = "";
const orderId = "";
const paymentDraft = {
key: "payment" + Math.random().toString(36).substring(2, 7),
amountPlanned: {
currencyCode: "EUR",
centAmount: 5000
},
pspName: "We_Do_Payments",
pspMethod: "CREDIT_CARD",
interfaceId: "we_pay_73636" + Math.random(), // Must be unique.
interactionId: "pay82626" + Math.random()
}
// create a cart and update the cartId variable
checkout.createCart(customerKey).then(log).catch(log);
// checkout.addLineItemsToCart(cartId, ["TULIPSEED02", "TULIPSEED03"]).then(log).catch(log);
// checkout.addDiscountCodeToCart(cartId, "SUMMER").then(log).catch(log);
// checkout.recalculate(cartId).then(log).catch(log);
// checkout.setShippingMethod(cartId).then(log).catch(log);
// create order from cart and update the orderId
// checkout.createOrderFromCart(cartId).then(log).catch(log);
// checkout.getOrderById(orderId).then(log).catch(log);
// set order state to confirmed and custom workflow state to order packed
// checkout.setOrderState(orderId, "Confirmed").then(log).catch(log);
// checkout.updateOrderCustomState(orderId, "tt-order-packed").then(log).catch(log);
const checkoutProcess = async () => {
let emptyCart = await checkout.createCart(customerKey);
let filledCart = await checkout.addLineItemsToCart(
emptyCart.body.id, ["TULIPSEED02", "TULIPSEED03"]
);
filledCart = await checkout.addDiscountCodeToCart(
filledCart.body.id, "SUMMER"
);
filledCart = await checkout.recalculate(filledCart.body.id);
filledCart = await checkout.setShippingMethod(filledCart.body.id);
const payment = await createPayment(paymentDraft);
filledCart = await checkout.addPaymentToCart(filledCart.body.id, payment.body.id);
let order = await checkout.createOrderFromCart(filledCart.body.id);
order = await checkout.setOrderState(order.body.id, "Confirmed");
order = await checkout.updateOrderCustomState(order.body.id, "tt-order-packed");
if (order) {
return {
status: 201,
message: "order created: " + order.body.id,
};
}
};
// checkoutProcess().then(log).catch(log);