-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathauto-capture-payments-when-an-order-is-created.json
26 lines (26 loc) · 6.54 KB
/
auto-capture-payments-when-an-order-is-created.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
{
"docs": "This task runs when an order is created and captures any authorized transactions, based on the configured risk levels. Optionally choose to filter orders for capture by a tag.\n\nIf you have additional apps or tasks informing an order's risk levels, increase the \"Minutes to wait before capturing\" to make sure they have time to contribute their data.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-capture payments when an order is created",
"online_store_javascript": null,
"options": {
"minutes_to_wait_before_capturing__number": null,
"filter_orders_by_this_tag": null,
"capture_orders_with_a_high_risk_level__boolean": false,
"capture_orders_with_a_medium_risk_level__boolean": false,
"capture_orders_with_a_low_risk_level__boolean": true
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign min = options.minutes_to_wait_before_capturing__number | at_least: 0 %}\n{% assign filter_orders_by_this_tag = options.filter_orders_by_this_tag %}\n{% assign capture_orders_with_a_high_risk_level = options.capture_orders_with_a_high_risk_level__boolean %}\n{% assign capture_orders_with_a_medium_risk_level = options.capture_orders_with_a_medium_risk_level__boolean %}\n{% assign capture_orders_with_a_low_risk_level = options.capture_orders_with_a_low_risk_level__boolean %}\n\n{% comment %}\n -- make sure that are least one risk level option is chosen\n{% endcomment %}\n\n{% unless capture_orders_with_a_high_risk_level\n or capture_orders_with_a_medium_risk_level\n or capture_orders_with_a_low_risk_level\n%}\n {% error \"Choose at least one risk level to capture for!\" %}\n{% endunless %}\n\n{% comment %}\n -- get the order statuses, risk assessments, and capturable transactions\n{% endcomment %}\n\n{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n displayFinancialStatus\n tags\n risk {\n assessments {\n riskLevel\n }\n }\n transactions(capturable: true) {\n id\n kind\n totalUnsettledSet {\n presentmentMoney {\n amount\n currencyCode\n }\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% comment %}\n -- set up a useful preview regardless of which combinations of risk levels are checked\n{% endcomment %}\n\n{% if event.preview %}\n {% if capture_orders_with_a_high_risk_level %}\n {% assign preview_risk_level = \"HIGH\" %}\n {% elsif capture_orders_with_a_medium_risk_level %}\n {% assign preview_risk_level = \"MEDIUM\" %}\n {% elsif capture_orders_with_a_low_risk_level %}\n {% assign preview_risk_level = \"LOW\" %}\n {% endif %}\n\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"displayFinancialStatus\": \"AUTHORIZED\",\n \"tags\": [{{ filter_orders_by_this_tag | json }}],\n \"capturable\": true,\n \"risk\": {\n \"assessments\": [\n {\n \"riskLevel\": {{ preview_risk_level | json }}\n }\n ]\n },\n \"transactions\": [\n {\n \"id\": \"gid://shopify/OrderTransaction/1234567890\",\n \"kind\": \"AUTHORIZATION\",\n \"status\": \"SUCCESS\",\n \"totalUnsettledSet\": {\n \"presentmentMoney\": {\n \"amount\": \"12.34\",\n \"currencyCode\": \"USD\"\n }\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{% comment %}\n -- can only capture authorized or partially paid orders; exit otherwise\n{% endcomment %}\n\n{% unless order.displayFinancialStatus == \"AUTHORIZED\" or order.displayFinancialStatus == \"PARTIALLY_PAID\" %}\n {% break %}\n{% endunless %}\n\n{% comment %}\n -- if an order tag filter is enabled, make sure the order has it to proceed\n{% endcomment %}\n\n{% if filter_orders_by_this_tag != blank %}\n {% unless order.tags contains filter_orders_by_this_tag %}\n {% break %}\n {% endunless %}\n{% endif %}\n\n{% comment %}\n -- 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\n{% endcomment %}\n\n{% assign order_risk_levels = order.risk.assessments | map: \"riskLevel\" %}\n\n{% assign do_capture = false %}\n\n{% if order_risk_levels contains \"HIGH\" and capture_orders_with_a_high_risk_level %}\n {% assign do_capture = true %}\n{% elsif order_risk_levels contains \"MEDIUM\" and capture_orders_with_a_medium_risk_level %}\n {% assign do_capture = true %}\n{% elsif order_risk_levels contains \"LOW\" and capture_orders_with_a_low_risk_level %}\n {% assign do_capture = true %}\n{% else %}\n {% log\n message: \"This order does not have any risk assessments with the configured risk level(s) to capture.\",\n order_risk_levels: order_risk_levels\n %}\n{% endif %}\n\n{% if do_capture %}\n {% comment %}\n -- capture any authorized transaction with a positive unsettled amount\n {% endcomment %}\n\n {% assign authorized_transactions = order.transactions | where: \"kind\", \"AUTHORIZATION\" %}\n\n {% for transaction in authorized_transactions %}\n {% assign unsettled_amount = transaction.totalUnsettledSet.presentmentMoney.amount | times: 1.0 %}\n\n {% if unsettled_amount > 0 %}\n {% action \"shopify\" %}\n mutation {\n orderCapture(\n input: {\n id: {{ order.id | json }}\n parentTransactionId: {{ transaction.id | json }}\n amount: {{ unsettled_amount | json }}\n currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}\n }\n ) {\n transaction {\n id\n status\n parentTransaction {\n id\n kind\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create+0.minutes"
],
"subscriptions_template": "shopify/orders/create+{{ options.minutes_to_wait_before_capturing__number | at_least: 0 }}.minutes",
"tags": [
"Orders",
"Payment",
"Risk"
]
}