diff --git a/src/__tests__/fixtures/pricing.results.ts b/src/__tests__/fixtures/pricing.results.ts index f3f7c49..705c725 100644 --- a/src/__tests__/fixtures/pricing.results.ts +++ b/src/__tests__/fixtures/pricing.results.ts @@ -5702,7 +5702,7 @@ export const computedPriceWithFixedDiscount = { unit_discount_amount_net_decimal: '4.545454545455', unit_amount_gross: 9500, unit_amount_gross_decimal: '95', - unit_amount_decimal: '100', + unit_amount_decimal: '95', amount_subtotal: 8636, amount_total: 9500, discount_amount: 500, @@ -5831,7 +5831,7 @@ export const computedPriceWithPercentageDiscount = { unit_discount_amount_net_decimal: '22.727272727273', unit_amount_gross: 7500, unit_amount_gross_decimal: '75', - unit_amount_decimal: '100', + unit_amount_decimal: '75', amount_subtotal: 6818, amount_total: 7500, discount_amount: 2500, @@ -5976,7 +5976,7 @@ export const computedPriceWithPercentageDiscountAndHighQuantity = { unit_discount_amount_net_decimal: '22.727272727273', unit_amount_gross: 7500, unit_amount_gross_decimal: '75', - unit_amount_decimal: '100', + unit_amount_decimal: '75', amount_subtotal: 34091, amount_total: 37500, discount_amount: 12500, @@ -6123,7 +6123,7 @@ export const computedPriceWithFixedDiscountAndHighQuantity = { unit_discount_amount_net_decimal: '4.545454545455', unit_amount_gross: 9500, unit_amount_gross_decimal: '95', - unit_amount_decimal: '100', + unit_amount_decimal: '95', amount_subtotal: 43182, amount_total: 47500, discount_amount: 2500, @@ -6224,7 +6224,7 @@ export const computedPriceWithFixedDiscountAndNoTax = { unit_discount_amount_net_decimal: '25', unit_amount_gross: 7500, unit_amount_gross_decimal: '75', - unit_amount_decimal: '100', + unit_amount_decimal: '75', amount_subtotal: 7500, amount_total: 7500, discount_amount: 2500, @@ -6372,7 +6372,7 @@ export const computedPriceWithPercentageDiscountAndExclusiveTax = { unit_discount_amount_net_decimal: '25', unit_amount_gross: 8250, unit_amount_gross_decimal: '82.5', - unit_amount_decimal: '100', + unit_amount_decimal: '75', amount_subtotal: 7500, amount_total: 8250, discount_amount: 2500, @@ -6519,7 +6519,7 @@ export const computedPriceWithFixedDiscountAndExclusiveTax = { unit_discount_amount_net_decimal: '5', unit_amount_gross: 10450, unit_amount_gross_decimal: '104.5', - unit_amount_decimal: '100', + unit_amount_decimal: '95', amount_subtotal: 9500, amount_total: 10450, discount_amount: 500, @@ -6648,7 +6648,7 @@ export const computedResultWithPricesWithAndWithoutCoupons = { unit_discount_amount_net_decimal: '22.727272727273', unit_amount_gross: 7500, unit_amount_gross_decimal: '75', - unit_amount_decimal: '100', + unit_amount_decimal: '75', amount_subtotal: 6818, amount_total: 7500, discount_amount: 2500, @@ -6734,7 +6734,7 @@ export const computedResultWithPricesWithAndWithoutCoupons = { unit_discount_amount_net_decimal: '22.727272727273', unit_amount_gross: 7500, unit_amount_gross_decimal: '75', - unit_amount_decimal: '100', + unit_amount_decimal: '75', amount_subtotal: 6818, amount_total: 7500, discount_amount: 2500, @@ -7578,7 +7578,7 @@ export const computedCompositePriceWithComponentsWithDiscounts = { unit_discount_amount_net_decimal: '4.545454545455', unit_amount_gross: 500, unit_amount_gross_decimal: '5', - unit_amount_decimal: '10.00', + unit_amount_decimal: '5', amount_subtotal: 455, amount_total: 500, discount_amount: 500, @@ -7685,7 +7685,7 @@ export const computedCompositePriceWithComponentsWithDiscounts = { unit_discount_amount_net_decimal: '9.090909090909', unit_amount_gross: 9000, unit_amount_gross_decimal: '90', - unit_amount_decimal: '100.00', + unit_amount_decimal: '90', amount_subtotal: 8182, amount_total: 9000, discount_amount: 1000, @@ -8154,7 +8154,7 @@ export const computedCompositePriceWithComponentsWithCashbacks = { unit_amount_net_decimal: '9.090909090909', unit_amount_gross: 1000, unit_amount_gross_decimal: '10', - unit_amount_decimal: '10.00', + unit_amount_decimal: '10', amount_subtotal: 909, amount_total: 1000, cashback_amount: 1000, @@ -8257,7 +8257,7 @@ export const computedCompositePriceWithComponentsWithCashbacks = { unit_amount_net_decimal: '90.909090909091', unit_amount_gross: 10000, unit_amount_gross_decimal: '100', - unit_amount_decimal: '100.00', + unit_amount_decimal: '100', amount_subtotal: 9091, amount_total: 10000, cashback_amount: 1000, diff --git a/src/pricing.ts b/src/pricing.ts index 5e0f511..4137469 100644 --- a/src/pricing.ts +++ b/src/pricing.ts @@ -823,7 +823,21 @@ export const computePriceItem = ( currency, ...(priceItemDescription && { description: priceItemDescription }), ...(Number.isInteger(itemValues.cashback_amount) && { cashback_period: cashbackPeriod ?? '0' }), - ...(Number.isInteger(itemValues.unit_amount) && { unit_amount: itemValues.unit_amount }), + /** + * @todo In the future the unit_amount_decimal should be derived from the unit_amount + * and not from the original price's unit_amount_decimal, + * as it can be affected by the discounting. + * Right now we have a solution that targets specifically discounted prices, + * but this should be generalized. + */ + ...(price?.pricing_model === PricingModel.perUnit && { unit_amount_decimal: unitAmountDecimal }), + ...(typeof itemValues.unit_amount === 'number' && + Number.isInteger(itemValues.unit_amount) && { + unit_amount: itemValues.unit_amount, + ...(coupon && { + unit_amount_decimal: toDineroFromInteger(itemValues.unit_amount).toUnit().toString(), + }), + }), ...(Number.isInteger(itemValues.before_discount_unit_amount) && { before_discount_unit_amount: itemValues.before_discount_unit_amount, }), @@ -833,7 +847,6 @@ export const computePriceItem = ( unit_discount_amount_net: itemValues.unit_discount_amount_net, }), ...(Number.isInteger(itemValues.unit_amount_gross) && { unit_amount_gross: itemValues.unit_amount_gross }), - ...(price?.pricing_model === PricingModel.perUnit && { unit_amount_decimal: unitAmountDecimal }), amount_subtotal: itemValues.amount_subtotal, amount_total: itemValues.amount_total, ...(itemValues.discount_amount && { discount_amount: itemValues.discount_amount }),