Auto-capture payments when an order is created, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever an order is created, with a 0 minute delay. Configuration includes minutes to wait before capturing, filter orders by this tag, 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 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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/create+{{ options.minutes_to_wait_before_capturing__number | at_least: 0 }}.minutes
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
minutes to wait before capturing (number), filter orders by this tag, capture orders with a high risk level (boolean), capture orders with a medium risk level (boolean), capture orders with a low risk level (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Capture orders with a low risk level
true