Skip to content

Commit

Permalink
Merge pull request #25 from commercetools/stripe-in-progress
Browse files Browse the repository at this point in the history
adjust code in the mapper to pass cart details to calculate tax
  • Loading branch information
praveenkumarct authored Dec 15, 2023
2 parents a8b8658 + f8f64ff commit 7922a60
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 149 deletions.
163 changes: 18 additions & 145 deletions tax-calculator/resources/cartRequest.json
Original file line number Diff line number Diff line change
@@ -1,160 +1,37 @@
{
"type": "Cart",
"id": "5dacd609-f79d-4db5-8501-af12f28b8eda",
"version": 25,
"customerId": "e2ef189a-9aa4-47a5-b47b-e19a1e9d4d3b",
"country": "DE",
"currency": "EUR",
"key": "stripe-tax-calculator-cart-1",
"locale": "en-US",
"lineItems": [
{
"id": "5e2c8458-74b3-47cc-a9eb-a9e88671f713",
"productId": "e4a679f3-e8f2-4b3d-bc51-2a48ae411794",
"key": "stripe-tax-calculator-lineItem-1",
"name": {
"en-US": "SwiftTech RapidType Keyboard and Mouse Kit ",
"sv-SE": "SwiftTech RapidType Keyboard and Mouse Kit "
"en-US": "SwiftTech RapidType Keyboard and Mouse Kit "
},
"productType": {
"typeId": "product-type",
"id": "ca1f3a54-e881-4878-8a92-59fb7b544a67",
"version": 16
},
"variant": {
"id": 1,
"sku": "SWIFT-RAPIDTYPE-KIT",
"prices": [
{
"id": "9fc634c5-88e8-4ede-b0b5-042474384f2c",
"value": {
"type": "centPrecision",
"currencyCode": "USD",
"centAmount": 6900,
"fractionDigits": 2
}
},
{
"id": "e4bf7cd2-3ad6-4103-b219-c381ec43b64a",
"value": {
"type": "centPrecision",
"currencyCode": "SEK",
"centAmount": 69900,
"fractionDigits": 2
}
}
],
"images": [],
"attributes": [],
"assets": [],
"availability": {
"isOnStock": true,
"availableQuantity": 300,
"version": 1,
"id": "992d6284-6f82-486f-ac4e-e4a689fc5142"
}
},
"price": {
"id": "9fc634c5-88e8-4ede-b0b5-042474384f2c",
"value": {
"type": "centPrecision",
"currencyCode": "USD",
"centAmount": 6900,
"fractionDigits": 2
}
},
"quantity": 5,
"discountedPricePerQuantity": [],
"perMethodTaxRate": [],
"priceMode": "Platform",
"lineItemMode": "Standard",
"productId": "06819bff-c8d1-4a2b-93f6-47c3ac6d4202",
"variantId": 1,
"quantity": 1,
"totalPrice": {
"type": "centPrecision",
"currencyCode": "USD",
"currencyCode": "EUR",
"centAmount": 34500,
"fractionDigits": 2
},
"taxedPricePortions": []
},
{
"id": "87838704-5aec-41ed-9147-7b9a20f45f18",
"productId": "3fe060ce-5aec-409b-81d1-12e5d8c961b3",
"name": {
"en-US": "Lenovo Ideapad 3 (2022) 15.6\" Laptop",
"sv-SE": "Lenovo Ideapad 3 (2022) 15.6\" Laptop"
},
"productType": {
"typeId": "product-type",
"id": "ca1f3a54-e881-4878-8a92-59fb7b544a67",
"version": 16
},
"variant": {
"id": 1,
"sku": "LENOVO-IDEAPAD3",
"prices": [
{
"id": "21c3cb15-b850-4ecf-8e71-5abd6689160a",
"value": {
"type": "centPrecision",
"currencyCode": "USD",
"centAmount": 39500,
"fractionDigits": 2
}
},
{
"id": "40fc5d92-f9a7-434b-9da1-29f37b6f6233",
"value": {
"type": "centPrecision",
"currencyCode": "SEK",
"centAmount": 349500,
"fractionDigits": 2
}
}
],
"images": [],
"attributes": [],
"assets": [],
"availability": {
"isOnStock": true,
"availableQuantity": 300,
"version": 1,
"id": "5114f296-b3a7-4171-b027-497740ebb253"
}
},
"price": {
"id": "21c3cb15-b850-4ecf-8e71-5abd6689160a",
"value": {
"type": "centPrecision",
"currencyCode": "USD",
"centAmount": 39500,
"fractionDigits": 2
}
},
"quantity": 2,
"discountedPricePerQuantity": [],
"perMethodTaxRate": [],
"priceMode": "Platform",
"lineItemMode": "Standard",
"totalPrice": {
"type": "centPrecision",
"currencyCode": "USD",
"centAmount": 79000,
"fractionDigits": 2
},
"taxedPricePortions": []
}
}
],
"cartState": "Active",
"totalPrice": {
"type": "centPrecision",
"currencyCode": "USD",
"currencyCode": "EUR",
"centAmount": 113500,
"fractionDigits": 2
},
"country": "US",
"shippingMode": "Single",
"shipping": [{
"postal_code": 12345,
"shippingAddress": {
"streetName": "testStreet"
}
}],
"shippingAddress": {
"country": "DE",
"streetName": "testStreet",
"postal_code": "12345"
},
"customLineItems": [],
"discountCodes": [],
"directDiscounts": [],
Expand All @@ -163,9 +40,5 @@
"taxRoundingMode": "HalfEven",
"taxCalculationMode": "LineItemLevel",
"deleteDaysAfterLastModification": 90,
"refusedGifts": [],
"origin": "Customer",
"itemShippingAddresses": [],
"totalLineItemQuantity": 7
}

"origin": "Customer"
}
13 changes: 9 additions & 4 deletions tax-calculator/src/controllers/tax.calculator.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ export const taxHandler = async (request, response) => {
function mapCartRequestToTaxRequest(cartRequest) {
let taxRequest = {customer_details: {address: {}}, line_items: []};

taxRequest.currency = cartRequest.totalPrice.currencyCode;
taxRequest.currency = cartRequest.totalPrice?.currencyCode;
taxRequest.customer_details.address.country = cartRequest.country;

const cartShippingAddress = cartRequest.shipping[0];
taxRequest.customer_details.address.postal_code = cartShippingAddress?.postal_code ? cartShippingAddress.postal_code : 12345;
taxRequest.customer_details.address.line1 = cartShippingAddress?.shippingAddress.streetName;
let cartShippingAddress = {};
if(cartRequest.shippingMode === 'Single'){
cartShippingAddress = cartRequest.shippingAddress;
} else {
cartShippingAddress = cartRequest.shipping[0]?.shippingAddress;
}
taxRequest.customer_details.address.postal_code = cartShippingAddress.postal_code;
taxRequest.customer_details.address.line1 = cartShippingAddress.streetName;
taxRequest.customer_details.address_source = 'shipping';

for (const cartLineItem of cartRequest.lineItems) {
Expand Down

0 comments on commit 7922a60

Please sign in to comment.