forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-capture-order-payments-based-on-kount-risk-assessments.json
24 lines (24 loc) · 4.6 KB
/
auto-capture-order-payments-based-on-kount-risk-assessments.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
{
"docs": "An integration with Kount, this task allows you to immediately capture payments for orders that Kount has deemed to be of a certain risk level.\n\nThis task requires your Shopify store to have order risk data provided by [Kount](https://www.kount.com).",
"halt_action_run_sequence_on_error": false,
"name": "Auto-capture order payments based on Kount risk assessments",
"online_store_javascript": null,
"options": {
"number_of_minutes_to_wait_before_capturing__number": null,
"capture_orders_with_a_high_risk_level__boolean": null,
"capture_orders_with_a_medium_risk_level__boolean": null,
"capture_orders_with_a_low_risk_level__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign n = options.number_of_minutes_to_wait_before_capturing__number %}\n{% if n != blank and n <= 0 %}\n {\"error\": \"'Number of minutes to wait before capturing' must be positive! :)\"}\n{% endif %}\n\n{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n risks {\n level\n message\n }\n transactions(first: 1, capturable: true) {\n id\n totalUnsettledSet {\n presentmentMoney {\n amount\n currencyCode\n }\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% if options.capture_orders_with_a_high_risk_level__boolean %}\n {% assign preview_risk_level = \"HIGH\" %}\n {% elsif options.capture_orders_with_a_medium_risk_level__boolean %}\n {% assign preview_risk_level = \"MEDIUM\" %}\n {% elsif options.capture_orders_with_a_low_risk_level__boolean %}\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 \"capturable\": true,\n \"risks\": [\n {\n \"level\": {{ preview_risk_level | json }},\n \"message\": \"[Kount]: The Kount Risk Assessment responded with a Score of XX and a recommendation of XXXXX for transaction ID: XXXXXXXXXXXX.\"\n }\n ],\n \"transactions\": [\n {\n \"id\": \"gid://shopify/OrderTransaction/1234567890\",\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 orderNode = result.data.order %}\n\n{% assign capturableTransactionNode = orderNode.transactions.first %}\n\n{% assign kountRiskNode = nil %}\n{% for riskNode in orderNode.risks %}\n {% unless riskNode.message contains \"[Kount]\" %}\n {% continue %}\n {% endunless %}\n\n {% assign kountRiskNode = riskNode %}\n {% break %}\n{% endfor %}\n\n{% assign do_capture = false %}\n{% if capturableTransactionNode and kountRiskNode %}\n {% if kountRiskNode.level == \"HIGH\" and options.capture_orders_with_a_high_risk_level__boolean %}\n {% assign do_capture = true %}\n {% elsif kountRiskNode.level == \"MEDIUM\" and options.capture_orders_with_a_medium_risk_level__boolean %}\n {% assign do_capture = true %}\n {% elsif kountRiskNode.level == \"LOW\" and options.capture_orders_with_a_low_risk_level__boolean %}\n {% assign do_capture = true %}\n {% endif %}\n{% endif %}\n\n{% if kountRiskNode == nil %}\n {\"log\": \"This order has no Kount risk on file.\"}\n{% endif %}\n\n{% if do_capture %}\n {% assign amount_to_capture = capturableTransactionNode.totalUnsettledSet.presentmentMoney.amount | times: 1 %}\n\n {% action \"shopify\" %}\n mutation {\n orderCapture(\n input: {\n id: {{ orderNode.id | json }}\n parentTransactionId: {{ capturableTransactionNode.id | json }}\n amount: {{ amount_to_capture | json }}\n currency: {{ capturableTransactionNode.totalUnsettledSet.presentmentMoney.currencyCode }}\n }\n ) {\n transaction {\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/create"
],
"subscriptions_template": "{% assign n = options.number_of_minutes_to_wait_before_capturing__number %}\n\nshopify/orders/create{% if n != blank and n > 0 %}+{{ n }}.minutes{% endif %}",
"tags": [
"Kount",
"Payment",
"Risk"
]
}