Mechanic Get Mechanic
Task library

Auto-capture order payment after x days

After waiting 7 days (or another timespan of your choice), this task attempts to capture all open authorized payments for an order. (Multiple authorizations can exist on edited orders or with post-purchase upsells.)

Try this task

When it runs

Occurs whenever an order is created, with a 7 day delay

What you choose

Days to wait before capturing

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.days_to_wait_before_capturing__number_required | default: 7 }}.days
How task subscriptions work

Option types and flags

Task code

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      name
      displayFinancialStatus
      transactions(capturable: true) {
        id
        kind
        totalUnsettledSet {
          presentmentMoney {
            amount
            currencyCode
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "name": "#SAMPLE",
          "displayFinancialStatus": "AUTHORIZED",
          "transactions": [
            {
              "id": "gid://shopify/OrderTransaction/1234567890",
              "kind": "AUTHORIZATION",
              "totalUnsettledSet": {
                "presentmentMoney": {
                  "amount": "12.34",
                  "currencyCode": "USD"
                }
              }
            }
          ]
        }
      }
    }
  {% endcapture %}

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

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

{% if order.displayFinancialStatus == "AUTHORIZED" or order.displayFinancialStatus == "PARTIALLY_PAID" %}
  {% 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

Days to wait before capturing
7