merchant

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

pg_insert_deposit_to_transfer.sql (4242B)


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