Mechanic Get Mechanic
Task library

Auto-capture payments when an order is created

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.

Try this task

When it runs

Occurs whenever an order is created, with a 0 minute delay

What you choose

  • Minutes to wait before capturing
  • Filter orders by this tag
  • Capture orders with a high risk level
  • Capture orders with a medium risk level
  • Capture orders with a low risk level

Documentation

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.

If 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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/create+{{ options.minutes_to_wait_before_capturing__number | at_least: 0 }}.minutes
How task subscriptions work

Option types and flags

Task code

{% 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 %}
Mechanic Liquid reference

Default option values

Capture orders with a high risk level
false
Capture orders with a medium risk level
false
Capture orders with a low risk level
true