| 1 | package OpenID::PayPal::LIPP; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use 5.008_005; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | our $VERSION = '0.02'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 3 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 3 | ▶ | use URI::QueryParam; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 3 | ▶ | use HTTP::Request::Common; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 3 | ▶ | use Convert::Base64; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 3 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 3 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 3 | has client_id => ( is => 'ro', required => 1 ); | |||||||||||||
| 19 | 3 | has client_secret => ( is => 'ro', required => 1 ); | |||||||||||||
| 20 | 3 | has account => ( is => 'ro', required => 1 ); | |||||||||||||
| 21 | 3 | has redirect_uri => ( is => 'ro', required => 1 ); | |||||||||||||
| 22 | 2 | ▶ | has mode => ( is => 'ro', default => sub { 'sandbox' }, isa => sub { die 'Must be live or sandbox' unless $_[0] =~ m/^live$|^sandbox$/; } ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||
| 23 | 2 | ▶ | has scope => ( is => 'ro', default => sub { [ qw(openid email) ]; } ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 3 | has paypal_openid_endpoint => ( is => 'lazy' ); | |||||||||||||
| 25 | 3 | has paypal_auth_token_service_endpoint => ( is => 'lazy' ); | |||||||||||||
| 26 | 3 | has paypal_auth_user_profile_end_point => ( is => 'lazy' ); | |||||||||||||
| 27 | 1 | ▶ | has logger => ( is => 'ro', default => sub{ sub{}; }, isa => sub { die 'Logger has to be a function' unless ref $_[0] eq 'CODE' } ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Subroutine
__ANON__: called CC=1 SCAR=0.1
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | 3 | ▶ | sub _log { | ||||||||||||
|
Subroutine
_log: called CC=2 SCAR=7.1
| |||||||||||||||
| 30 | 3 | my $self = shift; | |||||||||||||
| 31 | 3 | ▶ | if( defined $self->logger ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 3 | $self->logger->( @_ ); | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 2 | ▶ | sub _build_paypal_openid_endpoint { | ||||||||||||
|
Subroutine
_build_paypal_openid_endpoint: called CC=2 SCAR=7.6
| |||||||||||||||
| 37 | 2 | my $self = shift; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 2 | ▶ | if( $self->mode eq 'live' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 0 | return 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'; | |||||||||||||
| 41 | } else { | ||||||||||||||
| 42 | 2 | return 'https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | } | ||||||||||||||
| 45 | |||||||||||||||
| 46 | 1 | ▶ | sub _build_paypal_auth_token_service_endpoint { | ||||||||||||
|
Subroutine
_build_paypal_auth_token_service_endpoint: called CC=2 SCAR=7.6
| |||||||||||||||
| 47 | 1 | my $self = shift; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 1 | ▶ | if( $self->mode eq 'live' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 0 | return 'https://api.paypal.com/v1/identity/openidconnect/tokenservice'; | |||||||||||||
| 51 | } else { | ||||||||||||||
| 52 | 1 | return 'https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice'; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 1 | ▶ | sub _build_paypal_auth_user_profile_end_point { | ||||||||||||
|
Subroutine
_build_paypal_auth_user_profile_end_point: called CC=2 SCAR=7.6
| |||||||||||||||
| 57 | 1 | my $self = shift; | |||||||||||||
| 58 | |||||||||||||||
| 59 | 1 | ▶ | if( $self->mode eq 'live' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 0 | return 'https://api.paypal.com/v1/identity/openidconnect/userinfo'; | |||||||||||||
| 61 | } else { | ||||||||||||||
| 62 | 1 | return 'https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo'; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 3 | ▶ | sub login_url { | ||||||||||||
|
Subroutine
login_url: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 67 | 3 | my $self = shift; | |||||||||||||
| 68 | 3 | my $state = shift; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 3 | my $login_url = URI->new( $self->paypal_openid_endpoint ); | |||||||||||||
| 71 | 3 | $login_url->query_param( client_id => $self->client_id ); | |||||||||||||
| 72 | 3 | $login_url->query_param( response_type => 'code' ); | |||||||||||||
| 73 | 3 | $login_url->query_param( scope => join( ' ', @{$self->scope}) ); | |||||||||||||
| 74 | 3 | $login_url->query_param( redirect_uri => $self->redirect_uri ); | |||||||||||||
| 75 | 3 | ▶ | $login_url->query_param( state => $state ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | if defined $state; | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 3 | $self->_log( "Login url is : ".$login_url->as_string ); | |||||||||||||
| 79 | |||||||||||||||
| 80 | 3 | return $login_url->as_string; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 1 | ▶ | sub exchange_code { | ||||||||||||
|
Subroutine
exchange_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 84 | 1 | my ($self, $code) = @_; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 1 | my $post_data = { | |||||||||||||
| 87 | grant_type => 'authorization_code', | ||||||||||||||
| 88 | code => $code, | ||||||||||||||
| 89 | redirect_uri => $self->redirect_uri, | ||||||||||||||
| 90 | }; | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 1 | my $encoded_header = encode_base64($self->client_id . ":" . $self->client_secret ); | |||||||||||||
| 93 | 1 | my $data_json = $self->_post( | |||||||||||||
| 94 | $self->paypal_auth_token_service_endpoint, | ||||||||||||||
| 95 | $post_data, | ||||||||||||||
| 96 | { Authorization => "Basic $encoded_header" } | ||||||||||||||
| 97 | ); | ||||||||||||||
| 98 | 1 | my $data_hash = decode_json($data_json); | |||||||||||||
| 99 | |||||||||||||||
| 100 | return { | ||||||||||||||
| 101 | access_token => $data_hash->{access_token}, | ||||||||||||||
| 102 | refresh_token => $data_hash->{refresh_token}, | ||||||||||||||
| 103 | 1 | }; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | ▶ | sub refresh_token { | ||||||||||||
|
Subroutine
refresh_token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 107 | 0 | my ($self, $refresh_token) = @_; | |||||||||||||
| 108 | |||||||||||||||
| 109 | my $post_data = { | ||||||||||||||
| 110 | grant_type => 'refresh_token', | ||||||||||||||
| 111 | refresh_token => $refresh_token, | ||||||||||||||
| 112 | 0 | scope => join( ' ', @{$self->scope}), | |||||||||||||
| 113 | }; | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | my $encoded_header = encode_base64($self->client_id . ":" . $self->client_secret ); | |||||||||||||
| 116 | 0 | my $data_json = $self->_post( | |||||||||||||
| 117 | $self->paypal_auth_token_service_endpoint, | ||||||||||||||
| 118 | $post_data, | ||||||||||||||
| 119 | { Authorization => "Basic $encoded_header" } | ||||||||||||||
| 120 | ); | ||||||||||||||
| 121 | 0 | my $data_hash = decode_json($data_json); | |||||||||||||
| 122 | |||||||||||||||
| 123 | return { | ||||||||||||||
| 124 | access_token => $data_hash->{access_token}, | ||||||||||||||
| 125 | 0 | refresh_token => $refresh_token, | |||||||||||||
| 126 | }; | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | 1 | ▶ | sub get_user_details { | ||||||||||||
|
Subroutine
get_user_details: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||
| 130 | 1 | my ( $self, %params ) = @_; | |||||||||||||
| 131 | |||||||||||||||
| 132 | 1 | my $token = undef; | |||||||||||||
| 133 | 1 | ▶ | if( exists $params{authorization_code} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 134 | 0 | $token = $self->exchange_code( $params{authorization_code} )->{access_token}; | |||||||||||||
| 135 | } elsif( exists $params{access_token} ) { | ||||||||||||||
| 136 | 1 | $token = $params{access_token}; | |||||||||||||
| 137 | } else { | ||||||||||||||
| 138 | 0 | croak "Provide authorization code or access token"; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 1 | my $post_params = { | |||||||||||||
| 142 | schema => "openid", | ||||||||||||||
| 143 | access_token => $token, | ||||||||||||||
| 144 | }; | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 1 | my $customer_data_json = $self->_get( $self->paypal_auth_user_profile_end_point, $post_params ); | |||||||||||||
| 147 | 1 | my $customer_data_hash = decode_json( $customer_data_json ); | |||||||||||||
| 148 | |||||||||||||||
| 149 | 1 | return $customer_data_hash; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 2 | ▶ | sub _user_agent { | ||||||||||||
|
Subroutine
_user_agent: called CC=1 SCAR=0.0
| |||||||||||||||
| 153 | 2 | return LWP::UserAgent->new(); | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | |||||||||||||||
| 156 | sub _post | ||||||||||||||
| 157 | 1 | ▶ | { | ||||||||||||
|
Subroutine
_post: called CC=5 SCAR=18.9
| |||||||||||||||
| 158 | 1 | my ( $self, $url, $post, $headers ) = @_; | |||||||||||||
| 159 | |||||||||||||||
| 160 | 1 | my $request = HTTP::Request::Common::POST( $url, Content => [ %$post ]); | |||||||||||||
| 161 | 1 | ▶ | if( defined $headers ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | 1 | ▶ | while( my ($header, $value) = each %$headers ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 1 | $request->header( $header => $value ); | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 1 | my $response = $self->_user_agent->request( $request ); | |||||||||||||
| 168 | |||||||||||||||
| 169 | 1 | ▶ | if ( $response->is_error ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 170 | { | ||||||||||||||
| 171 | 0 | $self->_log( "Post request failed, url : " . $url . " , parameters: " ); | |||||||||||||
| 172 | 0 | ▶ | while( my ($key, $value) = each %$post ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 0 | $self->_log( "$key => $value" ); | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | 0 | croak "Error calling PayPal, PayPal Response : " . $response->content; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | 1 | return $response->content; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | |||||||||||||||
| 181 | |||||||||||||||
| 182 | sub _get | ||||||||||||||
| 183 | 1 | ▶ | { | ||||||||||||
|
Subroutine
_get: called CC=4 SCAR=14.4
| |||||||||||||||
| 184 | 1 | my ( $self, $url, $params ) = @_; | |||||||||||||
| 185 | |||||||||||||||
| 186 | 1 | my $uri = URI->new( $url ); | |||||||||||||
| 187 | |||||||||||||||
| 188 | 1 | ▶ | if( defined $params ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 189 | 1 | ▶ | while( my ($param, $value) = each %$params ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | 2 | $uri->query_param( $param => $value ); | |||||||||||||
| 191 | } | ||||||||||||||
| 192 | } | ||||||||||||||
| 193 | |||||||||||||||
| 194 | 1 | my $request = HTTP::Request->new( 'GET', $uri->as_string ); | |||||||||||||
| 195 | |||||||||||||||
| 196 | 1 | my $response = $self->_user_agent->request( $request ); | |||||||||||||
| 197 | 1 | ▶ | if ( $response->is_error ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 198 | { | ||||||||||||||
| 199 | 0 | $self->_log( "Get request failed, url : " . $uri->as_string ); | |||||||||||||
| 200 | 0 | croak "Error calling PayPal, PayPal Response : " . $response->content; | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | |||||||||||||||
| 203 | 1 | return $response->content; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | 3 | ▶ | no Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 207 | 3 | 1; | |||||||||||||
| 208 | __END__ | ||||||||||||||