Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect computations for unit_price_decimal for discounted prices #157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/__tests__/fixtures/pricing.results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions src/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look correct.

Did you understand why are we adding it only when it's per_unit?

...(typeof itemValues.unit_amount === 'number' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these conditions should be evaluated to true. Since we are using dineroJS to compute values it only uses and outputs integers, if you are receiving here any other thing the issue should be before this step.

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,
}),
Expand All @@ -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 }),
Expand Down
Loading