| 1 | package PagSeguro::API::Checkout; | ||
| 2 | use base 'PagSeguro::API::Base'; | ||
| 3 | |||
| 4 | use Carp; | ||
| 5 | use Try::Tiny; | ||
| 6 | use XML::Simple; | ||
| 7 | |||
| 8 | # constructor | ||
| 9 | sub new { | ||
| 10 | my $class = shift; | ||
| 11 | my $args = (@_ % 2 == 0)? {@_} : shift; | ||
| 12 | |||
| 13 | return bless { | ||
| 14 | _code => undef, | ||
| 15 | _context => ($args->{context})? $args->{context} : $args, | ||
| 16 | _transaction => undef, | ||
| 17 | }, $class; | ||
| 18 | } | ||
| 19 | |||
| 20 | # accessors | ||
| 21 | sub _context { | ||
| 22 | return $_[0]->{_context} if $_[0]->{_context}; | ||
| 23 | } | ||
| 24 | sub code { | ||
| 25 | return $_[0]->{_code} if $_[0]->{_code}; | ||
| 26 | } | ||
| 27 | |||
| 28 | # methods | ||
| 29 | sub send { | ||
| 30 | my $self = shift; | ||
| 31 | my $args = (@_ % 2 == 0) ? {@_} : undef; | ||
| 32 | |||
| 33 | $self->{_code} = undef; | ||
| 34 | |||
| 35 | if ($args) { | ||
| 36 | |||
| 37 | # parse and return by file | ||
| 38 | return XMLin( $args->{file} ) if $args->{file}; | ||
| 39 | |||
| 40 | $args->{email} = $self->_context->email; | ||
| 41 | $args->{token} = $self->_context->token; | ||
| 42 | $args->{currency} = $args->{currency} || 'BRL'; | ||
| 43 | $args->{shippingType} = $args->{shippingType} || '3'; | ||
| 44 | |||
| 45 | # getting response | ||
| 46 | my $uri = $self->_checkout_uri( $args ); | ||
| 47 | my $response = $self->post( $uri, $args ); | ||
| 48 | |||
| 49 | # debug | ||
| 50 | warn "[Debug] Checkout Response: $response\n" | ||
| 51 | if $ENV{PAGSEGURO_API_DEBUG}; | ||
| 52 | |||
| 53 | my $xml = XMLin($response); | ||
| 54 | |||
| 55 | $self->{_code} = $xml->{code} if $xml->{code}; | ||
| 56 | return $xml; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | # error | ||
| 60 | croak "Error: invalid paramether bound"; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | sub payment_url { | ||
| 65 | my ($self, $code) = @_; | ||
| 66 | |||
| 67 | $code = $self->code unless $code; | ||
| 68 | |||
| 69 | my $uri = join '', | ||
| 70 | ( | ||
| 71 | $self->_context->resource($ENV{PAGSEGURO_API_SANDBOX} | ||
| 72 | ? 'SANDBOX_CHECKOUT_PAYMENT' | ||
| 73 | : 'CHECKOUT_PAYMENT'), | ||
| 74 | "?code=", $code | ||
| 75 | ); | ||
| 76 | |||
| 77 | warn "[Debug] URI: $uri\n" if $ENV{PAGSEGURO_API_DEBUG}; | ||
| 78 | return $uri; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _checkout_uri { | ||
| 82 | my ( $self, $args ) = @_; | ||
| 83 | |||
| 84 | # add email and token | ||
| 85 | $args->{email} = $args->{email} || $self->_context->email; | ||
| 86 | $args->{token} = $args->{token} || $self->_context->token; | ||
| 87 | |||
| 88 | # build query string | ||
| 89 | my $query_string = join '&', map { "$_=$args->{$_}" } keys %$args; | ||
| 90 | |||
| 91 | my $uri = join '', | ||
| 92 | ( | ||
| 93 | $self->_context->resource( | ||
| 94 | ( $ENV{PAGSEGURO_API_SANDBOX} ? 'SANDBOX_URI' : 'BASE_URI' ) | ||
| 95 | ), | ||
| 96 | $self->_context->resource('CHECKOUT'), | ||
| 97 | "?", | ||
| 98 | $query_string | ||
| 99 | ); | ||
| 100 | |||
| 101 | warn "[Debug] URI: $uri\n" if $ENV{PAGSEGURO_API_DEBUG}; | ||
| 102 | return $uri; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; |