Shopify/active_shipping

Bug: Stamps.com rates are expressed in dollars in the API, ActiveShipping sometimes interprets them as cents

Opened this issue · 1 comments

Before I make a PR, I'd like advice on the best approach to fixing this issue.

The code in question is https://github.com/Shopify/active_shipping/blob/master/lib/active_shipping/package.rb#L117-L131

    def self.cents_from(money)
      return nil if money.nil?
      if money.respond_to?(:cents)
        return money.cents
      else
        case money
        when Float
          (money * 100).round
        when String
          money =~ /\./ ? (money.to_f * 100).round : money.to_i
        else
          money.to_i
        end
      end
    end

When querying the Stamps.com API for rates, it returns amount values that look like 10.50 representing $10.50, for example. However, when the value is an amount of whole dollars, it looks like 10 representing $10, for example. This causes the cents_from method above to assume that the 10 is a value in cents since it doesn't contain a ..

We discovered this when a customer was charged $0.57 for a shipment from California to Finland! Whoops!

Perhaps the best way would be to have the Stamps Carrier class return this value as an instance of a class that implements a cents method. What would be best practice here?

Thanks

Oh boy. This cents_from method is super painful.

I'm going to put this in the 2.0 milestone. We should replace this with proper money parsing.