-- Test process_payment_response
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(24);

set search_path to camper, public;

select has_function('camper', 'process_payment_response', array['uuid', 'redsys_response']);
select function_lang_is('camper', 'process_payment_response', array['uuid', 'redsys_response'], 'plpgsql');
select function_returns('camper', 'process_payment_response', array['uuid', 'redsys_response'], 'text');
select isnt_definer('camper', 'process_payment_response', array['uuid', 'redsys_response']);
select volatility_is('camper', 'process_payment_response', array['uuid', 'redsys_response'], 'volatile');
select function_privs_are('camper', 'process_payment_response', array ['uuid', 'redsys_response'], 'guest', array['EXECUTE']);
select function_privs_are('camper', 'process_payment_response', array ['uuid', 'redsys_response'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'process_payment_response', array ['uuid', 'redsys_response'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'process_payment_response', array ['uuid', 'redsys_response'], 'authenticator', array[]::text[]);

set client_min_messages to warning;
truncate payment_redsys_response cascade;
truncate payment cascade;
truncate campsite_type cascade;
truncate media cascade;
truncate media_content cascade;
truncate company cascade;
reset client_min_messages;

insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, rtc_number, tourist_tax, tourist_tax_max_days, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 350, 7, 'ES', 'EUR', 'ca')
;

insert into media_content (media_type, bytes)
values ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ffffff","a"};')
;

insert into media (media_id, company_id, original_filename, content_hash)
values (10, 2, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};'))
;

insert into campsite_type (campsite_type_id, slug, company_id, name, media_id, max_campers, bookable_nights, overflow_allowed)
values (12, 'c1b6f4fc-32c1-4cd5-b796-0c5059152a52', 2, 'Plots', 10, 6, '[1, 7]', true)
;

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, currency_code, zone_preferences, acsi_card, payment_status, created_at, updated_at)
values (22, '4ef35e2f-ef98-42d6-a724-913bd761ca8c', 2, 12, '2024-08-28', '2024-09-04', 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, 'EUR', 'pref I before E', false, 'draft', '2024-01-01 01:01:01', '2024-01-01 01:01:01')
     , (24, '6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'pending', '2024-01-02 02:02:02', '2024-01-02 02:02:02')
     , (26, '8d38a482-8a25-4d85-9929-e5f425fcac04', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'completed', '2024-01-03 03:03:03', '2024-01-03 03:03:03')
     , (28, 'b770f8b7-f148-4ab4-a786-aa070af598e5', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'failed', '2024-01-04 04:04:04', '2024-01-04 04:04:04')
     , (30, '31910d73-d343-44b7-8a29-f7e075b64933', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'refunded', '2024-01-05 05:05:05', '2024-01-05 05:05:05')
     , (32, 'c9488490-ac09-4402-90cd-f6f0546f04c0', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'pending', '2024-01-05 05:05:05', '2024-01-05 05:05:05')
     , (34, '5819823e-c0ac-4baa-a3ae-515fbb70e909', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'pending', '2024-01-05 05:05:05', '2024-01-06 06:06:06')
     , (36, 'f2871c2d-e11a-41e8-b264-0a8605c77dc1', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'pending', '2024-01-05 05:05:05', '2024-01-06 06:06:06')
     , (38, '01505d14-6f4d-48a2-9a98-3a2099ab7eef', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'preauth', '2024-01-05 05:05:05', '2024-01-06 06:06:06')
     , (40, '7cae7d1c-d626-41e0-b1c5-48359e515579', 2, 12, '2024-08-29', '2024-09-03', 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', '', false, 'preauth', '2024-01-05 05:05:05', '2024-01-06 06:06:06')
;

insert into payment_redsys_response (payment_id, response_code, date_time, secure_payment, transaction_type, amount, currency_code, order_number, authorization_code, merchant_code, terminal_number, error_code)
values (28, 0, '2023-01-01 01:01:01', false, 1, 1000, 'EUR', 'huh?', '123', '1234567', 5, '123')
;

select is(
	process_payment_response('4ef35e2f-ef98-42d6-a724-913bd761ca8c', row('3322445', 2, 0, '2024-02-02 12:23:34', true, 0, '12.35', 'EUR', '000000224ef3', '124', '')::redsys_response),
	'',
	'Should not change a draft payment'
);

select is(
	process_payment_response('6d1b8e4c-c3c6-4fe4-92c1-2cbf94526693', row('3322446', 2, 100, '2024-02-03 12:23:34', false, 0, '12.36', 'USD', '000000246d1b', '125', 'ERR')::redsys_response),
	'failed',
	'Should fail a pending payment if response code > 99'
);

select is(
	process_payment_response('8d38a482-8a25-4d85-9929-e5f425fcac04', row('3322447', 3, 0, '2024-02-04 12:23:34', true, 0, '12.37', 'EUR', '000000268d3a', '126', '')::redsys_response),
	'',
	'Should not change a completed payment'
);

select is(
	process_payment_response('b770f8b7-f148-4ab4-a786-aa070af598e5', row('3322448', 4, 99, '2024-02-05 12:23:34', true, 0, '12.38', 'EUR', '00000028b770', '127', '')::redsys_response),
	'completed',
	'Should change a failed payment if response code < 100'
);

select is(
	process_payment_response('31910d73-d343-44b7-8a29-f7e075b64933', row('3322449', 5, 0, '2024-02-06 12:23:34', false, 0, '12.39', 'EUR', '000000303190', '128', '')::redsys_response),
	'',
	'Should not change a refunded payment'
);

select is(
	process_payment_response('c9488490-ac09-4402-90cd-f6f0546f04c0', row('3322450', 6, 0, '2024-02-07 12:23:34', true, 0, '12.40', 'EUR', '00000032c948', '129', 'NOPE')::redsys_response),
	'completed',
	'Should change a pending payment if response code < 100'
);

select is(
	process_payment_response('c9488490-ac09-4402-90cd-f6f0546f04c0', row('3322450', 6, 0, '2024-02-07 12:23:34', true, 0, '12.40', 'EUR', '00000032c948', '129', '')::redsys_response),
	'',
	'Should NOT change a payment twice'
);

select is(
	process_payment_response('f2871c2d-e11a-41e8-b264-0a8605c77dc1', row('3322450', 5, 0, '2024-02-07 12:23:34', true, 1, '12.40', 'EUR', '00000032c948', '130', '')::redsys_response),
	'preauth',
	'Should preauth a pending payment if response code < 100'
);

select is(
	process_payment_response('f2871c2d-e11a-41e8-b264-0a8605c77dc1', row('3322450', 5, 900, '2024-02-07 12:23:34', true, 2, '12.40', 'EUR', '00000032c948', '131', '')::redsys_response),
	'completed',
	'Should confirm a preauth payment if response code = 900'
);

select is(
	process_payment_response('01505d14-6f4d-48a2-9a98-3a2099ab7eef', row('3322450', 5, 101, '2024-02-07 12:23:34', true, 2, '12.40', 'EUR', '00000032c948', '132', '')::redsys_response),
	'preauth',
	'Should leave a preauth payment as is if response code <> 900'
);

select is(
	process_payment_response('7cae7d1c-d626-41e0-b1c5-48359e515579', row('3322450', 5, 400, '2024-02-07 12:23:34', true, 9, '12.40', 'EUR', '00000032c948', '133', '')::redsys_response),
	'voided',
	'Should void a preauth payment if response code = 400'
);

select is(
	process_payment_response('01505d14-6f4d-48a2-9a98-3a2099ab7eef', row('3322450', 5, 900, '2024-02-07 12:23:34', true, 9, '12.40', 'EUR', '00000032c948', '134', '')::redsys_response),
	'preauth',
	'Should leave a preauth payment as is if response code <> 400'
);


select throws_ok(
	$$ select process_payment_response('5819823e-c0ac-4baa-a3ae-515fbb70e909', row('3322445', 2, 0, '2024-02-02 12:23:34', false, 13, '12.41', 'USD', '000000345819', '135', '')::redsys_response) $$,
	'22023', '13 is not a processable transaction type',
	'Only transaction types 0, 1, 2, 3, and 9 are allowed.'
);

select bag_eq(
	$$ select payment_id, payment_status, updated_at from payment $$,
	$$ values (22, 'draft', '2024-01-01 01:01:01')
	        , (24, 'failed', current_timestamp)
	        , (26, 'completed', '2024-01-03 03:03:03')
	        , (28, 'completed', current_timestamp)
	        , (30, 'refunded', '2024-01-05 05:05:05')
	        , (32, 'completed', current_timestamp)
	        , (34, 'pending', '2024-01-06 06:06:06')
	        , (36, 'completed', current_timestamp)
	        , (38, 'preauth', current_timestamp)
	        , (40, 'voided', current_timestamp)
	$$,
	'Should have updated payments'
);

select bag_eq(
	$$ select payment_id, merchant_code, terminal_number, response_code, date_time::text, secure_payment, transaction_type, amount, currency_code, order_number, authorization_code, error_code from payment_redsys_response $$,
	$$ values (22, '3322445', 2,   0, '2024-02-02 12:23:34',  true, 0, 1235, 'EUR', '000000224ef3', '124', '')
	        , (24, '3322446', 2, 100, '2024-02-03 12:23:34', false, 0, 1236, 'USD', '000000246d1b', '125', 'ERR')
	        , (26, '3322447', 3,   0, '2024-02-04 12:23:34',  true, 0, 1237, 'EUR', '000000268d3a', '126', '')
	        , (28, '1234567', 5,   0, '2023-01-01 01:01:01', false, 1, 1000, 'EUR',         'huh?', '123', '123')
	        , (28, '3322448', 4,  99, '2024-02-05 12:23:34',  true, 0, 1238, 'EUR', '00000028b770', '127', '')
	        , (30, '3322449', 5,   0, '2024-02-06 12:23:34', false, 0, 1239, 'EUR', '000000303190', '128', '')
	        , (32, '3322450', 6,   0, '2024-02-07 12:23:34',  true, 0, 1240, 'EUR', '00000032c948', '129', 'NOPE')
	        , (32, '3322450', 6,   0, '2024-02-07 12:23:34',  true, 0, 1240, 'EUR', '00000032c948', '129', '')
	        , (36, '3322450', 5,   0, '2024-02-07 12:23:34', true, 1, 1240, 'EUR', '00000032c948', '130', '')
	        , (36, '3322450', 5, 900, '2024-02-07 12:23:34', true, 2, 1240, 'EUR', '00000032c948', '131', '')
	        , (38, '3322450', 5, 101, '2024-02-07 12:23:34', true, 2, 1240, 'EUR', '00000032c948', '132', '')
	        , (38, '3322450', 5, 900, '2024-02-07 12:23:34', true, 9, 1240, 'EUR', '00000032c948', '134', '')
	        , (40, '3322450', 5, 400, '2024-02-07 12:23:34', true, 9, 1240, 'EUR', '00000032c948', '133', '')
	$$,
	'Should have added responses'
);


select *
from finish();

rollback;
