MultiSafepay / php-sdk

The default PHP library for connecting to the MultiSafepay REST API
https://docs.multisafepay.com/api
Open Software License 3.0
11 stars 10 forks source link

Unexpected behaviour of money object #12

Open Robertbaelde opened 2 months ago

Robertbaelde commented 2 months ago

There is some unexpected behaviour in the Money object regarding cents vs float amount. For this Issue I'm going to use euro as currency, but this holds true for all currencies containing decimals.

Looking at the Money object, we'd expect to pass the value in euro's since it accepts a float amount, and has a getAmount() method returning float and it contains a getAmountInCents() method that returns the amount multiplied by 100.

  /**
   * @return float
   */
  public function getAmountInCents(): float
  {
      return $this->amount * 100;
  }

When we look at the usage of the money object in the api and the MoneyFormatter we see that it actually is expected to pass the amount in cents to the money object.

In order to avoid confusion, I suggest to remove the getAmountInCents() method from the Money VO. (It isn't used afaik).

A more breaking change would be to actually change the cents amount value to int (float amounts for cents probably don't make sense in the context of a psp / the api).

Id suggest to add static Money::fromAmount(10,50) and Money::fromBaseAmount(1050) static methods to the money VO in order to have an even clearer api.

danielcivit commented 2 months ago

Hi @Robertbaelde.

We will review your comments, consider the suggested changes, and get back to you.

Thanks for your feedback.