\q
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.13
-- Dumped by pg_dump version 15.13

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: cardlink; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA cardlink;


ALTER SCHEMA cardlink OWNER TO postgres;

--
-- Name: create_invoice(numeric, text, vgate.cardlink_currency); Type: FUNCTION; Schema: cardlink; Owner: postgres
--

CREATE FUNCTION cardlink.create_invoice(amount numeric, order_id text, currency vgate.cardlink_currency) RETURNS json
    LANGUAGE plpgsql SECURITY DEFINER
    AS $$
DECLARE
    shop_id TEXT := current_setting('app.cardlink_shop_id');
    api_url TEXT := current_setting('app.api_cardlink_url');
    api_token TEXT := current_setting('app.cardlink_api_token');
    data JSONB;
    response JSON;
BEGIN
    RAISE NOTICE 'order_id: %', order_id;

    data := jsonb_build_object(
        'amount', amount,
        'currency_in', currency,
        'shop_id', shop_id,
        'order_id', order_id
    );

    RAISE LOG 'data: %', data;

    SELECT content::json
    FROM http((
        'POST',
        format('%s/bill/create', api_url),
        ARRAY[('Authorization', format('Bearer %s', api_token))::http_header],
        'application/json',
        data::VARCHAR
    )::http_request)
    INTO response;

    IF (response->>'success')::BOOLEAN != true THEN
        RAISE EXCEPTION 'Cardlink error: %s', response::TEXT;
    END IF;
        RAISE NOTICE 'response: %', response;

    /*
        {
          "bill_id": "...",
          "link_page_url": "...",
          "link_url": "...",
          "success": "..." // true or false
        }
    */
    RETURN response;
END
$$;


ALTER FUNCTION cardlink.create_invoice(amount numeric, order_id text, currency vgate.cardlink_currency) OWNER TO postgres;

--
-- PostgreSQL database dump complete
--

