Class Simplepay::Support::Amount
In: lib/simplepay/support/amount.rb
Parent: Object
RuntimeError Error Service Standard Marketplace Donation Subscription MarketplacePolicy Interval SubscriptionPeriod BillingFrequency Comparable Field Service Epoch Currency Amount Boolean ResponseStatusCode Services Authentication FormHelper NotificationHelper RailsHelper Helpers Interval Currency Support Boolean Simplepay dot/f_2.png

Amazon often represents dollar values as a combination of a value and a currency. In several types of requests, the combination is required for communication.

At the present time, Amazon only uses USD.


amount=   currency=   new   to_s  


amount  [R] 
currency  [R] 

Public Class methods


# File lib/simplepay/support/amount.rb, line 18
      def initialize(amount, currency = Simplepay::Currency::USD)
        self.amount   = amount
        self.currency = currency

Public Instance methods

Sets the amount of the currency value, such as "1" for 1 USD. This amount cannot be negative.


# File lib/simplepay/support/amount.rb, line 28
      def amount=(amount)
        raise(ArgumentError, "Amount cannot be nil") unless amount
        raise(ArgumentError, "Amount cannot be negative") if amount < 0
        @amount =

Sets the type of currency to use in the transaction. The parameter can either be a known currency code (see Simplepay::Currency) or a custom Simplepay::Currency::Currency instance.


# File lib/simplepay/support/amount.rb, line 39
      def currency=(currency)
        raise(ArgumentError, "Invalid currency, expected Simplepay::Support::Currency or currency code string.") unless currency.kind_of?(Simplepay::Support::Currency) || currency.kind_of?(String)
        if currency.kind_of?(String)
          currency =  Simplepay::Currencies.detect { |c| c.code == currency } ||
                      raise(ArgumentError, "Invalid currency, '#{currency}'. Must be one of: #{Simplepay::Currencies.join(', ')}")
        @currency = currency


# File lib/simplepay/support/amount.rb, line 48
      def to_s
        "#{currency.code} #{currency.format % amount}"