XTaTIK/Plugin/Cart/PayPal.pm

filter: Statementstmt 13.8% 13 / 94 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 33.3% 3 / 9 Podpod 0.0% 0 / 3 totaltotal 8.1% 16 / 196 CC 38 SCAR 46.0
CC
38
Cov
8%
checkout_review
132.0
_costs
132.0
checkout
110.0
CRAP 99.9
↑ Summary XTaTIK/Controller/User.pm »
1 package XTaTIK::Plugin::Cart::PayPal;
2  
3 1 our $VERSION = '0.005003'; # 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 0 ▶sub _add_routes {
Subroutine _add_routes: not called CC=1 SCAR=6.9
23 0 my ( $self, $rc ) = @_;
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 0 ▶sub thank_you {
Subroutine thank_you: not called CC=8 SCAR=42.8
Pod undocumented
36 0 my $self = shift;
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 map +( "cust_$_" => $self->session('customer_data')->{$_} ),
82 qw/address1 address2 city email lname name phone
83 province zip/, map +( $_->{email} ? $_->{email} : () ),
84 0 ▶ @{ $self->xtext('paypal_custom_fields') || [] },
Branch: 1 branch
BranchTrueFalseTotal
$_->{'email'} ? : 0 0 0
Condition0%0/4
$self->session("customer_data")->{"promo_code"} // "N/A"
l!l
00
$self->xtext('paypal_custom_fields') || []
l!l
00
MC/DC0%0/1
$self->session("customer_data")->{"promo_code"} // "N/A"
$self->session("customer_data")->{"promo_code"}
MC/DC0%0/1
$self->xtext('paypal_custom_fields') || []
$self->xtext('paypal_custom_fields')
Truth table0%0/2
A
A= $self->session("customer_data")->{"promo_code"}
Aresult
11
00
Truth table0%0/2
A
A= $self->xtext('paypal_custom_fields')
Aresult
11
00
85 );
86  
87 #Send order email to ourselves
88 $self->mail(
89 test => $self->config('mail')->{test},
90 to => $self->config('mail')->{to}{order},
91 from => $self->config('mail')->{from}{order},
92 subject => $self->stash('title'),
93 type => 'text/html',
94 0 data => $self->render_to_string( $TEMPLATES{email_to_company} ),
95 );
96  
97 # TODO: there's gotta be a nicer way of doing this...
98 # ... maybe stuff it into ->submit()
99 0 $self->stash(__cart => undef);
100 0 $self->session(cart_id => undef);
101 0 $self->cart;
102 0 $self->cart_dollars('refresh');
103 0 $self->cart_cents('refresh');
104  
105 0 $self->stash( template => $TEMPLATES{thank_you} );
106 }
107  
108 0 ▶sub checkout {
Subroutine checkout: not called CC=10 SCAR=47.0
Pod undocumented
109 0 my $self = shift;
110  
111 0 my @ids = map /(\d+)/, grep /^id/, $self->req->params->names->@*;
112  
113 0 for ( @ids ) {
114 0 $self->cart->alter_quantity(
115 $self->param('number_' . $_),
116 $self->param('quantity_' . $_)
117 );
118 }
119 0 ▶ @ids and $self->cart->save;
Branch: 1 branch
BranchTrueFalseTotal
@ids and $self->cart->save 0 0 0
120 0 $self->cart_dollars('refresh'); $self->cart_cents('refresh');
121  
122 0 for ( @CHECKOUT_FORM_FIELDS ) {
123 0 ▶ next if length $self->param($_);
Branch: 1 branch
BranchTrueFalseTotal
if length $self->param($_) 0 0 0
124  
125 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
126 if $_ eq 'province' and not length $self->session($_);
127  
128 0 ▶ next unless length $self->session($_);
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->session($_) 0 0 0
129 0 $self->param( $_ => $self->session($_) );
130 }
131  
132 0 my %items = $self->cart->all_items_cart_quote_kv;
133 $items{cart}->@* or $items{quote}->@*
134 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
135 $self->stash(
136 %items,
137 template => $TEMPLATES{checkout}
138 0 );
139 }
140  
141 0 ▶sub checkout_review {
Subroutine checkout_review: not called CC=11 SCAR=48.8
Pod undocumented
142 0 my $self = shift;
143  
144 $_->{callback} and $_->{callback}->( $self )
145 0 ▶ for @{ $self->xtext('paypal_custom_fields') || [] };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"callback"} and $_->{"callback"}->($self) 0 0 0
Condition0%0/2
$self->xtext('paypal_custom_fields') || []
l!l
00
MC/DC0%0/1
$self->xtext('paypal_custom_fields') || []
$self->xtext('paypal_custom_fields')
Truth table0%0/2
A
A= $self->xtext('paypal_custom_fields')
Aresult
11
00
146  
147 $self->session(
148 customer_data => {
149 map +( $_ => $self->param($_) ), qw/
150 address1 address2 city email
151 lname name phone promo_code province zip
152 /, map +( $_->{email} ? $_->{email} : () ),
153 0 ▶ @{ $self->xtext('paypal_custom_fields') || [] },
Branch: 1 branch
BranchTrueFalseTotal
$_->{'email'} ? : 0 0 0
Condition0%0/2
$self->xtext('paypal_custom_fields') || []
l!l
00
MC/DC0%0/1
$self->xtext('paypal_custom_fields') || []
$self->xtext('paypal_custom_fields')
Truth table0%0/2
A
A= $self->xtext('paypal_custom_fields')
Aresult
11
00
154 },
155 );
156  
157 0 my ( $cart ) = $self->cart->all_items_cart_quote;
158 0 ▶ @$cart or $self->redirect_to('/cart/thank-you');
Branch: 1 branch
BranchTrueFalseTotal
@$cart or $self->redirect_to("/cart/thank-you") 0 0 0
159  
160 0 ▶ if ( $self->param('do_save_address') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->param('do_save_address')) { } 0 0 0
161 $self->session( $_ => $self->param($_) )
162 0 for @CHECKOUT_FORM_FIELDS;
163 }
164 else {
165 $self->session( $_ => undef )
166 0 for @CHECKOUT_FORM_FIELDS;
167 }
168  
169 0 $self->form_checker(
170 rules => {
171 email => {
172 max => 300,
173 email => 'Email',
174 },
175 name => {
176 max => 300,
177 name => 'First name',
178 },
179 lname => {
180 max => 300,
181 name => 'Last name',
182 },
183 address1 => {
184 max => 1000,
185 name => 'Address line 1',
186 },
187 address2 => {
188 max => 1000,
189 name => 'Address line 2',
190 optional => 1,
191 },
192 city => {
193 max => 300,
194 },
195 do_save_address => {
196 optional => 1,
197 select => 1,
198 },
199 province=> {
200 valid_values => [
201 qw/AB BC MB NB NL NT NS NU ON PE QC SK YT/
202 ],
203 valid_values_error => 'Please specify province',
204 },
205 zip => {
206 max => 20,
207 name => 'Postal code',
208 },
209 phone => {
210 name => 'Phone number',
211 },
212 toc => {
213 mandatory_error => 'You must accept Terms and Conditions',
214 },
215 promo_code => {
216 name => 'Promo code',
217 optional => 1,
218 max => 100,
219 },
220 },
221 );
222  
223 0 ▶ unless ( $self->form_checker_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->form_checker_ok) 0 0 0
224 0 $self->flash(
225 form_checker_error_wrapped => $self->form_checker_error_wrapped,
226 );
227 0 $self->stash( cart => $cart );
228 0 $self->render(template => $TEMPLATES{checkout});
229 0 return;
230 }
231  
232 0 my $custom = $self->xtext('paypal_custom');
233 0 $custom =~ s/\$promo_code/$self->param('promo_code')/ge;
234 0 $custom =~ s/\$(\S+)/$self->param($1)/ge;
235  
236 $self->stash(
237 $self->cart->all_items_cart_quote_kv,
238 custom => $custom,
239 $self->_costs,
240 template => $TEMPLATES{checkout_review},
241 )
242 0 }
243  
244 ## AUXILIARY
245  
246 0 ▶sub __cur($) {
Subroutine __cur: not called CC=2 SCAR=17.9
247 0 ▶ return sprintf '%.02f', shift//0;
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
248 }
249  
250 0 ▶sub _costs {
Subroutine _costs: not called CC=11 SCAR=48.8
251 0 my $self = shift;
252  
253 0 my ( $shipping, $gst, $hst, $pst, $total_d, $total_c);
254 my $xtext_tax = $self->xtext('PST')->{
255 $self->param('province')
256 // ($self->session('customer_data') || {})->{province}
257 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
258  
259 0 ▶ if ( ref $xtext_tax ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $xtext_tax) { } 0 0 0
260 0 my $hst_rate = $$xtext_tax / 100;
261 0 $hst = __cur $self->cart->total * $hst_rate;
262 0 ▶ $shipping = __cur( defined( $self->xtext('shipping_free') )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->xtext('shipping_free') && $self->xtext('shipping_free') < $self->cart->total ? : 0 0 0
Condition0%0/3
defined $self->xtext("shipping_free") and $self->xtext("shipping_free") < $self->cart->total
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->xtext("shipping_free") and $self->xtext("shipping_free") < $self->cart->total
defined $self->xtext("shipping_free") $self->xtext("shipping_free") < $self->cart->total
Truth table0%0/3
A and B
A= defined $self->xtext("shipping_free")B= $self->xtext("shipping_free") < $self->cart->total
ABresult
0X0
100
111
263 && $self->xtext('shipping_free') < $self->cart->total
264 ? 0 : $self->xtext('shipping') );
265  
266 0 ( $total_d, $total_c ) = split /\./,
267 __cur +($self->cart->total + $shipping) * (1+$hst_rate);
268 0 $shipping *= 1 + $hst_rate;
269 }
270 else {
271 0 my $pst_rate = $xtext_tax / 100;
272 0 my $gst_rate = $self->xtext('GST') / 100;
273 0 $gst = __cur $self->cart->total * $gst_rate;
274 0 $pst = __cur $self->cart->total * $pst_rate;
275 0 ▶ $shipping = __cur( defined( $self->xtext('shipping_free') )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->xtext('shipping_free') && $self->xtext('shipping_free') < $self->cart->total ? : 0 0 0
Condition0%0/3
defined $self->xtext("shipping_free") and $self->xtext("shipping_free") < $self->cart->total
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->xtext("shipping_free") and $self->xtext("shipping_free") < $self->cart->total
defined $self->xtext("shipping_free") $self->xtext("shipping_free") < $self->cart->total
Truth table0%0/3
A and B
A= defined $self->xtext("shipping_free")B= $self->xtext("shipping_free") < $self->cart->total
ABresult
0X0
100
111
276 && $self->xtext('shipping_free') < $self->cart->total
277 ? 0 : $self->xtext('shipping') );
278  
279 0 ( $total_d, $total_c ) = split /\./,
280 __cur +($self->cart->total + $shipping) * (1+$pst_rate+$gst_rate);
281 0 $shipping *= 1 + $pst_rate + $gst_rate;
282  
283 }
284  
285 return (
286 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
287 pst => __cur $pst//0,
288 hst => __cur $hst//0,
289 shipping => __cur $shipping,
290 total_dollars => $total_d,
291 total_cents => $total_c,
292 );
293 }
294  
295 1 1;
296  
297 __END__
↑ Summary XTaTIK/Controller/User.pm »