XTaTIK/Plugin/Cart/PayPal.pm

filter: Statementstmt 14.8% 8 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 25.0% 2 / 8 Podpod 0.0% 0 / 2 totaltotal 10.6% 10 / 94 CC 12 SCAR 33.9
CC
12
Cov
10%
__costs
56.0
thank_you
30.0
__cur
6.0
CRAP 29.6
« XTaTIK/Controller/Cart.pm ↑ Summary XTaTIK/Controller/User.pm »
1 package XTaTIK::Plugin::Cart::PayPal;
2  
3 1 our $VERSION = '0.001002'; # VERSION
4  
5 1 ▶use Mojo::Base 'XTaTIK::Plugin::Cart::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub __cur($) {
9 0 ▶ return sprintf '%.02f', shift//'';
Subroutine __cur: not called CC=2 SCAR=17.9
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
10 }
11  
12 sub checkout {
13 0 ▶ my ( $self, $c ) = @_;
Subroutine checkout: not called CC=1 SCAR=6.9
Pod undocumented
14  
15 0 my $custom = $c->xtext('paypal_custom');
16 0 $custom =~ s/\$promo_code/$c->param('promo_code')/ge;
17  
18 0 $c->stash(
19 $c->cart->all_items_cart_quote_kv,
20 custom => $custom,
21 __costs($c),
22 );
23  
24 0 return $c->render_to_string( inline => $self->_template_checkout );
25 }
26  
27 sub thank_you {
28 0 ▶ my ( $self, $c ) = @_;
Subroutine thank_you: not called CC=5 SCAR=34.0
Pod undocumented
29  
30 return $c->redirect_to('/cart/')
31 0 ▶ unless @{$c->cart->all_items};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$c->cart->all_items;} 0 0 0
32  
33 0 my $order_num = sprintf $c->xtext('order_number'), $c->cart->id;
34 0 my $quote_num = sprintf $c->xtext('quote_number'), $c->cart->id;
35  
36 0 my ( $cart, $quote ) = $c->cart->all_items_cart_quote;
37 $c->cart->submit(
38 0 map +( $_ => $c->session('customer_data')->{$_} ),
39 qw/address1 address2 city email lname name phone
40 province zip/
41 );
42  
43 0 ▶ my $cart_title = @$cart ? "Order #$order_num " : '';
Branch: 1 branch
BranchTrueFalseTotal
@$cart ? : 0 0 0
44 0 ▶ my $quote_title = @$quote ? "Quote #$quote_num " : '';
Branch: 1 branch
BranchTrueFalseTotal
@$quote ? : 0 0 0
45 $c->stash(
46 cart => $cart,
47 quote => $quote,
48 visitor_ip => $c->tx->remote_address,
49 order_number => $order_num,
50 quote_number => $quote_num,
51 __costs($c),
52 title => "Your $cart_title $quote_title on "
53 . $c->config('text')->{website_domain},
54 0 );
55  
56 # Send order email to customer
57 0 eval { # eval, since we don't know what address we're trying to send to
58 $c->mail(
59 test => $c->config('mail')->{test},
60 to => $c->session('customer_data')->{email},
61 from => $c->config('mail')->{from}{order},
62 0 subject => $c->stash('title'),
63 type => 'text/html',
64 data => $c->render_to_string('email-templates/order-to-customer'),
65 );
66 };
67  
68 $c->stash(
69 title => "New $cart_title $quote_title on "
70 . $c->config('text')->{website_domain},
71 promo_code => $c->session('customer_data')->{promo_code} // 'N/A',
72 0 ▶ map +( "cust_$_" => $c->session('customer_data')->{$_} ),
Condition0%0/2
$c->session("customer_data")->{"promo_code"} // "N/A"
l!l
00
MC/DC0%0/1
$c->session("customer_data")->{"promo_code"} // "N/A"
$c->session("customer_data")->{"promo_code"}
Truth table0%0/2
A
A= $c->session("customer_data")->{"promo_code"}
Aresult
11
00
73 qw/address1 address2 city email lname name phone
74 province zip/
75 );
76  
77 #Send order email to ourselves
78 $c->mail(
79 test => $c->config('mail')->{test},
80 to => $c->config('mail')->{to}{order},
81 from => $c->config('mail')->{from}{order},
82 0 subject => $c->stash('title'),
83 type => 'text/html',
84 data => $c->render_to_string('email-templates/order-to-company'),
85 );
86  
87 # TODO: there's gotta be a nicer way of doing this...
88 # ... maybe stuff it into ->submit()
89 0 $c->stash(__cart => undef);
90 0 $c->session(cart_id => undef);
91 0 $c->cart;
92 0 $c->cart_dollars('refresh');
93 0 $c->cart_cents('refresh');
94  
95 0 return $c->render_to_string( inline => $self->_template_thank_you );
96 }
97  
98 sub __costs {
99 0 ▶ my $c = shift;
Subroutine __costs: not called CC=7 SCAR=40.3
100  
101 0 my ( $shipping, $gst, $hst, $pst, $total_d, $total_c);
102 my $xtext_tax = $c->xtext('PST')->{
103 $c->param('province')
104 // ($c->session('customer_data') || {})->{province}
105 0 ▶ };
Condition0%0/5
$c->param("province") // +($c->session("customer_data") || {})->{"province"}
l!l&&r!l&&!r
000
$c->session('customer_data') || {}
l!l
00
MC/DC0%0/2
$c->param("province") // +($c->session("customer_data") || {})->{"province"}
$c->param("province") $c->session('customer_data')
Truth table0%0/3
A // B
A= $c->param("province")B= $c->session('customer_data')
ABresult
1X1
011
000
106  
107 # TODO: are we sure shipping charges get the full tax??
108 0 ▶ if ( ref $xtext_tax ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $xtext_tax) { } 0 0 0
109 0 my $hst_rate = $$xtext_tax / 100;
110 0 $hst = __cur $c->cart->total * $hst_rate;
111 0 $shipping = __cur $c->xtext('shipping');
112 0 ( $total_d, $total_c ) = split /\./,
113 __cur +($c->cart->total + $shipping) * (1+$hst_rate);
114 0 $shipping *= 1 + $hst_rate;
115 }
116 else {
117 0 my $pst_rate = $xtext_tax / 100;
118 0 my $gst_rate = $c->xtext('GST') / 100;
119 0 $gst = __cur $c->cart->total * $gst_rate;
120 0 $pst = __cur $c->cart->total * $pst_rate;
121 0 $shipping = __cur $c->xtext('shipping');
122 0 ( $total_d, $total_c ) = split /\./,
123 __cur +($c->cart->total + $shipping) * (1+$pst_rate+$gst_rate);
124 0 $shipping *= 1 + $pst_rate + $gst_rate;
125  
126 }
127  
128 return (
129 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
130 pst => __cur $pst//0,
131 hst => __cur $hst//0,
132 shipping => __cur $shipping,
133 total_dollars => $total_d,
134 total_cents => $total_c,
135 );
136 }
137  
138 sub _template_thank_you {
139 0 ▶ return <<'END_HTML';
Subroutine _template_thank_you: not called CC=1 SCAR=6.9
140  
141 % if ( @{stash('cart')} ) {
142 <ul class="checkout_crumbs text-center">
143 <li class="col-md-15 label-success">Review products</li>
144 <li class="col-md-15 label-success">Enter contact information</li>
145 <li class="col-md-15 label-success">Review Pricing</li>
146 <li class="col-md-15 label-success">Pay for the order</li>
147 <li class="col-md-15 label-primary">Receive confirmation</li>
148 </ul>
149 % } else {
150 <ul class="checkout_crumbs text-center">
151 <li class="col-md-4 label-success">Review products</li>
152 <li class="col-md-4 label-success">Enter contact information</li>
153 <li class="col-md-4 label-primary">Receive confirmation</li>
154 </ul>
155 % }
156  
157 <div class="row">
158 % if ( items_in 'quote' ) {
159 <div class="col-md-<%= scalar(items_in 'cart') ? 6 : 12 %>">
160 <div class="panel panel-primary">
161 <div class="panel-heading">
162 <h3 class="panel-title"><i class="glyphicon glyphicon-comment"></i> Your Quote Request</h3>
163 </div>
164 <div class="panel-body">
165 <p>Thank you for your interest in our products.
166 A sales representative will contact you
167 within 2 business days.
168 </p>
169  
170 <p>Your quote number is
171 <strong><%= stash 'quote_number' %></strong>.
172 Have this number handy if you contact us with
173 any questions about your quote request.
174 </p>
175 </div>
176 </div>
177 </div>
178 % }
179 % if ( items_in 'cart' ) {
180 <div class="col-md-<%= scalar(items_in 'quote') ? 6 : 12 %>">
181 <div class="panel panel-primary">
182 <div class="panel-heading">
183 <h3 class="panel-title"><i class="glyphicon glyphicon-shopping-cart"></i> Your Purchase</h3>
184 </div>
185 <div class="panel-body">
186 <p>Thank you for your purchase!
187 Your order will be shipped on the <strong>next
188 business day</strong> and will arrive within
189 <strong>5–7 business days</strong>.
190 </p>
191  
192 <p>Your order number is
193 <strong><%= stash 'order_number' %></strong>.
194 Have this number handy if you contact us with any
195 questions about yourorder.
196 </p>
197 </div>
198 </div>
199 </div>
200 % }
201 </div>
202 END_HTML
203 }
204  
205 sub _template_checkout {
206 0 ▶ return <<'END_HTML';
Subroutine _template_checkout: not called CC=1 SCAR=6.9
207  
208 <dl class="dl-horizontal" id="checkout_totals">
209 <dt>Cost of products:</dt>
210 <dd>$<%= cart->total %></dd>
211  
212 % if ( stash('hst')+0 ) {
213 <dt><abbr title="Harmonized Sales Tax">HST</abbr>:</dt>
214 <dd><strong>$<%= stash 'hst' %></strong></dd>
215 % }
216  
217 % if ( stash('gst')+0 ) {
218 <dt><abbr title="Goods and Services Tax">GST</abbr>:</dt>
219 <dd><strong>$<%= stash 'gst' %></strong></dd>
220 % }
221  
222 % if ( stash('pst')+0 ) {
223 <dt><abbr title="Provincial Sales Tax">PST</abbr>:</dt>
224 <dd><strong>$<%= stash 'pst' %></strong></dd>
225 % }
226  
227 <dt>Shipping charge:</dt>
228 <dd>$<%= stash 'shipping' %>
229 <small>(includes applicable taxes)</small></dd>
230 <dt class="total">Total:</dt>
231 <dd class="total">$<%= stash 'total_dollars'
232 %><sup>.<%= stash 'total_cents' %></sup></dd>
233 </dl>
234  
235 <form action="https://www.paypal.com/ca/cgi-bin/webscr" method="POST"
236 id="checkout_paynow_form">
237 %= hidden_field 'upload' => 1
238 %= hidden_field 'cmd' => '_cart'
239 %= hidden_field 'custom' => stash 'custom'
240 %= hidden_field 'business' => xtext('paypal')
241 %= hidden_field 'currency_code' => xtext 'currency'
242 %= hidden_field 'cancel_return' => url_for('/cart/')->to_abs
243 %= hidden_field 'return' => url_for('/cart/thank-you')->to_abs
244 %= hidden_field 'tax_cart' => (stash('pst')+stash('gst')+stash('hst'))
245 %= hidden_field 'handling_cart' => stash 'shipping'
246 %= hidden_field 'address_override' => 1
247 %= hidden_field 'country' => 'CA'; # TODO: allow for others
248 %= hidden_field 'address1' => param('address1')
249 %= hidden_field 'address2' => param('address2')
250 %= hidden_field 'city' => param('city')
251 %= hidden_field 'state' => param('province')
252 %= hidden_field 'zip' => param('zip')
253 %= hidden_field 'night_phone_a' => 1; # TODO: sort phones out
254 %= hidden_field 'night_phone_b' => param('phone')
255  
256 % for ( 1 .. @{stash('cart')} ) {
257 % my $p = stash('cart')->[$_-1];
258 %= hidden_field 'item_name_' . $_ => $p->{title}
259 %= hidden_field 'item_number_' . $_ => $p->{number}
260 %= hidden_field 'amount_' . $_ => $p->{price}
261 %= hidden_field 'quantity_' . $_ => $p->{quantity}
262 % }
263  
264 %= submit_button 'Proceed to PayPal to complete this purchase', class => 'btn btn-lg btn-primary'
265 </form>
266 END_HTML
267 }
268  
269 1 1;
270  
271 __END__
272  
273  
274 =pod
275  
276 Override C<checkout> and C<thank_you> methods in your own cart plugin
277  
278 =cut
« XTaTIK/Controller/Cart.pm ↑ Summary XTaTIK/Controller/User.pm »