blib/lib/Business/Stripe/WebCheckout.pm

filter: Statementstmt 10.7% 18 / 167 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 29.4% 5 / 17 Podpod 100% 11 / 11 totaltotal 10.6% 34 / 319 CC 57 SCAR 46.1
CC
57
Cov
7%
new
240.0
_create_intent
132.0
add_product
110.0
CRAP 100.5
↑ Summary
1 package Business::Stripe::WebCheckout;
2
3 # TODO - Pre release
4 #
5 # TODO - Post release
6 #
7 # 12-04-21 - Improve obtaining success/cancel URLs from environment
8 # 14-04-21 - Add P&P
9 # 16-04-21 - Properly implement live testing without real Stripe keys
10 #
11
12 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 1 our $VERSION = '1.2';
20 1 $VERSION = eval $VERSION;
21
22 sub new {
23 0 ▶ my $class = shift;
Subroutine new: not called CC=15 SCAR=54.8
Pod documented
24 0 my %attrs = @_;
25
26 0 my @products;
27 0 $attrs{'trolley'} = \@products;
28
29 0 ▶ $attrs{'currency'} //= 'GBP';
Condition0%0/2
$attrs{"currency"} //= "GBP"
l!l
00
MC/DC0%0/1
$attrs{"currency"} //= "GBP"
$attrs{"currency"}
Truth table0%0/2
A
A= $attrs{"currency"}
Aresult
11
00
30
31 0 $attrs{'error'} = '';
32
33 0 ▶ $attrs{'cancel-url'} //= "$ENV{'REQUEST_SCHEME'}://$ENV{'HTTP_HOST'}$ENV{'SCRIPT_NAME'}";
Condition0%0/2
$attrs{"cancel-url"} //= qq[$ENV{"REQUEST_SCHEME"}://$ENV{"HTTP_HOST"}$ENV{"SCRIPT_NAME"}]
l!l
00
MC/DC0%0/1
$attrs{"cancel-url"} //= qq[$ENV{"REQUEST_SCHEME"}://$ENV{"HTTP_HOST"}$ENV{"SCRIPT_NAME"}]
$attrs{"cancel-url"}
Truth table0%0/2
A
A= $attrs{"cancel-url"}
Aresult
11
00
34 0 ▶ $attrs{'success-url'} //= "$ENV{'REQUEST_SCHEME'}://$ENV{'HTTP_HOST'}$ENV{'SCRIPT_NAME'}";
Condition0%0/2
$attrs{"success-url"} //= qq[$ENV{"REQUEST_SCHEME"}://$ENV{"HTTP_HOST"}$ENV{"SCRIPT_NAME"}]
l!l
00
MC/DC0%0/1
$attrs{"success-url"} //= qq[$ENV{"REQUEST_SCHEME"}://$ENV{"HTTP_HOST"}$ENV{"SCRIPT_NAME"}]
$attrs{"success-url"}
Truth table0%0/2
A
A= $attrs{"success-url"}
Aresult
11
00
35 0 ▶ $attrs{'error'} = 'cancel-url and success-url cannot be derived from the environment and need to be provided' unless ($attrs{'cancel-url'} and $attrs{'success-url'});
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{"cancel-url"} and $attrs{"success-url"} 0 0 0
Condition0%0/3
$attrs{"cancel-url"} and $attrs{"success-url"}
!ll&&!rl&&r
000
MC/DC0%0/2
$attrs{"cancel-url"} and $attrs{"success-url"}
$attrs{"cancel-url"} $attrs{"success-url"}
Truth table0%0/3
A and B
A= $attrs{"cancel-url"}B= $attrs{"success-url"}
ABresult
0X0
100
111
36
37 # This is changed during testing only
38 0 ▶ $attrs{'url'} //= 'https://api.stripe.com/v1/checkout/sessions';
Condition0%0/2
$attrs{"url"} //= "https://api.stripe.com/v1/checkout/sessions"
l!l
00
MC/DC0%0/1
$attrs{"url"} //= "https://api.stripe.com/v1/checkout/sessions"
$attrs{"url"}
Truth table0%0/2
A
A= $attrs{"url"}
Aresult
11
00
39
40 0 ▶ $attrs{'error'} = 'Public API key provided is not a valid key' if $attrs{'api-public'} and $attrs{'api-public'} !~ /^pk_/;
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"api-public"} and not $attrs{"api-public"} =~ /^pk_/ 0 0 0
Condition0%0/3
$attrs{"api-public"} and not $attrs{"api-public"} =~ /^pk_/
!ll&&!rl&&r
000
MC/DC0%0/2
$attrs{"api-public"} and not $attrs{"api-public"} =~ /^pk_/
$attrs{"api-public"} not $attrs{"api-public"} =~ /^pk_/
Truth table0%0/3
A and not(B)
A= $attrs{"api-public"}B= not $attrs{"api-public"} =~ /^pk_/
ABresult
0X0
100
111
41 0 ▶ $attrs{'error'} = 'Secret API key provided is not a valid key' unless $attrs{'api-secret'} =~ /^sk_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{"api-secret"} =~ /^sk_/ 0 0 0
42 0 ▶ $attrs{'error'} = 'Secret API key provided as Public key' if $attrs{'api-public'} and $attrs{'api-public'} =~ /^sk_/;
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"api-public"} and $attrs{"api-public"} =~ /^sk_/ 0 0 0
Condition0%0/3
$attrs{"api-public"} and $attrs{"api-public"} =~ /^sk_/
!ll&&!rl&&r
000
MC/DC0%0/2
$attrs{"api-public"} and $attrs{"api-public"} =~ /^sk_/
$attrs{"api-public"} $attrs{"api-public"} =~ /^sk_/
Truth table0%0/3
A and B
A= $attrs{"api-public"}B= $attrs{"api-public"} =~ /^sk_/
ABresult
0X0
100
111
43 0 ▶ $attrs{'error'} = 'Public API key provided as Secret key' if $attrs{'api-secret'} =~ /^pk_/;
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"api-secret"} =~ /^pk_/ 0 0 0
44 0 ▶ $attrs{'error'} = 'Secret API key is too short' unless length $attrs{'api-secret'} > 100;
Branch: 1 branch
BranchTrueFalseTotal
unless length $attrs{"api-secret"} > 100 0 0 0
45 0 ▶ $attrs{'error'} = 'Secret API key is missing' unless $attrs{'api-secret'};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{"api-secret"} 0 0 0
46
47 0 return bless \%attrs, $class;
48 }
49
50 sub success {
51 0 ▶ my $self = shift;
Subroutine success: not called CC=1 SCAR=6.9
Pod documented
52 0 return !$self->{'error'};
53 }
54
55 sub error {
56 0 ▶ my $self = shift;
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
57 0 return $self->{'error'};
58 }
59
60 sub add_product {
61 0 ▶ my ($self, %product) = @_;
Subroutine add_product: not called CC=10 SCAR=47.0
Pod documented
62 0 $self->{'error'} = '';
63
64 0 ▶ unless ($product{'price'} > 0 and $product{'price'} !~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($product{"price"} > 0 and not $product{"price"} =~ /\./) 0 0 0
Condition0%0/3
$product{"price"} > 0 and not $product{"price"} =~ /\./
!ll&&!rl&&r
000
MC/DC0%0/2
$product{"price"} > 0 and not $product{"price"} =~ /\./
$product{"price"} > 0 not $product{"price"} =~ /\./
Truth table0%0/3
A and not(B)
A= $product{"price"} > 0B= not $product{"price"} =~ /\./
ABresult
0X0
100
111
65 0 $self->{'error'} = 'Invalid price. Price is an integer of the lowest currency unit';
66 0 return;
67 }
68 0 ▶ unless ($product{'qty'} > 0 and $product{'qty'} !~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($product{"qty"} > 0 and not $product{"qty"} =~ /\./) 0 0 0
Condition0%0/3
$product{"qty"} > 0 and not $product{"qty"} =~ /\./
!ll&&!rl&&r
000
MC/DC0%0/2
$product{"qty"} > 0 and not $product{"qty"} =~ /\./
$product{"qty"} > 0 not $product{"qty"} =~ /\./
Truth table0%0/3
A and not(B)
A= $product{"qty"} > 0B= not $product{"qty"} =~ /\./
ABresult
0X0
100
111
69 0 $self->{'error'} = 'Invalid qty. Qty is a positive integer';
70 0 return;
71 }
72
73 0 ▶ unless ($product{'name'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($product{"name"}) 0 0 0
74 0 $self->{'error'} = 'No product name supplied';
75 0 return;
76 }
77 0 $self->{'intent'} = undef;
78 # Update existing Product by ID
79 0 foreach my $prod(@{$self->{'trolley'}}) {
80 0 ▶ if ($prod->{'id'} eq $product{'id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prod->{"id"} eq $product{"id"}) 0 0 0
81 0 foreach my $field('name', 'description', 'qty', 'price') {
82 0 $prod->{$field} = $product{$field};
83 }
84 0 return scalar @{$self->{'trolley'}};
85 }
86 }
87
88 0 my $new_product;
89 0 foreach my $field('id', 'name', 'description', 'qty', 'price') {
90 0 $new_product->{$field} = $product{$field};
91 }
92 0 push @{$self->{'trolley'}}, $new_product;
93 }
94
95 sub list_products {
96 0 ▶ my $self = shift;
Subroutine list_products: not called CC=2 SCAR=17.9
Pod documented
97 0 my @products;
98 0 foreach my $prod(@{$self->{'trolley'}}) {
99 0 push @products, $prod->{'id'};
100 }
101 0 return @products;
102 }
103
104 sub get_product {
105 0 ▶ my ($self, $id) = @_;
Subroutine get_product: not called CC=4 SCAR=30.0
Pod documented
106 0 $self->{'error'} = '';
107
108 0 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
109 0 $self->{'error'} = 'Product ID missing';
110 0 return;
111 }
112
113 0 foreach my $prod(@{$self->{'trolley'}}) {
114 0 ▶ if ($prod->{'id'} eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prod->{"id"} eq $id) 0 0 0
115 0 return $prod;
116 }
117 }
118 0 $self->{'error'} = "Product ID $id not found";
119 }
120
121 sub delete_product {
122 0 ▶ my ($self, $id) = @_;
Subroutine delete_product: not called CC=4 SCAR=30.0
Pod documented
123 0 $self->{'error'} = '';
124
125 0 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
126 0 $self->{'error'} = 'Product ID missing';
127 0 return;
128 }
129
130 0 ▶ for (my $i = 0; $i < scalar @{$self->{'trolley'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"trolley"};}) 0 0 0
131 0 ▶ if (${$self->{'trolley'}}[$i]->{'id'} eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $$self{"trolley"} }->[$i]{"id"} eq $id) 0 0 0
132 0 $self->{'intent'} = undef;
133 0 splice @{$self->{'trolley'}}, $i, 1;
134 0 return scalar @{$self->{'trolley'}};
135 }
136 }
137 0 $self->{'error'} = "Product ID $id not found";
138 }
139
140 # Private method called internally by get_intent and get_intent_id
141 # Attempts to obtain a new session intent from Stripe
142 # Returns existing session if it exists and Trolley hasn't changed
143 sub _create_intent {
144 0 ▶ my $self = shift;
Subroutine _create_intent: not called CC=11 SCAR=48.8
145
146 0 ▶ if ($self->{'intent'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"intent"}) 0 0 0
147 0 return $self->{'intent'};
148 }
149
150 0 ▶ $self->{'reference'} //= __PACKAGE__;
Condition0%0/2
$self->{"reference"} //= __PACKAGE__
l!l
00
MC/DC0%0/1
$self->{"reference"} //= __PACKAGE__
$self->{"reference"}
Truth table0%0/2
A
A= $self->{"reference"}
Aresult
11
00
151
152 0 my $http = HTTP::Tiny->new;
153 my $headers = {
154 0 'Authorization' => 'Bearer ' . $self->{'api-secret'},
155 'Stripe-Version' => '2020-08-27',
156 };
157
158 # Update URL and headers during stripe-live tests
159 0 ▶ if ($self->{'url'} =~ /^https:\/\/www\.boddison\.com/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"url"} =~ m[^https://www\.boddison\.com]) 0 0 0
160 0 $headers->{'BODTEST'} = __PACKAGE__ . " v$VERSION";
161 $headers->{'Authorization'} = undef,
162 0 ▶ $self->{'url'} .= '?fail' if $self->{'api-test-fail'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"api-test-fail"} 0 0 0
163 }
164
165 0 my $vars = {
166 'headers' => $headers,
167 'agent' => 'Perl-WebCheckout/$VERSION',
168 };
169 my $payload = {
170 'cancel_url' => $self->{'cancel-url'},
171 'success_url' => $self->{'success-url'},
172 'payment_method_types[0]' => 'card',
173 'mode' => 'payment',
174 0 'client_reference_id' => $self->{'reference'},
175 };
176 0 ▶ $payload->{'customer_email'} = $self->{'email'} if $self->{'email'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"email"} 0 0 0
177 0 ▶ if ($self->{'getShipping'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"getShipping"}) 0 0 0
178 0 $payload->{'shipping_address_collection[allowed_countries][0]'} = $self->{'getShipping'};
179 }
180
181 0 my $i = 0;
182 0 foreach my $prod(@{$self->{'trolley'}}) {
183 0 $payload->{"line_items[$i][currency]"} = $self->{'currency'};
184 0 $payload->{"line_items[$i][name]"} = $prod->{'name'};
185 0 ▶ $payload->{"line_items[$i][description]"} = $prod->{'description'} if $prod->{'description'};
Branch: 1 branch
BranchTrueFalseTotal
if $prod->{"description"} 0 0 0
186 0 $payload->{"line_items[$i][quantity]"} = $prod->{'qty'};
187 0 $payload->{"line_items[$i][amount]"} = $prod->{'price'};
188 0 $i++;
189 }
190
191 0 my $response = $http->post_form($self->{'url'}, $payload, $vars);
192
193 0 $self->{'error'} = '';
194 0 ▶ if ($response->{'success'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 0 0 0
195 0 $self->{'intent'} = $response->{'content'};
196 } else {
197 0 my $content = $response->{'content'};
198 0 eval {
199 0 $content = decode_json($response->{'content'});
200 };
201 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
202 0 $self->{'error'} = $content;
203 } else {
204 0 $self->{'error'} = $content->{'error'}->{'message'};
205 }
206 }
207 }
208
209 sub get_intent {
210 0 ▶ my ($self, %attrs) = @_;
Subroutine get_intent: not called CC=3 SCAR=24.8
Pod documented
211
212 0 ▶ $self->{'reference'} = $attrs{'reference'} if $attrs{'reference'};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"reference"} 0 0 0
213 0 ▶ $self->{'email'} = $attrs{'email'} if $attrs{'email'};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"email"} 0 0 0
214
215 0 $self->{'error'} = '';
216 0 return $self->_create_intent;
217 }
218
219 sub get_intent_id {
220 0 ▶ my ($self, %attrs) = @_;
Subroutine get_intent_id: not called CC=4 SCAR=30.0
Pod documented
221
222 0 ▶ $self->{'reference'} = $attrs{'reference'} if $attrs{'reference'};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"reference"} 0 0 0
223 0 ▶ $self->{'email'} = $attrs{'email'} if $attrs{'email'};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"email"} 0 0 0
224
225 0 $self->{'error'} = '';
226 0 my $intent = $self->_create_intent;
227 0 ▶ if ($self->{'error'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error'}) { } 0 0 0
228 0 return $intent;
229 } else {
230 0 return decode_json($intent)->{'id'};
231 }
232 }
233
234 sub get_ids {
235 0 ▶ my ($self, %attrs) = @_;
Subroutine get_ids: not called CC=9 SCAR=45.0
Pod documented
236
237 0 ▶ $self->{'public-key'} = $attrs{'public-key'} if $attrs{'public-key'};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"public-key"} 0 0 0
238
239 0 $self->{'error'} = '';
240 0 ▶ unless ($self->{'api-public'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"api-public"}) 0 0 0
241 0 $self->{'error'} = 'Required Public API Key missing';
242 0 return;
243 }
244
245 0 ▶ $self->{'reference'} = $attrs{'reference'} if $attrs{'reference'};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"reference"} 0 0 0
246 0 ▶ $self->{'email'} = $attrs{'email'} if $attrs{'email'};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"email"} 0 0 0
247
248 0 my $intent_id = $self->get_intent_id;
249
250 0 my %result;
251 0 ▶ if ($self->{'error'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error'}) { } 0 0 0
252 0 $result{'status'} = 'error';
253 0 $result{'message'} = $self->{'error'};
254 } else {
255 0 $result{'status'} = 'success';
256 0 $result{'api-key'} = $self->{'api-public'};
257 0 $result{'session'} = $intent_id;
258 }
259
260 0 ▶ $attrs{'format'} = 'text' unless $attrs{'format'};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{"format"} 0 0 0
261 0 ▶ return encode_json(\%result) if lc($attrs{'format'}) eq 'json';
Branch: 1 branch
BranchTrueFalseTotal
if lc $attrs{"format"} eq "json" 0 0 0
262 0 ▶ return $result{'message'} || "$result{'api-key'}:$result{'session'}";
Condition0%0/2
$result{'message'} || "$result{'api-key'}:$result{'session'}"
l!l
00
MC/DC0%0/1
$result{'message'} || "$result{'api-key'}:$result{'session'}"
$result{'message'}
Truth table0%0/2
A
A= $result{'message'}
Aresult
11
00
263 }
264
265 sub checkout {
266 0 ▶ my $self = shift;
Subroutine checkout: not called CC=4 SCAR=30.0
Pod documented
267
268 0 my $data = $self->get_ids( 'format' => 'text', @_);
269
270 0 ▶ return if $self->{'error'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 0 0
271
272 0 my ($key, $session) = split /:/, $data;
273
274 0 ▶ unless ($key and $session) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key and $session) 0 0 0
Condition0%0/3
$key and $session
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $session
$key $session
Truth table0%0/3
A and B
A= $keyB= $session
ABresult
0X0
100
111
275 0 $self->{'error'} = 'Error getting key and session';
276 0 return;
277 }
278
279 0 return <<"END_HTML";
280 Content-type: text/html
281
282 <html>
283 <head>
284 <script src="https://js.stripe.com/v3/"></script>
285 <script>
286 var stripe = Stripe('$key');
287 var result = stripe.redirectToCheckout({sessionId: '$session'});
288 if (result.error) {
289 alert(result.error.message);
290 }
291 </script>
292 </head>
293 <body>
294 END_HTML
295
296 }
297
298 1 1;
299
300 __END__
↑ Summary