-
Notifications
You must be signed in to change notification settings - Fork 52
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
Replace floating point numbers with a Money lib #62
Comments
You could do both. Use a protocol for the values sent to your public interface functions and convert those to money structs used internally. |
I think its important not to tie Gringotts to a single lib in any way. That would limit choices and potentially stifle innovation. I support @LostKobrakai's suggestion. Protocol support at the public api level where money is being submitted to the gateway. For a return value from the gateway use your own |
Building on the discussion in #62, this PR introduces a protocol to replace amount argument, and removes the need to specify currency in config or opts. Supporting `ex_money` and `monetized` money libs out of the box. Usage:- Money can now be passed like this while calling the public API methods. money = %{amount: Decimal.new(2017.18), currency: "USD"} Conversation for this happened here on elixir forum and https://elixirforum.com/t/gringotts-a-complete-payment-library-for-elixir-and-phoenix-framework/11054
Context
Additions to the protocol
|
Looks good. I believe that the integer output option should be a separate function. Following this discussion I have implemented, for ex_money two functions,
|
Moving further progress to #85 |
* Adds `to_string` and `to_integer` to the protocol * Fixes #62 and #85 * Implements all protocol methods for `ex_money` * Adds integration test with ex_money * Adapted Monei with protocol updates * Adds test for `Any` * The default rounding strategy for implementations of Gringotts.Money protocol is HALF-EVEN. * Updated public API docs with "perils of rounding".
Building on the discussion in #62, this PR introduces a protocol to replace amount argument, and removes the need to specify currency in config or opts. Supporting `ex_money` and `monetized` money libs out of the box. Usage:- Money can now be passed like this while calling the public API methods. money = %{amount: Decimal.new(2017.18), currency: "USD"} Conversation for this happened here on elixir forum and https://elixirforum.com/t/gringotts-a-complete-payment-library-for-elixir-and-phoenix-framework/11054
* Adds `to_string` and `to_integer` to the protocol * Fixes #62 and #85 * Implements all protocol methods for `ex_money` * Adds integration test with ex_money * Adapted Monei with protocol updates * Adds test for `Any` * The default rounding strategy for implementations of Gringotts.Money protocol is HALF-EVEN. * Updated public API docs with "perils of rounding".
Update: This is WIP here
[Recommendation]
It's clear that using floating point numbers for representing money is a bad idea, and using plain integers fairly limits the application since there exist currencies with more than 2 decimal places. This rules out supporting
money
as it uses integers.Which kind of monie then?
ex_money
andmonetized
both useDecimal
s to represent money, butex_money
offers some more features thanmonetized
, like:ex_cldr
. These rules define the number of fractional digits for a currency and the rounding increment where appropriate.ex_money
).Who benefits from
ex_money
There's not much to gain for Gringotts, all it does is format the
amount
(as documented in the gatewayor as per ISO4127) to construct a correctPOST
request for the gateway. The real benefit is to merchants already usingex_money
, or planning to do so. They won't have to convert their super awesomeMoney
structs intofloats
/ints
/string
and be sure that Gringotts does not cause them any loss.What will this break?
amount
parameter will now accept onlyMoney.t
.Money.t
only to convert them back into plain strings in the end.1/100th units
some inunits
, so if we allow integers, we'll have to decide if Gringotts works in1/100th units
orunit
, then enforce that in our API. Let's just useMoney.t
instead as our unambiguous "protocol".Migration tips (for merchants)
Any merchant doing even trivial financial accounting in their app would be using some Money lib.
ex_money
.Money.t
struct from whatever your representation is and pass that in Gringott's API methods.TL;DR
Let's use
ex_money
or a protocol and track it's integration into Gringotts here.The text was updated successfully, but these errors were encountered: