Skip to content

Commit

Permalink
Implement function for sending HTTP requests and refactor controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
wichmann committed Apr 6, 2024
1 parent e4051cb commit 2b60789
Showing 1 changed file with 63 additions and 29 deletions.
92 changes: 63 additions & 29 deletions src/catalog/controller/payment/free_for_all.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php
namespace Opencart\Catalog\Controller\Extension\FreeForAll\Payment;

class FreeForAll extends \Opencart\System\Engine\Controller {
class FreeForAll extends \Opencart\System\Engine\Controller
{

/**
* @return string
*/
public function index(): string {
public function index(): string
{
$this->load->language('extension/free_for_all/payment/free_for_all');

$data['language'] = $this->config->get('config_language');
Expand All @@ -17,7 +19,8 @@ public function index(): string {
/**
* @return void
*/
public function confirm(): void {
public function confirm(): void
{
$this->load->language('extension/free_for_all/payment/free_for_all');

$json = [];
Expand All @@ -41,7 +44,17 @@ public function confirm(): void {
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));

// Output information about the customer and the order
$this->output_information();

//$this->send_message();
$this->send_http_get();
}

/**
* Output information about the customer and the order.
*/
private function output_information(): void
{
$bestellung = $this->session->data['order_id'];
$this->log->write('Bestellung Nr. ' . $bestellung);
$kunde = $this->session->data['customer_id'];
Expand All @@ -60,47 +73,68 @@ public function confirm(): void {
}
}
//$this->log->write('Inhalt des Warenkorbs: ' . json_encode($this->cart->getProducts()));

// Send information to MES system
//$this->send_message('Data: ' . $bestellung . ',' . $kunde . ',' . $produkt);
}
}

private function send_message($message): void {
/* Get the port for the WWW service. */
$service_port = 4444; //getservbyname('www', 'tcp');
private function send_http_get(): void {
$ch = curl_init();
$url = 'http://192.168.10.52:4444';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}

/* Get the IP address for the target host. */
$address = '192.168.0.1'; //gethostbyname('www.example.com');
/**
* Send information to MES system.
*/
private function send_message(): void
{
// get the port for the WWW service
$service_port = 4444;

// get the IP address for the target host
$address = '192.168.10.52'; // gethostbyname('www.example.com');

/* Create a TCP/IP socket. */
// create a TCP/IP socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
$this->log->write("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
} else {
echo "OK.\n";
$this->log->write("OK.\n");
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$this->log->write("Attempting to connect to '$address' on port '$service_port'...");
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
$this->log->write("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n");
} else {
echo "OK.\n";
$this->log->write("OK.\n");
}

// collect all information to be sent to MES system
$bestellung = $this->session->data['order_id'];
$kunde = $this->session->data['customer_id'];
$produkte = '';
foreach ($this->cart->getProducts() as $produkt) {
$produkte .= $produkt['product_id'];
}
$message = 'Data: ' . $bestellung . ',' . $kunde . ',' . $produkte . "\r\n";

echo "Sending data...";
// write information to socket
$this->log->write("Sending data...");
socket_write($socket, $message, strlen($message));
echo "OK.\n";
$this->log->write("OK.\n");

echo "Reading response:\n\n";
$out = '';
while ($out = socket_read($socket, 2048)) {
echo $out;
}
//$this->log->write("Reading response:\n\n");
//$out = '';
//while ($out = socket_read($socket, 2048)) {
// $this->log->write($out);
//}

echo "Closing socket...";
$this->log->write("Closing socket...");
socket_close($socket);
echo "OK.\n\n";
$this->log->write("OK.\n\n");
}

}

0 comments on commit 2b60789

Please sign in to comment.