From 6180bf6ea51edbc288e0ee724f064c94490cbaea Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Mon, 16 Jan 2023 03:52:00 +0100 Subject: [PATCH] Add new hook_checkout_prepare, rollback earlier change of hook_checkout In hindsight, it was a bad idea to allow manipulating the cart (entries) in hook_checkout, because that hook is used by the `log` plugin. You now get unused entries in the log. Although that plugin should maybe have used hook_checkout_done, existing log file readers (including scripts) and custom plugins may depend on the CHECKOUT items in the log being before the BALANCE items. --- lib/RevBank/Cart.pm | 11 +++++++---- plugins/statiegeld_tokens | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/RevBank/Cart.pm b/lib/RevBank/Cart.pm index 58816da..9357300 100644 --- a/lib/RevBank/Cart.pm +++ b/lib/RevBank/Cart.pm @@ -79,15 +79,18 @@ sub checkout($self, $user) { RevBank::FileIO::with_lock { my $transaction_id = time() - 1300000000; + + RevBank::Plugins::call_hooks("checkout_prepare", $self, $user, $transaction_id); + for my $entry (@$entries) { + $entry->sanity_check; + $entry->user($user) if not $entry->user; + } + RevBank::Plugins::call_hooks("checkout", $self, $user, $transaction_id); my %deltas = ($user => RevBank::Amount->new(0)); for my $entry (@$entries) { - # In case entries were added by the hook - $entry->sanity_check; - $entry->user($user) if not $entry->user; - $deltas{$_->{user}} += $_->{amount} * $entry->quantity for $entry, $entry->contras; } diff --git a/plugins/statiegeld_tokens b/plugins/statiegeld_tokens index edb19ab..90be2ec 100644 --- a/plugins/statiegeld_tokens +++ b/plugins/statiegeld_tokens @@ -89,7 +89,7 @@ sub _handle_undo($cart) { } } -sub hook_checkout($class, $cart, $username, $transaction_id, @) { +sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) { if ($username eq '-undo') { _handle_undo($cart); return;