buttons in html integriert..
This commit is contained in:
+79
-119
@@ -7,144 +7,104 @@ class Belevo_Bezahl_Modul extends IPSModule
|
|||||||
{
|
{
|
||||||
// Die Standard-Create() Methode aufrufen
|
// Die Standard-Create() Methode aufrufen
|
||||||
parent::Create();
|
parent::Create();
|
||||||
$this->RegisterPropertyInteger("Reservate", 0);
|
|
||||||
$this->RegisterPropertyInteger("GetAmount", 0);
|
|
||||||
$this->RegisterPropertyInteger("HTMLBox", 0);
|
|
||||||
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0);
|
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0);
|
||||||
$this->RegisterVariableBoolean("AmountIsReserved", "AmountIsReserved", '', false);
|
$this->RegisterVariableBoolean("AmountIsReserved", "AmountIsReserved", '', false);
|
||||||
|
$this->RegisterVariableString("HTMLContent", "HTMLContent", '', '');
|
||||||
|
|
||||||
// Event-Handler registrieren
|
// Initiales HTML setzen
|
||||||
$this->RegisterMessage($this->ReadPropertyInteger("Reservate"), VM_UPDATE);
|
$this->SetHTMLContent();
|
||||||
$this->RegisterMessage($this->ReadPropertyInteger("GetAmount"), VM_UPDATE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ApplyChanges()
|
public function ApplyChanges()
|
||||||
{
|
{
|
||||||
parent::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)
|
private function SetHTMLContent()
|
||||||
{
|
{
|
||||||
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 mit Google Pay
|
|
||||||
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel
|
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel
|
||||||
|
|
||||||
// Öffnen eines Fensters zur Eingabe der Zahlungsinformationen über Google Pay
|
$html = '<html>
|
||||||
$html = '<script src="https://js.stripe.com/v3/"></script>
|
<head>
|
||||||
<script>
|
<script src="https://js.stripe.com/v3/"></script>
|
||||||
var stripe = Stripe("' . $apiKey . '");
|
</head>
|
||||||
var elements = stripe.elements();
|
<body>
|
||||||
var paymentRequest = stripe.paymentRequest({
|
<button onclick="reservateAmount()">Reservate</button>
|
||||||
country: "CH",
|
<button onclick="getAmount()">Get Amount</button>
|
||||||
currency: "chf",
|
<div id="payment-request-button"></div>
|
||||||
total: {
|
<script>
|
||||||
label: "Reservation Amount",
|
var stripe = Stripe("' . $apiKey . '");
|
||||||
amount: ' . ($amount * 100) . ',
|
var elements = stripe.elements();
|
||||||
},
|
var paymentRequest = stripe.paymentRequest({
|
||||||
requestPayerName: true,
|
country: "CH",
|
||||||
requestPayerEmail: true,
|
currency: "chf",
|
||||||
});
|
total: {
|
||||||
|
label: "Reservation Amount",
|
||||||
|
amount: 1000, // Beispielbetrag in Cent
|
||||||
|
},
|
||||||
|
requestPayerName: true,
|
||||||
|
requestPayerEmail: true,
|
||||||
|
});
|
||||||
|
|
||||||
var prButton = elements.create("paymentRequestButton", {
|
var prButton = elements.create("paymentRequestButton", {
|
||||||
paymentRequest: paymentRequest,
|
paymentRequest: paymentRequest,
|
||||||
});
|
});
|
||||||
|
|
||||||
paymentRequest.canMakePayment().then(function(result) {
|
paymentRequest.canMakePayment().then(function(result) {
|
||||||
if (result) {
|
if (result) {
|
||||||
prButton.mount("#payment-request-button");
|
prButton.mount("#payment-request-button");
|
||||||
} else {
|
} else {
|
||||||
document.getElementById("payment-request-button").style.display = "none";
|
document.getElementById("payment-request-button").style.display = "none";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
paymentRequest.on("token", function(ev) {
|
function reservateAmount() {
|
||||||
// Send the token to your server to charge it!
|
paymentRequest.on("token", function(ev) {
|
||||||
var xhr = new XMLHttpRequest();
|
// Send the token to your server to charge it!
|
||||||
xhr.open("POST", "/api/");
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.setRequestHeader("Content-Type", "application/json");
|
xhr.open("POST", "/api/");
|
||||||
xhr.send(JSON.stringify({
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
"jsonrpc": "2.0",
|
xhr.send(JSON.stringify({
|
||||||
"method": "SetValue",
|
"jsonrpc": "2.0",
|
||||||
"params": [' . $this->GetIDForIdent("AmountIsReserved") . ', true],
|
"method": "SetValue",
|
||||||
"id": 0
|
"params": [' . $this->GetIDForIdent("AmountIsReserved") . ', true],
|
||||||
}));
|
"id": 0
|
||||||
xhr.onload = function() {
|
}));
|
||||||
if (xhr.status === 200) {
|
xhr.onload = function() {
|
||||||
ev.complete("success");
|
if (xhr.status === 200) {
|
||||||
IPS_LogMessage("Belevo_Bezahl_Modul", "Reservierung erfolgreich.");
|
ev.complete("success");
|
||||||
} else {
|
console.log("Reservierung erfolgreich.");
|
||||||
ev.complete("fail");
|
} else {
|
||||||
IPS_LogMessage("Belevo_Bezahl_Modul", "Reservierung fehlgeschlagen.");
|
ev.complete("fail");
|
||||||
|
console.log("Reservierung fehlgeschlagen.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
<div id="payment-request-button"></div>';
|
|
||||||
|
|
||||||
SetValue($this->ReadPropertyInteger("HTMLBox"), $html); // Baut das HTML auf
|
function getAmount() {
|
||||||
}
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("POST", "/api/");
|
||||||
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
|
xhr.send(JSON.stringify({
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "SetValue",
|
||||||
|
"params": [' . $this->GetIDForIdent("AmountIsReserved") . ', false],
|
||||||
|
"id": 0
|
||||||
|
}));
|
||||||
|
xhr.onload = function() {
|
||||||
|
if (xhr.status === 200) {
|
||||||
|
console.log("Abzug erfolgreich.");
|
||||||
|
} else {
|
||||||
|
console.log("Abzug fehlgeschlagen.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>';
|
||||||
|
|
||||||
public function GetAmount($amount)
|
SetValue($this->GetIDForIdent("HTMLContent"), $html); // Baut das HTML auf
|
||||||
{
|
|
||||||
IPS_LogMessage("Belevo_Bezahl_Modul", "GetAmount: amount=$amount");
|
|
||||||
|
|
||||||
// Beispiel-Logik zum Abziehen des Betrags bei Stripe
|
|
||||||
$apiUrl = "https://api.stripe.com/v1/charges";
|
|
||||||
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel
|
|
||||||
|
|
||||||
$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']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
"compatibility": {
|
"compatibility": {
|
||||||
"version": "7.1"
|
"version": "7.1"
|
||||||
},
|
},
|
||||||
"version": "1.011",
|
"version": "1.012",
|
||||||
"build": 0,
|
"build": 0,
|
||||||
"date": 0
|
"date": 0
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user