Skip to content

Commit

Permalink
reduce capture amount when items are removed (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Sep 28, 2023
1 parent 8ebb8ad commit c34bf52
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/capture-order-payment-upon-fulfillment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ shopify/orders/fulfilled

Upon the fulfillment of an authorized or partially paid order, this task attempts to capture all open authorized payments for that order. (Multiple authorizations can exist on edited orders or with post-purchase upsells.)

If any items are removed from the order before fulfillment, then this task will reduce the amount captured by the subtotal of the removed line items (including their discounts and taxes, as calculated by Shopify). It will **not** reduce any shipping costs on the order.

For expected results, be sure to enable "Manually capture payment for orders" in Shopify, [using this guide](https://help.shopify.com/en/manual/payments/payment-authorization#set-up-manual-capture-of-credit-card-payments).

__Please note__: You are responsible for ensuring that fulfillment occurs within the order payment's authorization period.
Expand Down
30 changes: 27 additions & 3 deletions docs/capture-order-payment-upon-fulfillment/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
id
name
displayFinancialStatus
currentTotalPriceSet {
presentmentMoney {
amount
}
}
transactions(capturable: true) {
id
kind
Expand All @@ -28,13 +33,28 @@
"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": "12.34",
"amount": "20.00",
"currencyCode": "USD"
}
}
},
{
"id": "gid://shopify/OrderTransaction/2345678901",
"kind": "AUTHORIZATION",
"totalUnsettledSet": {
"presentmentMoney": {
"amount": "10.00",
"currencyCode": "USD"
}
}
Expand All @@ -49,21 +69,25 @@
{% endif %}

{% assign order = result.data.order %}
{% assign left_to_capture = order.currentTotalPriceSet.presentmentMoney.amount | times: 1.0 %}

{% if order.displayFinancialStatus == "AUTHORIZED" or order.displayFinancialStatus == "PARTIALLY_PAID" %}
{% assign authorized_transactions = order.transactions | where: "kind", "AUTHORIZATION" %}

{% for transaction in authorized_transactions %}
{% assign unsettled_amount = transaction.totalUnsettledSet.presentmentMoney.amount | times: 1.0 %}
{% assign amount_to_capture = unsettled_amount | at_most: left_to_capture %}

{% if unsettled_amount > 0 %}
{% if amount_to_capture > 0 %}
{% 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: {{ unsettled_amount | json }}
amount: {{ amount_to_capture | json }}
currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}
}
) {
Expand Down
13 changes: 4 additions & 9 deletions tasks/capture-order-payment-upon-fulfillment.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
{
"docs": "Upon the fulfillment of an authorized or partially paid order, this task attempts to capture all open authorized payments for that order. (Multiple authorizations can exist on edited orders or with post-purchase upsells.)\n\nFor expected results, be sure to enable \"Manually capture payment for orders\" in Shopify, [using this guide](https://help.shopify.com/en/manual/payments/payment-authorization#set-up-manual-capture-of-credit-card-payments).\n\n__Please note__: You are responsible for ensuring that fulfillment occurs within the order payment's authorization period.",
"docs": "Upon the fulfillment of an authorized or partially paid order, this task attempts to capture all open authorized payments for that order. (Multiple authorizations can exist on edited orders or with post-purchase upsells.)\n\nIf any items are removed from the order before fulfillment, then this task will reduce the amount captured by the subtotal of the removed line items (including their discounts and taxes, as calculated by Shopify). It will **not** reduce any shipping costs on the order.\n\nFor expected results, be sure to enable \"Manually capture payment for orders\" in Shopify, [using this guide](https://help.shopify.com/en/manual/payments/payment-authorization#set-up-manual-capture-of-credit-card-payments).\n\n__Please note__: You are responsible for ensuring that fulfillment occurs within the order payment's authorization period.",
"halt_action_run_sequence_on_error": true,
"name": "Capture order payment upon fulfillment",
"online_store_javascript": null,
"options": {},
"order_status_javascript": null,
"perform_action_runs_in_sequence": true,
"script": "{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n displayFinancialStatus\n transactions(capturable: true) {\n id\n kind\n totalUnsettledSet {\n presentmentMoney {\n amount\n currencyCode\n }\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"name\": \"#SAMPLE\",\n \"displayFinancialStatus\": \"AUTHORIZED\",\n \"transactions\": [\n {\n \"id\": \"gid://shopify/OrderTransaction/1234567890\",\n \"kind\": \"AUTHORIZATION\",\n \"totalUnsettledSet\": {\n \"presentmentMoney\": {\n \"amount\": \"12.34\",\n \"currencyCode\": \"USD\"\n }\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign order = result.data.order %}\n\n{% if order.displayFinancialStatus == \"AUTHORIZED\" or order.displayFinancialStatus == \"PARTIALLY_PAID\" %}\n {% assign authorized_transactions = order.transactions | where: \"kind\", \"AUTHORIZATION\" %}\n\n {% for transaction in authorized_transactions %}\n {% assign unsettled_amount = transaction.totalUnsettledSet.presentmentMoney.amount | times: 1.0 %}\n\n {% if unsettled_amount > 0 %}\n {% action \"shopify\" %}\n mutation {\n orderCapture(\n input: {\n id: {{ order.id | json }}\n parentTransactionId: {{ transaction.id | json }}\n amount: {{ unsettled_amount | json }}\n currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}\n }\n ) {\n transaction {\n id\n status\n parentTransaction {\n id\n kind\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"shopify/orders/fulfilled"
],
"script": "{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n id\n name\n displayFinancialStatus\n currentTotalPriceSet {\n presentmentMoney {\n amount\n }\n }\n transactions(capturable: true) {\n id\n kind\n totalUnsettledSet {\n presentmentMoney {\n amount\n currencyCode\n }\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"id\": \"gid://shopify/Order/1234567890\",\n \"name\": \"#SAMPLE\",\n \"displayFinancialStatus\": \"AUTHORIZED\",\n \"currentTotalPriceSet\": {\n \"presentmentMoney\": {\n \"amount\": \"23.45\"\n }\n },\n \"transactions\": [\n {\n \"id\": \"gid://shopify/OrderTransaction/1234567890\",\n \"kind\": \"AUTHORIZATION\",\n \"totalUnsettledSet\": {\n \"presentmentMoney\": {\n \"amount\": \"20.00\",\n \"currencyCode\": \"USD\"\n }\n }\n },\n {\n \"id\": \"gid://shopify/OrderTransaction/2345678901\",\n \"kind\": \"AUTHORIZATION\",\n \"totalUnsettledSet\": {\n \"presentmentMoney\": {\n \"amount\": \"10.00\",\n \"currencyCode\": \"USD\"\n }\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign order = result.data.order %}\n{% assign left_to_capture = order.currentTotalPriceSet.presentmentMoney.amount | times: 1.0 %}\n\n{% if order.displayFinancialStatus == \"AUTHORIZED\" or order.displayFinancialStatus == \"PARTIALLY_PAID\" %}\n {% assign authorized_transactions = order.transactions | where: \"kind\", \"AUTHORIZATION\" %}\n\n {% for transaction in authorized_transactions %}\n {% assign unsettled_amount = transaction.totalUnsettledSet.presentmentMoney.amount | times: 1.0 %}\n {% assign amount_to_capture = unsettled_amount | at_most: left_to_capture %}\n\n {% if amount_to_capture > 0 %}\n {% assign left_to_capture = left_to_capture | minus: amount_to_capture %}\n \n {% action \"shopify\" %}\n mutation {\n orderCapture(\n input: {\n id: {{ order.id | json }}\n parentTransactionId: {{ transaction.id | json }}\n amount: {{ amount_to_capture | json }}\n currency: {{ transaction.totalUnsettledSet.presentmentMoney.currencyCode }}\n }\n ) {\n transaction {\n id\n status\n parentTransaction {\n id\n kind\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": ["shopify/orders/fulfilled"],
"subscriptions_template": "shopify/orders/fulfilled",
"tags": [
"Fulfillment",
"Payment"
]
"tags": ["Fulfillment", "Payment"]
}

0 comments on commit c34bf52

Please sign in to comment.