| 1 | package Net::Moip::V2; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use IO::Socket::SSL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Furl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Net::Moip::V2::Endpoint; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = "0.05"; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | my $JSON = JSON::MaybeXS->new->utf8; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has 'ua', is => 'ro', default => sub { | ||||||||||||||||||||||||||||
| 17 | 2 | ▶ | Furl->new( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | agent => "Net-Moip-V2/$VERSION", | ||||||||||||||||||||||||||||
| 19 | timeout => 15, | ||||||||||||||||||||||||||||
| 20 | max_redirects => 3, | ||||||||||||||||||||||||||||
| 21 | # <perigrin> "SSL Wants a read first" I think is suggesting you | ||||||||||||||||||||||||||||
| 22 | # haven't read OpenSSL a bedtime story in too long and perhaps | ||||||||||||||||||||||||||||
| 23 | # it's feeling neglected and lonely? | ||||||||||||||||||||||||||||
| 24 | # see also: https://metacpan.org/pod/IO::Socket::SSL#SNI-Support | ||||||||||||||||||||||||||||
| 25 | # https://metacpan.org/pod/Furl#FAQ | ||||||||||||||||||||||||||||
| 26 | # https://rt.cpan.org/Public/Bug/Display.html?id=86684 | ||||||||||||||||||||||||||||
| 27 | ssl_opts => { | ||||||||||||||||||||||||||||
| 28 | SSL_verify_mode => SSL_VERIFY_PEER(), | ||||||||||||||||||||||||||||
| 29 | # forcing version yields better error message: | ||||||||||||||||||||||||||||
| 30 | SSL_version => 'TLSv1_2', | ||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | 2 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | has 'token', is => 'ro', required => 1; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | has 'key', is => 'ro', required => 1; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | has 'client_id', is => 'ro'; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | has 'client_secret', is => 'ro'; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | has 'access_token', is => 'rw'; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | has 'api_url', ( | |||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | writer => '_set_api_url', | ||||||||||||||||||||||||||||
| 49 | default => 'https://api.moip.com.br/v2' | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | has 'oauth_url', ( | |||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||
| 54 | writer => '_set_oauth_url', | ||||||||||||||||||||||||||||
| 55 | default => 'https://connect.moip.com.br' | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | has 'sandbox', ( | ||||||||||||||||||||||||||||
| 60 | is => 'rw', | ||||||||||||||||||||||||||||
| 61 | default => 0, | ||||||||||||||||||||||||||||
| 62 | trigger => sub { | ||||||||||||||||||||||||||||
| 63 | 4 | ▶ | my ($self, $sandbox) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | $self->_set_api_url( $sandbox | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | ? 'https://sandbox.moip.com.br/v2' | ||||||||||||||||||||||||||||
| 66 | : 'https://api.moip.com.br/v2' | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | 4 | ▶ | $self->_set_oauth_url( $sandbox | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | ? 'https://connect-sandbox.moip.com.br' | ||||||||||||||||||||||||||||
| 70 | : 'https://connect.moip.com.br' | ||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 2 | ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | has '_basic_auth_token', is => 'ro', lazy => 1, default => sub { | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 1 | 'Basic '.MIME::Base64::encode_base64( $self->token .':'. $self->key, ''); | |||||||||||||||||||||||||||
| 78 | 2 | }; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub build_authorization_url { | ||||||||||||||||||||||||||||
| 81 | 1 | ▶ | my ($self, $redirect_uri, $scope) = @_; | ||||||||||||||||||||||||||
|
Subroutine
build_authorization_url: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | die 'Method signature is: build_authorization_url($redirect_uri, $scope)' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$redirect_uri and $scope
MC/DC0%0/2
$redirect_uri and $scope
$redirect_uri
$scope
Truth table33%1/3
A and B
A= $redirect_uriB= $scope
| |||||||||||||||||||||||||||||
| 83 | unless $redirect_uri && $scope; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | my $url = URI->new($self->oauth_url.'/oauth/authorize'); | |||||||||||||||||||||||||||
| 86 | $url->query_form( | ||||||||||||||||||||||||||||
| 87 | response_type => 'code', | ||||||||||||||||||||||||||||
| 88 | client_id => $self->client_id, | ||||||||||||||||||||||||||||
| 89 | redirect_uri => $redirect_uri, | ||||||||||||||||||||||||||||
| 90 | 1 | scope => join(',', map { uc } @$scope) | |||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | 1 | $url; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub request_access_token { | ||||||||||||||||||||||||||||
| 96 | 2 | ▶ | my ($self, $redirect_uri, $code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
request_access_token: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 2 | ▶ | die 'Method signature is: request_access_token($redirect_uri, $code)' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$redirect_uri and $code
MC/DC0%0/2
$redirect_uri and $code
$redirect_uri
$code
Truth table33%1/3
A and B
A= $redirect_uriB= $code
| |||||||||||||||||||||||||||||
| 98 | unless $redirect_uri && $code; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | my $uri = URI->new($self->oauth_url.'/oauth/token'); | |||||||||||||||||||||||||||
| 101 | 2 | $uri->query_form( | |||||||||||||||||||||||||||
| 102 | client_id => $self->client_id, | ||||||||||||||||||||||||||||
| 103 | client_secret => $self->client_secret, | ||||||||||||||||||||||||||||
| 104 | grant_type => 'authorization_code', | ||||||||||||||||||||||||||||
| 105 | redirect_uri => $redirect_uri, | ||||||||||||||||||||||||||||
| 106 | code => $code | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | my ($url, $body) = $uri->as_string =~ /(.*?)\?(.*)/; | |||||||||||||||||||||||||||
| 110 | 2 | my $res = $self->ua->post($url, [ | |||||||||||||||||||||||||||
| 111 | 'Content-Type' => 'application/x-www-form-urlencoded', | ||||||||||||||||||||||||||||
| 112 | 'Authorization' => $self->_basic_auth_token, | ||||||||||||||||||||||||||||
| 113 | 'Cache-Control' => 'no-cache' | ||||||||||||||||||||||||||||
| 114 | ], $body); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | return { error => $res->status_line } if $res->code >= 500; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | $JSON->decode($res->content); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub endpoint { | ||||||||||||||||||||||||||||
| 124 | 8 | ▶ | my ($self, $path, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
endpoint: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 8 | ▶ | die "Syntax: moip->endpoint(<name>)" unless $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | Net::Moip::V2::Endpoint->new( | ||||||||||||||||||||||||||||
| 127 | 56 | (map { $_ => $self->$_ } qw/ ua api_url token key client_id client_secret access_token /), | |||||||||||||||||||||||||||
| 128 | 8 | ▶ | %{ $params || {} }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | path => $path, | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub get { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my $endpoint = shift; | |||||||||||||||||||||||||||
| 136 | 0 | $self->endpoint($endpoint)->get(@_); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub post { | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my $endpoint = shift; | |||||||||||||||||||||||||||
| 142 | 0 | $self->endpoint($endpoint)->post(@_); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub decode_json { | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
decode_json: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 0 | $JSON->decode($_[0]); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 2 | 1; | |||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||