| 1 | package PagSeguro::API::Notification; | ||
| 2 | use base 'PagSeguro::API::Base'; | ||
| 3 | |||
| 4 | use LWP::Simple; | ||
| 5 | use XML::Simple; | ||
| 6 | |||
| 7 | # constructor | ||
| 8 | sub new { | ||
| 9 | my $class = shift; | ||
| 10 | my %args = @_ if ( @_ % 2 ) == 0; | ||
| 11 | |||
| 12 | return bless { | ||
| 13 | email => $args{email} || undef, | ||
| 14 | token => $args{token} || undef, | ||
| 15 | |||
| 16 | _notification => undef, | ||
| 17 | }, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | # methods | ||
| 21 | sub load { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | my %args; | ||
| 25 | $args{file} = $_[1] if $_[0] && $_[1]; | ||
| 26 | |||
| 27 | if ( %args ) { | ||
| 28 | # parse and return by file | ||
| 29 | return XMLin( $args{file} ) if $args{file}; | ||
| 30 | } | ||
| 31 | |||
| 32 | my $response = get $self->_code_uri( $_[0] ) || ''; | ||
| 33 | |||
| 34 | # debug | ||
| 35 | warn "[Debug] Notification Response: $response\n" | ||
| 36 | if $ENV{PAGSEGURO_API_DEBUG}; | ||
| 37 | |||
| 38 | return XMLin($response); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _code_uri { | ||
| 42 | my ( $self, $code ) = @_; | ||
| 43 | |||
| 44 | my $uri = join '', | ||
| 45 | ( | ||
| 46 | $self->resource( | ||
| 47 | ( $ENV{PAGSEGURO_API_SANDBOX} ? 'SANDBOX_URI' : 'BASE_URI' ) | ||
| 48 | ), | ||
| 49 | $self->resource('TRANSACTION'), | ||
| 50 | $self->resource('NOTIFICATIONS'), | ||
| 51 | "${code}", | ||
| 52 | "?email=" . $self->{email}, | ||
| 53 | "&token=" . $self->{token}, | ||
| 54 | ); | ||
| 55 | |||
| 56 | warn "[Debug] URI: $uri\n" if $ENV{PAGSEGURO_API_DEBUG}; | ||
| 57 | return $uri; | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | __END__ |