From e34ed3fda87d9645bf14702015d261b4a217d934 Mon Sep 17 00:00:00 2001 From: Chris Parrish Date: Wed, 22 Jun 2011 22:25:30 -0600 Subject: [PATCH] Don't break if we're handed the older refund-notification XML format --- lib/google4r/checkout/notifications.rb | 8 +++--- .../chargeback_amount_notification_test.rb | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/google4r/checkout/notifications.rb b/lib/google4r/checkout/notifications.rb index def2dba..e3b28c1 100644 --- a/lib/google4r/checkout/notifications.rb +++ b/lib/google4r/checkout/notifications.rb @@ -368,9 +368,11 @@ def self.create_from_element(element, frontend) amount = (BigDecimal.new(element.elements['total-refund-amount'].text)*100).to_i refund.total_refund_amount = Money.new(amount, currency) - currency = element.elements['latest-fee-refund-amount'].attributes['currency'] - amount = (BigDecimal.new(element.elements['latest-fee-refund-amount'].text)*100).to_i - refund.latest_fee_refund_amount = Money.new(amount, currency) + if element.elements['latest-fee-refund-amount'] + currency = element.elements['latest-fee-refund-amount'].attributes['currency'] + amount = (BigDecimal.new(element.elements['latest-fee-refund-amount'].text)*100).to_i + refund.latest_fee_refund_amount = Money.new(amount, currency) + end refund.timestamp = Time.parse(element.elements['timestamp'].text) diff --git a/test/unit/chargeback_amount_notification_test.rb b/test/unit/chargeback_amount_notification_test.rb index d42a8b3..888b098 100644 --- a/test/unit/chargeback_amount_notification_test.rb +++ b/test/unit/chargeback_amount_notification_test.rb @@ -51,6 +51,18 @@ def setup 2006-03-18T20:25:31 } + + @example2_xml = %q{ + + + 841171949013218 + 226.06 + 226.06 + 2006-03-18T20:25:31 + +} + end def test_create_from_element_works_correctly @@ -66,4 +78,17 @@ def test_create_from_element_works_correctly assert_equal(Money.new(221, 'GBP'), notification.latest_fee_refund_amount) assert_equal(Money.new(1000, 'GBP'), notification.latest_chargeback_fee_amount) end + + def test_create_from_minimal_element_works_correctly + root = REXML::Document.new(@example2_xml).root + + notification = ChargebackAmountNotification.create_from_element(root, @frontend) + + assert_equal 'bea6bc1b-e1e2-44fe-80ff-0180e33a2614', notification.serial_number + assert_equal '841171949013218', notification.google_order_number + assert_equal Time.parse('2006-03-18T20:25:31'), notification.timestamp + assert_equal(Money.new(22606, 'GBP'), notification.total_chargeback_amount) + assert_equal(Money.new(22606, 'GBP'), notification.latest_chargeback_amount) + end + end