|
| 1 | +{% comment %} |
| 2 | + -- get GraphQL order and transaction data |
| 3 | +{% endcomment %} |
| 4 | + |
1 | 5 | {% capture query %}
|
2 | 6 | query {
|
3 | 7 | order(id: {{ order.admin_graphql_api_id | json }}) {
|
4 | 8 | id
|
5 | 9 | name
|
6 | 10 | displayFinancialStatus
|
| 11 | + currentTotalPriceSet { |
| 12 | + presentmentMoney { |
| 13 | + amount |
| 14 | + } |
| 15 | + } |
| 16 | + totalReceivedSet { |
| 17 | + presentmentMoney { |
| 18 | + amount |
| 19 | + currencyCode |
| 20 | + } |
| 21 | + } |
7 | 22 | transactions(capturable: true) {
|
8 | 23 | id
|
9 | 24 | kind
|
|
28 | 43 | "id": "gid://shopify/Order/1234567890",
|
29 | 44 | "name": "#SAMPLE",
|
30 | 45 | "displayFinancialStatus": "AUTHORIZED",
|
| 46 | + "currentTotalPriceSet": { |
| 47 | + "presentmentMoney": { |
| 48 | + "amount": "23.45" |
| 49 | + } |
| 50 | + }, |
31 | 51 | "transactions": [
|
32 | 52 | {
|
33 | 53 | "id": "gid://shopify/OrderTransaction/1234567890",
|
34 | 54 | "kind": "AUTHORIZATION",
|
35 | 55 | "totalUnsettledSet": {
|
36 | 56 | "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", |
38 | 68 | "currencyCode": "USD"
|
39 | 69 | }
|
40 | 70 | }
|
|
50 | 80 |
|
51 | 81 | {% assign order = result.data.order %}
|
52 | 82 |
|
| 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 | + |
53 | 100 | {% if order.displayFinancialStatus == "AUTHORIZED" or order.displayFinancialStatus == "PARTIALLY_PAID" %}
|
54 | 101 | {% assign authorized_transactions = order.transactions | where: "kind", "AUTHORIZATION" %}
|
55 | 102 |
|
| 103 | + {% comment %} |
| 104 | + -- multiple authorizations could be on the order due to upsells or item additions |
| 105 | + {% endcomment %} |
| 106 | + |
56 | 107 | {% 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 | + |
57 | 112 | {% 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 %} |
58 | 129 |
|
59 |
| - {% if unsettled_amount > 0 %} |
60 | 130 | {% action "shopify" %}
|
61 | 131 | mutation {
|
62 | 132 | orderCapture(
|
63 | 133 | input: {
|
64 | 134 | id: {{ order.id | json }}
|
65 | 135 | parentTransactionId: {{ transaction.id | json }}
|
66 |
| - amount: {{ unsettled_amount | json }} |
| 136 | + amount: {{ amount_to_capture | json }} |
67 | 137 | currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}
|
68 | 138 | }
|
69 | 139 | ) {
|
|
0 commit comments