Partially auto-capture payments as orders are fulfilled, with Mechanic.

Mechanic is a development platform for Shopify. :)

Partially auto-capture payments as orders are fulfilled

by Isaac Bowen (team@usemechanic.com)

Running when an order is updated, this task captures payment in proportion to the order value that has been fulfilled. For example, for an order with a subtotal of $10 and a total of $15 with shipping/taxes/discounts, this task will capture $7.50 when $5 of the order's value has been fulfilled.

Runs when an order is updated.

15-day free trial – unlimited tasks

Documentation

Running when an order is updated, this task captures payment in proportion to the order value that has been fulfilled. For example, for an order with a subtotal of $10 and a total of $15 with shipping/taxes/discounts, this task will capture $7.50 when $5 of the order's value has been fulfilled.

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 an order is updated (shopify/orders/updated)
Script
{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["financial_status"] = "partially_paid" %}

  {% capture preview_result_json %}
    {
      "data": {
        "order": {
          "transactions": [
            {
              "id": "gid://shopify/OrderTransaction/1234567890",
              "amountSet": {
                "shopMoney": {
                  "amount": "16.99",
                  "currencyCode": "USD"
                }
              }
            }
          ],
          "totalPriceSet": {
            "shopMoney": {
              "amount": "16.99"
            }
          },
          "subtotalPriceSet": {
            "shopMoney": {
              "amount": "10.0"
            }
          },
          "totalCapturableSet": {
            "shopMoney": {
              "amount": "16.54"
            }
          },
          "fulfillments": [
            {
              "name": "#1234-F1",
              "fulfillmentLineItems": {
                "pageInfo": {
                  "hasNextPage": false
                },
                "edges": [
                  {
                    "node": {
                      "discountedTotalSet": {
                        "shopMoney": {
                          "amount": "5.0"
                        }
                      }
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  {% endcapture %}
{% endif %}

{% if order.financial_status != "authorized" and order.financial_status != "partially_paid" %}
  {% log "Order is not either authorized or partially_paid." %}
{% else %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        transactions(capturable: true) {
          id
          amountSet {
            shopMoney {
              amount
              currencyCode
            }
          }
        }
        totalPriceSet {
          shopMoney {
            amount
          }
        }
        subtotalPriceSet {
          shopMoney {
            amount
          }
        }
        totalCapturableSet {
          shopMoney {
            amount
          }
        }
        fulfillments {
          name
          fulfillmentLineItems(first: 250) {
            pageInfo {
              hasNextPage
            }
            edges {
              node {
                discountedTotalSet {
                  shopMoney {
                    amount
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% assign result = preview_result_json | parse_json %}
  {% endif %}

  {% assign total = result.data.order.totalPriceSet.shopMoney.amount | times: 1.0 %}
  {% assign total_captured = total | minus: result.data.order.totalCapturableSet.shopMoney.amount %}
  {% assign subtotal = result.data.order.subtotalPriceSet.shopMoney.amount | times: 1.0 %}
  {% assign subtotal_fulfilled = 0.0 %}

  {% for fulfillment in result.data.order.fulfillments %}
    {% if fulfillment.fulfillmentLineItems.pageInfo.hasNextPage %}
      {% error "This order has a fulfillment with more than 250 line items in it. This is not supported by this task." %}
      {% break %}
    {% endif %}

    {% for fulfillment_line_item_edge in fulfillment.fulfillmentLineItems.edges %}
      {% assign subtotal_fulfilled = subtotal_fulfilled | plus: fulfillment_line_item_edge.node.discountedTotalSet.shopMoney.amount %}
    {% endfor %}
  {% endfor %}

  {% assign desired_total_captured = subtotal_fulfilled | divided_by: subtotal | times: total %}
  {% assign amount_to_capture = desired_total_captured | minus: total_captured | times: 100 | round | divided_by: 100.0 %}
  {% assign parent_transaction = result.data.order.transactions.first %}

  {% log 
    total: total,
    total_captured: total_captured,
    subtotal: subtotal,
    subtotal_fulfilled: subtotal_fulfilled,
    desired_total_captured: desired_total_captured,
    amount_to_capture: amount_to_capture,
    parent_transaction: parent_transaction
  %}

  {% if result.data.order.capturable == false %}
    {% log "This order is not capturable." %}
  {% elsif amount_to_capture == 0 %}
    {% log "Nothing to capture at this time." %}
  {% else %}
    {% action "shopify" %}
      mutation {
        orderCapture(
          input: {
            id: {{ order.admin_graphql_api_id | json }}
            parentTransactionId: {{ parent_transaction.id | json }}
            amount: {{ amount_to_capture | append: "" | json }}
            currency: {{ parent_transaction.amountSet.shopMoney.currencyCode }}
          }
        ) {
          transaction {
            id
            status
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}

  {% log result %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.