aviabird / gringotts

A complete payment library for Elixir and Phoenix Framework
https://hexdocs.pm/gringotts/Gringotts.html
MIT License
482 stars 53 forks source link

Introducing the Money protocol #71

Closed oyeb closed 6 years ago

oyeb commented 6 years ago

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. There's a fallback implementation for Any which enables us to support ex_money and monetized right out of the box.

Todo

Please push commits to resolve the following:

oyeb commented 6 years ago

Task 3

I've hit a wall with writing integration tests for the money libs. The problem is that all these libs have a "naked" Money module/type, so if I add them to our deps(), the module gets redefined, as expected!

Moreover, there's no way for me to refer to the Money type from ex_money differently from the Money type from money (note that there's always only 1 Money type at the end (of compilation)).

oyeb commented 6 years ago

Task 4

Because the module is naked, I can't implement the protocol for them in Gringotts. But we can send a PR to their projects, where they can implement our protocol. They won't have to add our lib as a dependency. It'll look something like:

# Add this in their money.ex
if Code.ensure_compiled?(Gringotts.Money) do
 defimpl value(money) do
    ...
 end
 defimpl currency(money) do
    ...
 end
end

If we ever change our protocol, we send PRs. If they make a breaking change later, it's their responsibility to mend it.