completed requests to ofd.ru
This commit is contained in:
52
net/net.cpp
52
net/net.cpp
@@ -1,5 +1,6 @@
|
||||
#include "net.h"
|
||||
#include <curl/curl.h>
|
||||
#include "../utils/utils.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
@@ -32,6 +33,12 @@ void write_modules(void *buffer, size_t size, size_t nmemb, void *modules) {
|
||||
}
|
||||
}
|
||||
|
||||
size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp) {
|
||||
size_t totalSize = size * nmemb;
|
||||
((std::string*)userp)->append(std::string((char*)contents));
|
||||
return totalSize;
|
||||
}
|
||||
|
||||
std::vector<std::string> Net::get_all_modules(std::string url) {
|
||||
CURL *handle = curl_easy_init();
|
||||
|
||||
@@ -47,7 +54,7 @@ std::vector<std::string> Net::get_all_modules(std::string url) {
|
||||
return modules;
|
||||
}
|
||||
|
||||
std::string Net::get_file(std::string url, std::string filename) {
|
||||
void Net::get_file(std::string url, std::string filename) {
|
||||
CURL *handle = curl_easy_init();
|
||||
|
||||
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
|
||||
@@ -58,6 +65,45 @@ std::string Net::get_file(std::string url, std::string filename) {
|
||||
auto success = curl_easy_perform(handle);
|
||||
|
||||
curl_easy_cleanup(handle);
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string Net::fetch_check_data_from_ofdru(std::string fn, std::string fd, std::string fi, std::string datetime, int operation, int total, std::string captcha) {
|
||||
CURL *handle = curl_easy_init();
|
||||
if (handle == nullptr) {
|
||||
std::cerr << "cannot initialize curl" << std::endl;
|
||||
return "";
|
||||
}
|
||||
struct curl_slist *headers = NULL;
|
||||
std::string readBuffer = "";
|
||||
|
||||
curl_easy_setopt(handle, CURLOPT_URL, "https://check.ofd.ru/Document/FetchReceiptFromFns");
|
||||
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json;charset=UTF-8");
|
||||
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
|
||||
|
||||
std::string dataJSON =
|
||||
"{"
|
||||
"\"TotalSum\":" + std::to_string(total) + ","
|
||||
"\"FnNumber\":\"" + fn + "\","
|
||||
"\"ReceiptOperationType\":\"" + std::to_string(operation) + "\","
|
||||
"\"DocNumber\":\"" + fd + "\","
|
||||
"\"DocFiscalSign\":\"" + fi + "\","
|
||||
"\"Captcha\":\"" + captcha + "\","
|
||||
"\"DocDateTime\":\"" + datetime + ".000Z\""
|
||||
"}";
|
||||
|
||||
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, dataJSON.c_str());
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeCallback);
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &readBuffer);
|
||||
|
||||
auto answer = curl_easy_perform(handle);
|
||||
|
||||
delete headers;
|
||||
curl_easy_cleanup(handle);
|
||||
|
||||
return readBuffer;
|
||||
}
|
||||
|
||||
void Net::get_captcha_from_ofdru() {
|
||||
get_file("https://check.ofd.ru/api/captcha/common/img", get_path_relative_to_home(".local/share/checks_parser/captcha.png"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user