| 1 | # vim: sw=4 ts=4 ft=perl | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | package # hide from pause | ||||||||||||||||||||||||||||||||||||
| 4 | WebService::Braintree::HTTP; | ||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use 5.010_001; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use strictures 1; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 22 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use DateTime; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 22 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 22 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | 22 | ▶ | use WebService::Braintree::Xml qw(hash_to_xml xml_to_hash); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | 22 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | 22 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | 22 | ▶ | use constant CLIENT_VERSION => $WebService::Braintree::VERSION || 'development'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$WebService::Braintree::VERSION || 'development'
MC/DC0%0/1
$WebService::Braintree::VERSION || 'development'
$WebService::Braintree::VERSION
Truth table50%1/2
A
A= $WebService::Braintree::VERSION
| |||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | has 'config' => (is => 'ro', default => sub { WebService::Braintree->configuration }); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | 22 | my $LF = "\r\n"; | |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub post { | ||||||||||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 26 | 0 | $self->make_request(POST => @_); | |||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub put { | ||||||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 31 | 0 | $self->make_request(PUT => @_); | |||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 36 | 0 | $self->make_request(GET => @_); | |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 41 | 0 | $self->make_request(DELETE => @_); | |||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub make_request { | ||||||||||||||||||||||||||||||||||
|
Subroutine
make_request: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 45 | 0 | my ($self, $verb, $path, $params, $file) = @_; | |||||||||||||||||||||||||||||||||||
| 46 | 0 | my $request = HTTP::Request->new($verb => $self->config->base_merchant_url . $path); | |||||||||||||||||||||||||||||||||||
| 47 | 0 | $request->headers->authorization_basic($self->config->public_key, $self->config->private_key); | |||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($file) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $boundary = DateTime->now->strftime('%Q'); | |||||||||||||||||||||||||||||||||||
| 51 | 0 | $request->content_type("multipart/form-data; boundary=${boundary}"); | |||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | my @form_params = map { | ||||||||||||||||||||||||||||||||||||
| 54 | 0 | $self->add_form_field($_, $params->{$_}) | |||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | } keys %{$params // {}}; | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$params // {}
MC/DC0%0/1
$params // {}
$params
Truth table0%0/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||
| 56 | 0 | push @form_params, $self->add_file_part(file => $file); | |||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | $request->content( | ||||||||||||||||||||||||||||||||||||
| 59 | join("", ( | ||||||||||||||||||||||||||||||||||||
| 60 | 0 | map { "--${boundary}${LF}${_}" } @form_params | |||||||||||||||||||||||||||||||||||
| 61 | )) . "--${boundary}--" | ||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||
| 64 | elsif ($params) { | ||||||||||||||||||||||||||||||||||||
| 65 | 0 | $request->content_type("text/xml; charset=utf-8"); | |||||||||||||||||||||||||||||||||||
| 66 | 0 | $request->content(hash_to_xml($params)); | |||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||
| 69 | 0 | $request->header("X-ApiVersion" => $self->config->api_version); | |||||||||||||||||||||||||||||||||||
| 70 | 0 | $request->header("environment" => $self->config->environment); | |||||||||||||||||||||||||||||||||||
| 71 | 0 | $request->header("User-Agent" => "Braintree Perl Module " . CLIENT_VERSION ); | |||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $agent = LWP::UserAgent->new(ssl_opts => {SSL_version => 'TLSv12:!SSLv2:!SSLv3:!TLSv1:!TLSv11'}); | |||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | warn Dumper $request if $ENV{WEBSERVICE_BRAINTREE_DEBUG}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $response; | |||||||||||||||||||||||||||||||||||
| 77 | 0 | my $tries = 1; | |||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | while ($tries < 5) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | 0 | $response = $agent->request($request); | |||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($response->code eq '500' && $response->message =~ /Connection timed out/i) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->code eq 500 and $response->message =~ /Connection timed out/i
MC/DC0%0/2
$response->code eq 500 and $response->message =~ /Connection timed out/i
$response->code eq 500
$response->message =~ /Connection timed out/i
Truth table0%0/3
A and B
A= $response->code eq 500B= $response->message =~ /Connection timed out/i
| |||||||||||||||||||||||||||||||||||||
| 81 | 0 | warn "Retrying timed-out connection after try $tries\n"; | |||||||||||||||||||||||||||||||||||
| 82 | 0 | $tries++; | |||||||||||||||||||||||||||||||||||
| 83 | 0 | next; | |||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||
| 85 | 0 | last; | |||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | warn Dumper $response->content if $ENV{WEBSERVICE_BRAINTREE_DEBUG}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->check_response_code($response->code); | |||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($response->header('Content-Length') > 1) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 92 | 0 | return xml_to_hash($response->content); | |||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||
| 94 | 0 | return {http_status => $response->code}; | |||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub check_response_code { | ||||||||||||||||||||||||||||||||||
|
Subroutine
check_response_code: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 99 | 0 | my ($self, $code) = @_; | |||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | confess "ClientError" if $code eq '400'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | confess "AuthenticationError" if $code eq '401'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | confess "AuthorizationError" if $code eq '403'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | confess "NotFoundError" if $code eq '404'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | confess "ServerError" if $code eq '500'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | confess "DownForMaintenance" if $code eq '503'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub add_form_field { | ||||||||||||||||||||||||||||||||||
|
Subroutine
add_form_field: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||||||||||
| 110 | 0 | return "Content-Disposition: form-data; name=\"${key}\"${LF}${LF}${value}${LF}"; | |||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub add_file_part { | ||||||||||||||||||||||||||||||||||
|
Subroutine
add_file_part: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 114 | 0 | my ($self, $key, $file) = @_; | |||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $mime_type = $self->mime_type_for_file_name($file); | |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $contents = do { | |||||||||||||||||||||||||||||||||||
| 119 | 0 | local $/; | |||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | open(my $fh, '<', $file) or die "Cannot open $file for reading: $!\n"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 121 | 0 | <$fh>; | |||||||||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||
| 124 | 0 | return "Content-Disposition: form-data; name=\"${key}\"; filename=\"${file}\"${LF}" | |||||||||||||||||||||||||||||||||||
| 125 | . "Content-Type: ${mime_type}${LF}${LF}${contents}${LF}"; | ||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub mime_type_for_file_name { | ||||||||||||||||||||||||||||||||||
|
Subroutine
mime_type_for_file_name: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 129 | 0 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||||||||||
| 130 | 0 | my ($ext) = $filename =~ /\.([^.]+)$/; | |||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | $ext = lc($ext // ''); | ||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$ext // ""
MC/DC0%0/1
$ext // ""
$ext
Truth table0%0/2
A
A= $ext
| |||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($ext eq 'jpeg' || $ext eq 'jpg') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$ext eq "jpeg" or $ext eq "jpg"
MC/DC0%0/2
$ext eq "jpeg" or $ext eq "jpg"
$ext eq "jpeg"
$ext eq "jpg"
Truth table0%0/3
A or B
A= $ext eq "jpeg"B= $ext eq "jpg"
| |||||||||||||||||||||||||||||||||||||
| 134 | 0 | return 'image/jpeg'; | |||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||
| 136 | elsif ($ext eq 'png') { | ||||||||||||||||||||||||||||||||||||
| 137 | 0 | return 'image/png'; | |||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||
| 139 | elsif ($ext eq 'pdf') { | ||||||||||||||||||||||||||||||||||||
| 140 | 0 | return 'application/pdf'; | |||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||||||||||
| 143 | 0 | return 'application/octet-stream'; | |||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | 22 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||
| 149 | 22 | 1; | |||||||||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||||||||||