merchant

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

pg_insert_deposit_to_transfer.sql (4198B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2024, 2025 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 
     18 DROP FUNCTION IF EXISTS merchant_insert_deposit_to_transfer;
     19 CREATE FUNCTION merchant_insert_deposit_to_transfer (
     20   IN in_deposit_serial INT8,
     21   IN in_coin_contribution taler_amount_currency,
     22   IN in_execution_time INT8,
     23   IN in_exchange_url TEXT,
     24   IN in_h_wire BYTEA,
     25   IN in_exchange_sig BYTEA,
     26   IN in_exchange_pub BYTEA,
     27   IN in_wtid BYTEA,
     28   OUT out_dummy BOOL)
     29 LANGUAGE plpgsql
     30 AS $$
     31 DECLARE
     32   my_signkey_serial INT8;
     33   my_account_serial INT8;
     34   my_decose INT8;
     35   my_expected_credit_serial INT8;
     36   my_wire_pending_cleared BOOL;
     37 BEGIN
     38   -- Just to return something (for now).
     39   out_dummy=FALSE;
     40 
     41 -- Find exchange sign key
     42 SELECT signkey_serial
     43   INTO my_signkey_serial
     44   FROM merchant_exchange_signing_keys
     45  WHERE exchange_pub=in_exchange_pub
     46    ORDER BY start_date DESC
     47    LIMIT 1;
     48 
     49 IF NOT FOUND
     50 THEN
     51   -- Maybe 'keys' is outdated, try again in 8 hours.
     52   UPDATE merchant_deposits
     53      SET settlement_last_ec=2029 -- MERCHANT_EXCHANGE_SIGN_PUB_UNKNOWN
     54         ,settlement_last_http_status=200
     55         ,settlement_last_detail=ENCODE(in_exchange_pub, 'hex')
     56         ,settlement_wtid=in_wtid
     57         ,settlement_retry_needed=TRUE
     58         ,settlement_retry_time=(EXTRACT(epoch FROM (CURRENT_TIME + interval '8 hours')) * 1000000)::INT8
     59    WHERE deposit_serial=in_deposit_serial;
     60   RETURN;
     61 END IF;
     62 
     63 -- Find deposit confirmation
     64 SELECT deposit_confirmation_serial
     65   INTO my_decose
     66   FROM merchant_deposits
     67  WHERE deposit_serial=in_deposit_serial;
     68 
     69 -- Find merchant account
     70 SELECT account_serial
     71   INTO my_account_serial
     72   FROM merchant_deposit_confirmations mdc
     73   JOIN merchant_accounts ma
     74     USING (account_serial)
     75  WHERE mdc.deposit_confirmation_serial=my_decose
     76    AND ma.h_wire=in_h_wire;
     77 
     78 IF NOT FOUND
     79 THEN
     80   -- Merchant account referenced in exchange response is unknown to us.
     81   -- Remember fatal error and do not try again.
     82   UPDATE merchant_deposits
     83      SET settlement_last_ec=2558 -- MERCHANT_EXCHANGE_TRANSFERS_TARGET_ACCOUNT_UNKNOWN
     84         ,settlement_last_http_status=200
     85         ,settlement_last_detail=ENCODE(in_h_wire, 'hex')
     86         ,settlement_wtid=in_wtid
     87         ,settlement_retry_needed=FALSE
     88         ,settlement_coin_contribution=in_coin_contribution
     89         ,signkey_serial=my_signkey_serial
     90         ,settlement_exchange_sig=in_exchange_sig
     91    WHERE deposit_serial=in_deposit_serial;
     92   RETURN;
     93 END IF;
     94 
     95 
     96 -- Make sure wire transfer is expected.
     97 SELECT expected_credit_serial
     98   INTO my_expected_credit_serial
     99   FROM merchant_expected_transfers
    100   WHERE wtid=in_wtid
    101     AND exchange_url=in_exchange_url
    102     AND account_serial=my_account_serial;
    103 
    104 IF NOT FOUND
    105 THEN
    106   INSERT INTO merchant_expected_transfers
    107     (exchange_url
    108     ,wtid
    109     ,account_serial
    110     ,expected_time)
    111    VALUES
    112     (in_exchange_url
    113     ,in_wtid
    114     ,my_account_serial
    115     ,in_execution_time)
    116    RETURNING expected_credit_serial
    117      INTO my_expected_credit_serial;
    118 END IF;
    119 
    120 -- Finally, update merchant_deposits so we do not try again.
    121 UPDATE merchant_deposits
    122    SET settlement_last_ec=0
    123       ,settlement_last_http_status=200
    124       ,settlement_last_detail=NULL
    125       ,settlement_wtid=in_wtid
    126       ,settlement_retry_needed=FALSE
    127       ,settlement_coin_contribution=in_coin_contribution
    128       ,settlement_expected_credit_serial=my_expected_credit_serial
    129       ,signkey_serial=my_signkey_serial
    130       ,settlement_exchange_sig=in_exchange_sig
    131  WHERE deposit_serial=in_deposit_serial;
    132 
    133 -- MERCHANT_WIRE_TRANSFER_EXPECTED
    134 NOTIFY XR6849FMRD2AJFY1E2YY0GWA8GN0YT407Z66WHJB0SAKJWF8G2Q60;
    135 
    136 END $$;