add class method orderTypes and fix POD nits
[Net-Ikano.git] / lib / Net / Ikano.pm
index acbc6c2..879e8ee 100644 (file)
@@ -24,6 +24,10 @@ our $SCHEMA_ROOT = 'https://orders.value.net/osiriswebservice/schema/v1';
 
 our $API_VERSION = "1.0";
 
+our @orderType = qw( NEW CANCEL CHANGE );
+
+our @orderStatus = qw( NEW PENDING CANCELLED COMPLETED ERROR );
+
 our $AUTOLOAD;
 
 =head1 SYNOPSIS
@@ -34,13 +38,15 @@ our $AUTOLOAD;
               'keyid' => $your_ikano_api_keyid,
               'password'  => $your_ikano_admin_user_password,
               'debug' => 1 # remove this for prod
-              'reqpreviewonly' => 1 # remove this for prod
-              'minimalQualResp' => 1 # on quals, return pairs of ProductCustomId+TermsId only
-              'minimalOrderResp' => 1 # return minimal data on order responses
-                            );
-    
+               'reqpreviewonly' => 1 # remove this for prod
+               'minimalQualResp' => 1 # on quals, return pairs of ProductCustomId+TermsId only
+               'minimalOrderResp' => 1 # return minimal data on order responses
+        );
+
 =head1 SUPPORTED API METHODS
 
+=over 4
+
 =item ORDER
 
 NOTE: supports orders by ProductCustomId only
@@ -122,6 +128,8 @@ $ikano->ACCOUNTSTATUSCHANGE(( {
            DSLServiecId => 123,
        } );
 
+=back
+
 =cut
 
 sub new {
@@ -143,12 +151,10 @@ sub new {
 sub req_ORDER {
    my ($self, $args) = (shift, shift);
 
-   my @validOrderTypes = qw( NEW CHANGE CANCEL );
-
-    die "invalid order data" unless defined $args->{orderType}
+    return "invalid order data" unless defined $args->{orderType}
        && defined $args->{ProductCustomId} && defined $args->{DSLPhoneNumber};
-   die "invalid order type ".$args->{orderType}
-    unless grep($_ eq $args->{orderType}, @validOrderTypes);
+   return "invalid order type ".$args->{orderType}
+    unless grep($_ eq $args->{orderType}, @orderType);
 
     # XXX: rewrite this uglyness?
     my @ignoreFields = qw( orderType ProductCustomId );
@@ -166,14 +172,14 @@ sub req_ORDER {
 
 sub resp_ORDER {
    my ($self, $resphash, $reqhash) = (shift, shift);
-   die "invalid order response" unless defined $resphash->{OrderResponse};
+   return "invalid order response" unless defined $resphash->{OrderResponse};
    return $resphash->{OrderResponse};
 }
 
 sub req_CANCEL {
    my ($self, $args) = (shift, shift);
 
-    die "no order id for cancel" unless defined $args->{OrderId};
+    return "no order id for cancel" unless defined $args->{OrderId};
 
     return Cancel => {
        OrderId => [ $args->{OrderId} ],
@@ -182,14 +188,14 @@ sub req_CANCEL {
 
 sub resp_CANCEL {
    my ($self, $resphash, $reqhash) = (shift, shift);
-   die "invalid cancel response" unless defined $resphash->{OrderResponse};
+   return "invalid cancel response" unless defined $resphash->{OrderResponse};
    return $resphash->{OrderResponse};
 }
 
 sub req_ORDERSTATUS {
    my ($self, $args) = (shift, shift);
 
-   die "ORDERSTATUS is supported by OrderId only" 
+   return "ORDERSTATUS is supported by OrderId only" 
     if defined $args->{PhoneNumber} || !defined $args->{OrderId};
 
     return OrderStatus => {
@@ -199,13 +205,13 @@ sub req_ORDERSTATUS {
 
 sub resp_ORDERSTATUS {
    my ($self, $resphash, $reqhash) = (shift, shift);
-   die "invalid order response" unless defined $resphash->{OrderResponse};
+   return "invalid order response" unless defined $resphash->{OrderResponse};
    return $resphash->{OrderResponse};
 }
 
 sub req_ACCOUNTSTATUSCHANGE {
    my ($self, $args) = (shift, shift);
-   die "invalid account status change request" unless defined $args->{type} 
+   return "invalid account status change request" unless defined $args->{type} 
     && defined $args->{DSLServiceId} && defined $args->{DSLPhoneNumber};
 
    return AccountStatusChange => {
@@ -217,7 +223,7 @@ sub req_ACCOUNTSTATUSCHANGE {
 
 sub resp_ACCOUNTSTATUSCHANGE {
    my ($self, $resphash, $reqhash) = (shift, shift);
-    die "invalid account status change response" 
+    return "invalid account status change response" 
        unless defined $resphash->{AccountStatusChangeResponse}
        && defined $resphash->{AccountStatusChangeResponse}->{Customer};
     return $resphash->{AccountStatusChangeResponse}->{Customer};
@@ -225,7 +231,7 @@ sub resp_ACCOUNTSTATUSCHANGE {
 
 sub req_CUSTOMERLOOKUP {
    my ($self, $args) = (shift, shift);
-   die "invalid customer lookup request" unless defined $args->{PhoneNumber};
+   return "invalid customer lookup request" unless defined $args->{PhoneNumber};
    return CustomerLookup => {
        PhoneNumber => [ $args->{PhoneNumber} ],
    };
@@ -233,7 +239,7 @@ sub req_CUSTOMERLOOKUP {
 
 sub resp_CUSTOMERLOOKUP {
    my ($self, $resphash, $reqhash) = (shift, shift);
-   die "invalid customer lookup response" 
+   return "invalid customer lookup response" 
     unless defined $resphash->{CustomerLookupResponse}
        && defined $resphash->{CustomerLookupResponse}->{Customer};
    return $resphash->{CustomerLookupResponse}->{Customer};
@@ -241,7 +247,7 @@ sub resp_CUSTOMERLOOKUP {
 
 sub req_PASSWORDCHANGE {
    my ($self, $args) = (shift, shift);
-   die "invalid arguments to PASSWORDCHANGE" 
+   return "invalid arguments to PASSWORDCHANGE" 
        unless defined $args->{DSLPhoneNumber} && defined $args->{NewPassword};
 
    return PasswordChange => {
@@ -252,8 +258,10 @@ sub req_PASSWORDCHANGE {
 
 sub resp_PASSWORDCHANGE {
    my ($self, $resphash, $reqhash) = (shift, shift);
-   die "invalid change password response" unless defined $resphash->{ChangePasswordResponse};
-   return $resphash->{ChangePasswordResponse};
+   return "invalid change password response"
+      unless defined $resphash->{ChangePasswordResponse}
+         && defined $resphash->{ChangePasswordResponse}->{Customer};
+   $resphash->{ChangePasswordResponse}->{Customer};
 }
 
 sub req_PREQUAL {
@@ -273,10 +281,14 @@ sub req_PREQUAL {
 
 sub resp_PREQUAL {
     my ($self, $resphash, $reqhash) = (shift, shift);
-    die "invalid prequal response" unless defined $resphash->{PreQualResponse};
+    return "invalid prequal response" unless defined $resphash->{PreQualResponse};
     return $resphash->{PreQualResponse};
 }
 
+sub orderTypes {
+  @orderType;
+}
+
 sub AUTOLOAD {
     my $self = shift;
    
@@ -309,25 +321,33 @@ sub AUTOLOAD {
    
     # XXX: validate against their schema to ensure we're not sending invalid XML?
 
-    print "DEBUG REQUEST\n\tHASH:\n ".Dumper($reqhash)."\n\tXML:\n $reqxml \n\n" if $self->{debug};
+    warn "DEBUG REQUEST\n\tHASH:\n ".Dumper($reqhash)."\n\tXML:\n $reqxml \n\n"
+       if $self->{debug};
     
     my $ua = LWP::UserAgent->new;
 
-    die "posting disabled for testing" if $self->{reqpreviewonly};
+    return "posting disabled for testing" if $self->{reqpreviewonly};
 
     my $resp = $ua->post($URL, Content_Type => 'text/xml', Content => $reqxml);
-    die $resp->status_line unless $resp->is_success;
+    return "invalid HTTP response from Ikano: " . $resp->status_line
+       unless $resp->is_success;
     my $respxml = $resp->decoded_content;
+
+    $xs = new Net::Ikano::XMLUtil(RootName => undef, SuppressEmpty => '',
+       ForceArray => [ 'Address', 'Product', 'StaticIp', 'OrderNotes' ] );
     my $resphash = $xs->XMLin($respxml);
 
-    print "DEBUG RESPONSE\n\tHASH:\n ".Dumper($resphash)."\n\tXML:\n $respxml" if $self->{debug};
+    warn "DEBUG RESPONSE\n\tHASH:\n ".Dumper($resphash)."\n\tXML:\n $respxml"
+       if $self->{debug};
 
     # XXX: validate against their schema to ensure they didn't send us invalid XML?
 
-    die "invalid response" unless defined $resphash->{responseid} 
-       && defined $resphash->{version} && defined $resphash->{type};
+    return "invalid response received from Ikano" 
+       unless defined $resphash->{responseid} && defined $resphash->{version}
+           && defined $resphash->{type};
 
-    die "FAILURE response received: ".$resphash->{FailureResponse}->{FailureMessage}
+    return "FAILURE response received from Ikano: " 
+       . $resphash->{FailureResponse}->{FailureMessage} 
        if $resphash->{type} eq 'FAILURE';
 
     my $validRespTypes = {
@@ -340,7 +360,7 @@ sub AUTOLOAD {
        'CUSTOMERLOOKUP' => qw( CUSTOMERLOOKUP ),
     };
 
-    die "invalid response type for request type"
+    return "invalid response type ".$resphash->{type}." for request type $cmd"
        unless grep( $_ eq $resphash->{type}, $validRespTypes->{$cmd});
 
     return $self->$respsub($resphash,$reqhash);