blib/lib/Verotel/FlexPay.pm

filter: Statementstmt 100% 78 / 78 Branchbran 100% 14 / 14 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 18 / 18 Podpod 85.7% 6 / 7 totaltotal 99.2% 124 / 125 SCAR 23.0
CC
10
Cov
100%
_generate_URL
6.0
validate_signature
3.0
_signature
3.0
CRAP 10.0
↑ Summary
1 package Verotel::FlexPay;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Digest::SHA qw( sha256_hex sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # WARNING: Use X.X.X format for $VERSION -> see: https://rt.cpan.org/Public/Bug/Display.html?id=119713
14 # because 0.20.1 < 0.1 (0.1 is converted to real number)
15 1 our $VERSION = '4.0.1';
16  
17 1 our @EXPORT_OK = qw(
18 get_signature
19 get_status_URL
20 get_purchase_URL
21 get_subscription_URL
22 get_upgrade_subscription_URL
23 get_cancel_subscription_URL
24 validate_signature
25 );
26  
27 1 my $STATUS_URL = 'https://secure.verotel.com/salestatus';
28 1 my $FLEXPAY_URL = 'https://secure.verotel.com/startorder';
29 1 my $CANCEL_URL = 'https://secure.verotel.com/cancel-subscription';
30 1 my $PROTOCOL_VERSION = '3.5';
31  
32 =head1 NAME
33  
34 Verotel::FlexPay
35  
36 =head1 DESCRIPTION
37  
38 This library allows merchants to use Verotel payment gateway and get paid by their users via Credit Card and other payment methods.
39  
40 =head2 get_signature($secret, %params)
41  
42 Returns signature for the given parameters using L<$secret>.
43  
44 Signature is an SHA-256 hash as hexadecimal number generated from L<$secret>
45 followed by the parameters joined with colon (:). Parameters ("$key=$value")
46 are alphabeticaly orderered by their keys. Only the following parameters are
47 considered for signing:
48  
49 =head1 AUTHOR
50  
51 Verotel dev team
52  
53 =head1 SUPPORT
54  
55 Documentation PDF for the library can be found on the Verotel blog (http://blog.verotel.com/downloads/).
56  
57 =over 2
58  
59 version,
60 shopID, saleID, referenceID,
61 priceAmount, priceCurrency,
62 description, name
63 custom1, custom2, custom3
64 subscriptionType
65 period
66 trialAmount, trialPeriod
67 cancelDiscountPercentage
68  
69 =back
70  
71 =head3 Example:
72  
73 get_signature('aaB',
74 shopID => '123',
75 custom1 => 'xyz',
76 custom2 => undef ,
77 ignored => 'bla'
78 );
79  
80 returns the SHA-256 string for "aaB:custom1=xyz:custom2=:shopID=123" converted to lowercase.
81  
82 =cut
83  
84 sub get_signature {
85 13 ▶ my $secret = shift;
Subroutine get_signature: called CC=1 SCAR=0.0
Pod documented
86 13 my %params = @_;
87 13 %params = _filter_params( %params );
88 13 return _signature($secret, \%params);
89 }
90  
91  
92 =head2 validate_signature($secret, %params)
93  
94 Returns true if the signature passed in the parameters match the signature computed from B<all> parameters (except for the signature itself).
95  
96 =head3 Example:
97  
98 validate_signature('aaB',
99 shopID => 123,
100 saleID => 345,
101 signature => 'acb4dd91827bc79999a04ac2082d0e43bb018a9ce563dfd3e863fbae32e5f381'
102 );
103  
104 returns true as the signature passed as the parameter is the same as the signature computed for "aaB:saleID=345:shopID=123"
105  
106 Note: It accepts SHA-256 signature, but for now accepts also old SHA-1 signature for backward compatiblity.
107  
108 =cut
109  
110 sub validate_signature {
111 3 ▶ my ($secret, %params) = @_;
Subroutine validate_signature: called CC=3 SCAR=11.0
Pod documented
112 3 my $verified_sign = lc(delete $params{signature});
113 3 my $calculated_sign = _signature($secret, \%params);
114 3 ▶ return 1 if $verified_sign eq $calculated_sign;
Branch: 1 branch
BranchTrueFalseTotal
if $verified_sign eq $calculated_sign 1 2 3
115  
116 2 my $old_sha1_sign = _signature($secret, \%params, \&sha1_hex);
117 2 ▶ return ($verified_sign eq $old_sha1_sign ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$verified_sign eq $old_sha1_sign ? : 1 1 2
118 }
119  
120  
121 =head2 get_purchase_URL($secret, %params)
122  
123 Return URL for purchase with signed parameters (only the parameters listed in the description of get_signature() are considered for signing).
124  
125 =head3 Example:
126  
127 get_purchase_URL('mySecret', shopID => 65147, priceAmount => '6.99', priceCurrency => 'USD');
128  
129 returns
130  
131 "https://secure.verotel.com/startorder?priceAmount=6.99&priceCurrency=USD&shopID=65147&type=purchase&version=3.5&signature=37d56280eae410d2e5d6b67ccd29fd84173f2eed5a329c9b2f7fe9a77ad95441"
132  
133 =cut
134  
135 sub get_purchase_URL {
136 5 ▶ my ($secret, %params) = @_;
Subroutine get_purchase_URL: called CC=1 SCAR=0.0
Pod documented
137 5 return _generate_URL($FLEXPAY_URL, $secret, 'purchase', %params);
138 }
139  
140 =head2 get_subscription_URL($secret, %params)
141  
142 Return URL for subscription with signed parameters (only the parameters listed in the description of get_signature() are considered for signing).
143  
144 =head3 Example:
145  
146 get_subscription_URL('mySecret', shopID => 65147, subscriptionType => 'recurring', period => 'P1M');
147  
148 returns
149  
150 "https://secure.verotel.com/startorder?period=P1M&shopID=65147&subscriptionType=recurring&type=subscription&version=3.5&signature=2f2ffd9ba91dec62be74b143d0093ce7cefc62d1dab237aa3a327d76188cf77c"
151  
152 =cut
153  
154 sub get_subscription_URL {
155 3 ▶ my ($secret, %params) = @_;
Subroutine get_subscription_URL: called CC=1 SCAR=0.0
Pod documented
156 3 return _generate_URL($FLEXPAY_URL, $secret, 'subscription', %params);
157 }
158  
159 =head2 get_subscription_URL($secret, %params)
160  
161 Return URL for upgrade subscription with signed parameters (only the parameters listed in the description of get_signature() are considered for signing).
162  
163 =head3 Example:
164  
165 get_upgrade_subscription_URL('mySecret', shopID => 65147, subscriptionType => 'recurring', period => 'P1M');
166  
167 returns
168  
169 "https://secure.verotel.com/startorder?period=P1M&shopID=65147&subscriptionType=recurring&type=upgradesubscription&version=3.5&signature=2276fd3aea2ca4027641515c731c6783ec2def70504c5276c5f4599039129e52"
170  
171 =cut
172  
173 sub get_upgrade_subscription_URL {
174 1 ▶ my ($secret, %params) = @_;
Subroutine get_upgrade_subscription_URL: called CC=1 SCAR=0.0
Pod undocumented
175 1 return _generate_URL($FLEXPAY_URL, $secret, 'upgradesubscription', %params);
176 }
177  
178  
179 =head2 get_status_URL($secret, %params)
180  
181 Return URL for status with signed parameters (only the parameters listed in the description of get_signature() are considered for signing).
182  
183 =head3 Example:
184  
185 get_status_URL('mySecret', shopID => '65147', saleID => '1485');
186  
187 returns
188  
189 "https://secure.verotel.com/salestatus?saleID=1485&shopID=65147&version=3.5&signature=1a24a2d189824c6800d85131f11a2fca0ebbc233f31cad6d45e947496e423ff7"
190  
191 =cut
192  
193 sub get_status_URL {
194 3 ▶ my ($secret, %params) = @_;
Subroutine get_status_URL: called CC=1 SCAR=0.0
Pod documented
195 3 return _generate_URL($STATUS_URL, $secret, undef, %params);
196 }
197  
198  
199 =head2 get_cancel_subscription_URL($secret, %params)
200  
201 Return URL for cancel subscription with signed parameters (only the parameters listed in the description of get_signature() are considered for signing).
202  
203 =head3 Example:
204  
205 get_cancel_subscription_URL('mySecret', shopID => '65147', saleID => '1485');
206  
207 returns
208  
209 "https://secure.verotel.com/cancel-subscription?saleID=1485&shopID=65147&version=3.5&signature=1a24a2d189824c6800d85131f11a2fca0ebbc233f31cad6d45e947496e423ff7"
210  
211 =cut
212  
213 sub get_cancel_subscription_URL {
214 3 ▶ my ($secret, %params) = @_;
Subroutine get_cancel_subscription_URL: called CC=1 SCAR=0.0
Pod documented
215 3 return _generate_URL($CANCEL_URL, $secret, undef, %params);
216 }
217  
218  
219 ################ PRIVATE METHODS ##########################
220  
221  
222 sub _generate_URL {
223 15 ▶ my ($baseURL, $secret, $type, %params) = (@_);
Subroutine _generate_URL: called CC=6 SCAR=17.9
224  
225 15 ▶ if (!$secret) {croak "no secret given"};
Branch: 1 branch
BranchTrueFalseTotal
unless ($secret) 4 11 15
226 11 ▶ if (!%params) {croak "no params given"};
Branch: 1 branch
BranchTrueFalseTotal
unless (%params) 4 7 11
227  
228 7 $params{version} = $PROTOCOL_VERSION;
229 7 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 5 2 7
230 5 $params{type} = $type;
231 }
232  
233 # remove empty values:
234 7 my @sorted_params = map { (defined($params{$_}) && $params{$_} ne '')
235 51 ▶ ? ($_ => $params{$_})
Branch: 1 branch
BranchTrueFalseTotal
defined $params{$_} && $params{$_} ne '' ? : 49 2 51
Condition100%3/3
defined $params{$_} and $params{$_} ne ""
!ll&&!rl&&r
1149
MC/DC100%2/2
defined $params{$_} and $params{$_} ne ""
defined $params{$_} $params{$_} ne ""
Truth table100%3/3
A and B
A= defined $params{$_}B= $params{$_} ne ""
ABresult
0X0
100
111
236 : ()
237 } sort keys %params;
238  
239 7 my $url = new URI($baseURL);
240 7 my $signature = get_signature($secret, @sorted_params);
241  
242 7 $url->query_form(@sorted_params, signature => $signature);
243  
244 7 return $url->as_string();
245 }
246  
247 sub _signature {
248 18 ▶ my ($secret, $params_ref, $algorigthm_func) = @_;
Subroutine _signature: called CC=3 SCAR=11.0
249 18 ▶ my @values = map { $_.'='.(defined $params_ref->{$_} ? $params_ref->{$_} : "") }
Branch: 1 branch
BranchTrueFalseTotal
defined $params_ref->{$_} ? : 97 5 102
250 sort keys %$params_ref;
251 18 my $encString = join(":", $secret, @values);
252 18 utf8::encode($encString);
253  
254 18 ▶ $algorigthm_func ||= \&sha256_hex;
Condition100%2/2
$algorigthm_func ||= \&Verotel::FlexPay::sha256_hex
l!l
216
MC/DC100%1/1
$algorigthm_func ||= \&Verotel::FlexPay::sha256_hex
$algorigthm_func
Truth table100%2/2
A
A= $algorigthm_func
Aresult
11
00
255 18 return lc($algorigthm_func->($encString));
256 }
257  
258 sub _filter_params {
259 13 ▶ my (%params) = @_;
Subroutine _filter_params: called CC=1 SCAR=0.0
260  
261 13 my @keys = grep { m/ ^(
262 version
263 | shopID
264 | price(Amount|Currency)
265 | paymentMethod
266 | description
267 | referenceID
268 | saleID
269 | custom[123]
270 | subscriptionType
271 | period
272 | name
273 | trialAmount
274 | trialPeriod
275 | cancelDiscountPercentage
276 | type
277 | backURL
278 | declineURL
279 | precedingSaleID
280 | upgradeOption
281 )$
282 /x } keys %params;
283  
284 13 my %filtered = map { $_ => $params{$_} } @keys;
285  
286 13 return %filtered;
287 }
288  
289 1 1;
↑ Summary