File Coverage

blib/lib/Net/Async/Webservice/UPS/Payment.pm
Criterion Covered Total %
statement 15 18 83.3
branch 0 10 0.0
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 21 35 60.0


line stmt bran cond sub pod time code
1             package Net::Async::Webservice::UPS::Payment;
2             $Net::Async::Webservice::UPS::Payment::VERSION = '1.1.3';
3             {
4             $Net::Async::Webservice::UPS::Payment::DIST = 'Net-Async-Webservice-UPS';
5             }
6 3     3   1899 use Moo;
  3         5  
  3         16  
7 3     3   953 use 5.010;
  3         9  
  3         121  
8 3     3   14 use Types::Standard qw(Str Bool Enum);
  3         4  
  3         28  
9 3     3   2470 use Net::Async::Webservice::UPS::Types ':types';
  3         6  
  3         20  
10 3     3   11952 use namespace::autoclean;
  3         6  
  3         27  
11              
12             # ABSTRACT: a payment method for UPS shipments
13              
14              
15             has method => (
16             is => 'ro',
17             isa => Enum[qw(prepaid third_party freight_collect)],
18             default => 'prepaid',
19             );
20              
21              
22             has account_number => (
23             is => 'ro',
24             isa => Str,
25             required => 0,
26             );
27              
28              
29             has credit_card => (
30             is => 'ro',
31             isa => CreditCard,
32             required => 0,
33             );
34              
35              
36             has address => (
37             is => 'ro',
38             isa => Address,
39             required => 0,
40             );
41              
42              
43             around BUILDARGS => sub {
44             my ($orig,$class,@etc) = @_;
45             my $args = $class->$orig(@etc);
46              
47             if ($args->{method} eq 'prepaid') {
48             if (not ($args->{credit_card} or $args->{account_number})) {
49             require Carp;
50             Carp::croak "account_number or credit_card required when payment method is 'prepaid'";
51             }
52             }
53             elsif ($args->{method} eq 'third_party') {
54             if (not ($args->{account_number} and $args->{address})) {
55             require Carp;
56             Carp::croak "account_number and address required when payment method is 'third_party'";
57             }
58             }
59             elsif ($args->{method} eq 'freight_collect') {
60             if (not ($args->{account_number} and $args->{address})) {
61             require Carp;
62             Carp::croak "account_number required when payment method is 'freight_collect'";
63             }
64             }
65              
66             return $args;
67             };
68              
69              
70             sub as_hash {
71 0     0 1   my ($self) = @_;
72              
73             return {
74 0           ($self->method eq 'prepaid') ? ( Prepaid => {
75             BillShipper => {
76             ( $self->account_number ? ( AccountNumber => $self->account_number ) :
77             ( $self->credit_card ? ( CreditCard => $self->credit_card->as_hash ) :
78             () ) ),
79             },
80             } ) :
81             ($self->method eq 'third_party') ? ( BillThirdParty => {
82             BillThirdPartyShipper => {
83             AccountNumber => $self->account_number,
84             ThirdPartyShipper => $self->address->as_hash('Ship'),
85             },
86             } ) :
87             ($self->method eq 'freight_collect') ? ( FreightCollect => {
88             BillReceiver => {
89             AccountNumber => $self->account_number,
90 0 0         %{$self->address->as_hash('Ship')},
    0          
    0          
    0          
    0          
91             },
92             } ) : ()
93             };
94             }
95              
96             1;
97              
98             __END__
99              
100             =pod
101              
102             =encoding UTF-8
103              
104             =head1 NAME
105              
106             Net::Async::Webservice::UPS::Payment - a payment method for UPS shipments
107              
108             =head1 VERSION
109              
110             version 1.1.3
111              
112             =head1 ATTRIBUTES
113              
114             =head2 C<method>
115              
116             Enum, one of C<prepaid> C<third_party> C<freight_collect>. Defaults to
117             C<prepaid>.
118              
119             =head2 C<account_number>
120              
121             A UPS account number to bill, required for C<third_party> and
122             C<freight_collect> payment methods. For C<prepaid>, either this or
123             L</credit_card> must be set.
124              
125             =head2 C<credit_card>
126              
127             A credit card (instance of L<Net::Async::Webservice::UPS::CreditCard>)
128             to bill. For C<prepaid>, either this or L</account_number> must be
129             set.
130              
131             =head2 C<address>
132              
133             An address (instance of L<Net::Async::Webservice::UPS::Address>),
134             required for C<third_party> and C<freight_collect> payment methods.
135              
136             =head1 METHODS
137              
138             =head2 C<as_hash>
139              
140             Returns a hashref that, when passed through L<XML::Simple>, will
141             produce the XML fragment needed in UPS requests to represent this
142             payment method.
143              
144             =for Pod::Coverage BUILDARGS
145              
146             =head1 AUTHORS
147              
148             =over 4
149              
150             =item *
151              
152             Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
153              
154             =item *
155              
156             Sherzod B. Ruzmetov <sherzodr@cpan.org>
157              
158             =back
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is copyright (c) 2015 by Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>.
163              
164             This is free software; you can redistribute it and/or modify it under
165             the same terms as the Perl 5 programming language system itself.
166              
167             =cut