Zuora.pm

filter: Statementstmt 100% 18 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 24 / 24 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Zuora/Types.pm ↑ Summary
1 package Net::Zuora;
2 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use File::ShareDir qw/module_dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use MooseX::Types::Moose qw/Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Net::Zuora::Types qw/CreateOrUpdate/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MooseX::Lexical::Types qw/Object CreateOrUpdate/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use MooseX::Types::Common::String qw/NonEmptySimpleStr/;
9 use MooseX::Types::Path::Class;
10 use SOAP::Lite; # +trace => [qw/transport debug/];
11 BEGIN { $SOAP::Constants::PREFIX_ENV = 'SOAP-ENV'; }
12 use Path::Class qw/file/;
13 use Data::Dumper;
14 use Net::Zuora::QueryIterator;
15 use namespace::autoclean;
16  
17 our $VERSION = '0.000000_01';
18 $VERSION = eval $VERSION;
19  
20 BEGIN { # If we are in a persistent environment (e.g. mod_perl)
21 # then detect this (if prefork is installed) and preload
22 # everything.
23 if (
24 eval { require prefork }
25 && $prefork::FORKING
26 && eval { require Module::Pluggable::Object }
27 ) {
28 Class::MOP::load_class($_)
29 for Module::Pluggable::Object->new(
30 search_path => ['Net::Zuora'],
31 )->plugins;
32 }
33 }
34  
35 has wsdl_file => (
36 is => 'ro',
37 isa => 'Path::Class::File',
38 coerce => 1,
39 default => sub { file( module_dir(ref shift), 'zuora.11.0.wsdl' ) },
40 );
41  
42 has _soap => (
43 is => 'ro',
44 isa => Object,
45 lazy_build => 1,
46 );
47  
48 has username => (
49 is => 'ro',
50 isa => NonEmptySimpleStr,
51 required => 1,
52 );
53  
54 has password => (
55 is => 'ro',
56 isa => NonEmptySimpleStr,
57 required => 1,
58 );
59  
60 sub _build__soap {
61 SOAP::Lite->service(
62 "file://" . shift->wsdl_file->absolute
63 )->proxy('https://www.zuora.com/apps/services/a/11.0');
64 }
65  
66 sub BUILD {
67 my ($self) = @_;
68 $self->session_id; # Login on construction so that we throw an
69 # exception straight away if credentials are wrong.
70 }
71  
72 has session_id => (
73 is => 'ro',
74 isa => NonEmptySimpleStr,
75 lazy => 1,
76 builder => '_do_login',
77 );
78  
79 sub _do_login {
80 my ($self) = @_;
81 my $res = $self->_soap->call(
82 SOAP::Data->name('login')->attr({
83 'xmlns:zns' => 'http://api.zuora.com/'}),
84 SOAP::Data->name('zns:username', $self->username),
85 SOAP::Data->name('zns:password', $self->password),
86 );
87 return $res->result->{Session}
88 if $res->result;
89 die("Could not login, fault detail: " . Dumper($res->fault));
90 }
91  
92 sub _load_object_class {
93 my ($self, $type) = @_;
94 my $class = "Net::Zuora::$type";
95 Class::MOP::load_class($class);
96 return $class;
97 }
98  
99 sub new_object {
100 my ($self, $type, %p) = @_;
101 my $class = $self->_load_object_class($type);
102 $class->new(%p, _api => $self);
103 }
104  
105 sub query_objects {
106 my ($self, $type, %p) = @_;
107 my $class = $self->_load_object_class($type);
108 my $query_string = "select " . join(', ', $self->_get_public_attribute_names($class)) . " from $type";
109 if (scalar(keys %p)) {
110 $query_string .= " WHERE "
111 . join(" AND ", map { $_ . "= '" . $p{$_} . "'" } keys %p);
112 }
113  
114 my $res = $self->_soap->call(
115 $self->_soap_headers,
116 SOAP::Data->name('zns:query')->attr({
117 'xmlns:zns' => 'http://api.zuora.com/',
118 }),
119 SOAP::Data->name('zns:queryString', $query_string)
120 );
121 Carp::confess(Dumper($res->fault)) if $res->fault;
122 die(Dumper($res->result)) unless $res->result->{done} eq 'true';
123 return Net::Zuora::QueryIterator->new(_api => $self, records => $res->result->{records}, type => $type);
124 }
125  
126 sub _soap_headers {
127 SOAP::Header->name(
128 'zns:SessionHeader'
129 => \SOAP::Data->name('zns:session' => shift->session_id)
130 )->attr({
131 'xmlns:zns' => 'http://api.zuora.com/',
132 'SOAP-ENV:mustUnderstand' => '0',
133 })
134 }
135  
136 # FIXME - support attr trait or something..
137 sub _get_public_attribute_names {
138 my ($self, $class) = @_;
139 grep { ! /^_/ }
140 map { $_->name }
141 $class->meta->get_all_attributes;
142 }
143  
144 sub _get_updateable_attribute_names {
145 my ($self, $class) = @_;
146 grep {
147 $_ eq 'Id' or
148 $class->meta->find_attribute_by_name($_)->has_write_method
149 }
150 $self->_get_public_attribute_names($class);
151 }
152  
153 foreach my $type (qw/ create update /) {
154 __PACKAGE__->meta->add_method( "_do_$type" => sub {
155 my $self = shift;
156 $self->_do_create_or_update($type, @_);
157 });
158 }
159  
160 sub _do_create_or_update {
161 my $self = shift;
162 my CreateOrUpdate $type = shift;
163 my Object $object = shift;
164  
165 my $ob_type = ref($object);
166 $ob_type =~ s/.*:://;
167  
168 my $get_attr_names_method
169 = ($type eq 'create' ? '_get_public_attribute_names'
170 : '_get_updateable_attribute_names' );
171 my @ob_data =
172 map { SOAP::Data->name("objns:$_", $object->$_()) }
173 $self->$get_attr_names_method($object);
174  
175 my $res = $self->_soap->call(
176 $self->_soap_headers,
177 SOAP::Data->name("zns:$type")->attr({
178 'xmlns:zns' => 'http://api.zuora.com/',
179 }),
180 SOAP::Data->name('zns:zObjects', \SOAP::Data->value(
181 @ob_data
182 ))->attr({
183 'xmlns:objns' => 'http://object.api.zuora.com/',
184 'xsi:type' => "objns:$ob_type",
185 })
186 );
187 Carp::confess(Dumper($res->fault)) if $res->fault;
188 die($res->result) unless $res->result->{Success} eq 'true';
189 return 1;
190 }
191  
192 =head1 NAME
193  
194 Net::Zuora - SOAP::Lite wrapper around the Zuora Z-Billing API
195  
196 =head1 SYNOPSIS
197  
198 use Net::Zuora;
199 my $z = Net::Zuora->new(
200 usename => 'MyAPIUserName',
201 password => 'MyAPIPassword',
202 );
203  
204 =head1 DESCRIPTION
205  
206 Perl wrapper around L<SOAP::Lite> to interface with the Zuora Z-Billing API
207 (L<http://www.zuora.com>)
208  
209 =head1 NOTE
210  
211 B<THIS CODE IS INCOMPLETE> - it does not currently support all the features
212 of the Zuora API, and is not currently being developed.
213  
214 It is here as a guide/starting point for someone looking to implement a
215 wrapper to Zuora's API in perl.
216  
217 =head1 METHODS
218  
219 =head1 BUGS
220  
221 Plenty, along with missing features. This module was implemented whilst
222 we were evaluating the Zuora solution, however it is in no way feature complete,
223 but I thought I should share it to give the next person coming to this
224 problem a useful base to start from.
225  
226 Code is available on github from: L<http://github.com/bobtfish/net-zuora>.
227  
228 Patches (and taking over this project entirely) welcome.
229  
230 =head1 AUTHOR
231  
232 Tomas Doran (t0m) C<< <t0m@state51.co.uk> >>
233  
234 =head1 COPYRIGHT & LICENSE
235  
236 All perl code is Copyright (c) 2009 state51 and is licensed under the same
237 terms as perl itself.
238  
239 The .wsdl file distributed with this distribution is Copyright (c) Zuora
240 and is free to distribute unmodified as long as this copyright notice
241 is maintained.
242  
243 =cut
244  
245 1;
« Zuora/Types.pm ↑ Summary