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

CREATE SCHEMA cryptomus;


ALTER SCHEMA cryptomus OWNER TO postgres;

--
-- Name: create_invoice(numeric, text, uuid, text, text, text, text, boolean, integer, text, integer, numeric, text, text[], text[], text, text, integer, boolean); Type: FUNCTION; Schema: cryptomus; Owner: postgres
--

CREATE FUNCTION cryptomus.create_invoice(amount numeric, currency text, order_id uuid, network text DEFAULT NULL::text, url_return text DEFAULT NULL::text, url_success text DEFAULT NULL::text, url_callback text DEFAULT NULL::text, is_payment_multiple boolean DEFAULT NULL::boolean, lifetime integer DEFAULT NULL::integer, to_currency text DEFAULT NULL::text, subtract integer DEFAULT NULL::integer, accuracy_payment_percent numeric DEFAULT NULL::numeric, additional_data text DEFAULT NULL::text, currencies text[] DEFAULT NULL::text[], except_currencies text[] DEFAULT NULL::text[], course_source text DEFAULT NULL::text, from_referral_code text DEFAULT NULL::text, discount_percent integer DEFAULT NULL::integer, is_refresh boolean DEFAULT NULL::boolean) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    data JSONB;
    merchant_id TEXT := current_setting('app.cryptomus_merchant');
    response_status INT;
    response JSON;
BEGIN
    data := jsonb_build_object(
        'amount', amount::TEXT,
        'currency', currency,
        'order_id', order_id
    );

    IF network IS NOT NULL THEN
        data := data || jsonb_build_object('network', network);
    END IF;
    IF url_return IS NOT NULL THEN
        data := data || jsonb_build_object('url_return', url_return);
    END IF;
    IF url_success IS NOT NULL THEN
        data := data || jsonb_build_object('url_success', url_success);
    END IF;
    IF url_callback IS NOT NULL THEN
        data := data || jsonb_build_object('url_callback', url_callback);
    END IF;
    IF is_payment_multiple IS NOT NULL THEN
        data := data || jsonb_build_object('is_payment_multiple', is_payment_multiple);
    END IF;
    IF lifetime IS NOT NULL THEN
        data := data || jsonb_build_object('lifetime', lifetime);
    END IF;
    IF to_currency IS NOT NULL THEN
        data := data || jsonb_build_object('to_currency', to_currency);
    END IF;
    IF subtract IS NOT NULL THEN
        data := data || jsonb_build_object('subtract', subtract);
    END IF;
    IF accuracy_payment_percent IS NOT NULL THEN
        data := data || jsonb_build_object('accuracy_payment_percent', accuracy_payment_percent);
    END IF;
    IF additional_data IS NOT NULL THEN
        data := data || jsonb_build_object('additional_data', additional_data);
    END IF;
    IF currencies IS NOT NULL THEN
        data := data || jsonb_build_object('currencies', currencies);
    END IF;
    IF except_currencies IS NOT NULL THEN
        data := data || jsonb_build_object('except_currencies', except_currencies);
    END IF;
    IF course_source IS NOT NULL THEN
        data := data || jsonb_build_object('course_source', course_source);
    END IF;
    IF from_referral_code IS NOT NULL THEN
        data := data || jsonb_build_object('from_referral_code', from_referral_code);
    END IF;
    IF discount_percent IS NOT NULL THEN
        data := data || jsonb_build_object('discount_percent', discount_percent);
    END IF;
    IF is_refresh IS NOT NULL THEN
        data := data || jsonb_build_object('is_refresh', is_refresh);
    END IF;

    SELECT status, content::json
    FROM http(
        (
            'POST',
            format(
                '%s/payment',
                current_setting('app.api_cryptomus_url')
            ),
            ARRAY[
                http_header('merchant', merchant_id),
                http_header('sign', cryptomus.sign(data))
            ],
            'application/json',
            data::TEXT
        )::http_request
    ) INTO response_status, response;

    IF response_status != 200 THEN
        RAISE EXCEPTION 'Cryptomus error: %', response::TEXT;
    END IF;

    RETURN response;
END
$$;


ALTER FUNCTION cryptomus.create_invoice(amount numeric, currency text, order_id uuid, network text, url_return text, url_success text, url_callback text, is_payment_multiple boolean, lifetime integer, to_currency text, subtract integer, accuracy_payment_percent numeric, additional_data text, currencies text[], except_currencies text[], course_source text, from_referral_code text, discount_percent integer, is_refresh boolean) OWNER TO postgres;

--
-- Name: sign(jsonb); Type: FUNCTION; Schema: cryptomus; Owner: postgres
--

CREATE FUNCTION cryptomus.sign(data jsonb) RETURNS text
    LANGUAGE plpgsql
    AS $$
DECLARE
    api_key TEXT := current_setting('app.cryptomus_api_key');
    b64_data_hex TEXT;
    sign TEXT;
BEGIN
    RAISE NOTICE 'data: %', data::TEXT;
    RAISE NOTICE 'api_key: %', api_key;

    b64_data_hex := REPLACE(encode(data::TEXT::BYTEA, 'base64'), E'\n', '');
    RAISE NOTICE 'b64_data_hex: %', b64_data_hex;

    sign := md5(
        b64_data_hex || api_key
    );
    RAISE NOTICE 'sign: %', sign;

    RETURN sign;
END
$$;


ALTER FUNCTION cryptomus.sign(data jsonb) OWNER TO postgres;

--
-- PostgreSQL database dump complete
--

