Skip to content

Commit 69bcecf

Browse files
committed
Removed tax calculation use case for webhooks in preparation for a new extension point for tax processing
1 parent a3dba64 commit 69bcecf

File tree

1 file changed

+0
-243
lines changed

1 file changed

+0
-243
lines changed

src/pages/webhooks/use-cases.md

Lines changed: 0 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -994,249 +994,6 @@ response.body = JSON.stringify({
994994
})
995995
```
996996

997-
## Overwrite tax calculation
998-
999-
When a shopper goes to the checkout, a third-party system calculates taxes based on quote details and overwrites the default tax values.
1000-
1001-
**webhook.xml configuration:**
1002-
1003-
```xml
1004-
<method name="plugin.magento.tax.api.tax_calculation.calculate_tax" type="after">
1005-
<hooks>
1006-
<batch name="order_updates">
1007-
<hook name="update_order" url="{env:APP_BUILDER_URL}/calculate-taxes" method="POST" timeout="5000" softTimeout="1000" priority="300" required="false" fallbackErrorMessage="The taxes can not be calculated">
1008-
<headers>
1009-
<header name="x-gw-ims-org-id">{env:APP_BUILDER_IMS_ORG_ID}</header>
1010-
<header name="Authorization">Bearer {env:APP_BUILDER_AUTH_TOKEN}</header>
1011-
</headers>
1012-
<fields>
1013-
<field name="quoteDetails" />
1014-
</fields>
1015-
</hook>
1016-
</batch>
1017-
</hooks>
1018-
</method>
1019-
```
1020-
1021-
The third-party endpoint receives the following payload, which is based on the configured list of fields:
1022-
1023-
```json
1024-
{
1025-
"quoteDetails": {
1026-
"billing_address": {
1027-
"region": {
1028-
"region_code": null,
1029-
"region": null,
1030-
"region_id": 57
1031-
},
1032-
"country_id": "US",
1033-
"street": [
1034-
"123 Domain Dr"
1035-
],
1036-
"postcode": "78768",
1037-
"city": "Austin"
1038-
},
1039-
"shipping_address": {
1040-
...
1041-
},
1042-
"customer_tax_class_key": {
1043-
"type": "id",
1044-
"value": "3"
1045-
},
1046-
"items": [
1047-
{
1048-
"code": "sequence-1",
1049-
"quantity": 1,
1050-
"tax_class_key": {
1051-
"type": "id",
1052-
"value": "2"
1053-
},
1054-
"is_tax_included": false,
1055-
"type": "product",
1056-
"extension_attributes": {
1057-
"price_for_tax_calculation": 200
1058-
},
1059-
"unit_price": 200,
1060-
"discount_amount": 0,
1061-
"parent_code": null
1062-
},
1063-
{
1064-
"code": "sequence-2",
1065-
"quantity": 1,
1066-
"tax_class_key": {
1067-
"type": "id",
1068-
"value": "2"
1069-
},
1070-
"is_tax_included": false,
1071-
"type": "product",
1072-
"extension_attributes": {
1073-
"price_for_tax_calculation": 100
1074-
},
1075-
"unit_price": 100,
1076-
"discount_amount": 0,
1077-
"parent_code": null
1078-
},
1079-
{
1080-
"type": "shipping",
1081-
"code": "shipping",
1082-
"quantity": 1,
1083-
"unit_price": 10,
1084-
"tax_class_key": {
1085-
"type": "id",
1086-
"value": 0
1087-
},
1088-
"is_tax_included": false,
1089-
"extension_attributes": []
1090-
}
1091-
],
1092-
"customer_id": null
1093-
}
1094-
}
1095-
```
1096-
1097-
Based on the input arguments and third-party endpoint logic, the tax percentage should be `19`. The response operations list can look like:
1098-
1099-
```json
1100-
[
1101-
{
1102-
"op": "replace",
1103-
"path": "result/items/sequence-1",
1104-
"value": {
1105-
"row_tax": 38,
1106-
"price_incl_tax": 238,
1107-
"row_total_incl_tax": 238,
1108-
"tax_percent": 19
1109-
}
1110-
},
1111-
{
1112-
"op": "replace",
1113-
"path": "result/items/sequence-2",
1114-
"value": {
1115-
"row_tax": 19,
1116-
"price_incl_tax": 119,
1117-
"row_total_incl_tax": 119,
1118-
"tax_percent": 19
1119-
}
1120-
},
1121-
{
1122-
"op": "replace",
1123-
"path": "result/items/shipping",
1124-
"value": {
1125-
"row_tax": 1.9,
1126-
"price_incl_tax": 11.9,
1127-
"row_total_incl_tax": 11.9,
1128-
"tax_percent": 19
1129-
}
1130-
},
1131-
{
1132-
"op": "replace",
1133-
"path": "result/tax_amount",
1134-
"value": 58.9
1135-
},
1136-
{
1137-
"op": "replace",
1138-
"path": "result/applied_taxes/amount",
1139-
"value": 58.9
1140-
},
1141-
{
1142-
"op": "replace",
1143-
"path": "result/applied_taxes/percent",
1144-
"value": 19
1145-
}
1146-
]
1147-
```
1148-
1149-
The result of `plugin.magento.tax.api.tax_calculation.calculate_tax` will be modified based on the provided operations.
1150-
1151-
**Endpoint code example for the AppBuilder action:**
1152-
1153-
```js
1154-
const { Core } = require('@adobe/aio-sdk')
1155-
const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../utils')
1156-
1157-
// main function that will be executed by Adobe I/O Runtime
1158-
async function main (params) {
1159-
// create a Logger
1160-
const logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })
1161-
1162-
try {
1163-
// 'info' is the default level if not set
1164-
logger.info('Calling the main action')
1165-
1166-
// log parameters, only if params.LOG_LEVEL === 'debug'
1167-
logger.debug(stringParameters(params))
1168-
1169-
//check for missing request input parameters and headers
1170-
const requiredParams = [/* add required params */]
1171-
const requiredHeaders = ['Authorization']
1172-
const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)
1173-
if (errorMessage) {
1174-
// return and log client errors
1175-
return errorResponse(400, errorMessage, logger)
1176-
}
1177-
1178-
const quoteDetails = params.quoteDetails;
1179-
let total = 0;
1180-
let operations = [];
1181-
1182-
// Skip the tax calculation if the billing address does not contain a postcode.
1183-
if (!quoteDetails.billing_address.postcode) {
1184-
return {
1185-
statusCode: 200,
1186-
body: JSON.stringify({
1187-
op: "success"
1188-
})
1189-
}
1190-
}
1191-
1192-
// Just for demo purposes, the taxPercent is equal to: last number in zip code + 10.
1193-
const taxPercent = 10 + parseInt(quoteDetails.billing_address.postcode.slice(-1));
1194-
1195-
quoteDetails.items.forEach((item) => {
1196-
total += item.quantity * item.unit_price;
1197-
const itemTax = item.unit_price * taxPercent / 100;
1198-
operations.push({
1199-
op: 'replace',
1200-
path: 'result/items/' + item.code,
1201-
value: {
1202-
'row_tax': itemTax * item.quantity,
1203-
'price_incl_tax': item.unit_price + itemTax,
1204-
'row_total_incl_tax': item.unit_price + itemTax * item.quantity,
1205-
'tax_percent': taxPercent,
1206-
}
1207-
})
1208-
})
1209-
operations.push({
1210-
op: 'replace',
1211-
path: 'result/tax_amount',
1212-
value: total * taxPercent / 100
1213-
});
1214-
operations.push({
1215-
op: 'replace',
1216-
path: 'result/applied_taxes/amount',
1217-
value: total * taxPercent / 100
1218-
});
1219-
operations.push({
1220-
op: 'replace',
1221-
path: 'result/applied_taxes/percent',
1222-
value: taxPercent
1223-
});
1224-
1225-
return {
1226-
statusCode: 200,
1227-
body: JSON.stringify(operations)
1228-
}
1229-
} catch (error) {
1230-
// log any server errors
1231-
logger.error(error)
1232-
// return with 500
1233-
return errorResponse(500, 'server error', logger)
1234-
}
1235-
}
1236-
1237-
exports.main = main
1238-
```
1239-
1240997
## Get shipping quote
1241998

1242999
When a shopper is checking out and edits the shipping address, a third-party system is used to calculate and modify the shipping quote.

0 commit comments

Comments
 (0)