merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

delete_order.sql (2261B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2026 Taler Systems SA
      4 --
      5 -- TALER is free software; you can redistribute it and/or modify it under the
      6 -- terms of the GNU General Public License as published by the Free Software
      7 -- Foundation; either version 3, or (at your option) any later version.
      8 --
      9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 -- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 --
     13 -- You should have received a copy of the GNU General Public License along with
     14 -- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 --
     16 
     17 DROP FUNCTION IF EXISTS merchant_do_delete_order;
     18 CREATE FUNCTION merchant_do_delete_order (
     19   IN in_order_id TEXT,
     20   IN in_now INT8,
     21   IN in_force BOOLEAN,
     22   OUT out_deleted BOOLEAN)
     23 LANGUAGE plpgsql
     24 AS $$
     25 DECLARE
     26   my_paid BOOLEAN;
     27   my_claimed BOOLEAN;
     28 BEGIN
     29 
     30 -- An order that was claimed by a wallet has a row in
     31 -- merchant_contract_terms; the row in merchant_orders (if any) then
     32 -- merely holds the not-yet-claimed proposal.  Both deletions must
     33 -- happen together, or a failure between them would leave the contract
     34 -- terms behind without the order.  Doing both in one stored procedure
     35 -- gives us that atomicity even though the caller runs in autocommit.
     36 SELECT paid
     37   INTO my_paid
     38   FROM merchant_contract_terms
     39  WHERE order_id=in_order_id
     40    FOR UPDATE;
     41 my_claimed = FOUND;
     42 
     43 DELETE FROM merchant_orders
     44  WHERE order_id=in_order_id
     45    AND (  (pay_deadline < in_now)
     46        OR (NOT my_claimed)
     47        OR (in_force AND (NOT my_paid)) );
     48 out_deleted = FOUND;
     49 
     50 IF NOT in_force
     51 THEN
     52   RETURN;
     53 END IF;
     54 
     55 DELETE FROM merchant_contract_terms
     56  WHERE order_id=in_order_id
     57    AND NOT paid;
     58 IF FOUND
     59 THEN
     60   out_deleted = TRUE;
     61 END IF;
     62 
     63 END $$;
     64 
     65 COMMENT ON FUNCTION merchant_do_delete_order(TEXT, INT8, BOOLEAN)
     66   IS 'Deletes an order. The unclaimed proposal is removed if the pay deadline'
     67      ' has passed, if the order was never claimed, or (with in_force) if it'
     68      ' was claimed but not paid.  With in_force the claimed-but-unpaid'
     69      ' contract terms are removed in the same transaction.  Returns'
     70      ' out_deleted=TRUE if anything was deleted.';