line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Business::CPI::Gateway::MercadoPago; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Business::CPI's Mercado Pago driver |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
31745
|
use Moo; |
|
2
|
|
|
|
|
39057
|
|
|
2
|
|
|
|
|
12
|
|
6
|
2
|
|
|
2
|
|
4322
|
use Carp; |
|
2
|
|
|
|
|
17
|
|
|
2
|
|
|
|
|
171
|
|
7
|
2
|
|
|
2
|
|
1907
|
use URI; |
|
2
|
|
|
|
|
13168
|
|
|
2
|
|
|
|
|
67
|
|
8
|
2
|
|
|
2
|
|
15664
|
use Data::Dumper; |
|
2
|
|
|
|
|
16193
|
|
|
2
|
|
|
|
|
184
|
|
9
|
2
|
|
|
2
|
|
28185
|
use LWP::UserAgent (); |
|
2
|
|
|
|
|
118063
|
|
|
2
|
|
|
|
|
65
|
|
10
|
2
|
|
|
2
|
|
2171
|
use JSON; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
extends 'Business::CPI::Gateway::Base'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.102'; # VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has '+currency' => ( default => sub { 'BRL' } ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has _base_url => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
default => sub { 'https://api.mercadolibre.com' }, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has [ 'token', 'back_url' ] => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
required => 1 |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has _user_agent_name => ( |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
default => sub { |
31
|
|
|
|
|
|
|
my $base = 'Business::CPI::Gateway::MercadoPago'; |
32
|
|
|
|
|
|
|
my $version = __PACKAGE__->VERSION; |
33
|
|
|
|
|
|
|
return $version ? "$base/$version" : $base; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has user_agent => ( |
38
|
|
|
|
|
|
|
is => 'lazy', |
39
|
|
|
|
|
|
|
default => sub { |
40
|
|
|
|
|
|
|
my $self = shift; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
43
|
|
|
|
|
|
|
$ua->agent( $self->_user_agent_name ); |
44
|
|
|
|
|
|
|
$ua->default_header( 'Accept' => 'application/json' ); |
45
|
|
|
|
|
|
|
$ua->default_header( 'Content-Type' => 'application/json' ); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
return $ua; |
48
|
|
|
|
|
|
|
}, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has access_token => ( |
52
|
|
|
|
|
|
|
is => 'ro', |
53
|
|
|
|
|
|
|
init_arg => undef, |
54
|
|
|
|
|
|
|
lazy => 1, |
55
|
|
|
|
|
|
|
builder => '_builder_access_token' |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _builder_access_token { |
59
|
|
|
|
|
|
|
my $self = shift; |
60
|
|
|
|
|
|
|
my $auth_url = $self->_build_uri('/oauth/token'); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my $ua = $self->user_agent; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
$ua->default_header( |
65
|
|
|
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded' ); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $r = $ua->post( |
68
|
|
|
|
|
|
|
$auth_url, |
69
|
|
|
|
|
|
|
{ |
70
|
|
|
|
|
|
|
grant_type => 'client_credentials', |
71
|
|
|
|
|
|
|
client_id => $self->receiver_email, |
72
|
|
|
|
|
|
|
client_secret => $self->token |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
die "Couldn't connect to '$auth_url': " . $r->status_line |
76
|
|
|
|
|
|
|
if $r->is_error; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $json = from_json( $r->content ); |
79
|
|
|
|
|
|
|
my $access_token = $json->{access_token}; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
die "Coundn't retried access_token" unless $access_token; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
return $access_token; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _build_uri { |
87
|
|
|
|
|
|
|
my ( $self, $path, $info ) = @_; |
88
|
|
|
|
|
|
|
my $uri = URI->new( $self->_base_url . $path ); |
89
|
|
|
|
|
|
|
return $uri->as_string; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _make_json { |
93
|
|
|
|
|
|
|
my ( $self, $info ) = @_; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $items; |
96
|
|
|
|
|
|
|
for my $item ( @{ $info->{items} } ) { |
97
|
|
|
|
|
|
|
my $item_ref = { |
98
|
|
|
|
|
|
|
id => $item->id, |
99
|
|
|
|
|
|
|
title => $item->description, |
100
|
|
|
|
|
|
|
description => $item->description, |
101
|
|
|
|
|
|
|
quantity => $item->quantity, |
102
|
|
|
|
|
|
|
unit_price => $item->price * 1, |
103
|
|
|
|
|
|
|
currency_id => $self->currency, |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# picture_url (?) |
106
|
|
|
|
|
|
|
}; |
107
|
|
|
|
|
|
|
push( @{$items}, $item_ref ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
my $request = { |
111
|
|
|
|
|
|
|
items => $items, |
112
|
|
|
|
|
|
|
external_reference => $info->{payment_id}, |
113
|
|
|
|
|
|
|
payer => { |
114
|
|
|
|
|
|
|
name => $info->{buyer}->name, |
115
|
|
|
|
|
|
|
email => $info->{buyer}->email |
116
|
|
|
|
|
|
|
}, |
117
|
|
|
|
|
|
|
back_urls => { |
118
|
|
|
|
|
|
|
success => $self->back_url, |
119
|
|
|
|
|
|
|
failure => $self->back_url, |
120
|
|
|
|
|
|
|
pending => $self->back_url |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
}; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
return to_json( $request, { utf8 => 1, pretty => 1 } ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub get_checkout_code { |
128
|
|
|
|
|
|
|
my ( $self, $info ) = @_; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $ua = $self->user_agent; |
131
|
|
|
|
|
|
|
my $url = $self->_build_uri( |
132
|
|
|
|
|
|
|
'/checkout/preferences?access_token=' . $self->access_token ); |
133
|
|
|
|
|
|
|
my $json = $self->_make_json($info); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my $req = HTTP::Request->new( 'POST', $url ); |
136
|
|
|
|
|
|
|
$req->content_type('application/json'); |
137
|
|
|
|
|
|
|
$req->content($json); |
138
|
|
|
|
|
|
|
my $res = $ua->request($req); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
die $res->status_line unless $res->is_success; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
my $content = $res->content; |
143
|
|
|
|
|
|
|
$json = from_json($content); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $init_point = $json->{'init_point'}; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$self->checkout_url($init_point); |
148
|
|
|
|
|
|
|
return $init_point; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
__END__ |