XTaTIK/Plugin/Cart/PayPal.pm

filter: Statementstmt 14.7% 13 / 88 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 33.3% 3 / 9 Podpod 0.0% 0 / 3 totaltotal 9.1% 16 / 175 CC 31 SCAR 43.7
CC
31
Cov
9%
_costs
132.0
checkout
110.0
thank_you
42.0
CRAP 78.8
↑ Summary XTaTIK/Model/Blog.pm »
1 package XTaTIK::Plugin::Cart::PayPal;
2  
3 1 our $VERSION = '0.004001'; # VERSION
4  
5 1 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use experimental 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 my @CHECKOUT_FORM_FIELDS = qw/
10 address1 address2 city do_save_address email
11 lname name phone promo_code province toc zip
12 /;
13  
14 1 my %TEMPLATES = qw{
15 thank_you cart/Plugins/PayPal/thank_you
16 checkout cart/Plugins/PayPal/checkout
17 checkout_review cart/Plugins/PayPal/checkout_review
18 email_to_customer cart/Plugins/PayPal/email/order-to-customer
19 email_to_company cart/Plugins/PayPal/email/order-to-company
20 };
21  
22 sub _add_routes {
23 0 ▶ my ( $self, $rc ) = @_;
Subroutine _add_routes: not called CC=1 SCAR=6.9
24  
25 0 my ( $ns, $c ) = qw/XTaTIK::Plugin::Cart PayPal/;
26 0 $rc->any('/thank-you')
27 ->to( namespace => $ns, controller => $c, action => 'thank_you' );
28 0 $rc->post('/checkout')
29 ->to( namespace => $ns, controller => $c, action => 'checkout' );
30 0 $rc->post('/checkout-review')
31 ->to( namespace => $ns, controller => $c, action => 'checkout_review');
32 }
33  
34 ## ROUTES
35 sub thank_you {
36 0 ▶ my $self = shift;
Subroutine thank_you: not called CC=6 SCAR=37.4
Pod undocumented
37  
38 return $self->redirect_to('/cart/')
39 0 ▶ unless @{$self->cart->all_items}
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->cart->all_items;} and $self->param("cart_id") eq $self->cart->id 0 0 0
Condition0%0/3
@{$self->cart->all_items;} and $self->param("cart_id") eq $self->cart->id
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self->cart->all_items;} and $self->param("cart_id") eq $self->cart->id
@{$self->cart->all_items;} $self->param("cart_id") eq $self->cart->id
Truth table0%0/3
A and B
A= @{$self->cart->all_items;}B= $self->param("cart_id") eq $self->cart->id
ABresult
0X0
100
111
40 and $self->param('cart_id') eq $self->cart->id;
41  
42 0 my $order_num = sprintf $self->xtext('order_number'), $self->cart->id;
43 0 my $quote_num = sprintf $self->xtext('quote_number'), $self->cart->id;
44  
45 0 my ( $cart, $quote ) = $self->cart->all_items_cart_quote;
46 $self->cart->submit(
47 0 map +( $_ => $self->session('customer_data')->{$_} ),
48 qw/address1 address2 city email lname name phone
49 province zip/
50 );
51  
52 0 ▶ my $cart_title = @$cart ? "Order #$order_num " : '';
Branch: 1 branch
BranchTrueFalseTotal
@$cart ? : 0 0 0
53 0 ▶ my $quote_title = @$quote ? "Quote #$quote_num " : '';
Branch: 1 branch
BranchTrueFalseTotal
@$quote ? : 0 0 0
54 $self->stash(
55 cart => $cart,
56 quote => $quote,
57 visitor_ip => $self->tx->remote_address,
58 order_number => $order_num,
59 quote_number => $quote_num,
60 $self->_costs,
61 title => "Your $cart_title $quote_title on "
62 . $self->config('text')->{website_domain},
63 0 );
64  
65 # Send order email to customer
66 0 eval { # eval, since we don't know what address we're trying to send to
67 $self->mail(
68 test => $self->config('mail')->{test},
69 to => $self->session('customer_data')->{email},
70 from => $self->config('mail')->{from}{order},
71 subject => $self->stash('title'),
72 type => 'text/html',
73 0 data => $self->render_to_string($TEMPLATES{email_to_customer}),
74 );
75 };
76  
77 $self->stash(
78 title => "New $cart_title $quote_title on "
79 . $self->config('text')->{website_domain},
80 promo_code => $self->session('customer_data')->{promo_code} // 'N/A',
81 0 ▶ map +( "cust_$_" => $self->session('customer_data')->{$_} ),
Condition0%0/2
$self->session("customer_data")->{"promo_code"} // "N/A"
l!l
00
MC/DC0%0/1
$self->session("customer_data")->{"promo_code"} // "N/A"
$self->session("customer_data")->{"promo_code"}
Truth table0%0/2
A
A= $self->session("customer_data")->{"promo_code"}
Aresult
11
00
82 qw/address1 address2 city email lname name phone
83 province zip/
84 );
85  
86 #Send order email to ourselves
87 $self->mail(
88 test => $self->config('mail')->{test},
89 to => $self->config('mail')->{to}{order},
90 from => $self->config('mail')->{from}{order},
91 subject => $self->stash('title'),
92 type => 'text/html',
93 0 data => $self->render_to_string( $TEMPLATES{email_to_company} ),
94 );
95  
96 # TODO: there's gotta be a nicer way of doing this...
97 # ... maybe stuff it into ->submit()
98 0 $self->stash(__cart => undef);
99 0 $self->session(cart_id => undef);
100 0 $self->cart;
101 0 $self->cart_dollars('refresh');
102 0 $self->cart_cents('refresh');
103  
104 0 $self->stash( template => $TEMPLATES{thank_you} );
105 }
106  
107 sub checkout {
108 0 ▶ my $self = shift;
Subroutine checkout: not called CC=10 SCAR=47.0
Pod undocumented
109  
110 0 my @ids = map /(\d+)/, grep /^id/, $self->req->params->names->@*;
111  
112 0 for ( @ids ) {
113 0 $self->cart->alter_quantity(
114 $self->param('number_' . $_),
115 $self->param('quantity_' . $_)
116 );
117 }
118 0 ▶ @ids and $self->cart->save;
Branch: 1 branch
BranchTrueFalseTotal
@ids and $self->cart->save 0 0 0
119 0 $self->cart_dollars('refresh'); $self->cart_cents('refresh');
120  
121 0 for ( @CHECKOUT_FORM_FIELDS ) {
122 0 ▶ next if length $self->param($_);
Branch: 1 branch
BranchTrueFalseTotal
if length $self->param($_) 0 0 0
123  
124 0 ▶ $self->param( $_ => $self->geoip_region )
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "province" and not length $self->session($_) 0 0 0
Condition0%0/3
$_ eq "province" and not length $self->session($_)
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq "province" and not length $self->session($_)
$_ eq "province" not length $self->session($_)
Truth table0%0/3
A and not(B)
A= $_ eq "province"B= not length $self->session($_)
ABresult
0X0
100
111
125 if $_ eq 'province' and not length $self->session($_);
126  
127 0 ▶ next unless length $self->session($_);
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->session($_) 0 0 0
128 0 $self->param( $_ => $self->session($_) );
129 }
130  
131 0 my %items = $self->cart->all_items_cart_quote_kv;
132 $items{cart}->@* or $items{quote}->@*
133 0 ▶ or return $self->redirect_to('/cart/');
Branch: 1 branch
BranchTrueFalseTotal
@{$items{"cart"}} or @{$items{"quote"}} or return $self->redirect_to("/cart/") 0 0 0
Condition0%0/3
@{$items{"cart"}} or @{$items{"quote"}}
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$items{"cart"}} or @{$items{"quote"}}
@{$items{"cart"}} @{$items{"quote"}}
Truth table0%0/3
A or B
A= @{$items{"cart"}}B= @{$items{"quote"}}
ABresult
1X1
011
000
134 $self->stash(
135 %items,
136 template => $TEMPLATES{checkout}
137 0 );
138 }
139  
140 sub checkout_review {
141 0 ▶ my $self = shift;
Subroutine checkout_review: not called CC=6 SCAR=37.4
Pod undocumented
142  
143 0 $self->session(
144 customer_data => {
145 map +( $_ => $self->param($_) ), qw/
146 address1 address2 city email
147 lname name phone promo_code province zip
148 /
149 },
150 );
151  
152 0 my ( $cart ) = $self->cart->all_items_cart_quote;
153 0 ▶ @$cart or $self->redirect_to('/cart/thank-you');
Branch: 1 branch
BranchTrueFalseTotal
@$cart or $self->redirect_to("/cart/thank-you") 0 0 0
154  
155 0 ▶ if ( $self->param('do_save_address') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('do_save_address')) { } 0 0 0
156 $self->session( $_ => $self->param($_) )
157 0 for @CHECKOUT_FORM_FIELDS;
158 }
159 else {
160 $self->session( $_ => undef )
161 0 for @CHECKOUT_FORM_FIELDS;
162 }
163  
164 0 $self->form_checker(
165 rules => {
166 email => {
167 max => 300,
168 email => 'Email',
169 },
170 name => {
171 max => 300,
172 name => 'First name',
173 },
174 lname => {
175 max => 300,
176 name => 'Last name',
177 },
178 address1 => {
179 max => 1000,
180 name => 'Address line 1',
181 },
182 address2 => {
183 max => 1000,
184 name => 'Address line 2',
185 optional => 1,
186 },
187 city => {
188 max => 300,
189 },
190 do_save_address => {
191 optional => 1,
192 select => 1,
193 },
194 province=> {
195 valid_values => [
196 qw/AB BC MB NB NL NT NS NU ON PE QC SK YT/
197 ],
198 valid_values_error => 'Please specify province',
199 },
200 zip => {
201 max => 20,
202 name => 'Postal code',
203 },
204 phone => {
205 name => 'Phone number',
206 },
207 toc => {
208 mandatory_error => 'You must accept Terms and Conditions',
209 },
210 promo_code => {
211 name => 'Promo code',
212 optional => 1,
213 max => 100,
214 },
215 },
216 );
217  
218 0 ▶ unless ( $self->form_checker_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->form_checker_ok) 0 0 0
219 0 $self->flash(
220 form_checker_error_wrapped => $self->form_checker_error_wrapped,
221 );
222 0 $self->stash( cart => $cart );
223 0 $self->render(template => $TEMPLATES{checkout});
224 0 return;
225 }
226  
227 0 my $custom = $self->xtext('paypal_custom');
228 0 $custom =~ s/\$promo_code/$self->param('promo_code')/ge;
229  
230 $self->stash(
231 $self->cart->all_items_cart_quote_kv,
232 custom => $custom,
233 $self->_costs,
234 template => $TEMPLATES{checkout_review},
235 )
236 0 }
237  
238 ## AUXILIARY
239  
240 sub __cur($) {
241 0 ▶ return sprintf '%.02f', shift//0;
Subroutine __cur: not called CC=2 SCAR=17.9
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
242 }
243  
244 sub _costs {
245 0 ▶ my $self = shift;
Subroutine _costs: not called CC=11 SCAR=48.8
246  
247 0 my ( $shipping, $gst, $hst, $pst, $total_d, $total_c);
248 my $xtext_tax = $self->xtext('PST')->{
249 $self->param('province')
250 // ($self->session('customer_data') || {})->{province}
251 0 ▶ };
Condition0%0/5
$self->param("province") // +($self->session("customer_data") || {})->{"province"}
l!l&&r!l&&!r
000
$self->session('customer_data') || {}
l!l
00
MC/DC0%0/2
$self->param("province") // +($self->session("customer_data") || {})->{"province"}
$self->param("province") $self->session('customer_data')
Truth table0%0/3
A // B
A= $self->param("province")B= $self->session('customer_data')
ABresult
1X1
011
000
252  
253 0 ▶ if ( ref $xtext_tax ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $xtext_tax) { } 0 0 0
254 0 my $hst_rate = $$xtext_tax / 100;
255 0 $hst = __cur $self->cart->total * $hst_rate;
256 0 ▶ $shipping = __cur defined( $self->xtext('shipping_free') )
Branch: 1 branch
BranchTrueFalseTotal
&__cur(defined $self->xtext('shipping_free')) && $self->xtext('shipping_free') < $self->cart->total ? : 0 0 0
Condition0%0/3
&__cur(defined $self->xtext("shipping_free")) and $self->xtext("shipping_free") < $self->cart->total
!ll&&!rl&&r
000
MC/DC0%0/2
&__cur(defined $self->xtext("shipping_free")) and $self->xtext("shipping_free") < $self->cart->total
&__cur(defined $self->xtext("shipping_free")) $self->xtext("shipping_free") < $self->cart->total
Truth table0%0/3
A and B
A= &__cur(defined $self->xtext("shipping_free"))B= $self->xtext("shipping_free") < $self->cart->total
ABresult
0X0
100
111
257 && $self->xtext('shipping_free') < $self->cart->total
258 ? 0 : $self->xtext('shipping');
259  
260 0 ( $total_d, $total_c ) = split /\./,
261 __cur +($self->cart->total + $shipping) * (1+$hst_rate);
262 0 $shipping *= 1 + $hst_rate;
263 }
264 else {
265 0 my $pst_rate = $xtext_tax / 100;
266 0 my $gst_rate = $self->xtext('GST') / 100;
267 0 $gst = __cur $self->cart->total * $gst_rate;
268 0 $pst = __cur $self->cart->total * $pst_rate;
269 0 ▶ $shipping = __cur defined( $self->xtext('shipping_free') )
Branch: 1 branch
BranchTrueFalseTotal
&__cur(defined $self->xtext('shipping_free')) && $self->xtext('shipping_free') < $self->cart->total ? : 0 0 0
Condition0%0/3
&__cur(defined $self->xtext("shipping_free")) and $self->xtext("shipping_free") < $self->cart->total
!ll&&!rl&&r
000
MC/DC0%0/2
&__cur(defined $self->xtext("shipping_free")) and $self->xtext("shipping_free") < $self->cart->total
&__cur(defined $self->xtext("shipping_free")) $self->xtext("shipping_free") < $self->cart->total
Truth table0%0/3
A and B
A= &__cur(defined $self->xtext("shipping_free"))B= $self->xtext("shipping_free") < $self->cart->total
ABresult
0X0
100
111
270 && $self->xtext('shipping_free') < $self->cart->total
271 ? 0 : $self->xtext('shipping');
272  
273 0 ( $total_d, $total_c ) = split /\./,
274 __cur +($self->cart->total + $shipping) * (1+$pst_rate+$gst_rate);
275 0 $shipping *= 1 + $pst_rate + $gst_rate;
276  
277 }
278  
279 return (
280 0 ▶ gst => __cur $gst//0,
Condition0%0/6
&__cur($gst) // 0
l!l
00
&__cur($pst) // 0
l!l
00
&__cur($hst) // 0
l!l
00
MC/DC0%0/1
&__cur($gst) // 0
&__cur($gst)
MC/DC0%0/1
&__cur($pst) // 0
&__cur($pst)
MC/DC0%0/1
&__cur($hst) // 0
&__cur($hst)
Truth table0%0/2
A
A= &__cur($gst)
Aresult
11
00
Truth table0%0/2
A
A= &__cur($pst)
Aresult
11
00
Truth table0%0/2
A
A= &__cur($hst)
Aresult
11
00
281 pst => __cur $pst//0,
282 hst => __cur $hst//0,
283 shipping => __cur $shipping,
284 total_dollars => $total_d,
285 total_cents => $total_c,
286 );
287 }
288  
289 1 1;
290  
291 __END__
292  
293  
294 =pod
295  
296  
297 =cut
↑ Summary XTaTIK/Model/Blog.pm »