113 lines
4.0 KiB
PHP
113 lines
4.0 KiB
PHP
<?php
|
|
|
|
class Belevo_Bezahl_Modul extends IPSModule
|
|
{
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
$this->RegisterPropertyInteger("Reservate", 0);
|
|
$this->RegisterPropertyInteger("GetAmount", 0);
|
|
$this->RegisterPropertyInteger("HTMLBox", 0);
|
|
$this->RegisterVariableInteger("ReservationAmount", "ReservationAmount", '', 0);
|
|
$this->RegisterVariableBoolean("AmountIsReserved", "AmountIsReserved", '', false);
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
$this->SetHTMLContent();
|
|
}
|
|
|
|
private function SetHTMLContent()
|
|
{
|
|
$apiKey = "pk_test_51Qkr79LJAcsNrpivA90lt7ULEzyXKR8l0pAqTBgfeuAIWlsLS4A3BdIBITc9UooFANbImvlJQ2F2jOZ0X5j8GI7Q00hNNasvQm"; // Test-API-Schlüssel
|
|
|
|
$html = "<html>
|
|
<head>
|
|
<script src=\"https://js.stripe.com/v3/\"></script>
|
|
</head>
|
|
<body>
|
|
<h1>Stripe Payment Request API Integration</h1>
|
|
<button id=\"reservate-button\">Reservate</button>
|
|
<button id=\"get-amount-button\">Get Amount</button>
|
|
<div id=\"payment-request-button\"></div>
|
|
|
|
<script>
|
|
const stripe = Stripe('$apiKey');
|
|
const elements = stripe.elements();
|
|
|
|
const paymentRequest = stripe.paymentRequest({
|
|
country: 'CH',
|
|
currency: 'chf',
|
|
total: {
|
|
label: 'Reservation Amount',
|
|
amount: 1000,
|
|
},
|
|
requestPayerName: true,
|
|
requestPayerEmail: true,
|
|
requestPayerPhone: true,
|
|
});
|
|
|
|
paymentRequest.on('paymentmethod', function(ev) {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('POST', '/api/');
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
xhr.send(JSON.stringify({
|
|
'jsonrpc': '2.0',
|
|
'method': 'SetValue',
|
|
'params': ['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.');
|
|
}
|
|
};
|
|
});
|
|
|
|
const 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';
|
|
}
|
|
});
|
|
|
|
document.getElementById('reservate-button').addEventListener('click', function() {
|
|
document.getElementById('payment-request-button').style.display = 'block';
|
|
});
|
|
|
|
document.getElementById('get-amount-button').addEventListener('click', function() {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('POST', '/api/');
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
xhr.send(JSON.stringify({
|
|
'jsonrpc': '2.0',
|
|
'method': 'SetValue',
|
|
'params': ['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);
|
|
}
|
|
}
|