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

CREATE SCHEMA freekassa;


ALTER SCHEMA freekassa OWNER TO postgres;

--
-- Name: create_invoice(numeric, text, text); Type: FUNCTION; Schema: freekassa; Owner: postgres
--

CREATE FUNCTION freekassa.create_invoice(amount numeric, currency text, order_id text) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    "form_data" TEXT;
    "secret" TEXT := common.get_setting('app.freekassa_secret_1');
    "shop_id" INT := common.get_setting('app.freekassa_shop_id');
    "sign" TEXT := md5(
        format(
            '%s:%s:%s:%s:%s',
            "shop_id",
            "amount",
            "secret",
            "currency",
            "order_id"
        )
    );
BEGIN
    "form_data" = format(
        'm=%s&oa=%s&currency=%s&o=%s&s=%s',
        "shop_id", "amount", "currency", "order_id", "sign"
    );

    RETURN json_build_object(
        'invoice_url',
        format(
            '%s?%s', common.get_setting('app.freekassa_form_url'), "form_data"
        )
    );
END
$$;


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

--
-- Name: create_invoice_api(integer, text, text, numeric, text, text, text, text, text, text, text, text, text, numeric); Type: FUNCTION; Schema: freekassa; Owner: postgres
--

CREATE FUNCTION freekassa.create_invoice_api(i integer, email text, ip text, amount numeric, currency text, payment_id text DEFAULT NULL::text, tel text DEFAULT NULL::text, success_url text DEFAULT NULL::text, failure_url text DEFAULT NULL::text, notification_url text DEFAULT NULL::text, recurrent text DEFAULT NULL::text, recurrent_period text DEFAULT NULL::text, recurrent_description text DEFAULT NULL::text, recurrent_order_id numeric DEFAULT NULL::numeric) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    "response_status" INT;
    "response" JSON;
    "data" JSONB;
    "shop_id" INT := common.get_setting('app.freekassa_shop_id');
BEGIN
    "data" := jsonb_build_object(
        'nonce', (SELECT nextval('freekassa_nonce')),
        'shopId', "shop_id",
        'i', "i",
        'email', "email",
        'ip', "ip",
        'amount', "amount",
        'currency', "currency"
    );

    IF "payment_id" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{paymentId}', to_jsonb("payment_id"));
	END IF;
    IF "tel" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{tel}', to_jsonb("tel"));
	END IF;
    IF "success_url" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{success_url}', to_jsonb("success_url"));
	END IF;
    IF "failure_url" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{failure_url}', to_jsonb("failure_url"));
	END IF;
    IF "notification_url" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{notification_url}', to_jsonb("notification_url"));
	END IF;
    IF "recurrent" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{recurrent}', to_jsonb("recurrent"));
	END IF;
    IF "recurrent_period" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{recurrent_period}', to_jsonb("recurrent_period"));
	END IF;
    IF "recurrent_description" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{recurrent_description}', to_jsonb("recurrent_description"));
	END IF;
    IF "recurrent_order_id" IS NOT NULL THEN 
		"data" := jsonb_set("data", '{recurrent_order_id}', to_jsonb("recurrent_order_id"));
	END IF;

    "data" := freekassa.sign_data("data");

    RAISE NOTICE 'freekassa data: %', "data";

    SELECT "status", "content"::JSON
    FROM http(
        (
            'POST',
            format(
                '%s/orders/create',
                common.get_setting('app.freekassa_api_url')
            ),
            NULL,
            'application/json',
            "data"::TEXT
        )::http_request
    ) INTO "response_status", "response";

    IF response_status != 200 THEN
        RAISE EXCEPTION 'Freekassa error: %', "response"::TEXT;
    ELSE
        RETURN "response";
    END IF;
END
$$;


ALTER FUNCTION freekassa.create_invoice_api(i integer, email text, ip text, amount numeric, currency text, payment_id text, tel text, success_url text, failure_url text, notification_url text, recurrent text, recurrent_period text, recurrent_description text, recurrent_order_id numeric) OWNER TO postgres;

--
-- Name: sign_data(jsonb); Type: FUNCTION; Schema: freekassa; Owner: postgres
--

CREATE FUNCTION freekassa.sign_data(data jsonb) RETURNS jsonb
    LANGUAGE plpgsql
    AS $$
DECLARE
    "data_values" TEXT;
    "sign" TEXT;
    "api_key" TEXT = common.get_setting('app.freekassa_api_key');
BEGIN
    SELECT string_agg("value", '|' ORDER BY "key")
    FROM jsonb_each_text("data")
    INTO "data_values";

    IF "data_values" IS NULL THEN
        RAISE EXCEPTION 'freekassa sign: empty json data';
    END IF;

    RAISE NOTICE 'freekassa sign: %', "data_values";

    sign := encode(
        hmac(data_values, api_key, 'sha256'),
        'hex'
    );

	"data" := jsonb_set("data", '{signature}', to_jsonb("sign"));

    RAISE NOTICE 'freekassa sign: %', "data";

    RETURN "data";
END
$$;


ALTER FUNCTION freekassa.sign_data(data jsonb) OWNER TO postgres;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: currency; Type: TABLE; Schema: freekassa; Owner: postgres
--

CREATE TABLE freekassa.currency (
    id integer NOT NULL,
    name text NOT NULL
)
INHERITS (common.created_at, common.updated_at);


ALTER TABLE freekassa.currency OWNER TO postgres;

--
-- Name: currency created_at; Type: DEFAULT; Schema: freekassa; Owner: postgres
--

ALTER TABLE ONLY freekassa.currency ALTER COLUMN created_at SET DEFAULT now();


--
-- Name: currency updated_at; Type: DEFAULT; Schema: freekassa; Owner: postgres
--

ALTER TABLE ONLY freekassa.currency ALTER COLUMN updated_at SET DEFAULT now();


--
-- Name: currency currency_pkey; Type: CONSTRAINT; Schema: freekassa; Owner: postgres
--

ALTER TABLE ONLY freekassa.currency
    ADD CONSTRAINT currency_pkey PRIMARY KEY (id);


--
-- PostgreSQL database dump complete
--

