BEGIN;
  CREATE OR REPLACE FUNCTION get_merchant_secret(merchantID uuid)
  RETURNS TABLE(first_key text, second_key text) AS $$
  BEGIN
    UPDATE
      merchants AS m
    SET
      first_key = m.second_key,
      first_ttl = m.second_ttl,
      second_key = md5((''::text || now()::text) || random()::text),
      second_ttl = CURRENT_TIMESTAMP + (10 * interval '1 minute')
    WHERE
      CURRENT_TIMESTAMP > m.first_ttl
    AND CURRENT_TIMESTAMP < m.second_ttl;
    UPDATE
      merchants
    SET
      first_key = md5((''::text || now()::text) || random()::text),
      first_ttl = CURRENT_TIMESTAMP + (10 * interval '1 minute'),
      second_key = md5((''::text || now()::text) || random()::text),
      second_ttl = CURRENT_TIMESTAMP + (20 * interval '1 minute')
    WHERE
      CURRENT_TIMESTAMP > second_ttl;
    RETURN QUERY SELECT m.first_key, m.second_key FROM merchants m WHERE m.id = merchantID LIMIT 1;
  END
  $$ LANGUAGE PLPGSQL;
END;