Skip to content

Commit 1dc75b0

Browse files
authored
update capture tasks for enhanced order capabilities (#486)
1 parent cba9236 commit 1dc75b0

File tree

9 files changed

+312
-81
lines changed

9 files changed

+312
-81
lines changed

docs/auto-capture-order-payment-after-x-days/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ shopify/orders/create+{{ options.days_to_wait_before_capturing__number_required
3030

3131
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.)
3232

33+
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. Refunds that are not associated with an item adjustment are not supported by this task.
34+
3335
## Installing this task
3436

3537
Find this task [in the library at tasks.mechanic.dev](https://tasks.mechanic.dev/auto-capture-order-payment-after-x-days), and use the "Try this task" button. Or, import [this task's JSON export](../../tasks/auto-capture-order-payment-after-x-days.json) – see [Importing and exporting tasks](https://learn.mechanic.dev/core/tasks/import-and-export) to learn how imports work.

docs/auto-capture-order-payment-after-x-days/script.liquid

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
{% comment %}
2+
-- get GraphQL order and transaction data
3+
{% endcomment %}
4+
15
{% capture query %}
26
query {
37
order(id: {{ order.admin_graphql_api_id | json }}) {
48
id
59
name
610
displayFinancialStatus
11+
currentTotalPriceSet {
12+
presentmentMoney {
13+
amount
14+
}
15+
}
16+
totalReceivedSet {
17+
presentmentMoney {
18+
amount
19+
currencyCode
20+
}
21+
}
722
transactions(capturable: true) {
823
id
924
kind
@@ -28,13 +43,28 @@
2843
"id": "gid://shopify/Order/1234567890",
2944
"name": "#SAMPLE",
3045
"displayFinancialStatus": "AUTHORIZED",
46+
"currentTotalPriceSet": {
47+
"presentmentMoney": {
48+
"amount": "23.45"
49+
}
50+
},
3151
"transactions": [
3252
{
3353
"id": "gid://shopify/OrderTransaction/1234567890",
3454
"kind": "AUTHORIZATION",
3555
"totalUnsettledSet": {
3656
"presentmentMoney": {
37-
"amount": "12.34",
57+
"amount": "20.00",
58+
"currencyCode": "USD"
59+
}
60+
}
61+
},
62+
{
63+
"id": "gid://shopify/OrderTransaction/2345678901",
64+
"kind": "AUTHORIZATION",
65+
"totalUnsettledSet": {
66+
"presentmentMoney": {
67+
"amount": "10.00",
3868
"currencyCode": "USD"
3969
}
4070
}
@@ -50,20 +80,60 @@
5080

5181
{% assign order = result.data.order %}
5282

83+
{% comment %}
84+
-- the current total price will reflect changes made after the initial sale: discounts, shipping changes, item adjustments
85+
-- reducing by total received (i.e. partially paid orders) is for payment gateways that support multiple captures (e.g. Shopify Payments)
86+
{% endcomment %}
87+
88+
{% assign current_total_price = order.currentTotalPriceSet.presentmentMoney.amount | times: 1.0 %}
89+
{% assign total_received = order.totalReceivedSet.presentmentMoney.amount | times: 1.0 %}
90+
{% assign left_to_capture = current_total_price | minus: total_received %}
91+
92+
{% unless event.preview %}
93+
{% log
94+
current_total_price: current_total_price,
95+
total_received: total_received,
96+
left_to_capture: left_to_capture
97+
%}
98+
{% endunless %}
99+
53100
{% if order.displayFinancialStatus == "AUTHORIZED" or order.displayFinancialStatus == "PARTIALLY_PAID" %}
54101
{% assign authorized_transactions = order.transactions | where: "kind", "AUTHORIZATION" %}
55102

103+
{% comment %}
104+
-- multiple authorizations could be on the order due to upsells or item additions
105+
{% endcomment %}
106+
56107
{% for transaction in authorized_transactions %}
108+
{% comment %}
109+
-- capture the unsettled amount of this transaction without exceeding the amount left to capture
110+
{% endcomment %}
111+
57112
{% assign unsettled_amount = transaction.totalUnsettledSet.presentmentMoney.amount | times: 1.0 %}
113+
{% assign amount_to_capture = unsettled_amount | at_most: left_to_capture %}
114+
115+
{% unless event.preview %}
116+
{% log
117+
transaction_id: transaction.id,
118+
unsettled_amount: unsettled_amount,
119+
amount_to_capture: amount_to_capture
120+
%}
121+
{% endunless %}
122+
123+
{% if amount_to_capture > 0 %}
124+
{% comment %}
125+
-- reduce the amount left to capture by the amount captured for this transaction
126+
{% endcomment %}
127+
128+
{% assign left_to_capture = left_to_capture | minus: amount_to_capture %}
58129

59-
{% if unsettled_amount > 0 %}
60130
{% action "shopify" %}
61131
mutation {
62132
orderCapture(
63133
input: {
64134
id: {{ order.id | json }}
65135
parentTransactionId: {{ transaction.id | json }}
66-
amount: {{ unsettled_amount | json }}
136+
amount: {{ amount_to_capture | json }}
67137
currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}
68138
}
69139
) {

docs/capture-all-authorized-payments/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Tags: Payment
44

5-
This task will scan for all orders that have a financial status of "authorized", and will capture payment for them. This task can be scheduled to run daily, and can be run on demand.
5+
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.
66

77
* View in the task library: [tasks.mechanic.dev/capture-all-authorized-payments](https://tasks.mechanic.dev/capture-all-authorized-payments)
88
* Task JSON, for direct import: [task.json](../../tasks/capture-all-authorized-payments.json)
@@ -14,7 +14,7 @@ This task will scan for all orders that have a financial status of "authorized",
1414
{
1515
"include_partially_paid_orders__boolean": false,
1616
"run_daily__boolean": false,
17-
"hours_to_wait_after_midnight_when_running_daily__number": null
17+
"hours_to_wait_after_midnight_when_running_daily__range_min0_max23_required": 0
1818
}
1919
```
2020

@@ -23,17 +23,19 @@ This task will scan for all orders that have a financial status of "authorized",
2323
## Subscriptions
2424

2525
```liquid
26-
{% if options.run_daily__boolean %}mechanic/scheduler/daily{% if options.hours_to_wait_after_midnight_when_running_daily__number %}+{{ options.hours_to_wait_after_midnight_when_running_daily__number | times: 60 | round }}.minutes{% endif %}{% endif %}
26+
{% if options.run_daily__boolean %}
27+
mechanic/scheduler/daily+{{ options.hours_to_wait_after_midnight_when_running_daily__range_min0_max23_required }}.hours
28+
{% endif %}
2729
mechanic/user/trigger
2830
```
2931

3032
[Learn about event subscriptions in Mechanic](https://learn.mechanic.dev/core/tasks/subscriptions)
3133

3234
## Documentation
3335

34-
This task will scan for all orders that have a financial status of "authorized", and will capture payment for them. This task can be scheduled to run daily, and can be run on demand.
36+
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.
3537

36-
This task will scan for all orders that have a financial status of "authorized", and will capture payment for them. Enable "Run daily" to perform this every day at midnight, or use the "Run task" button to perform this scan on demand.
38+
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.
3739

3840
## Installing this task
3941

0 commit comments

Comments
 (0)