line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::MyAffiliates; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
72972
|
use strict; |
|
4
|
|
|
|
|
35
|
|
|
4
|
|
|
|
|
112
|
|
4
|
4
|
|
|
4
|
|
21
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
148
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
21
|
use Carp; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
340
|
|
8
|
4
|
|
|
4
|
|
2318
|
use Mojo::UserAgent; |
|
4
|
|
|
|
|
1858906
|
|
|
4
|
|
|
|
|
37
|
|
9
|
4
|
|
|
4
|
|
210
|
use Mojo::Util qw(b64_encode url_escape); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
206
|
|
10
|
4
|
|
|
4
|
|
3244
|
use XML::Simple 'XMLin'; ## no critic |
|
4
|
|
|
|
|
36719
|
|
|
4
|
|
|
|
|
33
|
|
11
|
|
|
|
|
|
|
|
12
|
4
|
|
|
4
|
|
357
|
use vars qw/$errstr/; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
5315
|
|
13
|
0
|
|
|
0
|
1
|
0
|
sub errstr { return $errstr } |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { ## no critic (ArgUnpacking) |
16
|
2
|
|
|
2
|
1
|
207
|
my $class = shift; |
17
|
2
|
50
|
|
|
|
23
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
|
|
7
|
for (qw/user pass host/) { |
20
|
6
|
50
|
|
|
|
22
|
$args{$_} || croak "Param $_ is required."; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# fix host with schema |
24
|
2
|
50
|
|
|
|
15
|
$args{host} = 'http://' . $args{host} unless $args{host} =~ m{^https?\://}; |
25
|
2
|
|
|
|
|
7
|
$args{host} =~ s{/$}{}; |
26
|
|
|
|
|
|
|
|
27
|
2
|
|
50
|
|
|
16
|
$args{timeout} ||= 30; # for ua timeout |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
|
|
7
|
return bless \%args, $class; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub __ua { |
33
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
34
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
0
|
return $self->{ua} if exists $self->{ua}; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
my $ua = Mojo::UserAgent->new; |
38
|
0
|
|
|
|
|
0
|
$ua->max_redirects(3); |
39
|
0
|
|
|
|
|
0
|
$ua->inactivity_timeout($self->{timeout}); |
40
|
0
|
|
|
|
|
0
|
$ua->proxy->detect; # env proxy |
41
|
0
|
|
|
|
|
0
|
$ua->max_connections(100); |
42
|
0
|
|
|
|
|
0
|
$self->{ua} = $ua; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
return $ua; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+1%3A+Users+Feed |
48
|
|
|
|
|
|
|
sub get_users { ## no critic (ArgUnpacking) |
49
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
50
|
0
|
0
|
|
|
|
0
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
51
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=1'); |
52
|
0
|
0
|
|
|
|
0
|
$url->query(\%args) if %args; |
53
|
0
|
|
|
|
|
0
|
return $self->request($url->to_string); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub create_affiliate { ## no critic (ArgUnpacking) |
57
|
6
|
|
|
6
|
1
|
1478
|
my $self = shift; |
58
|
6
|
50
|
|
|
|
18
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
6
|
|
|
|
|
39
|
|
59
|
6
|
|
|
|
|
21
|
my $parameters = +{map { ('PARAM_' . $_ => $args{$_}) } keys %args}; |
|
64
|
|
|
|
|
159
|
|
60
|
|
|
|
|
|
|
|
61
|
6
|
|
|
|
|
39
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=26'); |
62
|
6
|
|
|
|
|
633
|
$url->query($parameters); |
63
|
6
|
|
|
|
|
3170
|
my $res = $self->request($url->to_string); |
64
|
|
|
|
|
|
|
|
65
|
6
|
|
|
|
|
6303
|
my $error_count = $res->{INIT}->{ERROR_COUNT}; |
66
|
6
|
100
|
|
|
|
18
|
if ($error_count) { |
67
|
3
|
|
|
|
|
7
|
my $init = $res->{INIT}; |
68
|
3
|
100
|
|
|
|
13
|
my @errors = ref $init->{ERROR} eq 'ARRAY' ? $init->{ERROR}->@* : ($init->{ERROR}); |
69
|
3
|
|
|
|
|
8
|
$errstr = map { $_->{MSG} . " " . $_->{DETAIL} } @errors; |
|
4
|
|
|
|
|
13
|
|
70
|
3
|
|
|
|
|
37
|
return; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
3
|
|
|
|
|
7
|
delete $res->{PASSWORD}; |
74
|
|
|
|
|
|
|
|
75
|
3
|
|
|
|
|
30
|
return $res; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub get_user { |
79
|
0
|
|
|
0
|
1
|
0
|
my ($self, $id) = @_; |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
0
|
$id or croak "id is required."; |
82
|
0
|
0
|
|
|
|
0
|
my $user = $self->get_users(USER_ID => $id) or return; |
83
|
0
|
|
|
|
|
0
|
return $user->{USER}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+4%3A+Decode+Token |
87
|
|
|
|
|
|
|
sub decode_token { |
88
|
1
|
|
|
1
|
1
|
598
|
my $self = shift; |
89
|
1
|
50
|
|
|
|
22
|
my @tokens = @_ or croak 'Must pass at least one token.'; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
0
|
return $self->request('/feeds.php?FEED_ID=4&TOKENS=' . url_escape(join(',', @tokens))); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+5%3A+Encode+Token |
95
|
|
|
|
|
|
|
sub encode_token { ## no critic (ArgUnpacking) |
96
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
97
|
0
|
0
|
|
|
|
0
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
98
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
0
|
$args{USER_ID} or croak "USER_ID is required."; |
100
|
0
|
0
|
|
|
|
0
|
$args{SETUP_ID} or croak "SETUP_ID is required."; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=5'); |
103
|
0
|
0
|
|
|
|
0
|
$url->query(\%args) if %args; |
104
|
0
|
|
|
|
|
0
|
return $self->request($url->to_string); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+6%3A+User+Transactions+Feed |
108
|
|
|
|
|
|
|
sub get_user_transactions { ## no critic (ArgUnpacking) |
109
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
110
|
0
|
0
|
|
|
|
0
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
0
|
$args{FROM_DATE} or croak 'FROM_DATE is reqired.'; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=6'); |
115
|
0
|
0
|
|
|
|
0
|
$url->query(\%args) if %args; |
116
|
0
|
|
|
|
|
0
|
return $self->request($url->to_string); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub get_customers { ## no critic (ArgUnpacking) |
120
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
121
|
0
|
0
|
|
|
|
0
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=10'); |
124
|
0
|
0
|
|
|
|
0
|
$url->query(\%args) if %args; |
125
|
0
|
|
|
|
|
0
|
my $res = $self->request($url->to_string); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $customers = |
128
|
|
|
|
|
|
|
!exists $res->{PLAYER} ? [] |
129
|
|
|
|
|
|
|
: ref $res->{PLAYER} eq 'ARRAY' ? $res->{PLAYER} |
130
|
0
|
0
|
|
|
|
0
|
: [$res->{PLAYER}]; |
|
|
0
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
0
|
return $customers; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub request { |
136
|
0
|
|
|
0
|
1
|
0
|
my ($self, $url, $method, %params) = @_; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
0
|
|
|
0
|
$method ||= 'GET'; |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
0
|
my $ua = $self->__ua; |
141
|
0
|
|
|
|
|
0
|
my $header = {Authorization => 'Basic ' . b64_encode($self->{user} . ':' . $self->{pass}, '')}; |
142
|
0
|
0
|
|
|
|
0
|
my @extra = %params ? (form => \%params) : (); |
143
|
0
|
|
|
|
|
0
|
my $tx = $ua->build_tx($method => $self->{host} . $url => $header => @extra); |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
0
|
$tx = $ua->start($tx); |
146
|
|
|
|
|
|
|
# use Data::Dumper; print STDERR Dumper(\$tx); |
147
|
0
|
0
|
0
|
|
|
0
|
if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ 'text/xml') { |
148
|
0
|
|
|
|
|
0
|
return XMLin($tx->res->body); |
149
|
|
|
|
|
|
|
} |
150
|
0
|
0
|
|
|
|
0
|
if (!$tx->success) { |
151
|
0
|
|
|
|
|
0
|
$errstr = $tx->error->{message}; |
152
|
0
|
|
|
|
|
0
|
return; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
0
|
$errstr = "Unknown Response."; |
156
|
0
|
|
|
|
|
0
|
return; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
## un-documented helper |
160
|
|
|
|
|
|
|
sub get_affiliate_id_from_token { |
161
|
1
|
|
|
1
|
1
|
792
|
my ($self, $token) = @_; |
162
|
|
|
|
|
|
|
|
163
|
1
|
50
|
|
|
|
11
|
$token or croak 'Must pass a token to get_affiliate_id_from_token.'; |
164
|
|
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
|
my $token_info = $self->decode_token($token) or return; |
166
|
0
|
|
|
|
|
|
return $token_info->{TOKEN}->{USER_ID}; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 get_affiliate_details |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Get affiliate detail from user token |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=over 4 |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item * C - token to get detail from |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=back |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Returns token_info hash |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=cut |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub get_affiliate_details { |
184
|
0
|
|
|
0
|
1
|
|
my ($self, $token) = @_; |
185
|
|
|
|
|
|
|
|
186
|
0
|
0
|
|
|
|
|
croak 'Must pass a token to get_affiliate_email_from_token.' unless $token; |
187
|
|
|
|
|
|
|
|
188
|
0
|
0
|
|
|
|
|
my $token_info = $self->decode_token($token) or return; |
189
|
0
|
|
|
|
|
|
return $token_info; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
1; |
193
|
|
|
|
|
|
|
__END__ |