Files
Symcon_Belevo_Energiemanage…/Belevo_Bezahl_Modul/module.php
2025-01-28 14:45:10 +01:00

115 lines
4.0 KiB
PHP

<?php
class Belevo_Bezahl_Modul extends IPSModule
{
public function Create()
{
// Die Standard-Create() Methode aufrufen
parent::Create();
$this->RegisterVariableBoolean("Reservate", "Reservate", '', false);
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0);
$this->RegisterVariableBoolean("AmountIsReserved", "AmountIsReserved", '', false);
$this->RegisterVariableBoolean("Get", "Get", '', false);
$this->RegisterVariableInteger("GetAmount", "GetAmount", '', 0);
// Event-Handler registrieren
$this->RegisterMessage($this->GetIDForIdent("Reservate"), VM_UPDATE);
$this->RegisterMessage($this->GetIDForIdent("Get"), VM_UPDATE);
}
public function ApplyChanges()
{
parent::ApplyChanges();
}
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
switch ($SenderID) {
case $this->GetIDForIdent("Reservate"):
if (GetValueBoolean($this->GetIDForIdent("Reservate"))) {
$this->ReservateAmount(GetValueInteger($this->GetIDForIdent("ReservationAmount")));
}
break;
case $this->GetIDForIdent("Get"):
if (GetValueBoolean($this->GetIDForIdent("Get"))) {
$this->GetAmount(GetValueInteger($this->GetIDForIdent("GetAmount")));
}
break;
}
}
public function ReservateAmount($amount)
{
// Beispiel-Logik zur Reservierung des Betrags bei Wallee
$apiUrl = "https://api.wallee.com/transaction/create";
$apiKey = "DEIN_API_KEY"; // Ersetze dies durch deinen tatsächlichen API-Schlüssel
$data = [
"amount" => $amount,
"currency" => "CHF", // Ersetze dies durch die gewünschte Währung
"paymentMethod" => "creditcard" // Beispiel: Kreditkarte
];
$options = [
'http' => [
'header' => "Content-type: application/json\r\nAuthorization: Bearer $apiKey\r\n",
'method' => 'POST',
'content' => json_encode($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($apiUrl, false, $context);
if ($result === FALSE) {
echo "Reservierung fehlgeschlagen.";
} else {
$response = json_decode($result, true);
if ($response['success']) {
SetValueBoolean($this->GetIDForIdent("AmountIsReserved"), true);
} else {
echo "Reservierung fehlgeschlagen: " . $response['message'];
}
}
}
public function GetAmount($amount)
{
// Beispiel-Logik zum Abziehen des Betrags bei Wallee
$apiUrl = "https://api.wallee.com/transaction/charge";
$apiKey = "DEIN_API_KEY"; // Ersetze dies durch deinen tatsächlichen API-Schlüssel
$data = [
"amount" => $amount,
"currency" => "CHF", // Ersetze dies durch die gewünschte Währung
"paymentMethod" => "creditcard" // Beispiel: Kreditkarte
];
$options = [
'http' => [
'header' => "Content-type: application/json\r\nAuthorization: Bearer $apiKey\r\n",
'method' => 'POST',
'content' => json_encode($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($apiUrl, false, $context);
if ($result === FALSE) {
echo "Abzug fehlgeschlagen.";
} else {
$response = json_decode($result, true);
if ($response['success']) {
SetValueBoolean($this->GetIDForIdent("Reservate"), false);
SetValueBoolean($this->GetIDForIdent("AmountIsReserved"), false);
SetValueBoolean($this->GetIDForIdent("Get"), false);
} else {
echo "Abzug fehlgeschlagen: " . $response['message'];
}
}
}
}
?>