Capture all authorized payments with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Capture all authorized payments

by Isaac Bowen (team@usemechanic.com)

This task will scan for all orders that have a financial status of "authorized", and will capture payment for them. This task can be scheduled to run daily, and can be run on demand.

Runs when a user triggers the task. Configuration includes include partially paid orders, run daily, and hours to wait after midnight when running daily.

15-day free trial – unlimited tasks

Documentation

This task will scan for all orders that have a financial status of "authorized", and will capture payment for them. Enable "Run daily" to perform this every day at midnight, or use the "Run task" button to perform this scan on demand.

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 a user triggers the task (mechanic/user/trigger)
Options
include partially paid orders (boolean), run daily (boolean), hours to wait after midnight when running daily (number)
Script
{% if event.preview %}
  {% action "shopify" %}
    mutation {
      orderCapture(
        input: {
          id: "gid://shopify/Order/1234567890"
          parentTransactionId: "gid://shopify/OrderTransaction/1234567890"
          amount: "10.00"
          currency: {{ shop.currency }}
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% else %}
  {% assign cursor = nil %}
  {% assign conditions = array %}
  {% assign conditions[0] = "financial_status:authorized" %}

  {% if options.include_partially_paid_orders__boolean %}
    {% assign conditions[1] = "financial_status:partially_paid" %}
  {% endif %}

  {% for n in (0..100) %}
    {% capture query %}
      query {
        orders(
          first: 200
          after: {{ cursor | json }}
          query: {{ conditions | join: " OR " | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              transactions(
                capturable: true
              ) {
                id
                kind
              }
              totalCapturableSet {
                presentmentMoney {
                  amount
                  currencyCode
                }
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% for edge in result.data.orders.edges %}
      {% assign transaction = edge.node.transactions | where: "kind", "AUTHORIZATION" | first %}

      {% action "shopify" %}
        mutation {
          orderCapture(
            input: {
              id: {{ edge.node.id | json }}
              parentTransactionId: {{ transaction.id | json }}
              amount: {{ edge.node.totalCapturableSet.presentmentMoney.amount | json }}
              currency: {{ edge.node.totalCapturableSet.presentmentMoney.currencyCode }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}

    {% if result.data.pageInfo.hasNextPage %}
      {% assign cursor = result.data.orders.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.