Commit | Line | Data |
---|---|---|
6e33dd68 MG |
1 | package App::Web::Oof; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use utf8; | |
7 | use parent qw/Plack::Component/; | |
8 | ||
d90c4f6e | 9 | our $VERSION = '0.000_003'; |
6e33dd68 MG |
10 | |
11 | use DBIx::Simple; | |
1576fc41 | 12 | use File::Slurp; |
6e33dd68 MG |
13 | use HTML::TreeBuilder; |
14 | use HTML::Element::Library; | |
15 | use JSON::MaybeXS qw/encode_json decode_json/; | |
16 | use Plack::Builder; | |
17 | use Plack::Request; | |
1576fc41 | 18 | use Try::Tiny; |
6e33dd68 MG |
19 | |
20 | sub HTML::Element::iter3 { | |
21 | my ($self, $data, $code) = @_; | |
22 | my $orig = $self; | |
23 | my $prev = $orig; | |
24 | for my $el (@$data) { | |
25 | my $current = $orig->clone; | |
26 | $code->($el, $current); | |
27 | $prev->postinsert($current); | |
28 | $prev = $current; | |
29 | } | |
30 | $orig->detach; | |
31 | } | |
32 | ||
33 | sub HTML::Element::fid { shift->look_down(id => shift) } | |
34 | sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) } | |
35 | ||
36 | ################################################## | |
37 | ||
1576fc41 MG |
38 | my %db; |
39 | my ($form, $continue, $order, $details, $pay); | |
6e33dd68 MG |
40 | |
41 | { | |
42 | sub parse_html { | |
43 | my $builder = HTML::TreeBuilder->new; | |
44 | $builder->ignore_unknown(0); | |
45 | $builder->parse_file("tmpl/$_[0].html"); | |
46 | $builder | |
47 | } | |
48 | ||
49 | $form = parse_html 'form'; | |
50 | $continue = parse_html 'continue'; | |
51 | $order = parse_html 'order'; | |
1576fc41 MG |
52 | $details = parse_html 'details'; |
53 | $pay = parse_html 'pay'; | |
6e33dd68 MG |
54 | } |
55 | ||
56 | sub stringify_money { sprintf "£%.2f", $_[0] / 100 } | |
57 | ||
1576fc41 MG |
58 | sub make_slug { |
59 | my $slug = $_[0]; | |
60 | $slug =~ y/ /-/; | |
61 | $slug =~ y/a-zA-Z0-9-//cd; | |
62 | $slug | |
63 | } | |
64 | ||
6e33dd68 MG |
65 | sub form_table_row { |
66 | my ($data, $tr) = @_; | |
67 | $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle stock/; | |
68 | $tr->fclass('price')->replace_content(stringify_money $data->{price}); | |
8bbff1bc | 69 | $tr->fclass('freepost')->detach unless $data->{freepost}; |
6e33dd68 | 70 | $tr->fclass('title')->attr('data-product', $data->{product}); |
1576fc41 MG |
71 | $tr->fclass('title')->attr('href', '/details/'.$data->{product}.'/'.make_slug $data->{title}); |
72 | # $tr->fclass('title')->attr('data-summary', $data->{summary}); | |
6e33dd68 MG |
73 | $tr->look_down(_tag => 'input')->attr(max => $data->{stock}); |
74 | $tr->look_down(_tag => 'input')->attr(name => 'quant'.$data->{product}); | |
75 | } | |
76 | ||
77 | sub form_app { | |
78 | my ($env) = @_; | |
1576fc41 | 79 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); |
6e33dd68 | 80 | |
1576fc41 | 81 | my $data = $db{$$}->select(products => '*', {}, 'product')->hashes; |
6e33dd68 MG |
82 | my $tree = $form->clone; |
83 | $tree->find('tbody')->find('tr')->iter3($data, \&form_table_row); | |
84 | ||
85 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] | |
86 | } | |
87 | ||
88 | sub continue_table_row { | |
89 | my ($data, $tr) = @_; | |
90 | $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle quantity/; | |
8bbff1bc | 91 | $tr->fclass('freepost')->detach unless $data->{freepost}; |
6e33dd68 MG |
92 | $tr->fclass('price')->replace_content(stringify_money $data->{subtotal}); |
93 | $tr->fclass('title')->attr('data-product', $data->{product}); | |
94 | } | |
95 | ||
96 | sub continue_app { | |
97 | my ($env) = @_; | |
1576fc41 | 98 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); |
6e33dd68 MG |
99 | my $tree = $continue->clone; |
100 | my $req = Plack::Request->new($env); | |
101 | my $params = $req->body_parameters; | |
102 | ||
8bbff1bc | 103 | my ($quant, $quant_freepost, $total, @data, @notes) = (0) x 3; |
6e33dd68 MG |
104 | for (sort keys %$params) { |
105 | next unless /^quant/; | |
106 | next unless $params->{$_}; | |
1576fc41 | 107 | my $data = $db{$$}->select(products => '*', {product => substr $_, 5})->hash; |
6e33dd68 MG |
108 | $data->{quantity} = $params->{$_}; |
109 | if ($data->{stock} == 0) { | |
110 | push @notes, 'Item is out of stock and was removed from order: '.$data->{title}; | |
111 | next | |
112 | } | |
113 | if ($data->{quantity} > $data->{stock}) { | |
114 | $data->{quantity} = $data->{stock}; | |
115 | push @notes, 'Not enough units of "'.$data->{title}.'" available. Quantity reduced to '.$data->{quantity} | |
116 | } | |
117 | $data->{subtotal} = $data->{price} * $data->{quantity}; | |
118 | $quant += $data->{quantity}; | |
8bbff1bc | 119 | $quant_freepost += $data->{quantity} if $data->{freepost}; |
6e33dd68 MG |
120 | $total += $data->{subtotal}; |
121 | push @data, $data | |
122 | } | |
123 | ||
1576fc41 MG |
124 | return [500, ['Content-type' => 'text/plain'], ['Error: no items in order.']] unless $quant; |
125 | ||
6e33dd68 MG |
126 | $tree->fid('subtotal')->replace_content(stringify_money $total); |
127 | my $dvalue; | |
128 | if ($params->{discount}) { | |
1576fc41 | 129 | my $discount = $db{$$}->select(discounts => '*', {discount => $params->{discount}})->hash; |
6e33dd68 MG |
130 | if (!defined $discount) { |
131 | push @notes, 'Discount code incorrect. No discount applied.' | |
1576fc41 | 132 | } elsif ($db{$$}->select(orders => 'COUNT(*)', {discount => $params->{discount}})->list) { |
6e33dd68 MG |
133 | push @notes, 'Discount code already used once. No discount applied.' |
134 | } else { | |
135 | $dvalue = int (0.5 + $discount->{fraction} * $total) if $discount->{fraction}; | |
136 | $dvalue = $discount->{flat} if $discount->{flat}; | |
137 | $tree->fid('discount')->replace_content('-'.stringify_money $dvalue); | |
138 | $total -= $dvalue; | |
139 | $tree->look_down(name => 'discount')->attr(value => $params->{discount}); | |
140 | push @notes, 'Discount applied.' | |
141 | } | |
142 | } | |
143 | $tree->look_down(name => 'discount')->detach unless $dvalue; | |
144 | $tree->fid('discount_tr')->detach unless $dvalue; | |
8bbff1bc MG |
145 | my $postage = 220 + 50 * ($quant - $quant_freepost); |
146 | $postage = 0 if $quant == $quant_freepost; | |
6e33dd68 MG |
147 | $tree->fid('postage')->replace_content(stringify_money $postage); |
148 | $total += $postage; | |
149 | $tree->fid('total')->replace_content(stringify_money $total); | |
150 | ||
151 | $tree->fid('order')->find('tbody')->find('tr')->iter3(\@data, \&continue_table_row); | |
152 | $tree->iter($tree->fid('notes')->find('li') => @notes); | |
153 | ||
154 | $tree->look_down(name => 'products')->attr(value => encode_json \@data); | |
155 | $tree->look_down(name => 'total')->attr(value => $total); | |
156 | ||
157 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] | |
158 | } | |
159 | ||
160 | sub order_app { | |
161 | my ($env) = @_; | |
1576fc41 | 162 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); |
6e33dd68 MG |
163 | my $tree = $order->clone; |
164 | my $req = Plack::Request->new($env); | |
1576fc41 MG |
165 | my ($id) = $env->{PATH_INFO} =~ m,^/([0-9A-F]+),; |
166 | if ($id) { | |
167 | my $total = $db{$$}->select(orders => 'total', {id => $id})->list; | |
168 | $tree->fid('orderid')->replace_content($id); | |
169 | $tree->look_down(name => 'order')->attr(value => $id); | |
170 | $tree->fid('total')->replace_content(stringify_money $total); | |
171 | $tree->find('script')->attr('data-amount', $total); | |
172 | return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] | |
173 | } else { | |
174 | my %parms = %{$req->body_parameters}; | |
175 | my $id = sprintf "%X%04X", time, $$; | |
176 | my $err; | |
177 | try { | |
178 | $db{$$}->begin_work; | |
179 | my $products = decode_json $req->body_parameters->{products}; | |
180 | for my $prod (@$products) { | |
181 | my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list; | |
182 | die "Not enough of " .$prod->{title}."\n" if $prod->{quantity} > $stock; | |
183 | $db{$$}->update(products => {stock => $stock - $prod->{quantity}}, {product => $prod->{product}}); | |
184 | } | |
185 | $db{$$}->insert(orders => {id => $id, %parms}); | |
186 | $db{$$}->commit; | |
187 | } catch { | |
188 | $db{$$}->rollback; | |
189 | $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]] | |
190 | }; | |
191 | return $err if $err; | |
192 | return [303, [Location => "/order/$id"], []] | |
6e33dd68 | 193 | } |
1576fc41 MG |
194 | } |
195 | ||
196 | sub details_list_element { | |
197 | my ($data, $li) = @_; | |
198 | $li->find('a')->attr(href => "/$data"); | |
199 | my $thumb = $data =~ s/fullpics/thumbs/r; | |
200 | $thumb = $data unless -f $thumb; | |
201 | $li->find('img')->attr(src => "/$thumb"); | |
202 | } | |
203 | ||
204 | sub details_app { | |
205 | my ($env) = @_; | |
206 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); | |
207 | my $tree = $details->clone; | |
208 | my ($id) = $env->{PATH_INFO} =~ m,^/(\d+),; | |
209 | my $title = $db{$$}->select(products => 'title', {product => $id})->list; | |
210 | my @pics = <static/fullpics/$id-*>; | |
211 | my $slug = make_slug $title; | |
5d7b5400 | 212 | $tree->find('title')->replace_content("Pictures of $title | ledparts4you"); |
1576fc41 MG |
213 | $tree->find('h2')->replace_content($title); |
214 | $tree->look_down(rel => 'canonical')->attr(href => "/details/$id/$slug"); | |
b66954a0 | 215 | $tree->fid('pictures')->find('li')->iter3(\@pics, \&details_list_element); |
6e33dd68 | 216 | |
6e33dd68 MG |
217 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] |
218 | } | |
219 | ||
1576fc41 MG |
220 | sub pay_app { |
221 | my ($env) = @_; | |
222 | my $req = Plack::Request->new($env); | |
223 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); | |
224 | my $order = $req->body_parameters->{order}; | |
225 | my $token = $req->body_parameters->{stripeToken}; | |
226 | return [500, ['Content-type' => 'text/html; charset=utf-8'], ['No token received, payment did not succeed.']] unless $token; | |
227 | $db{$$}->update(orders => {stripe_token => $token}, {id => $order}); | |
228 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]]; | |
229 | } | |
230 | ||
6e33dd68 | 231 | sub app { |
1576fc41 | 232 | my $footer = read_file 'tmpl/footer.html'; |
6e33dd68 | 233 | builder { |
1576fc41 MG |
234 | enable sub { |
235 | my $app = shift; | |
236 | sub { | |
237 | my $res = $app->(@_); | |
fb23ef22 | 238 | push @{$res->[2]}, $footer if $res->[0] == 200; |
1576fc41 MG |
239 | $res; |
240 | } | |
241 | }; | |
6e33dd68 MG |
242 | mount '/' => sub { [301, [Location => '/form'], []] }; |
243 | mount '/form' => \&form_app; | |
244 | mount '/continue' => \&continue_app; | |
245 | mount '/order' => \&order_app; | |
1576fc41 MG |
246 | mount '/details' => \&details_app; |
247 | mount '/pay' => \&pay_app; | |
6e33dd68 MG |
248 | } |
249 | } | |
250 | ||
251 | 1; | |
252 | __END__ | |
253 | ||
254 | =head1 NAME | |
255 | ||
256 | App::Web::Oof - Oversimplified order form / ecommerce website | |
257 | ||
258 | =head1 SYNOPSIS | |
259 | ||
260 | use App::Web::Oof; | |
261 | ||
262 | =head1 DESCRIPTION | |
263 | ||
264 | Oof (Oversimplified order form) is a very simple ecommerce website. | |
39be4169 MG |
265 | It is the code behind L<https://ledparts4you.uk.to>. |
266 | ||
267 | This version is reasonably functional, yet not very reusable, hence | |
268 | the version number. | |
6e33dd68 MG |
269 | |
270 | =head1 AUTHOR | |
271 | ||
272 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
273 | ||
274 | =head1 COPYRIGHT AND LICENSE | |
275 | ||
276 | Copyright (C) 2016 by Marius Gavrilescu | |
277 | ||
278 | This library is free software; you can redistribute it and/or modify | |
279 | it under the same terms as Perl itself, either Perl version 5.22.1 or, | |
280 | at your option, any later version of Perl 5 you may have available. | |
281 | ||
282 | ||
283 | =cut |