Auto-capture order payments based on Kount risk assessments, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-capture order payments based on Kount risk assessments

by Isaac Bowen (team@usemechanic.com)

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.

Runs when an order is created. Configuration includes number of minutes to wait before capturing, capture orders with a high risk level, capture orders with a medium risk level, and capture orders with a low risk level.

15-day free trial – unlimited tasks

Documentation

This task requires your Shopify store to have order risk data provided by Kount.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

Events
when an order is created (shopify/orders/create)
Options
number of minutes to wait before capturing (number), capture orders with a high risk level (boolean), capture orders with a medium risk level (boolean), capture orders with a low risk level (boolean)
Script
{% assign n = options.number_of_minutes_to_wait_before_capturing__number %}
{% if n != blank and n <= 0 %}
  {"error": "'Number of minutes to wait before capturing' must be positive! :)"}
{% endif %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      risks {
        level
        message
      }
      transactions(first: 1, capturable: true) {
        id
        totalUnsettledSet {
          shopMoney {
            amount
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% if options.capture_orders_with_a_high_risk_level__boolean %}
    {% assign preview_risk_level = "HIGH" %}
  {% elsif options.capture_orders_with_a_medium_risk_level__boolean %}
    {% assign preview_risk_level = "MEDIUM" %}
  {% elsif options.capture_orders_with_a_low_risk_level__boolean %}
    {% assign preview_risk_level = "LOW" %}
  {% endif %}
  
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "capturable": true,
          "risks": [
            {
              "level": {{ preview_risk_level | json }},
              "message": "[Kount]: The Kount Risk Assessment responded with a Score of XX and a recommendation of XXXXX for transaction ID: XXXXXXXXXXXX."
            }
          ],
          "transactions": [
            {
              "id": "gid://shopify/OrderTransaction/1234567890",
              "totalUnsettledSet": {
                "shopMoney": {
                  "amount": "200.0"
                }
              }
            }
          ]
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign orderNode = result.data.order %}

{% assign capturableTransactionNode = orderNode.transactions.first %}

{% assign kountRiskNode = nil %}
{% for riskNode in orderNode.risks %}
  {% unless riskNode.message contains "[Kount]" %}
    {% continue %}
  {% endunless %}

  {% assign kountRiskNode = riskNode %}
  {% break %}
{% endfor %}

{% assign do_capture = false %}
{% if capturableTransactionNode and kountRiskNode %}
  {% if kountRiskNode.level == "HIGH" and options.capture_orders_with_a_high_risk_level__boolean %}
    {% assign do_capture = true %}
  {% elsif kountRiskNode.level == "MEDIUM" and options.capture_orders_with_a_medium_risk_level__boolean %}
    {% assign do_capture = true %}
  {% elsif kountRiskNode.level == "LOW" and options.capture_orders_with_a_low_risk_level__boolean %}
    {% assign do_capture = true %}
  {% endif %}
{% endif %}

{% if kountRiskNode == nil %}
  {"log": "This order has no Kount risk on file."}
{% endif %}

{% if do_capture %}
  {% assign amount_to_capture = capturableTransactionNode.totalUnsettledSet.shopMoney.amount | times: 1 %}

  {% action "shopify" %}
    mutation {
      orderCapture(
        input: {
          id: {{ orderNode.id | json }}
          parentTransactionId: {{ capturableTransactionNode.id | json }}
          amount: {{ amount_to_capture | json }}
        }
      ) {
        transaction {
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.