Google-Checkout

 view release on metacpan or  search on metacpan

lib/Google/Checkout/General/MerchantCalculationResults.pm  view on Meta::CPAN

Returns the XML.

=back

=cut

=head1 COPYRIGHT

Copyright 2006 Google. All rights reserved.

=head1 SEE ALSO

Google::Checkout::XML::Writer
Google::Checkout::General::MerchantCalculationResult

=cut

#--
#--   <merchant-calculation-results>
#--

use strict;
use warnings;

use Google::Checkout::XML::Constants;
use Google::Checkout::General::Util qw/format_tax_rate/;

use Google::Checkout::XML::Writer;
our @ISA = qw/Google::Checkout::XML::Writer/;

sub new
{
  my ($class, %args) = @_;

  delete $args{root};

  my $self = $class->SUPER::new(%args);

  $self->{gco} = $args{gco};

  my $schema = Google::Checkout::XML::Constants::XML_SCHEMA;

  my $xml_schema = '';
  if ($args{gco}->reader()) {
    $xml_schema = $args{gco}->reader()->get($schema);
  } else {
    $xml_schema = $args{gco}->{__xml_schema};
  }

  $self->add_element(name => Google::Checkout::XML::Constants::MERCHANT_CALCULATION_RESULTS,
                     attr => [xmlns => $xml_schema]);
  $self->add_element(name => Google::Checkout::XML::Constants::RESULTS);

  #--
  #-- If there is any merchant calculation result, add them now
  #-- This should be an array reference because we can have multiple result
  #--
  $self->{merchant_calculation_result} = $args{merchant_calculation_result}
    if $args{merchant_calculation_result};

  return bless $self => $class;
}

sub get_merchant_calculation_result 
{ 
  my ($self) = @_;

  return $self->{merchant_calculation_result} || []; 
}

sub add_merchant_calculation_result
{
  my ($self, $result) = @_;

  push(@{$self->{merchant_calculation_result}}, $result) if $result;
}

sub done
{
  my ($self) = @_;

  my $currency = Google::Checkout::XML::Constants::CURRENCY_SUPPORTED;

  my $currency_supported = '';
  if ($self->{gco}->reader()) {
    $currency_supported = $self->{gco}->reader->get($currency);
  } else {
    $currency_supported = $self->{gco}->{__currency_supported};
  }

  for my $result (@{$self->get_merchant_calculation_result})
  {
    $self->add_element(
      name => Google::Checkout::XML::Constants::RESULT,
      attr => [Google::Checkout::XML::Constants::SHIPPING_NAME => $result->get_shipping_name,
               Google::Checkout::XML::Constants::ADDRESS_ID => $result->get_address_id ]);

    if ($result->has_total_tax)
    {
      $self->add_element(
         name => Google::Checkout::XML::Constants::TOTAL_TAX,
         attr => [Google::Checkout::XML::Constants::ITEM_CURRENCY =>
                  $currency_supported],
         data => $result->get_total_tax, close => 1);
    }

    if ($result->has_shipping_rate)
    {
      $self->add_element(
         name => Google::Checkout::XML::Constants::SHIPPING_RATE,
         attr => [Google::Checkout::XML::Constants::ITEM_CURRENCY => 
                  $currency_supported],
         data => $result->get_shipping_rate, close => 1);
    }

    if ($result->has_shippable)
    {
      $self->add_element(name => Google::Checkout::XML::Constants::SHIPPALBE,
                         data => $result->is_shippable, close => 1);
    }



( run in 0.583 second using v1.01-cache-2.11-cpan-ff9377addf4 )