\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: aaio; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA aaio;


ALTER SCHEMA aaio OWNER TO postgres;

--
-- Name: currency; Type: TYPE; Schema: aaio; Owner: postgres
--

CREATE TYPE aaio.currency AS ENUM (
    'USD',
    'EUR',
    'UAH',
    'RUB'
);


ALTER TYPE aaio.currency OWNER TO postgres;

--
-- Name: create_invoice(text); Type: FUNCTION; Schema: aaio; Owner: postgres
--

CREATE FUNCTION aaio.create_invoice(order_id text) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    merchant_id UUID = current_setting('app.aaio_merchant')::UUID;
    data TEXT;
    response_status INT;
    response JSON;
BEGIN
    data = format(
        'merchant_id=%s&order_id=%s',
        merchant_id,
        order_id
    );

    SELECT status, content::json
    FROM http((
            'GET',
            format(
                'https://aaio.so/api/info-pay',
                current_setting('app.api_aaio_url')
            ),
            ARRAY[
                http_header('Accept', 'application/json'),
                http_header('Content-Type', 'application/x-www-form-urlencoded'),
                http_header('X-Api-Key', current_setting('app.aaio_api_key'))
            ],
            'application/x-www-form-urlencoded',
            data
        )::http_request
    ) INTO response_status, response;

    IF response_status != 200 THEN
        RAISE sqlstate 'PT503' USING
            message = format('Failed to create invoice: %s', response::TEXT);
    ELSE
        RETURN response;
    END IF;
END
$$;


ALTER FUNCTION aaio.create_invoice(order_id text) OWNER TO postgres;

--
-- Name: create_invoice(double precision, aaio.currency, uuid, text, text, text, text); Type: FUNCTION; Schema: aaio; Owner: postgres
--

CREATE FUNCTION aaio.create_invoice(amount double precision, currency aaio.currency, order_id uuid, method text DEFAULT NULL::text, description text DEFAULT NULL::text, email text DEFAULT NULL::text, lang text DEFAULT NULL::text) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    merchant_id UUID = current_setting('app.aaio_merchant')::UUID;
    sign TEXT;
    data TEXT;
    response_status INT;
    response JSON;
BEGIN
    sign = encode(
        digest(
            format(
                '%s:%s:%s:%s:%s',
                merchant_id,
                amount,
                currency,
                current_setting('app.aaio_secret_1'),
                order_id
            ),
            'sha256'
        ), 
        'hex'
    );
    data = format(
        'merchant_id=%s&amount=%s&order_id=%s&sign=%s&currency=%s',
        merchant_id,
        amount,
        order_id,
        sign, 
        currency
    );
    IF method IS NOT NULL THEN
        data = data || format('&method=%s', method);
    END IF;
    IF description IS NOT NULL THEN
        data = data || format('&desc=%s', description);
    END IF;
    IF email IS NOT NULL THEN
        data = data || format('&email=%s', email);
    END IF;
    IF lang IS NOT NULL THEN
        data = data || format('&lang=%s', lang);
    END IF;

    SELECT status, content::json
    FROM http((
            'POST',
            format(
                '%s/get_pay_url',
                current_setting('app.api_aaio_url')
            ),
            ARRAY[
                http_header('Accept', 'application/json'),
                http_header('Content-Type', 'application/x-www-form-urlencoded')
            ],
            'application/x-www-form-urlencoded',
            data
        )::http_request
    ) INTO response_status, response;

    IF response_status != 200 THEN
        RAISE sqlstate 'PT503' USING
            message = format('Failed to create invoice: %s', response::TEXT);
    ELSE
        RETURN response;
    END IF;
END
$$;


ALTER FUNCTION aaio.create_invoice(amount double precision, currency aaio.currency, order_id uuid, method text, description text, email text, lang text) OWNER TO postgres;

--
-- Name: create_invoice(numeric, aaio.currency, uuid, text, text, text, text); Type: FUNCTION; Schema: aaio; Owner: postgres
--

CREATE FUNCTION aaio.create_invoice(amount numeric, currency aaio.currency, order_id uuid, method text DEFAULT NULL::text, description text DEFAULT NULL::text, email text DEFAULT NULL::text, lang text DEFAULT NULL::text) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    merchant_id UUID = current_setting('app.aaio_merchant')::UUID;
    sign TEXT;
    data TEXT;
    response_status INT;
    response JSON;
BEGIN
    sign = encode(
        digest(
            format(
                '%s:%s:%s:%s:%s',
                merchant_id,
                amount,
                currency,
                current_setting('app.aaio_secret_1'),
                order_id
            ),
            'sha256'
        ), 
        'hex'
    );
    data = format(
        'merchant_id=%s&amount=%s&order_id=%s&sign=%s&currency=%s',
        merchant_id,
        amount,
        order_id,
        sign, 
        currency
    );
    IF method IS NOT NULL THEN
        data = data || format('&method=%s', method);
    END IF;
    IF description IS NOT NULL THEN
        data = data || format('&desc=%s', description);
    END IF;
    IF email IS NOT NULL THEN
        data = data || format('&email=%s', email);
    END IF;
    IF lang IS NOT NULL THEN
        data = data || format('&lang=%s', lang);
    END IF;

    SELECT status, content::json
    FROM http((
            'POST',
            format(
                '%s/get_pay_url',
                current_setting('app.api_aaio_url')
            ),
            ARRAY[
                http_header('Accept', 'application/json'),
                http_header('Content-Type', 'application/x-www-form-urlencoded')
            ],
            'application/x-www-form-urlencoded',
            data
        )::http_request
    ) INTO response_status, response;

    IF response_status != 200 THEN
        RAISE sqlstate 'PT503' USING
            message = format('Failed to create invoice: %s', response::TEXT);
    ELSE
        RETURN response;
    END IF;
END
$$;


ALTER FUNCTION aaio.create_invoice(amount numeric, currency aaio.currency, order_id uuid, method text, description text, email text, lang text) OWNER TO postgres;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: invoice; Type: TABLE; Schema: aaio; Owner: postgres
--

CREATE TABLE aaio.invoice (
    id text NOT NULL,
    amount numeric NOT NULL,
    user_id text NOT NULL,
    payment_system text NOT NULL,
    general_state jsonb NOT NULL
)
INHERITS (common.created_at, common.updated_at);


ALTER TABLE aaio.invoice OWNER TO postgres;

--
-- Name: invoice created_at; Type: DEFAULT; Schema: aaio; Owner: postgres
--

ALTER TABLE ONLY aaio.invoice ALTER COLUMN created_at SET DEFAULT now();


--
-- Name: invoice updated_at; Type: DEFAULT; Schema: aaio; Owner: postgres
--

ALTER TABLE ONLY aaio.invoice ALTER COLUMN updated_at SET DEFAULT now();


--
-- Name: invoice invoice_pkey; Type: CONSTRAINT; Schema: aaio; Owner: postgres
--

ALTER TABLE ONLY aaio.invoice
    ADD CONSTRAINT invoice_pkey PRIMARY KEY (id);


--
-- Name: SCHEMA aaio; Type: ACL; Schema: -; Owner: postgres
--

GRANT USAGE ON SCHEMA aaio TO vgate_provider;
GRANT USAGE ON SCHEMA aaio TO anon;


--
-- Name: FUNCTION create_invoice(order_id text); Type: ACL; Schema: aaio; Owner: postgres
--

GRANT ALL ON FUNCTION aaio.create_invoice(order_id text) TO vgate_provider;


--
-- Name: FUNCTION create_invoice(amount double precision, currency aaio.currency, order_id uuid, method text, description text, email text, lang text); Type: ACL; Schema: aaio; Owner: postgres
--

GRANT ALL ON FUNCTION aaio.create_invoice(amount double precision, currency aaio.currency, order_id uuid, method text, description text, email text, lang text) TO vgate_provider;


--
-- Name: TABLE invoice; Type: ACL; Schema: aaio; Owner: postgres
--

GRANT SELECT,INSERT ON TABLE aaio.invoice TO vgate_provider;


--
-- PostgreSQL database dump complete
--

