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

feat(payments-plugin): allow partial payments #2092

Conversation

martijnvdbrug
Copy link
Collaborator

No description provided.

@martijnvdbrug martijnvdbrug marked this pull request as ready for review March 22, 2023 14:25
isAuthorized: true,
authorizedAsOwnerOnly: false,
channel: await server.app.get(ChannelService).getDefaultChannel(),
describe('Payment intent creation', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry for the large diff, but I grouped the testcases for readability:
image

* Return to number of cents
*/
export function amountToCents(amount: Amount): number {
return Math.round(Number(amount.value) * 100); // Round because floating point errors after Number() conversion
Copy link
Contributor

@StampixSMO StampixSMO Mar 22, 2023

Choose a reason for hiding this comment

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

I would suggest to use a dedicated library for any currency conversions/math to avoid rounding issues in general:
image

Copy link
Member

Choose a reason for hiding this comment

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

@martijnvdbrug Interesting - I was wondering about this and I see from the Mollie SDK that amount.value is a string like '10.00'. Checked some values and indeed you do get rounding issues!

Number('10.21') * 100
// => 1021.0000000000001

In general @StampixSMO I agree we need to be cautious with math on currency - that's why Vendure only stores integers for currency values. In this case, I think the handling is fine because we're dealing here with an artifact of "floating-point numbers in binary" rather than an actual rounding error caused by multiplying by a non-integer, so the error is only ever so tiny as to be ignored.

Copy link
Contributor

Choose a reason for hiding this comment

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

Mollie returns a string on purpose, it avoids FP errors due to frameworks along the way. It's meant to be used with a professional library like currency.js for math and parsing.

Thanks for confirming the Vendure strategy. With integers, you implicitly mean that you store the least significant currency unit, right? E.g. dollarcents, eurocents, ... instead of dollars and euros.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oke, the library is tiny, so I'll add it :-)

Copy link
Member

Choose a reason for hiding this comment

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

@StampixSMO yes, we store the minor unit.

adminClient,
order.lines[0].id,
10,
order.payments[2].id,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Use the payments[2] now instead of [1] here, because we've added an extra test with partial payment

@martijnvdbrug martijnvdbrug requested review from michaelbromley and StampixSMO and removed request for michaelbromley and StampixSMO March 23, 2023 16:37
@martijnvdbrug
Copy link
Collaborator Author

@michaelbromley Reallyyyy..., now its only the MySQL test that's failing. Same error. I might need your help on this, still don't quite understand whats going on under the hood with the multiple refunds.

@michaelbromley michaelbromley merged commit 7398a72 into vendure-ecommerce:master Mar 24, 2023
@martijnvdbrug martijnvdbrug deleted the fix/mollie-partial-payments branch March 24, 2023 10:37
michaelbromley added a commit that referenced this pull request Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants