Initial import of Net::Ikano
[Net-Ikano.git] / lib / Net / Ikano.pm
1 package Net::Ikano;
2
3 use warnings;
4 use strict;
5 use Net::Ikano::XMLUtil;
6 use LWP::UserAgent;
7 use Data::Dumper;
8
9 =head1 NAME
10
11 Net::Ikano - Interface to Ikano wholesale DSL API
12
13 =head1 VERSION
14
15 Version 0.01
16
17 =cut
18
19 our $VERSION = '0.01';
20
21 our $URL = 'https://orders.value.net/OsirisWebService/XmlApi.aspx';
22
23 our $SCHEMA_ROOT = 'https://orders.value.net/osiriswebservice/schema/v1';
24
25 our $API_VERSION = "1.0";
26
27 our $AUTOLOAD;
28
29 =head1 SYNOPSIS
30
31     use Net::Ikano;
32
33     my $ikano = Net::Ikano->new(
34                'keyid' => $your_ikano_api_keyid,
35                'password'  => $your_ikano_admin_user_password,
36                'debug' => 1 # remove this for prod
37                'reqpreviewonly' => 1 # remove this for prod
38                'minimalQualResp' => 1 # on quals, return pairs of ProductCustomId+TermsId only
39                'minimalOrderResp' => 1 # return minimal data on order responses
40                              );
41     
42 =head1 SUPPORTED API METHODS
43
44 =item ORDER
45
46 NOTE: supports orders by ProductCustomId only
47
48 $ikano->ORDER(
49     {
50         orderType => 'NEW',
51         ProductCustomId => 'abc123',
52         TermsId => '123',
53         DSLPhoneNumber => '4167800000',
54         Password => 'abc123',
55         PrequalId => '12345',
56         CompanyName => 'abc co',
57         FirstName => 'first',
58         LastName => 'last',
59         MiddleName => '',
60         ContactMethod => 'PHONE',
61         ContactPhoneNumber => '4167800000',
62         ContactEmail => 'x@x.ca',
63         ContactFax => '',
64         DateToOrder => '2010-11-29',
65         RequestClientIP => '127.0.0.1',
66         IspChange => 'NO',
67         IspPrevious => '',
68         CurrentProvider => '',
69     }
70 );
71
72
73 =item CANCEL
74
75 $i->CANCEL(
76     { OrderId => 555 }
77 );
78
79
80 =item PREQUAL
81
82 $ikano->PREQUAL( {
83     AddressLine1 => '123 Test Rd',
84     AddressUnitType => '', 
85     AddressUnitValue =>  '',
86     AddressCity =>  'Toronto',
87     AddressState => 'ON',
88     ZipCode => 'M6C 2J9', # or 12345
89     Country => 'CA', # or US
90     LocationType => 'R', # or B
91     PhoneNumber => '4167800000',
92     RequestClientIP => '127.0.0.1',
93     CheckNetworks => 'ATT,BELLCA,VER', # either one or command-separated like this
94 } );
95
96
97 =item ORDERSTATUS
98
99 $ikano->ORDERSTATUS( 
100     { OrderId => 1234 }
101 );
102
103
104 =item PASSWORDCHANGE 
105
106 $ikano->PASSWORDCHANGE( {
107             DSLPhoneNumber => '4167800000',
108             NewPassword => 'xxx',
109         } );
110
111
112 =item CUSTOMERLOOKUP
113
114 $ikano->CUSTOMERLOOKUP( { PhoneNumber => '4167800000' } );
115
116
117 =item ACCOUNTSTATUSCHANGE
118
119 $ikano->ACCOUNTSTATUSCHANGE(( {
120             type => 'SUSPEND',
121             DSLPhoneNumber => '4167800000',
122             DSLServiecId => 123,
123         } );
124
125 =cut
126
127 sub new {
128     my ($class,%data) = @_;
129     die "missing keyid and/or password" 
130         unless defined $data{'keyid'} && defined $data{'password'};
131     my $self = { 
132         'keyid' => $data{'keyid'},
133         'password' => $data{'password'},
134         'username' => $data{'username'} ? $data{'username'} : 'admin',
135         'debug' => $data{'debug'} ? $data{'debug'} : 0,
136         'reqpreviewonly' => $data{'reqpreviewonly'} ? $data{'reqpreviewonly'} : 0,
137         };
138     bless $self, $class;
139     return $self;
140 }
141
142
143 sub req_ORDER {
144    my ($self, $args) = (shift, shift);
145
146    my @validOrderTypes = qw( NEW CHANGE CANCEL );
147
148     die "invalid order data" unless defined $args->{orderType}
149         && defined $args->{ProductCustomId} && defined $args->{DSLPhoneNumber};
150    die "invalid order type ".$args->{orderType}
151     unless grep($_ eq $args->{orderType}, @validOrderTypes);
152
153     # XXX: rewrite this uglyness?
154     my @ignoreFields = qw( orderType ProductCustomId );
155     my %orderArgs = ();
156     while ( my ($k,$v) = each(%$args) ) {
157         $orderArgs{$k} = [ $v ] unless grep($_ eq $k,@ignoreFields);
158     }
159
160     return Order => {
161         type => $args->{orderType},
162         %orderArgs,
163         ProductCustomId => [ split(',',$args->{ProductCustomId}) ],
164     };
165 }
166
167 sub resp_ORDER {
168    my ($self, $resphash, $reqhash) = (shift, shift);
169    die "invalid order response" unless defined $resphash->{OrderResponse};
170    return $resphash->{OrderResponse};
171 }
172
173 sub req_CANCEL {
174    my ($self, $args) = (shift, shift);
175
176     die "no order id for cancel" unless defined $args->{OrderId};
177
178     return Cancel => {
179         OrderId => [ $args->{OrderId} ],
180     };
181 }
182
183 sub resp_CANCEL {
184    my ($self, $resphash, $reqhash) = (shift, shift);
185    die "invalid cancel response" unless defined $resphash->{OrderResponse};
186    return $resphash->{OrderResponse};
187 }
188
189 sub req_ORDERSTATUS {
190    my ($self, $args) = (shift, shift);
191
192    die "ORDERSTATUS is supported by OrderId only" 
193     if defined $args->{PhoneNumber} || !defined $args->{OrderId};
194
195     return OrderStatus => {
196         OrderId => [ $args->{OrderId} ],
197     };
198 }
199
200 sub resp_ORDERSTATUS {
201    my ($self, $resphash, $reqhash) = (shift, shift);
202    die "invalid order response" unless defined $resphash->{OrderResponse};
203    return $resphash->{OrderResponse};
204 }
205
206 sub req_ACCOUNTSTATUSCHANGE {
207    my ($self, $args) = (shift, shift);
208    die "invalid account status change request" unless defined $args->{type} 
209     && defined $args->{DSLServiceId} && defined $args->{DSLPhoneNumber};
210
211    return AccountStatusChange => {
212        type => $args->{type},
213         DSLPhoneNumber => [ $args->{DSLPhoneNumber} ],
214         DSLServiceId => [ $args->{DSLServiceId} ],
215     };
216 }
217
218 sub resp_ACCOUNTSTATUSCHANGE {
219    my ($self, $resphash, $reqhash) = (shift, shift);
220     die "invalid account status change response" 
221         unless defined $resphash->{AccountStatusChangeResponse}
222         && defined $resphash->{AccountStatusChangeResponse}->{Customer};
223     return $resphash->{AccountStatusChangeResponse}->{Customer};
224 }
225
226 sub req_CUSTOMERLOOKUP {
227    my ($self, $args) = (shift, shift);
228    die "invalid customer lookup request" unless defined $args->{PhoneNumber};
229    return CustomerLookup => {
230         PhoneNumber => [ $args->{PhoneNumber} ],
231    };
232 }
233
234 sub resp_CUSTOMERLOOKUP {
235    my ($self, $resphash, $reqhash) = (shift, shift);
236    die "invalid customer lookup response" 
237     unless defined $resphash->{CustomerLookupResponse}
238         && defined $resphash->{CustomerLookupResponse}->{Customer};
239    return $resphash->{CustomerLookupResponse}->{Customer};
240 }
241
242 sub req_PASSWORDCHANGE {
243    my ($self, $args) = (shift, shift);
244    die "invalid arguments to PASSWORDCHANGE" 
245         unless defined $args->{DSLPhoneNumber} && defined $args->{NewPassword};
246
247    return PasswordChange => {
248         DSLPhoneNumber => [ $args->{DSLPhoneNumber} ],
249         NewPassword => [ $args->{NewPassword} ],
250    };
251 }
252
253 sub resp_PASSWORDCHANGE {
254    my ($self, $resphash, $reqhash) = (shift, shift);
255    die "invalid change password response" unless defined $resphash->{ChangePasswordResponse};
256    return $resphash->{ChangePasswordResponse};
257 }
258
259 sub req_PREQUAL {
260    my ($self, $args) = (shift, shift);
261    return PreQual => { 
262         Address =>  [ { ( 
263             map { $_ => [ $args->{$_} ]  }  
264                 qw( AddressLine1 AddressUnitType AddressUnitValue AddressCity 
265                     AddressState ZipCode LocationType Country ) 
266             )  } ],
267         ( map { $_ => [ $args->{$_} ] } qw( PhoneNumber RequestClientIP ) ),
268         CheckNetworks => [ {
269             Network => [ split(',',$args->{CheckNetworks}) ]
270         } ],
271        };
272 }
273
274 sub resp_PREQUAL {
275     my ($self, $resphash, $reqhash) = (shift, shift);
276     die "invalid prequal response" unless defined $resphash->{PreQualResponse};
277     return $resphash->{PreQualResponse};
278 }
279
280 sub AUTOLOAD {
281     my $self = shift;
282    
283     $AUTOLOAD =~ /(^|::)(\w+)$/ or die "invalid AUTOLOAD: $AUTOLOAD";
284     my $cmd = $2;
285     return if $cmd eq 'DESTROY';
286
287     my $reqsub = "req_$cmd";
288     my $respsub = "resp_$cmd";
289     die "invalid request type $cmd" 
290         unless defined &$reqsub && defined &$respsub;
291
292     my $reqargs = shift;
293
294     my $xs = new Net::Ikano::XMLUtil(RootName => undef, SuppressEmpty => 1 );
295     my $reqhash = {
296             OsirisRequest   => {
297                 type    => $cmd,
298                 keyid   => $self->{keyid},
299                 username => $self->{username},
300                 password => $self->{password},
301                 version => $API_VERSION,
302                 xmlns   => "$SCHEMA_ROOT/osirisrequest.xsd",
303                 $self->$reqsub($reqargs),
304             }
305         };
306
307
308     my $reqxml = "<?xml version=\"1.0\"?>\n".$xs->XMLout($reqhash, NoSort => 1);
309    
310     # XXX: validate against their schema to ensure we're not sending invalid XML?
311
312     print "DEBUG REQUEST\n\tHASH:\n ".Dumper($reqhash)."\n\tXML:\n $reqxml \n\n" if $self->{debug};
313     
314     my $ua = LWP::UserAgent->new;
315
316     die "posting disabled for testing" if $self->{reqpreviewonly};
317
318     my $resp = $ua->post($URL, Content_Type => 'text/xml', Content => $reqxml);
319     die $resp->status_line unless $resp->is_success;
320     my $respxml = $resp->decoded_content;
321     my $resphash = $xs->XMLin($respxml);
322
323     print "DEBUG RESPONSE\n\tHASH:\n ".Dumper($resphash)."\n\tXML:\n $respxml" if $self->{debug};
324
325     # XXX: validate against their schema to ensure they didn't send us invalid XML?
326
327     die "invalid response" unless defined $resphash->{responseid} 
328         && defined $resphash->{version} && defined $resphash->{type};
329
330     die "FAILURE response received: ".$resphash->{FailureResponse}->{FailureMessage}
331         if $resphash->{type} eq 'FAILURE';
332
333     my $validRespTypes = {
334         'PREQUAL' => qw( PREQUAL ),
335         'ORDERSTATUS' => qw( ORDERSTATUS ),
336         'ORDER' => qw( NEWORDER CHANGEORDER CANCELORDER ),
337         'CANCEL' => qw( ORDERCANCEL ),
338         'PASSWORDCHANGE' => qw( PASSWORDCHANGE ),
339         'ACCOUNTSTATUSCHANGE' => qw( ACCOUNTSTATUSCHANGE ),
340         'CUSTOMERLOOKUP' => qw( CUSTOMERLOOKUP ),
341     };
342
343     die "invalid response type for request type"
344         unless grep( $_ eq $resphash->{type}, $validRespTypes->{$cmd});
345
346     return $self->$respsub($resphash,$reqhash);
347 }
348
349
350 =head1 AUTHOR
351
352 Erik Levinson, C<< <levinse at freeside.biz> >>
353
354 =head1 BUGS
355
356 Please report any bugs or feature requests to C<bug-net-ikano at rt.cpan.org>, or through
357 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Ikano>.  I will be notified, and then you'll
358 automatically be notified of progress on your bug as I make changes.
359
360 =head1 SUPPORT
361
362 You can find documentation for this module with the perldoc command.
363
364     perldoc Net::Ikano
365
366 You can also look for information at:
367
368 =over 4
369
370 =item * RT: CPAN's request tracker
371
372 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Ikano>
373
374 =item * AnnoCPAN: Annotated CPAN documentation
375
376 L<http://annocpan.org/dist/Net-Ikano>
377
378 =item * CPAN Ratings
379
380 L<http://cpanratings.perl.org/d/Net-Ikano>
381
382 =item * Search CPAN
383
384 L<http://search.cpan.org/dist/Net-Ikano>
385
386 =back
387
388 =head1 ACKNOWLEDGEMENTS
389
390 This module was developed by Freeside Internet Services, Inc.
391 If you need a complete, open-source web-based application to manage your
392 customers, billing and trouble ticketing, please visit http://freeside.biz/
393
394 =head1 COPYRIGHT & LICENSE
395
396 Copyright 2010 Freeside Internet Services, Inc.
397 All rights reserved.
398
399 This program is free software; you can redistribute it and/or modify it
400 under the same terms as Perl itself.
401
402 =cut
403
404 1;
405