Class: Google4R::Checkout::MerchantCode

Inherits:
Object
  • Object
show all
Defined in:
lib/google4r/checkout/notifications.rb

Overview

MerchantCodes represent gift certificates or coupons that have been used in an order.

Only used with Merchant Calculations.

Constant Summary collapse

GIFT_CERTIFICATE =
"GIFT_CERTIFICATE".freeze
COUPON =
"COUPON".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#applied_amountObject

The amount of the adjustment that has been applied to the cart’s total (Money).



614
615
616
# File 'lib/google4r/checkout/notifications.rb', line 614

def applied_amount
  @applied_amount
end

#calculated_amountObject

The amount of money that has been calculated as the adjustment’s worth (Money, optional).



611
612
613
# File 'lib/google4r/checkout/notifications.rb', line 611

def calculated_amount
  @calculated_amount
end

#codeObject

The adjustment’s code (String).



608
609
610
# File 'lib/google4r/checkout/notifications.rb', line 608

def code
  @code
end

#messageObject

The message associated with the direct adjustment (String, optional).



617
618
619
# File 'lib/google4r/checkout/notifications.rb', line 617

def message
  @message
end

#typeObject

The type of the adjustment. Can be one of GIFT_CERTIFICATE and COUPON.



605
606
607
# File 'lib/google4r/checkout/notifications.rb', line 605

def type
  @type
end

Class Method Details

.create_from_element(element) ⇒ Object

Creates the MerchantCode from the given REXML::Element instance. The Element’s name must be “gift-certificate-adjustment” or “coupon-adjustment”.



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/google4r/checkout/notifications.rb', line 621

def self.create_from_element(element)
  result = MerchantCode.new
  
  result.type =
    case element.name
    when 'gift-certificate-adjustment' then
      GIFT_CERTIFICATE
    when 'coupon-adjustment' then
      COUPON
    else
      raise ArgumentError, "Invalid tag name: #{element.name} in \n—-\n#{element.to_s}\n—-."
    end
  
  result.code = element.elements['code'].text
  
  amount = (BigDecimal.new(element.elements['calculated-amount'].text)*100).to_i rescue nil
  currency = element.elements['calculated-amount'].attributes['currency'] rescue nil
  result.calculated_amount = Money.new(amount, currency) unless amount.nil?
  
  amount = (BigDecimal.new(element.elements['applied-amount'].text)*100).to_i
  currency = element.elements['applied-amount'].attributes['currency']
  result.applied_amount = Money.new(amount, currency)
  
  result.message = element.elements['message'].text rescue nil
  
  return result
end