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 | ||
bf8c3839 | 9 | our $VERSION = '0.000_005'; |
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 | ||
8bb7ab90 MG |
65 | sub product_to_schemaorg { |
66 | my ($include_url, %data) = @_; | |
67 | my $stock = $data{stock} > 0 ? 'InStock' : 'OutOfStock'; | |
68 | +{ | |
69 | '@context' => 'http://schema.org/', | |
70 | '@type' => 'Product', | |
71 | name => $data{title}, | |
72 | image => "/static/fullpics/$data{product}-1.jpg", | |
73 | description => $data{summary}, | |
74 | offers => { | |
75 | '@type' => 'Offer', | |
76 | price => ($data{price} =~ s/(..)$/\.$1/r), | |
77 | priceCurrency => 'GBP', | |
78 | availability => "http://schema.org/$stock", | |
79 | ($include_url ? (url => "/details/$data{product}/" . make_slug $data{title}) : ()) | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
fc536c37 | 84 | our %highlight; |
6e33dd68 MG |
85 | sub form_table_row { |
86 | my ($data, $tr) = @_; | |
fc536c37 | 87 | $tr->attr(class => 'highlight') if $highlight{$data->{product}}; |
6e33dd68 MG |
88 | $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle stock/; |
89 | $tr->fclass('price')->replace_content(stringify_money $data->{price}); | |
8bbff1bc | 90 | $tr->fclass('freepost')->detach unless $data->{freepost}; |
6e33dd68 | 91 | $tr->fclass('title')->attr('data-product', $data->{product}); |
1576fc41 MG |
92 | $tr->fclass('title')->attr('href', '/details/'.$data->{product}.'/'.make_slug $data->{title}); |
93 | # $tr->fclass('title')->attr('data-summary', $data->{summary}); | |
6e33dd68 MG |
94 | $tr->look_down(_tag => 'input')->attr(max => $data->{stock}); |
95 | $tr->look_down(_tag => 'input')->attr(name => 'quant'.$data->{product}); | |
96 | } | |
97 | ||
98 | sub form_app { | |
99 | my ($env) = @_; | |
1576fc41 | 100 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); |
fc536c37 | 101 | my $req = Plack::Request->new($env); |
6e33dd68 | 102 | |
fc536c37 | 103 | local %highlight = map { $_ => 1 } $req->param('highlight'); |
1576fc41 | 104 | my $data = $db{$$}->select(products => '*', {}, 'product')->hashes; |
6e33dd68 MG |
105 | my $tree = $form->clone; |
106 | $tree->find('tbody')->find('tr')->iter3($data, \&form_table_row); | |
107 | ||
108 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] | |
109 | } | |
110 | ||
111 | sub continue_table_row { | |
112 | my ($data, $tr) = @_; | |
113 | $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle quantity/; | |
8bbff1bc | 114 | $tr->fclass('freepost')->detach unless $data->{freepost}; |
6e33dd68 MG |
115 | $tr->fclass('price')->replace_content(stringify_money $data->{subtotal}); |
116 | $tr->fclass('title')->attr('data-product', $data->{product}); | |
117 | } | |
118 | ||
119 | sub continue_app { | |
120 | my ($env) = @_; | |
1576fc41 | 121 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); |
6e33dd68 MG |
122 | my $tree = $continue->clone; |
123 | my $req = Plack::Request->new($env); | |
124 | my $params = $req->body_parameters; | |
125 | ||
8bbff1bc | 126 | my ($quant, $quant_freepost, $total, @data, @notes) = (0) x 3; |
6e33dd68 MG |
127 | for (sort keys %$params) { |
128 | next unless /^quant/; | |
129 | next unless $params->{$_}; | |
1576fc41 | 130 | my $data = $db{$$}->select(products => '*', {product => substr $_, 5})->hash; |
6e33dd68 MG |
131 | $data->{quantity} = $params->{$_}; |
132 | if ($data->{stock} == 0) { | |
133 | push @notes, 'Item is out of stock and was removed from order: '.$data->{title}; | |
134 | next | |
135 | } | |
136 | if ($data->{quantity} > $data->{stock}) { | |
137 | $data->{quantity} = $data->{stock}; | |
138 | push @notes, 'Not enough units of "'.$data->{title}.'" available. Quantity reduced to '.$data->{quantity} | |
139 | } | |
140 | $data->{subtotal} = $data->{price} * $data->{quantity}; | |
141 | $quant += $data->{quantity}; | |
8bbff1bc | 142 | $quant_freepost += $data->{quantity} if $data->{freepost}; |
6e33dd68 MG |
143 | $total += $data->{subtotal}; |
144 | push @data, $data | |
145 | } | |
146 | ||
1576fc41 MG |
147 | return [500, ['Content-type' => 'text/plain'], ['Error: no items in order.']] unless $quant; |
148 | ||
6e33dd68 MG |
149 | $tree->fid('subtotal')->replace_content(stringify_money $total); |
150 | my $dvalue; | |
151 | if ($params->{discount}) { | |
1576fc41 | 152 | my $discount = $db{$$}->select(discounts => '*', {discount => $params->{discount}})->hash; |
6e33dd68 MG |
153 | if (!defined $discount) { |
154 | push @notes, 'Discount code incorrect. No discount applied.' | |
1576fc41 | 155 | } elsif ($db{$$}->select(orders => 'COUNT(*)', {discount => $params->{discount}})->list) { |
6e33dd68 MG |
156 | push @notes, 'Discount code already used once. No discount applied.' |
157 | } else { | |
158 | $dvalue = int (0.5 + $discount->{fraction} * $total) if $discount->{fraction}; | |
159 | $dvalue = $discount->{flat} if $discount->{flat}; | |
160 | $tree->fid('discount')->replace_content('-'.stringify_money $dvalue); | |
161 | $total -= $dvalue; | |
162 | $tree->look_down(name => 'discount')->attr(value => $params->{discount}); | |
163 | push @notes, 'Discount applied.' | |
164 | } | |
165 | } | |
166 | $tree->look_down(name => 'discount')->detach unless $dvalue; | |
167 | $tree->fid('discount_tr')->detach unless $dvalue; | |
8bbff1bc MG |
168 | my $postage = 220 + 50 * ($quant - $quant_freepost); |
169 | $postage = 0 if $quant == $quant_freepost; | |
6e33dd68 MG |
170 | $tree->fid('postage')->replace_content(stringify_money $postage); |
171 | $total += $postage; | |
172 | $tree->fid('total')->replace_content(stringify_money $total); | |
173 | ||
174 | $tree->fid('order')->find('tbody')->find('tr')->iter3(\@data, \&continue_table_row); | |
175 | $tree->iter($tree->fid('notes')->find('li') => @notes); | |
176 | ||
177 | $tree->look_down(name => 'products')->attr(value => encode_json \@data); | |
178 | $tree->look_down(name => 'total')->attr(value => $total); | |
179 | ||
180 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] | |
181 | } | |
182 | ||
183 | sub order_app { | |
184 | my ($env) = @_; | |
1576fc41 | 185 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); |
6e33dd68 MG |
186 | my $tree = $order->clone; |
187 | my $req = Plack::Request->new($env); | |
1576fc41 MG |
188 | my ($id) = $env->{PATH_INFO} =~ m,^/([0-9A-F]+),; |
189 | if ($id) { | |
85e0d9a2 MG |
190 | my $total = $db{$$}->select(orders => 'total', {id => $id})->list or |
191 | return [500, ['Content-type', 'text/plain'], ['Order not found']]; | |
1576fc41 MG |
192 | $tree->fid('orderid')->replace_content($id); |
193 | $tree->look_down(name => 'order')->attr(value => $id); | |
194 | $tree->fid('total')->replace_content(stringify_money $total); | |
195 | $tree->find('script')->attr('data-amount', $total); | |
196 | return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] | |
197 | } else { | |
198 | my %parms = %{$req->body_parameters}; | |
199 | my $id = sprintf "%X%04X", time, $$; | |
200 | my $err; | |
201 | try { | |
202 | $db{$$}->begin_work; | |
203 | my $products = decode_json $req->body_parameters->{products}; | |
204 | for my $prod (@$products) { | |
205 | my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list; | |
206 | die "Not enough of " .$prod->{title}."\n" if $prod->{quantity} > $stock; | |
207 | $db{$$}->update(products => {stock => $stock - $prod->{quantity}}, {product => $prod->{product}}); | |
208 | } | |
209 | $db{$$}->insert(orders => {id => $id, %parms}); | |
210 | $db{$$}->commit; | |
211 | } catch { | |
212 | $db{$$}->rollback; | |
213 | $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]] | |
214 | }; | |
215 | return $err if $err; | |
216 | return [303, [Location => "/order/$id"], []] | |
6e33dd68 | 217 | } |
1576fc41 MG |
218 | } |
219 | ||
5eeddbd0 MG |
220 | sub cancel { |
221 | my ($order) = @_; | |
222 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); | |
223 | $order = $db{$$}->select(orders => '*', {id => $order})->hash; | |
224 | my $products = decode_json $order->{products}; | |
225 | $db{$$}->begin_work; | |
226 | try { | |
227 | for my $prod (@$products) { | |
228 | my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list; | |
229 | $db{$$}->update(products => {stock => $stock + $prod->{quantity}}, {product => $prod->{product}}); | |
230 | } | |
231 | $db{$$}->delete(orders => {id => $order->{id}}); | |
232 | $db{$$}->commit; | |
233 | } catch { | |
234 | $db{$$}->rollback; | |
235 | die $_ | |
236 | } | |
237 | } | |
238 | ||
1576fc41 MG |
239 | sub details_list_element { |
240 | my ($data, $li) = @_; | |
241 | $li->find('a')->attr(href => "/$data"); | |
242 | my $thumb = $data =~ s/fullpics/thumbs/r; | |
243 | $thumb = $data unless -f $thumb; | |
244 | $li->find('img')->attr(src => "/$thumb"); | |
245 | } | |
246 | ||
247 | sub details_app { | |
248 | my ($env) = @_; | |
249 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); | |
250 | my $tree = $details->clone; | |
251 | my ($id) = $env->{PATH_INFO} =~ m,^/(\d+),; | |
8bb7ab90 | 252 | my %data = %{$db{$$}->select(products => '*', {product => $id})->hash}; |
1576fc41 | 253 | my @pics = <static/fullpics/$id-*>; |
8bb7ab90 MG |
254 | my $slug = make_slug $data{title}; |
255 | $tree->find('title')->replace_content("$data{title} | ledparts4you"); | |
256 | $tree->find('h2')->replace_content($data{title}); | |
257 | $tree->fid('summary')->replace_content($data{summary}); | |
1576fc41 | 258 | $tree->look_down(rel => 'canonical')->attr(href => "/details/$id/$slug"); |
b66954a0 | 259 | $tree->fid('pictures')->find('li')->iter3(\@pics, \&details_list_element); |
8bb7ab90 | 260 | $tree->fid('jsonld')->replace_content(encode_json product_to_schemaorg '', %data); |
6e33dd68 | 261 | |
fc536c37 MG |
262 | for my $ahref ($tree->find('a')) { |
263 | $ahref->attr(href => "/form?highlight=$id") if $ahref->attr('href') eq '/'; | |
264 | } | |
265 | ||
6e33dd68 MG |
266 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]] |
267 | } | |
268 | ||
1576fc41 MG |
269 | sub pay_app { |
270 | my ($env) = @_; | |
271 | my $req = Plack::Request->new($env); | |
272 | $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:'); | |
273 | my $order = $req->body_parameters->{order}; | |
274 | my $token = $req->body_parameters->{stripeToken}; | |
275 | return [500, ['Content-type' => 'text/html; charset=utf-8'], ['No token received, payment did not succeed.']] unless $token; | |
276 | $db{$$}->update(orders => {stripe_token => $token}, {id => $order}); | |
277 | [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]]; | |
278 | } | |
279 | ||
6e33dd68 | 280 | sub app { |
1576fc41 | 281 | my $footer = read_file 'tmpl/footer.html'; |
6e33dd68 | 282 | builder { |
1576fc41 MG |
283 | enable sub { |
284 | my $app = shift; | |
285 | sub { | |
286 | my $res = $app->(@_); | |
0c3c4e70 | 287 | $res->[2][0] =~ s,</body>,$footer</body>, if $res->[0] == 200; |
1576fc41 MG |
288 | $res; |
289 | } | |
290 | }; | |
6e33dd68 MG |
291 | mount '/' => sub { [301, [Location => '/form'], []] }; |
292 | mount '/form' => \&form_app; | |
293 | mount '/continue' => \&continue_app; | |
294 | mount '/order' => \&order_app; | |
1576fc41 MG |
295 | mount '/details' => \&details_app; |
296 | mount '/pay' => \&pay_app; | |
6e33dd68 MG |
297 | } |
298 | } | |
299 | ||
300 | 1; | |
301 | __END__ | |
302 | ||
303 | =head1 NAME | |
304 | ||
305 | App::Web::Oof - Oversimplified order form / ecommerce website | |
306 | ||
307 | =head1 SYNOPSIS | |
308 | ||
309 | use App::Web::Oof; | |
310 | ||
311 | =head1 DESCRIPTION | |
312 | ||
313 | Oof (Oversimplified order form) is a very simple ecommerce website. | |
39be4169 MG |
314 | It is the code behind L<https://ledparts4you.uk.to>. |
315 | ||
316 | This version is reasonably functional, yet not very reusable, hence | |
317 | the version number. | |
6e33dd68 MG |
318 | |
319 | =head1 AUTHOR | |
320 | ||
321 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
322 | ||
323 | =head1 COPYRIGHT AND LICENSE | |
324 | ||
325 | Copyright (C) 2016 by Marius Gavrilescu | |
326 | ||
327 | This library is free software; you can redistribute it and/or modify | |
328 | it under the same terms as Perl itself, either Perl version 5.22.1 or, | |
329 | at your option, any later version of Perl 5 you may have available. | |
330 | ||
331 | ||
332 | =cut |