declare
    l_request clob;
    l_response clob;
    l_idempotency_key varchar2(35);
    l_source_id varchar2(64);   -- token
    l_location_id varchar2(32); -- location id
    l_amount_money_amount number;
    e_create_payment_failed exception;
begin
    -- guid for idempotency_key.
    l_idempotency_key := regexp_replace(lower(rawtohex(sys_guid())),'(........)(....)(....)(................)','\1-\2-\3-\4');
    l_amount_money_amount := to_number(:P1_AMOUNT);
    l_source_id           := apex_application.g_x01;
    l_location_id         := apex_application.g_x02;
    select json_object(
        key 'source_id' value l_source_id,
        key 'location_id' value l_location_id,
        key 'idempotency_key' value l_idempotency_key,
        key 'amount_money' value json_object(
            key 'amount' value l_amount_money_amount,
            key 'currency' value 'JPY'
        )
    ) into l_request from dual;
    apex_debug.info(l_request);
    apex_web_service.clear_request_headers();
    apex_web_service.set_request_headers('Square-Version','2023-08-16',p_reset => false);
    apex_web_service.set_request_headers('Content-Type','application/json',p_reset => false);
    l_response := apex_web_service.make_rest_request(
        p_url => :SQUARE_ENDPOINT || '/v2/payments'
        ,p_http_method => 'POST'
        ,p_body => l_request
        ,p_credential_static_id => 'SQUARE_CRED'
    );
    if apex_web_service.g_status_code <> 200 then
        raise e_create_payment_failed;
    end if;
    htp.p(l_response);
end;