-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.liquid
178 lines (156 loc) · 5.13 KB
/
script.liquid
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
{% assign min = options.minutes_to_wait_before_capturing__number | at_least: 0 %}
{% assign filter_orders_by_this_tag = options.filter_orders_by_this_tag %}
{% assign capture_orders_with_a_high_risk_level = options.capture_orders_with_a_high_risk_level__boolean %}
{% assign capture_orders_with_a_medium_risk_level = options.capture_orders_with_a_medium_risk_level__boolean %}
{% assign capture_orders_with_a_low_risk_level = options.capture_orders_with_a_low_risk_level__boolean %}
{% comment %}
-- make sure that are least one risk level option is chosen
{% endcomment %}
{% unless capture_orders_with_a_high_risk_level
or capture_orders_with_a_medium_risk_level
or capture_orders_with_a_low_risk_level
%}
{% error "Choose at least one risk level to capture for!" %}
{% endunless %}
{% comment %}
-- get the order statuses, risk assessments, and capturable transactions
{% endcomment %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
displayFinancialStatus
tags
risk {
assessments {
riskLevel
}
}
transactions(capturable: true) {
id
kind
totalUnsettledSet {
presentmentMoney {
amount
currencyCode
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% comment %}
-- set up a useful preview regardless of which combinations of risk levels are checked
{% endcomment %}
{% if event.preview %}
{% if capture_orders_with_a_high_risk_level %}
{% assign preview_risk_level = "HIGH" %}
{% elsif capture_orders_with_a_medium_risk_level %}
{% assign preview_risk_level = "MEDIUM" %}
{% elsif capture_orders_with_a_low_risk_level %}
{% assign preview_risk_level = "LOW" %}
{% endif %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"displayFinancialStatus": "AUTHORIZED",
"tags": [{{ filter_orders_by_this_tag | json }}],
"capturable": true,
"risk": {
"assessments": [
{
"riskLevel": {{ preview_risk_level | json }}
}
]
},
"transactions": [
{
"id": "gid://shopify/OrderTransaction/1234567890",
"kind": "AUTHORIZATION",
"status": "SUCCESS",
"totalUnsettledSet": {
"presentmentMoney": {
"amount": "12.34",
"currencyCode": "USD"
}
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% comment %}
-- can only capture authorized or partially paid orders; exit otherwise
{% endcomment %}
{% unless order.displayFinancialStatus == "AUTHORIZED" or order.displayFinancialStatus == "PARTIALLY_PAID" %}
{% break %}
{% endunless %}
{% comment %}
-- if an order tag filter is enabled, make sure the order has it to proceed
{% endcomment %}
{% if filter_orders_by_this_tag != blank %}
{% unless order.tags contains filter_orders_by_this_tag %}
{% break %}
{% endunless %}
{% endif %}
{% comment %}
-- check risk levels in order of severity, capturing if that level is enabled and there is at least one found across all the risk assesments
{% endcomment %}
{% assign order_risk_levels = order.risk.assessments | map: "riskLevel" %}
{% assign do_capture = false %}
{% if order_risk_levels contains "HIGH" and capture_orders_with_a_high_risk_level %}
{% assign do_capture = true %}
{% elsif order_risk_levels contains "MEDIUM" and capture_orders_with_a_medium_risk_level %}
{% assign do_capture = true %}
{% elsif order_risk_levels contains "LOW" and capture_orders_with_a_low_risk_level %}
{% assign do_capture = true %}
{% else %}
{% log
message: "This order does not have any risk assessments with the configured risk level(s) to capture.",
order_risk_levels: order_risk_levels
%}
{% endif %}
{% if do_capture %}
{% comment %}
-- capture any authorized transaction with a positive unsettled amount
{% endcomment %}
{% assign authorized_transactions = order.transactions | where: "kind", "AUTHORIZATION" %}
{% for transaction in authorized_transactions %}
{% assign unsettled_amount = transaction.totalUnsettledSet.presentmentMoney.amount | times: 1.0 %}
{% if unsettled_amount > 0 %}
{% action "shopify" %}
mutation {
orderCapture(
input: {
id: {{ order.id | json }}
parentTransactionId: {{ transaction.id | json }}
amount: {{ unsettled_amount | json }}
currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}
}
) {
transaction {
id
status
parentTransaction {
id
kind
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}