From 7e39e5f5496eda0251837aa64447936591b606ca Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Tue, 13 Feb 2024 20:16:12 +0100 Subject: [PATCH] Create new drafts if trying to modify an already pending payment This can happen when the customer reaches the payment page, but then returns back to the booking form via the back button: the browser remembers the URI with the cart slug, trying to make it ready, and then it fails because it is already pending. I did not like the idea of modifying a payment that is already not a draft, because it seems to me that can lead to errors if we receive Redsys notifications of payments that are being changed back to draft. In fact, i believe that draft payments maybe should go to a different relation altogether, so that i can prevent UPDATE on payment by guests, but maybe i am going overboard now. --- deploy/draft_payment.sql | 3 +++ test/draft_payment.sql | 25 +++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/deploy/draft_payment.sql b/deploy/draft_payment.sql index 887f10a..1c3e4c6 100644 --- a/deploy/draft_payment.sql +++ b/deploy/draft_payment.sql @@ -22,6 +22,9 @@ $$ declare p payment; begin + if exists(select 1 from payment where slug = payment_slug and payment_status <> 'draft') then + payment_slug = null; + end if; insert into payment ( slug , company_id diff --git a/test/draft_payment.sql b/test/draft_payment.sql index 49c65b2..26ded34 100644 --- a/test/draft_payment.sql +++ b/test/draft_payment.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(13); +select plan(14); set search_path to camper, public; @@ -95,13 +95,15 @@ values (16, 4, 800) , (20, 8, 590) ; -insert into payment (payment_id, slug, company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences, created_at, updated_at) -values (22, '7cccfe16-695e-486d-a6a5-1162fb85cafb', 2, 12, '2024-08-30', '2024-09-01', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', '2024-01-01 01:01:01', '2024-01-01 01:01:01') +insert into payment (payment_id, slug, company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences, payment_status, created_at, updated_at) +values (22, '7cccfe16-695e-486d-a6a5-1162fb85cafb', 2, 12, '2024-08-30', '2024-09-01', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', 'draft', '2024-01-01 01:01:01', '2024-01-01 01:01:01') + , (24, '6eeae04c-2fea-4d67-97dc-a4b8a83df99f', 2, 12, '2024-08-31', '2024-09-01', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', 'pending', '2024-01-01 02:02:02', '2024-01-01 02:02:02') ; insert into payment_option (payment_id, campsite_type_option_id, units, subtotal) values (22, 16, 1, 0) , (22, 18, 1, 0) + , (24, 16, 2, 0) ; select lives_ok( @@ -114,18 +116,29 @@ select lives_ok( 'Should be able to update the draft for Plots' ); +select results_ne( + $$ select slug::text from draft_payment('6eeae04c-2fea-4d67-97dc-a4b8a83df99f', '2024-08-01', '2024-08-06', 'b065f4e3-2cc8-491d-a413-d015d7d00183', 2, 1, 1, 1, 'under a tree', array[(16, 1), (18, 1)]::option_units[]) $$, + $$ values ('6eeae04c-2fea-4d67-97dc-a4b8a83df99f') $$, + 'When trying to draft a payment already pending, completed, failed, or refunded, create a new instead' +); + select bag_eq( $$ select company_id, campsite_type_id, arrival_date::text, departure_date::text, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences, payment_status, created_at, updated_at from payment $$, $$ values (2, 12, '2024-08-28', '2024-09-04', 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, 'pref I before E', 'draft', '2024-01-01 01:01:01', current_timestamp) , (2, 14, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, '', 'draft', current_timestamp, current_timestamp) + , (2, 12, '2024-08-31', '2024-09-01', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', 'pending', '2024-01-01 02:02:02', '2024-01-01 02:02:02') + , (2, 14, '2024-08-01', '2024-08-06', 85000, 2, 0, 1, 0, 1, 0, 1, 0, 3500, 96000, 'under a tree', 'draft', current_timestamp, current_timestamp) $$, 'Should have added and updated payments' ); select bag_eq( - $$ select payment_id, campsite_type_option_id, units, subtotal from payment_option $$, - $$ values (22, 16, 2, 10200) - , (22, 20, 3, 2070) + $$ select case payment_id when 22 then 'a' when 24 then 'b' else 'c' end, campsite_type_option_id, units, subtotal from payment_option $$, + $$ values ('a', 16, 2, 10200) + , ('a', 20, 3, 2070) + , ('b', 16, 2, 0) + , ('c', 16, 1, 4000) + , ('c', 18, 1, 3500) $$, 'Should have added, updated, and removed payment options' );