Mechanic Get Mechanic
Task library

Capture order payment upon fulfillment

Upon the fulfillment of an authorized or partially paid order, this task attempts to capture all open authorized payments for that order. (Multiple authorizations can exist on edited orders or with post-purchase upsells.)

Try this task

When it runs

Occurs whenever an order is fulfilled

Documentation

Upon the fulfillment of an authorized or partially paid order, this task attempts to capture all open authorized payments for that order. (Multiple authorizations can exist on edited orders or with post-purchase upsells.)

If any items are removed from the order before fulfillment, then this task will reduce the amount captured by the subtotal of the removed line items (including their discounts and taxes, as calculated by Shopify). It will not reduce any shipping costs on the order.

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

Please note: You are responsible for ensuring that fulfillment occurs within the order payment's authorization period.

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/fulfilled
How task subscriptions work

Task code

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      name
      displayFinancialStatus
      currentTotalPriceSet {
        presentmentMoney {
          amount
        }
      }
      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",
          "currentTotalPriceSet": {
            "presentmentMoney": {
              "amount": "23.45"
            }
          },
          "transactions": [
            {
              "id": "gid://shopify/OrderTransaction/1234567890",
              "kind": "AUTHORIZATION",
              "totalUnsettledSet": {
                "presentmentMoney": {
                  "amount": "20.00",
                  "currencyCode": "USD"
                }
              }
            },
            {
              "id": "gid://shopify/OrderTransaction/2345678901",
              "kind": "AUTHORIZATION",
              "totalUnsettledSet": {
                "presentmentMoney": {
                  "amount": "10.00",
                  "currencyCode": "USD"
                }
              }
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% assign order = result.data.order %}
{% assign left_to_capture = order.currentTotalPriceSet.presentmentMoney.amount | times: 1.0 %}

{% 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 %}
    {% assign amount_to_capture = unsettled_amount | at_most: left_to_capture %}

    {% if amount_to_capture > 0 %}
      {% assign left_to_capture = left_to_capture | minus: amount_to_capture %}
      
      {% action "shopify" %}
        mutation {
          orderCapture(
            input: {
              id: {{ order.id | json }}
              parentTransactionId: {{ transaction.id | json }}
              amount: {{ amount_to_capture | json }}
              currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}
            }
          ) {
            transaction {
              id
              status
              parentTransaction {
                id
                kind
              }
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference