-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckout_rules.lua
More file actions
80 lines (71 loc) · 2.72 KB
/
checkout_rules.lua
File metadata and controls
80 lines (71 loc) · 2.72 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
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
local function add_tag(decision, tag)
table.insert(decision.tags, tag)
end
local function has_category(order, category)
for _, item in ipairs(order.items) do
if item.category == category then
return true
end
end
return false
end
function evaluate_order(order)
audit(string.format(
"evaluating order=%s total=%.2f customer=%s",
order.id,
order.total_cents / 100,
order.customer.email
))
local decision = {
approved = true,
action = "approve",
discount_cents = 0,
shipping_tier = "standard",
eta_days = 0,
reason = "default approval",
tags = {},
}
for _, item in ipairs(order.items) do
if not inventory_available(item.sku, item.quantity) then
decision.approved = false
decision.action = "reject"
decision.reason = "inventory unavailable for " .. item.sku
add_tag(decision, "inventory_block")
return decision
end
end
local risk = risk_score(order.customer.email, order.total_cents, order.customer.country)
add_tag(decision, "risk:" .. tostring(risk))
if order.customer.country == "BR" and has_category(order, "hazmat") then
decision.approved = false
decision.action = "manual_review"
decision.reason = "hazmat export requires compliance review"
decision.shipping_tier = "hold"
add_tag(decision, "compliance")
elseif risk >= 85 then
decision.approved = false
decision.action = "manual_review"
decision.reason = "high risk checkout requires analyst approval"
decision.shipping_tier = "hold"
add_tag(decision, "fraud_review")
elseif order.customer.vip and order.total_cents >= 10000 then
decision.discount_cents = math.floor(order.total_cents * 0.15)
decision.shipping_tier = "express"
decision.reason = "vip customer received dynamic discount"
add_tag(decision, "vip")
elseif order.coupon_code == "FLASH10" and order.total_cents >= 5000 then
decision.discount_cents = math.floor(order.total_cents * 0.10)
decision.reason = "campaign coupon accepted"
add_tag(decision, "campaign")
end
if decision.action == "approve" and order.customer.loyalty_points >= 1000 then
decision.shipping_tier = "priority"
add_tag(decision, "loyalty_upgrade")
end
if decision.action == "approve" and has_category(order, "fragile") and decision.shipping_tier == "standard" then
decision.shipping_tier = "priority"
add_tag(decision, "fragile_handling")
end
decision.eta_days = shipping_eta(order.customer.country, decision.shipping_tier)
return decision
end