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 taskWhen it runs
Occurs whenever an order is created, with a 7 day delay
What you choose
Days to wait before capturing
Documentation
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.)
If the order is modified before capturing, due to applying discounts, changing shipping fees, and/or making item adjustments, then this task will only capture up to a maximum of the current order total.
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
- Days to wait before capturing (number, required)
Task code
{% comment %}
-- get GraphQL order and transaction data
{% endcomment %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
name
displayFinancialStatus
currencyCode
currentTotalPriceSet {
presentmentMoney {
amount
}
}
transactions {
id
kind
status
parentTransaction {
id
kind
status
}
amountSet {
presentmentMoney {
amount
currencyCode
}
}
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",
"currencyCode": "USD",
"currentTotalPriceSet": {
"presentmentMoney": {
"amount": "23.45"
}
},
"transactions": [
{
"id": "gid://shopify/OrderTransaction/1234567890",
"kind": "AUTHORIZATION",
"status": "SUCCESS",
"totalUnsettledSet": {
"presentmentMoney": {
"amount": "20.00",
"currencyCode": "USD"
}
}
},
{
"id": "gid://shopify/OrderTransaction/2345678901",
"kind": "AUTHORIZATION",
"status": "SUCCESS",
"totalUnsettledSet": {
"presentmentMoney": {
"amount": "10.00",
"currencyCode": "USD"
}
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% unless event.preview %}
{% log order_data: order %}
{% endunless %}
{% unless order.displayFinancialStatus == "AUTHORIZED" or order.displayFinancialStatus == "PARTIALLY_PAID" %}
{% log
message: "Order financial status is not AUTHORIZED or PARTIALLY_PAID on this order",
order: order
%}
{% break %}
{% endunless %}
{% comment %}
-- the current total price will reflect changes made after the initial sale: discounts, shipping changes, item adjustments
{% endcomment %}
{% assign current_total_price = order.currentTotalPriceSet.presentmentMoney.amount | times: 1.0 %}
{% comment %}
-- loop through transactions to see what the actual balance received is (SALE + CAPTURED - REFUND)
{% endcomment %}
{% assign net_received = 0.0 %}
{% for transaction in order.transactions %}
{% unless transaction.status == "SUCCESS" %}
{% continue %}
{% endunless %}
{% if transaction.kind == "SALE" or transaction.kind == "CAPTURE" %}
{% assign net_received = net_received | plus: transaction.amountSet.presentmentMoney.amount %}
{% elsif transaction.kind == "REFUND" %}
{% assign net_received = net_received | minus: transaction.amountSet.presentmentMoney.amount %}
{% endif %}
{% endfor %}
{% assign left_to_capture = current_total_price | minus: net_received | at_least: 0.0 %}
{% unless event.preview %}
{% log
current_total_price: current_total_price,
net_received: net_received,
left_to_capture: left_to_capture
%}
{% endunless %}
{% comment %}
-- multiple authorizations could be on the order due to upsells or item additions
-- capture authorizations up to the amount left to capture
{% endcomment %}
{% assign authorized_transactions = order.transactions | where: "kind", "AUTHORIZATION" %}
{% for transaction in authorized_transactions %}
{% unless transaction.status == "SUCCESS" %}
{% continue %}
{% endunless %}
{% comment %}
-- capture the unsettled amount of this transaction without exceeding the amount left to capture
{% endcomment %}
{% assign unsettled_amount = transaction.totalUnsettledSet.presentmentMoney.amount | times: 1.0 %}
{% assign amount_to_capture = unsettled_amount | at_most: left_to_capture %}
{% unless event.preview %}
{% log
transaction_id: transaction.id,
unsettled_amount: unsettled_amount,
amount_to_capture: amount_to_capture
%}
{% endunless %}
{% if amount_to_capture > 0 %}
{% comment %}
-- reduce the amount left to capture by the amount captured for this transaction
{% endcomment %}
{% 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 %}
Mechanic Liquid reference
Default option values
- Days to wait before capturing
7