SockectComm.cpp
int retries = 6;
while (retries > 0) {
// Connect to server
if (connect(sockfd, reinterpret_cast<sockaddr *>(&serv_addr), sizeof(serv_addr)) == SOCKET_ERROR) {
std::cerr << "Unable to connect to server: " << WSAGetLastError() << std::endl;
if (retries > 1) {
int result = MessageBox(NULL, "Няма връзка със сървъра, n Следващ опит след 10 секунди", "Error",
MB_RETRYCANCEL | MB_ICONERROR);
if (result == IDCANCEL) {
return false; // User clicked Cancel, return false
}
Sleep(10000); // Sleep for 10 seconds before retrying
}
retries--;
} else {
return true; // Connection successful
}
}
// If all retries failed, clean up and return false
closesocket(sockfd);
WSACleanup();
return false;
}
//--------------------------------------------------------------------------------------------
Finalizetpayment.cpp
void FinalizePayment::generateCardPayment(const int &receiptId, int terminalId, float paymentSum) {
m_query->Close();
m_query->SQL->Text = "SELECT pos_name FROM ref_pos_account WHERE REF_POS_ACCOUNT_ID = :REF_POS_ACCOUNT_ID";
m_query->ParamByName("REF_POS_ACCOUNT_ID")->AsInteger = terminalId;
m_query->ExecQuery();
bool datecsFound = false;
while (!m_query->Eof) {
AnsiString posNameAnsi = m_query->FieldByName("pos_name")->AsString;
std::string posName = posNameAnsi.c_str();
if (posName.find("Датекс") != std::string::npos) {
datecsFound = true;
break;
}
m_query->Next();
}
m_query->Close();
// Proceed with the socket communication only if "Датекс" terminal is found
int transactionNo = 0;
if (datecsFound) {
// Socket communication
float totalPrice = getTotal(receiptId);
SocketCommunication socketComm;
if (!socketComm.connectToServer()) {
// It is returning answer from DatecsSocket in 10 seconds retrying to connect the server 6 times
return;
}
std::ostringstream ss;
ss << "{"sell": """", "sum": " << totalPrice << "}";
std::string jsonData = ss.str();
if (!socketComm.sendJSONData(jsonData)) {
return;
}
int transactionStatus;
if (!socketComm.waitForUpdates(transactionStatus, transactionNo)) {
return;
}
if (transactionStatus != PAY_TRANSACTION_APPROVED) {
throw std::runtime_error("Моля изберете друг метод на плащане или пробвайте отново с карта!");
}
}
// Perform the database query regardless of the terminal type
try {
m_query->Close();
m_query->SQL->Text = "SELECT RECEIPT_SUM, PAYMENT_ID FROM POS_MAKE_PAYMENT_FROM_RECEIPT2(:GS_RECEIPTS_HEAD_ID,"
" :REF_POS_ACCOUNT_ID, :PAYMENT_SUM, :DATECS_TRANSACTION_ID)";
m_query->ParamByName("GS_RECEIPTS_HEAD_ID")->AsInteger = receiptId;
m_query->ParamByName("REF_POS_ACCOUNT_ID")->AsInteger = terminalId;
m_query->ParamByName("DATECS_TRANSACTION_ID")->AsInteger = transactionNo;
if (paymentSum != 0)
m_query->ParamByName("PAYMENT_SUM")->AsFloat = paymentSum;
else
m_query->ParamByName("PAYMENT_SUM")->Clear();
m_query->ExecQuery();
m_query->Close();
} catch (const std::exception &e) {
// Handle the exception, perhaps log it
}
}
I am trying to use “Cancel button” from SocketComm.cpp in Finalizepayment.cpp , when is pressed “Cancel” to stop tries to connect and to proceed with other function to execute the database query , even is not connected. just to save the payment in database.
For now is only terminating everything , and does not save anything in the database.