blib/lib/Web/MarketReceipt/Verifier/AppStore.pm

filter: Statementstmt 74.7% 62 / 83 Branchbran 43.7% 7 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 1 totaltotal 72.2% 86 / 119 SCAR 24.2
CC
9
Cov
70%
_send_request
12.0
verify
8.3
throw
2.0
CRAP 11.3
↑ Summary blib/lib/Web/MarketReceipt/Verifier/GooglePlay.pm »
1 package Web::MarketReceipt::Verifier::AppStore;
2 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 extends 'Web::MarketReceipt::Verifier';
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Web::MarketReceipt;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub verify {
17 2 ▶ my ($self, %args) = @_;
Subroutine verify: called CC=7 SCAR=21.2
Pod undocumented
18  
19 2 my $environment = 'Production';
20 2 my $receipt = $args{receipt};
21 2 my $password = $args{password};
22 2 my $exclude_old_transactions = $args{exclude_old_transactions};
23  
24 2 my $hash = {
25 'receipt-data' => $receipt,
26 };
27 2 ▶ if ($password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($password) 0 2 2
28 0 $hash->{password} = $password;
29 }
30 2 ▶ if ($exclude_old_transactions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude_old_transactions) 0 2 2
31 0 $hash->{'exclude-old-transactions'} = \1;
32 }
33  
34 # try production environment first. cf.
35 # https://developer.apple.com/library/ios/technotes/tn2259/_index.html FAQ16
36 # http://nantekottai.com/2012/08/27/verifying-receipts/
37 my $res_json = $self->_send_request(
38 environment => $environment,
39 hash => $hash,
40 opts => $args{opts},
41 2 );
42  
43 2 ▶ if ($res_json->{status} == 21007) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_json->{"status"} == 21007) 0 2 2
44 0 $environment = 'Sandbox';
45 0 $res_json = $self->_send_request(
46 environment => $environment,
47 hash => $hash,
48 );
49 }
50  
51 2 my $raw_json = $res_json->{receipt};
52 Web::MarketReceipt->new(
53 is_success => $res_json->{status} == 0 ? 1 : 0,
54 store => 'AppStore',
55 raw => $raw_json,
56 $res_json->{status} == 0 ? (
57 exists $raw_json->{in_app} ? (
58 2 ▶ orders => [ map {$self->_order2hash($_, $environment)} @{ $raw_json->{in_app}}],
Branch: 3 branches
BranchTrueFalseTotal
$res_json->{'status'} == 0 ? : 2 0 2
$res_json->{'status'} == 0 ? : 2 0 2
exists $raw_json->{'in_app'} ? : 1 1 2
59 ) : (
60 orders => [$self->_order2hash($raw_json, $environment)],
61 ),
62 ) : (),
63 );
64 }
65  
66 sub _order2hash {
67 3 ▶ my ($self, $raw_json, $environment) = @_;
Subroutine _order2hash: called CC=1 SCAR=0.0
68  
69 return {
70 product_identifier => $raw_json->{product_id},
71 unique_identifier => 'AppStore:' . $raw_json->{original_transaction_id},
72 purchased_epoch => $raw_json->{original_purchase_date_ms},
73 quantity => $raw_json->{quantity},
74 3 environment => $environment,
75 state => 'purchased',
76 }
77 }
78  
79 sub _send_request {
80 0 ▶ my ($self, %args) = @_;
Subroutine _send_request: not called CC=3 SCAR=24.8
81  
82 0 my $environment = $args{environment};
83 my $url = {
84 Production => 'https://buy.itunes.apple.com/verifyReceipt',
85 Sandbox => 'https://sandbox.itunes.apple.com/verifyReceipt',
86 0 }->{$environment};
87  
88 0 state $json_driver = JSON::XS->new->utf8;
89 0 my $json_str = $json_driver->encode($args{hash});
90  
91 0 state $furl = Furl->new(%{ $args{opts} });
92 0 my $res = $furl->post($url, [], $json_str);
93  
94 0 ▶ if ($res->status != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->status != 200) 0 0 0
95 0 Web::MarketReceipt::Verifier::AppStore::Exception->throw(
96 message => sprintf('HTTP Status Error(%s):%s %s', $environment, $res->status, $res->message),
97 response => $res,
98 environment => $environment,
99 );
100 }
101  
102 0 ▶ my $res_json = eval { $json_driver->decode($res->body) } or Web::MarketReceipt::Verifier::AppStore::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
my $res_json = eval { do { $json_driver->decode($res->body) } } or "Web::MarketReceipt::Verifier::AppStore::Exception"->throw("message", sprintf('body is not json(%s)', $environment), "response", $res, "environment", $environment) 0 0 0
103 message => sprintf('body is not json(%s)', $environment),
104 response => $res,
105 environment => $environment,
106 );
107 0 $res_json;
108 }
109  
110 package Web::MarketReceipt::Verifier::AppStore::Exception;
111 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 2 ▶use parent 'Exception::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 use Class::Accessor::Lite (
118 2 ro => [qw/response environment/]
119 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 sub throw {
122 0 ▶ my ($class, %args) = @_;
Subroutine throw: not called CC=1 SCAR=6.9
123  
124 0 ($args{package}, $args{file}, $args{line}) = caller(2);
125 0 $args{subroutine} = (caller(3))[3];
126  
127 0 die $class->new(%args);
128 }
129  
130 2 1;
↑ Summary blib/lib/Web/MarketReceipt/Verifier/GooglePlay.pm »