113 lines
5.2 KiB
PHP
113 lines
5.2 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->RegisterPropertyInteger("HTMLBox", 0);
|
|
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0);
|
|
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0);
|
|
$this->RegisterVariableBoolean("AmountIsReserved", "AmountIsReserved", '', false);
|
|
|
|
// Initiales HTML setzen
|
|
$this->SetHTMLContent();
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
}
|
|
|
|
private function SetHTMLContent()
|
|
{
|
|
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel
|
|
|
|
$html = '<html>
|
|
<head>
|
|
<script src="https://js.stripe.com/v3/"></script>
|
|
</head>
|
|
<body>
|
|
<button onclick="reservateAmount()">Reservate</button>
|
|
<button onclick="getAmount()">Get Amount</button>
|
|
<div id="payment-request-button"></div>
|
|
<script>
|
|
var stripe = Stripe("' . $apiKey . '");
|
|
var elements = stripe.elements();
|
|
var paymentRequest = stripe.paymentRequest({
|
|
country: "CH",
|
|
currency: "chf",
|
|
total: {
|
|
label: "Reservation Amount",
|
|
amount: 1000, // Beispielbetrag in Cent
|
|
},
|
|
requestPayerName: true,
|
|
requestPayerEmail: true,
|
|
});
|
|
|
|
var prButton = elements.create("paymentRequestButton", {
|
|
paymentRequest: paymentRequest,
|
|
});
|
|
|
|
paymentRequest.canMakePayment().then(function(result) {
|
|
if (result) {
|
|
prButton.mount("#payment-request-button");
|
|
} else {
|
|
document.getElementById("payment-request-button").style.display = "none";
|
|
}
|
|
});
|
|
|
|
function reservateAmount() {
|
|
paymentRequest.on("token", function(ev) {
|
|
// Send the token to your server to charge it!
|
|
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") . ', true],
|
|
"id": 0
|
|
}));
|
|
xhr.onload = function() {
|
|
if (xhr.status === 200) {
|
|
ev.complete("success");
|
|
console.log("Reservierung erfolgreich.");
|
|
} else {
|
|
ev.complete("fail");
|
|
console.log("Reservierung fehlgeschlagen.");
|
|
}
|
|
};
|
|
});
|
|
}
|
|
|
|
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>';
|
|
|
|
SetValue($this->ReadPropertyInteger("HTMLBox"), $html); // Baut das HTML auf
|
|
}
|
|
}
|
|
?>
|