line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Web::MarketReceipt::Verifier::AppStore; |
2
|
2
|
|
|
2
|
|
70861
|
use 5.010; |
|
2
|
|
|
|
|
8
|
|
3
|
2
|
|
|
2
|
|
348
|
use Mouse; |
|
2
|
|
|
|
|
21427
|
|
|
2
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
extends 'Web::MarketReceipt::Verifier'; |
5
|
2
|
|
|
2
|
|
1122
|
use utf8; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
8
|
|
6
|
2
|
|
|
2
|
|
49
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
104
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
8
|
no Mouse; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
8
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
591
|
use Web::MarketReceipt; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
38
|
|
11
|
2
|
|
|
2
|
|
767
|
use Furl; |
|
2
|
|
|
|
|
37758
|
|
|
2
|
|
|
|
|
56
|
|
12
|
2
|
|
|
2
|
|
1076
|
use JSON::XS; |
|
2
|
|
|
|
|
6222
|
|
|
2
|
|
|
|
|
87
|
|
13
|
2
|
|
|
2
|
|
426
|
use Try::Tiny; |
|
2
|
|
|
|
|
1643
|
|
|
2
|
|
|
|
|
80
|
|
14
|
2
|
|
|
2
|
|
725
|
use MIME::Base64; |
|
2
|
|
|
|
|
913
|
|
|
2
|
|
|
|
|
876
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub verify { |
17
|
2
|
|
|
2
|
0
|
4772
|
my ($self, %args) = @_; |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
|
|
3
|
my $environment = 'Production'; |
20
|
2
|
|
|
|
|
15
|
my $receipt = $args{receipt}; |
21
|
2
|
|
|
|
|
5
|
my $password = $args{password}; |
22
|
2
|
|
|
|
|
4
|
my $exclude_old_transactions = $args{exclude_old_transactions}; |
23
|
|
|
|
|
|
|
|
24
|
2
|
|
|
|
|
4
|
my $hash = { |
25
|
|
|
|
|
|
|
'receipt-data' => $receipt, |
26
|
|
|
|
|
|
|
}; |
27
|
2
|
50
|
|
|
|
16
|
if ($password) { |
28
|
0
|
|
|
|
|
0
|
$hash->{password} = $password; |
29
|
|
|
|
|
|
|
} |
30
|
2
|
50
|
|
|
|
5
|
if ($exclude_old_transactions) { |
31
|
0
|
|
|
|
|
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
|
|
|
|
|
47
|
); |
42
|
|
|
|
|
|
|
|
43
|
2
|
50
|
|
|
|
37
|
if ($res_json->{status} == 21007) { |
44
|
0
|
|
|
|
|
0
|
$environment = 'Sandbox'; |
45
|
0
|
|
|
|
|
0
|
$res_json = $self->_send_request( |
46
|
|
|
|
|
|
|
environment => $environment, |
47
|
|
|
|
|
|
|
hash => $hash, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
4
|
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
|
50
|
|
|
|
10
|
orders => [ map {$self->_order2hash($_, $environment)} @{ $raw_json->{in_app}}], |
|
2
|
100
|
|
|
|
4
|
|
|
1
|
50
|
|
|
|
2
|
|
59
|
|
|
|
|
|
|
) : ( |
60
|
|
|
|
|
|
|
orders => [$self->_order2hash($raw_json, $environment)], |
61
|
|
|
|
|
|
|
), |
62
|
|
|
|
|
|
|
) : (), |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _order2hash { |
67
|
3
|
|
|
3
|
|
6
|
my ($self, $raw_json, $environment) = @_; |
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
|
|
|
|
|
54
|
environment => $environment, |
75
|
|
|
|
|
|
|
state => 'purchased', |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _send_request { |
80
|
0
|
|
|
0
|
|
|
my ($self, %args) = @_; |
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} }); |
|
0
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $res = $furl->post($url, [], $json_str); |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
if ($res->status != 200) { |
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
|
0
|
|
|
|
|
my $res_json = eval { $json_driver->decode($res->body) } or Web::MarketReceipt::Verifier::AppStore::Exception->throw( |
|
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
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
45
|
|
112
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
56
|
|
113
|
2
|
|
|
2
|
|
9
|
use utf8; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
7
|
|
114
|
|
|
|
|
|
|
|
115
|
2
|
|
|
2
|
|
728
|
use parent 'Exception::Tiny'; |
|
2
|
|
|
|
|
423
|
|
|
2
|
|
|
|
|
8
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
use Class::Accessor::Lite ( |
118
|
2
|
|
|
|
|
8
|
ro => [qw/response environment/] |
119
|
2
|
|
|
2
|
|
12838
|
); |
|
2
|
|
|
|
|
4
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub throw { |
122
|
0
|
|
|
0
|
|
|
my ($class, %args) = @_; |
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
|
|
|
|
|
|
|
1; |