blib/lib/Plack/Middleware/AppStoreReceipt.pm

filter: Statementstmt 40.0% 26 / 65 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 53.3% 8 / 15 Podpod 100% 2 / 2 totaltotal 28.8% 36 / 125 CC 16 SCAR 29.5
CC
16
Cov
26%
prepare_app
42.0
_verify_receipt
30.0
call
20.0
CRAP 19.2
↑ Summary
1 package Plack::Middleware::AppStoreReceipt;
2 # ABSTRACT: The Plack::Middleware that verifies Apple App Store Receipts for In-App Purchases
3  
4 1 our $VERSION = '0.03';
5  
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Plack::Util::Accessor qw(route method path receipt_data allow_sandbox shared_secret);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=6 SCAR=37.4
Pod documented
16 0 my $self = shift;
17  
18 0 ▶ $self->allow_sandbox( $self->allow_sandbox || 0 );
Condition0%0/2
$self->allow_sandbox || 0
l!l
00
MC/DC0%0/1
$self->allow_sandbox || 0
$self->allow_sandbox
Truth table0%0/2
A
A= $self->allow_sandbox
Aresult
11
00
19  
20 0 ▶ if ( !$self->route ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->route) { } 0 0 0
21 0 ▶ $self->method( 'POST' ) if !$self->method;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->method 0 0 0
22 0 ▶ $self->path( '/receipts/validate' ) if !$self->path;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->path 0 0 0
23 } else {
24 0 ▶ while (my ($key, $value) = each %{$self->route}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->route;}) 0 0 0
25 0 $self->method( uc $key );
26 0 $self->path( $value );
27 }
28 }
29 }
30  
31 0 ▶sub call {
Subroutine call: not called CC=4 SCAR=30.0
Pod documented
32 0 my ( $self, $env ) = @_;
33  
34 0 my $vpath = ($env->{'PATH_INFO'} eq $self->path);
35 return [
36 405,
37 [
38 'Allow' => $self->method,
39 'Content-Type' => 'text/plain',
40 ],
41 ['Method not allowed'],
42 0 ▶ ] if $vpath && $env->{'REQUEST_METHOD'} ne $self->method;
Branch: 1 branch
BranchTrueFalseTotal
if $vpath and $env->{"REQUEST_METHOD"} ne $self->method 0 0 0
Condition0%0/3
$vpath and $env->{"REQUEST_METHOD"} ne $self->method
!ll&&!rl&&r
000
MC/DC0%0/2
$vpath and $env->{"REQUEST_METHOD"} ne $self->method
$vpath $env->{"REQUEST_METHOD"} ne $self->method
Truth table0%0/3
A and B
A= $vpathB= $env->{"REQUEST_METHOD"} ne $self->method
ABresult
0X0
100
111
43  
44 0 ▶ my $res = try { $self->app->($env) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
45  
46 0 ▶ $res = $self->_verify_receipt($env) if $vpath;
Branch: 1 branch
BranchTrueFalseTotal
if $vpath 0 0 0
47  
48 0 return $res;
49 };
50  
51 0 ▶sub _verify_receipt {
Subroutine _verify_receipt: not called CC=5 SCAR=34.0
52 0 my ( $self, $env ) = @_;
53  
54 0 my $plack_req = Plack::Request->new($env);
55 0 ▶ my $receipt_data_param = $plack_req->param('receipt_data') || $plack_req->param($self->receipt_data);
Condition0%0/3
$plack_req->param('receipt_data') || $plack_req->param($self->receipt_data)
l!l&&r!l&&!r
000
MC/DC0%0/2
$plack_req->param('receipt_data') || $plack_req->param($self->receipt_data)
$plack_req->param('receipt_data') $plack_req->param($self->receipt_data)
Truth table0%0/3
A || B
A= $plack_req->param('receipt_data')B= $plack_req->param($self->receipt_data)
ABresult
1X1
011
000
56 0 my %params = ("receipt-data" => $receipt_data_param);
57 0 ▶ $params{password} = $self->shared_secret if $self->shared_secret;
Branch: 1 branch
BranchTrueFalseTotal
if $self->shared_secret 0 0 0
58 0 my $receipt_data = encode_json (\%params);
59  
60 0 my $res;
61 0 $res = $self->_post_receipt_to_itunes( 'production', $receipt_data, $env );
62 0 ▶ if ( $res->[0] == 200 && $self->_is_sandboxed( $self->_parse_success_response( $res ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 200 and $self->_is_sandboxed($self->_parse_success_response($res))) 0 0 0
Condition0%0/3
$res->[0] == 200 and $self->_is_sandboxed($self->_parse_success_response($res))
!ll&&!rl&&r
000
MC/DC0%0/2
$res->[0] == 200 and $self->_is_sandboxed($self->_parse_success_response($res))
$res->[0] == 200 $self->_is_sandboxed($self->_parse_success_response($res))
Truth table0%0/3
A and B
A= $res->[0] == 200B= $self->_is_sandboxed($self->_parse_success_response($res))
ABresult
0X0
100
111
63 #should request to sandbox url since it's sandbox receipt!!
64 0 print "Retrying to post to sandbox environment...\n";
65 0 $res = $self->_post_receipt_to_itunes( 'sandbox', $receipt_data, $env );
66 }
67 0 return $res;
68 }
69  
70 0 ▶sub _post_receipt_to_itunes {
Subroutine _post_receipt_to_itunes: not called CC=4 SCAR=30.0
71 0 my ( $self, $itunes_env, $receipt_data, $env ) = @_;
72  
73 0 ▶ die "sandbox request is not allowed" if $itunes_env eq 'sandbox' && !$self->allow_sandbox;
Branch: 1 branch
BranchTrueFalseTotal
if $itunes_env eq "sandbox" and not $self->allow_sandbox 0 0 0
Condition0%0/3
$itunes_env eq "sandbox" and not $self->allow_sandbox
!ll&&!rl&&r
000
MC/DC0%0/2
$itunes_env eq "sandbox" and not $self->allow_sandbox
$itunes_env eq "sandbox" not $self->allow_sandbox
Truth table0%0/3
A and not(B)
A= $itunes_env eq "sandbox"B= not $self->allow_sandbox
ABresult
0X0
100
111
74  
75 0 my $endpoint = {
76 'production' => 'https://buy.itunes.apple.com/verifyReceipt',
77 'sandbox' => 'https://sandbox.itunes.apple.com/verifyReceipt',
78 };
79  
80 0 my $furl = Furl->new(
81 agent => 'Furl/2.15',
82 timeout => 10,
83 );
84  
85 my $res = $furl->post(
86 0 $endpoint->{$itunes_env}, # URL
87 ['Content-Type' => 'application/json'], # headers
88 $receipt_data, # form data (HashRef/FileHandle are also okay)
89 );
90  
91 0 ▶ return [200, ['Content-Type' => 'application/json'], [$res->content]] if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
92 0 return [500, ['Content-Type' => 'text/plain' ], ["error: ".$res->status_line."\n"]];
93 }
94  
95 0 ▶sub _parse_success_response {
Subroutine _parse_success_response: not called CC=1 SCAR=6.9
96 0 my ( $self, $res ) = @_;
97 0 return decode_json $res->[2]->[0];
98 }
99  
100 0 ▶sub _is_sandboxed {
Subroutine _is_sandboxed: not called CC=1 SCAR=6.9
101 0 my ( $self, $json ) = @_;
102 0 return ( $json->{'status'} == 21007 ); #should be sandboxed!
103 }
104  
105 1 1;
106  
107  
108 __END__
↑ Summary