Files
Symcon_Belevo_Energiemanage…/Belevo_Bezahl_Modul/module.php
2025-01-28 15:21:23 +01:00

131 lines
5.4 KiB
PHP

<?php
class Belevo_Bezahl_Modul extends IPSModule
{
public function Create()
{
// Die Standard-Create() Methode aufrufen
parent::Create();
$this->RegisterPropertyInteger("Reservate", 0);
$this->RegisterPropertyInteger("GetAmount", 0);
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0);
$this->RegisterVariableBoolean("AmountIsReserved", "AmountIsReserved", '', false);
// Event-Handler registrieren
$this->RegisterMessage($this->ReadPropertyInteger("Reservate"), VM_UPDATE);
$this->RegisterMessage($this->ReadPropertyInteger("GetAmount"), VM_UPDATE);
}
public function ApplyChanges()
{
parent::ApplyChanges();
// Event-Handler erneut registrieren, falls sich die Property-IDs geändert haben
$this->RegisterMessage($this->ReadPropertyInteger("Reservate"), VM_UPDATE);
$this->RegisterMessage($this->ReadPropertyInteger("GetAmount"), VM_UPDATE);
}
public function MessageSink($TimeStamp, $SenderID, $Message, $Data)
{
IPS_LogMessage("Belevo_Bezahl_Modul", "MessageSink: SenderID=$SenderID, Message=$Message");
switch ($SenderID) {
case $this->ReadPropertyInteger("Reservate"):
if (GetValueBoolean($this->ReadPropertyInteger("Reservate"))) {
$this->ReservateAmount(GetValueInteger($this->GetIDForIdent("ReservationAmount")));
}
break;
case $this->ReadPropertyInteger("GetAmount"):
if (GetValueBoolean($this->ReadPropertyInteger("GetAmount"))) {
$this->GetAmount(GetValueInteger($this->ReadPropertyInteger("GetAmount")));
}
break;
}
}
public function ReservateAmount($amount)
{
IPS_LogMessage("Belevo_Bezahl_Modul", "ReservateAmount: amount=$amount");
// Beispiel-Logik zur Reservierung des Betrags bei Stripe
<<<<<<< HEAD
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Ersetze dies durch deinen tatsächlichen API-Schlüssel
=======
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel
>>>>>>> e2bee19 (key angepasst)
// Öffnen eines Fensters zur Eingabe der Zahlungsinformationen
$html = '<script>
var windowObjectReference;
var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
function openRequestedPopup() {
windowObjectReference = window.open("https://checkout.stripe.com/pay", "Stripe_Window", strWindowFeatures);
}
openRequestedPopup();
</script>';
<<<<<<< HEAD
SetValue(23828, $html); // Baut das HTML auf
=======
SetValue($this->ReadPropertyInteger("Reservate"), $html); // Baut das HTML auf
>>>>>>> e2bee19 (key angepasst)
// Simulierte erfolgreiche Reservierung
$reservationSuccessful = true;
if ($reservationSuccessful) {
SetValueBoolean($this->GetIDForIdent("AmountIsReserved"), true);
IPS_LogMessage("Belevo_Bezahl_Modul", "Reservierung erfolgreich.");
} else {
IPS_LogMessage("Belevo_Bezahl_Modul", "Reservierung fehlgeschlagen.");
}
}
public function GetAmount($amount)
{
IPS_LogMessage("Belevo_Bezahl_Modul", "GetAmount: amount=$amount");
// Beispiel-Logik zum Abziehen des Betrags bei Stripe
$apiUrl = "https://api.stripe.com/v1/charges";
<<<<<<< HEAD
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Ersetze dies durch deinen tatsächlichen API-Schlüssel
=======
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel
>>>>>>> e2bee19 (key angepasst)
$data = [
"amount" => $amount * 100, // Betrag in Cent
"currency" => "CHF", // Ersetze dies durch die gewünschte Währung
"source" => "tok_visa" // Beispiel: Test-Token von Stripe
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\nAuthorization: Bearer $apiKey\r\n",
'method' => 'POST',
'content' => http_build_query($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($apiUrl, false, $context);
if ($result === FALSE) {
IPS_LogMessage("Belevo_Bezahl_Modul", "Abzug fehlgeschlagen.");
} else {
$response = json_decode($result, true);
if (isset($response['id'])) {
SetValueBoolean($this->ReadPropertyInteger("Reservate"), false);
SetValueBoolean($this->GetIDForIdent("AmountIsReserved"), false);
SetValueBoolean($this->ReadPropertyInteger("GetAmount"), false);
IPS_LogMessage("Belevo_Bezahl_Modul", "Abzug erfolgreich.");
} else {
IPS_LogMessage("Belevo_Bezahl_Modul", "Abzug fehlgeschlagen: " . $response['error']['message']);
}
}
}
}
?>