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

CREATE SCHEMA crystalpay;


ALTER SCHEMA crystalpay OWNER TO postgres;

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

CREATE FUNCTION crystalpay.create_invoice(amount numeric, lifetime integer, invoice_type text DEFAULT 'purchase'::text, amount_currency text DEFAULT NULL::text, required_method text DEFAULT NULL::text, payer_details text DEFAULT NULL::text, description text DEFAULT NULL::text, extra text DEFAULT NULL::text, redirect_url text DEFAULT NULL::text, callback_url text DEFAULT NULL::text) RETURNS json
    LANGUAGE plpgsql
    AS $$
DECLARE
    data JSONB;
    auth_login TEXT := (SELECT current_setting('app.crystalpay_login'));
    auth_secret TEXT := (SELECT current_setting('app.crystalpay_secret'));
    response_status INT;
    redirect_url TEXT;
    response JSON;
BEGIN
    data := jsonb_build_object(
        'auth_login', auth_login,
        'auth_secret', auth_secret,
        'amount', amount,
        'type', invoice_type,
        'lifetime', lifetime
    );
    IF amount_currency IS NOT NULL THEN
        data := data || jsonb_build_object('amount_currency', amount_currency);
    END IF;
    IF required_method IS NOT NULL THEN
        data := data || jsonb_build_object('required_method', required_method);
    END IF;
    IF payer_details IS NOT NULL THEN
        data := data || jsonb_build_object('payer_details', payer_details);
    END IF;
    IF description IS NOT NULL THEN
        data := data || jsonb_build_object('description', description);
    END IF;
    IF extra IS NOT NULL THEN
        data := data || jsonb_build_object('extra', extra);
    END IF;
    IF redirect_url IS NOT NULL THEN
        data := data || jsonb_build_object('redirect_url', redirect_url);
    END IF;
    IF callback_url IS NOT NULL THEN
        data := data || jsonb_build_object('callback_url', callback_url);
    END IF;

    SELECT content::json
    FROM http_post(
        format('%s/invoice/create/', current_setting('app.api_crystalpay_url')),
        data::VARCHAR,
        'application/json'
    )
    INTO response;

    IF (response->>'error')::BOOLEAN != false THEN
        RAISE EXCEPTION 'Crystalpay error: %', response->>'errors';
    END IF;

    return response;
END
$$;


ALTER FUNCTION crystalpay.create_invoice(amount numeric, lifetime integer, invoice_type text, amount_currency text, required_method text, payer_details text, description text, extra text, redirect_url text, callback_url text) OWNER TO postgres;

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

GRANT USAGE ON SCHEMA crystalpay TO vgate_provider;


--
-- PostgreSQL database dump complete
--

