-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathautomatically-cancel-high-risk-orders.json
28 lines (28 loc) · 5.5 KB
/
automatically-cancel-high-risk-orders.json
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
{
"docs": "This task cancels orders as soon as Shopify (or another risk-analysis app) determines it to be high risk. Optionally, this task can also auto-tag the order, email the customer, restock the inventory, and/or refund payment.\n\nValid cancellation reasons to set:\n\n* customer: The customer wanted to cancel the order.\n* declined: Payment was declined.\n* fraud: The order was fraudulent.\n* inventory: There was insufficient inventory.\n* staff: Staff made an error.\n* other: The order was canceled for an unlisted reason.\n\n__NOTE:__ This task will not cancel orders that have been partially or fully fulfilled",
"halt_action_run_sequence_on_error": false,
"name": "Automatically cancel high-risk orders",
"online_store_javascript": null,
"options": {
"cancellation_reason_to_set": null,
"ignore_unpaid_orders__boolean": null,
"refund_payment_for_cancelled_orders__boolean": null,
"restock_inventory_for_cancelled_orders__boolean": null,
"email_customer_when_cancelling__boolean": null,
"staff_note_for_timeline": null,
"add_this_order_tag_when_cancelling": ""
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign cancellation_reason = options.cancellation_reason_to_set | default: \"other\" %}\n{% assign ignore_unpaid_orders = options.ignore_unpaid_orders__boolean %}\n{% assign refund_payment = options.refund_payment_for_cancelled_orders__boolean %}\n{% assign restock_inventory = options.restock_inventory_for_cancelled_orders__boolean %}\n{% assign notify_customer = options.email_customer_when_cancelling__boolean %}\n{% assign staff_note = options.staff_note_for_timeline %}\n{% assign order_tag_to_apply = options.add_this_order_tag_when_cancelling %}\n\n{% comment %}\n -- check that a valid cancellation reason has been configured; it will default to 'other' if left blank\n{% endcomment %}\n\n{% assign valid_cancellation_reasons = \"customer,declined,fraud,inventory,other,staff\" | split: \",\" %}\n\n{% unless valid_cancellation_reasons contains cancellation_reason %}\n {% error %}\n {{ \"Cancellation reason \" | append: cancellation_reason | append: \" - must be one of 'customer', 'declined', 'fraud', 'inventory', 'other', or 'staff'.\" | json }}\n {% enderror %}\n{% endunless %}\n\n{% comment %}\n -- get the order statuses and risk assessments\n{% endcomment %}\n\n{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n cancelledAt\n displayFinancialStatus\n displayFulfillmentStatus\n risk {\n assessments {\n riskLevel\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"displayFinancialStatus\": \"PAID\",\n \"displayFulfillmentStatus\": \"UNFULFILLED\",\n \"risk\": {\n \"assessments\": [\n {\n \"riskLevel\": \"NONE\"\n },\n {\n \"riskLevel\": \"HIGH\"\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign order = result.data.order %}\n\n{% log order: order %}\n\n{% if order.cancelledAt %}\n {% log \"This order has already been cancelled.\" %}\n {% break %}\n{% endif %}\n\n{% if ignore_unpaid_orders and order.displayFinancialStatus != \"PAID\" %}\n {% log \"This order has not been paid and the ignore unpaid orders option is enabled.\" %}\n {% break %}\n{% endif %}\n\n{% comment %}\n -- get the risk level from each risk assessment\n{% endcomment %}\n\n{% assign order_risk_levels = order.risk.assessments | map: \"riskLevel\" %}\n\n{% unless order_risk_levels contains \"HIGH\" %}\n {% log \"The current risk assessments for this order do not contain a HIGH risk level.\" %}\n {% break %}\n{% endunless %}\n\n{% comment %}\n -- check to make sure order is unfulfilled to avoid cancellation error\n{% endcomment %}\n\n{% if order.displayFulfillmentStatus == \"FULFILLED\" or order.displayFulfillmentStatus == \"PARTIALLY_FULFILLED\" %}\n {% log \"This order has already been fulfilled or partially fulfilled and cannot be cancelled.\" %}\n {% break %}\n{% endif %}\n\n{% comment %}\n -- cancel the order with configured options\n{% endcomment %}\n\n{% action \"shopify\" %}\n mutation {\n orderCancel(\n orderId: {{ order.id | json }}\n notifyCustomer: {{ notify_customer | json }}\n reason: {{ cancellation_reason | upcase }}\n refund: {{ refund_payment | json }}\n restock: {{ restock_inventory | json }}\n staffNote: {{ staff_note | json }}\n ) {\n job {\n id\n }\n orderCancelUserErrors {\n code\n field\n message\n }\n }\n }\n{% endaction %}\n\n{% comment %}\n -- tag the order if there is a tag configured to apply on cancellation\n{% endcomment %}\n\n{% if order_tag_to_apply != blank %}\n {% action \"shopify\" %}\n mutation {\n tagsAdd(\n id: {{ order.id | json }}\n tags: {{ order_tag_to_apply | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/updated"
],
"subscriptions_template": "shopify/orders/updated",
"tags": [
"Cancel",
"Orders",
"Risk"
]
}