Interchange6.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Interchange6/Routes/Checkout.pm ↑ Summary Interchange6/Cart/DBIC.pm »
1 package Dancer::Plugin::Interchange6;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Dancer qw(:syntax !before !after);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Dancer::Plugin::DBIC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Dancer::Plugin::Auth::Extensible;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Dancer::Plugin::Interchange6::Cart;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Dancer::Plugin::Interchange6::Business::OnlinePayment;
13  
14 =head1 NAME
15  
16 Dancer::Plugin::Interchange6 - Interchange6 Shop Plugin for Dancer
17  
18 =head1 VERSION
19  
20 Version 0.071
21  
22 =cut
23  
24 our $VERSION = '0.071';
25  
26 =head1 REQUIREMENTS
27  
28 All Interchange6 Dancer applications need to use the L<Dancer::Session::DBIC>
29 engine.
30  
31 The easiest way to configure this is in your main module, just after all
32 the C<use> statements:
33  
34 set session => 'DBIC';
35 set session_options => {schema => schema};
36  
37 =head1 ROUTES
38  
39 You can use the L<Dancer::Plugin::Interchange6::Routes> plugin bundled with this
40 plugin to setup standard routes for:
41  
42 =over 4
43  
44 =item product listing
45  
46 =item product display
47  
48 =item cart display
49  
50 =item checkout form
51  
52 =back
53  
54 To enable these routes, you put the C<shop_setup_routes> keyword at the end
55 of your main module:
56  
57 package MyShop;
58  
59 use Dancer ':syntax';
60 use Dancer::Plugin::Interchange6;
61 use Dancer::Plugin::Interchange6::Routes;
62  
63 get '/shop' => sub {
64 ...
65 };
66  
67 ...
68  
69 shop_setup_routes;
70  
71 true;
72  
73 Please refer to L<Dancer::Plugin::Interchange6::Routes> for configuration options
74 and further information.
75  
76 =head1 KEYWORDS
77  
78 =head2 shop_cart
79  
80 Returns L<Dancer::Plugin::Interchange6::Cart> object.
81  
82 =head2 shop_charge
83  
84 Creates payment order and authorizes amount.
85  
86 =head2 shop_schema
87  
88 Returns L<Interchange6::Schema> object.
89  
90 =head2 shop_...
91  
92 Accessors for L<Interchange6::Schema> result classes. You can use it
93 to retrieve a single object or the corresponding result set.
94  
95 shop_product('F0001')->uri;
96  
97 shop_navigation->search({type => 'manufacturer',
98 active => 1});
99  
100 Available accessors are C<shop_address>, C<shop_attribute>, C<shop_country>,
101 C<shop_message>, C<shop_navigation>, C<shop_order>, C<shop_product>,
102 C<shop_state> and C<shop_user>.
103  
104 =head1 HOOKS
105  
106 This plugin installs the following hooks:
107  
108 =head2 Add to cart
109  
110 The functions registered for these hooks receive the cart object
111 and the item to be added as parameters.
112  
113 =over 4
114  
115 =item before_cart_add_validate
116  
117 Triggered before item is validated for adding to the cart.
118  
119 =item before_cart_add
120  
121 Triggered before item is added to the cart.
122  
123 =item after_cart_add
124  
125 Triggered after item is added to the cart.
126 Used by DBI backend to save item to the database.
127  
128 =back
129  
130 =head2 Update cart
131  
132 The functions registered for these hooks receive the cart object,
133 the current item in the cart and the updated item.
134  
135 =over 4
136  
137 =item before_cart_update
138  
139 Triggered before cart item is updated (changing quantity).
140  
141 =item after_cart_update
142  
143 Triggered after cart item is updated (changing quantity).
144 Used by DBI backend to update item to the database.
145  
146 =back
147  
148 =head2 Remove from cart
149  
150 The functions registered for these hooks receive the cart object
151 and the item to be added as parameters.
152  
153 =over 4
154  
155 =item before_cart_remove_validate
156  
157 Triggered before item is validated for removal.
158 Receives cart object and item SKU.
159  
160 =item before_cart_remove
161  
162 Triggered before item is removed from the cart.
163 Receives cart object and item.
164  
165 =item after_cart_remove
166  
167 Triggered after item is removed from the cart.
168 Used by DBI backend to delete item from the database.
169 Receives cart object and item.
170  
171 =back
172  
173 =head2 Clear cart
174  
175 =over 4
176  
177 =item before_cart_clear
178  
179 Triggered before cart is cleared.
180  
181 =item after_cart_clear
182  
183 Triggered after cart is cleared.
184  
185 =back
186  
187 =head2 Rename cart
188  
189 The functions registered for these hooks receive the cart object,
190 the old name and the new name.
191  
192 =over 4
193  
194 =item before_cart_rename
195  
196 Triggered before cart is renamed.
197  
198 =item after_cart_rename
199  
200 Triggered after cart is renamed.
201  
202 =item before_cart_set_users_id
203  
204 Triggered before users_id is set for the cart.
205  
206 =item after_cart_set_users_id
207  
208 Triggered after users_id is set for the cart.
209  
210 =item before_cart_set_sessions_id
211  
212 Triggered before sessions_id is set for the cart.
213  
214 =item after_cart_set_sessions_id
215  
216 Triggered after sessions_id is set for the cart.
217  
218 =back
219  
220 =head1 EXPIRE DBIC SESSIONS
221  
222 This command expires/manages DBIC sessions and carts. NOTE: For proper
223 functionality please copy/link to Dancer App/bin directory.
224  
225 interchange6-expire-sessions
226  
227 =cut
228  
229 register_hook(qw/before_cart_add_validate
230 before_cart_add after_cart_add
231 before_cart_update after_cart_update
232 before_cart_remove_validate
233 before_cart_remove after_cart_remove
234 before_cart_rename after_cart_rename
235 before_cart_clear after_cart_clear
236 before_cart_set_users_id after_cart_set_users_id
237 before_cart_set_sessions_id after_cart_set_sessions_id
238 /);
239  
240 register shop_schema => sub {
241 _shop_schema(@_);
242 };
243  
244 register shop_address => sub {
245 _shop_resultset('Address', @_);
246 };
247  
248 register shop_attribute => sub {
249 _shop_resultset('Attribute', @_);
250 };
251  
252 register shop_country => sub {
253 _shop_resultset('Country', @_);
254 };
255  
256 register shop_message => sub {
257 _shop_resultset('Message', @_);
258 };
259  
260 register shop_navigation => sub {
261 _shop_resultset('Navigation', @_);
262 };
263  
264 register shop_order => sub {
265 _shop_resultset('Order', @_);
266 };
267  
268 register shop_product => sub {
269 _shop_resultset('Product', @_);
270 };
271  
272 register shop_state => sub {
273 _shop_resultset('State', @_);
274 };
275  
276 register shop_user => sub {
277 _shop_resultset('User', @_);
278 };
279  
280 register shop_charge => sub {
281 my (%args) = @_;
282 my ($schema, $bop_object, $payment_settings, $provider, $provider_settings);
283  
284 $payment_settings = plugin_setting->{payment};
285  
286 # determine payment provider
287 if (exists $args{provider} && $args{provider}) {
288 $provider = $args{provider};
289 }
290 else {
291 $provider = $payment_settings->{default_provider};
292 }
293  
294 if (exists $payment_settings->{providers}->{$provider}) {
295 $provider_settings = $payment_settings->{providers}->{$provider};
296 }
297 else {
298 die "Settings for provider $provider missing.";
299 }
300  
301 my %payment_data = (payment_mode => $provider,
302 status => 'request',
303 sessions_id => session->id,
304 payment_action => 'charge',
305 amount => $args{amount},
306 users_id => session('logged_in_user_id'),
307 );
308  
309 # create payment order
310 $schema = _shop_schema();
311  
312 my $payment_order = $schema->resultset('PaymentOrder')->create(\%payment_data);
313  
314 # create BOP object wrapper with provider settings
315 $bop_object = Dancer::Plugin::Interchange6::Business::OnlinePayment->new($provider, %$provider_settings);
316  
317 $bop_object->payment_order($payment_order);
318  
319 # call charge method
320 debug "Charging with the following parameters: ", \%args;
321  
322 $bop_object->charge(%args);
323  
324 if ($bop_object->is_success) {
325 $payment_order->update({
326 status => 'success',
327 auth_code => $bop_object->authorization,
328 });
329 }
330 else {
331 $payment_order->update({
332 status => 'failure',
333 payment_error_code => $bop_object->error_code,
334 payment_error_message => $bop_object->error_message,
335 });
336 }
337  
338 return $bop_object;
339 };
340  
341 register cart => \&_shop_cart;
342 register shop_cart => \&_shop_cart;
343  
344 sub _shop_cart {
345 my (%args, $user_ref);
346  
347 %args = (
348 sessions_id => session->id,
349 execute_hook => sub {execute_hook(@_)},
350 );
351  
352 # we have a cart name
353 $args{name} = $_[0] if @_ == 1;
354  
355 if ($user_ref = logged_in_user) {
356  
357 # user is logged in
358 $args{users_id} = $user_ref->users_id;
359 }
360  
361 return Dancer::Plugin::Interchange6::Cart->new( %args );
362 };
363  
364 sub _shop_schema {
365 my $schema_key;
366  
367 if (@_) {
368 $schema_key = $_[0];
369 }
370 else {
371 $schema_key = 'default';
372 }
373  
374 return schema($schema_key);
375 };
376  
377 sub _shop_resultset {
378 my ($name, $key) = @_;
379  
380 if (defined $key) {
381 return resultset($name)->find($key);
382 }
383  
384 return resultset($name);
385 };
386  
387 register_plugin;
388  
389 =head1 ACKNOWLEDGEMENTS
390  
391 The L<Dancer> developers and community for their great application framework
392 and for their quick and competent support.
393  
394 Peter Mottram for his patches.
395  
396 =head1 LICENSE AND COPYRIGHT
397  
398 Copyright 2010-2015 Stefan Hornburg (Racke).
399  
400 This program is free software; you can redistribute it and/or modify it
401 under the terms of either: the GNU General Public License as published
402 by the Free Software Foundation; or the Artistic License.
403  
404 See http://dev.perl.org/licenses/ for more information.
405  
406 =head1 SEE ALSO
407  
408 L<Interchange6>, L<Interchange6::Schema>
409  
410 =cut
411  
412 1;
« Interchange6/Routes/Checkout.pm ↑ Summary Interchange6/Cart/DBIC.pm »