Mechanic is a development and ecommerce automation platform for Shopify. :)
This task will scan for all orders that have a financial status of "authorized" or optionally "partially paid", and will attempt to capture all open authorized payments for them. This task can be scheduled to run daily, and can be run on demand.
Runs Occurs when a user manually triggers the task. Configuration includes include partially paid orders, run daily, and hours to wait after midnight when running daily.
This task will scan for all orders that have a financial status of "authorized" or optionally "partially paid", and will attempt to capture all open authorized payments for them. This task can be scheduled to run daily, and can be run on demand.
If an order is modified after creation but 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. Refunds that are not associated with an item adjustment are not supported by this task.
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?)
{% if options.run_daily__boolean %} mechanic/scheduler/daily+{{ options.hours_to_wait_after_midnight_when_running_daily__range_min0_max23_required }}.hours {% endif %} mechanic/user/trigger
{% assign include_partially_paid_orders = options.include_partially_paid_orders__boolean %} {% comment %} -- get GraphQL order and transaction data for authorized, uncancelled orders (and optionally partially paid orders) {% endcomment %} {% assign search_query = "-status:cancelled financial_status:authorized" %} {% if include_partially_paid_orders %} {% assign search_query = "-status:cancelled AND (financial_status:authorized OR financial_status:partially_paid)" %} {% endif %} {% assign cursor = nil %} {% assign orders = array %} {% for n in (1..100) %} {% capture query %} query { orders( first: 250 after: {{ cursor | json }} query: {{ search_query | json }} ) { pageInfo { hasNextPage endCursor } nodes { id name displayFinancialStatus currentTotalPriceSet { presentmentMoney { amount } } totalReceivedSet { presentmentMoney { amount currencyCode } } transactions(capturable: true) { id kind totalUnsettledSet { presentmentMoney { amount currencyCode } } } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "orders": { "nodes": [ { "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 orders = orders | concat: result.data.orders.nodes %} {% if result.data.orders.pageInfo.hasNextPage %} {% assign cursor = result.data.orders.pageInfo.endCursor %} {% else %} {% break %} {% endif %} {% endfor %} {% assign order_names = orders | map: "name" %} {% unless event.preview %} {% log count_orders_to_review: orders.size, order_names: order_names %} {% endunless %} {% for order in orders %} {% comment %} -- the current total price will reflect changes made after the initial sale: discounts, shipping changes, item adjustments -- reducing by total received (i.e. partially paid orders) is for payment gateways that support multiple captures (e.g. Shopify Payments) {% endcomment %} {% assign current_total_price = order.currentTotalPriceSet.presentmentMoney.amount | times: 1.0 %} {% assign total_received = order.totalReceivedSet.presentmentMoney.amount | times: 1.0 %} {% assign left_to_capture = current_total_price | minus: total_received %} {% assign authorized_transactions = order.transactions | where: "kind", "AUTHORIZATION" %} {% unless event.preview %} {% log order_name: order.name, current_total_price: current_total_price, total_received: total_received, left_to_capture: left_to_capture, count_authorized_transactions: authorized_transactions.size %} {% endunless %} {% comment %} -- multiple authorizations could be on the order due to upsells or item additions {% endcomment %} {% for transaction in authorized_transactions %} {% 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 order_name: order.name, 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 %} {% endfor %}
0