Auto-capture order payment based on shipping method, with Mechanic.

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

Auto-capture order payment based on shipping method

This task auto-captures payment if the customer has selected a certain shipping method. Useful for expedited order fulfillments.

Runs Occurs whenever an order is created. Configuration includes allowed shipping methods.

15-day free trial – unlimited tasks

Documentation

This task auto-captures payment if the customer has selected a certain shipping method. Useful for expedited order fulfillments.

To use this task, populate the "Allowed shipping methods" list with shipping method titles (e.g. "Economy"). In Shopify, shipping method titles are configured as "rates"; you can find yours by navigating to Settings > Shipping and delivery, and then looking at the rates within each shipping zone.

For expected results, be sure to enable "Manually capture payment for orders" in Shopify, using this guide.

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
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
allowed shipping methods (required, array)
Code
{% unless options.allowed_shipping_methods__required_array contains order.shipping_lines.first.title or event.preview %}
  {% log message: "This order is not using a configured shipping method; skipping." %}
  {% break %}
{% endunless %}

{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more