| 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 | _context => ($args->{context})? $args->{context} : $args, | ||
| 14 | _notification => undef, | ||
| 15 | }, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | # methods | ||
| 19 | sub load { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | my %args; | ||
| 23 | $args{file} = $_[1] if $_[0] && $_[1]; | ||
| 24 | |||
| 25 | if ( %args ) { | ||
| 26 | # parse and return by file | ||
| 27 | return XMLin( $args{file} ) if $args{file}; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $response = get $self->_code_uri( $_[0] ) || ''; | ||
| 31 | |||
| 32 | # debug | ||
| 33 | warn "[Debug] Notification Response: $response\n" | ||
| 34 | if $ENV{PAGSEGURO_API_DEBUG}; | ||
| 35 | |||
| 36 | return XMLin($response); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _code_uri { | ||
| 40 | my ( $self, $code ) = @_; | ||
| 41 | |||
| 42 | my $uri = join '', | ||
| 43 | ( | ||
| 44 | $self->_context->resource( | ||
| 45 | ( $ENV{PAGSEGURO_API_SANDBOX} ? 'SANDBOX_URI' : 'BASE_URI' ) | ||
| 46 | ), | ||
| 47 | $self->_context->resource('TRANSACTION'), | ||
| 48 | $self->_context->resource('NOTIFICATIONS'), | ||
| 49 | "${code}", | ||
| 50 | "?email=" . $self->_context->email, | ||
| 51 | "&token=" . $self->_context->token, | ||
| 52 | ); | ||
| 53 | |||
| 54 | warn "[Debug] URI: $uri\n" if $ENV{PAGSEGURO_API_DEBUG}; | ||
| 55 | return $uri; | ||
| 56 | } | ||
| 57 | |||
| 58 | 1; | ||
| 59 | __END__ |