genderwatchprotocol/donations/donations.php

83 lines
3.1 KiB
PHP
Raw Normal View History

2023-02-16 14:25:14 +01:00
<?php
2023-02-16 21:07:33 +01:00
2023-02-16 20:46:12 +01:00
require $_SERVER['DOCUMENT_ROOT'] . '/databaseConnection/databaseConnection.php';
$conn = getDatabaseConnection();
$sql = "SELECT `value` FROM `dolibarrInformation` WHERE `information` = 'apiKey'";
$result = $conn->query($sql);
2023-02-16 20:51:20 +01:00
$dolibarrAPIKey = $result->fetch_assoc()["value"];
2023-02-16 20:46:12 +01:00
$conn = getDatabaseConnection();
$sql = "SELECT `value` FROM `dolibarrInformation` WHERE `information` = 'url'";
$result = $conn->query($sql);
2023-02-16 20:51:20 +01:00
$dolibarrURL = $result->fetch_assoc()["value"];
2023-02-16 20:48:27 +01:00
2023-02-16 21:03:06 +01:00
$conn = getDatabaseConnection();
$sql = "SELECT `value` FROM `dolibarrInformation` WHERE `information` = 'socid'";
$result = $conn->query($sql);
$BusinesspartnerID = $result->fetch_assoc()["value"];
2023-02-16 20:46:12 +01:00
2023-02-16 21:03:06 +01:00
function createDonation($amount, $dolibarrURL, $dolibarrAPIKey, $BusinesspartnerID) {
2023-02-16 20:46:12 +01:00
$pulicNote = "Automatisch generiert von genderwatchprotocol";
2023-02-16 14:25:14 +01:00
$ch = curl_init();
2023-02-16 19:41:27 +01:00
curl_setopt($ch, CURLOPT_URL, $dolibarrURL."api/index.php/donations");
2023-02-16 14:25:14 +01:00
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
2023-02-16 20:46:12 +01:00
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"amount\": ".$amount.", \"date\": ".time().", \"socid\": ".$BusinesspartnerID.", \"note_public\": \"".$pulicNote."\"}");
2023-02-16 14:25:14 +01:00
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
2023-02-16 20:14:11 +01:00
$headers[] = 'Dolapikey: '.$dolibarrAPIKey;
2023-02-16 14:25:14 +01:00
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
2023-02-16 14:27:36 +01:00
return $result;
2023-02-16 14:25:14 +01:00
}
2023-02-16 20:14:11 +01:00
function validateDonation($donationID, $dolibarrURL, $dolibarrAPIKey) {
2023-02-16 19:41:27 +01:00
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dolibarrURL."api/index.php/donations/".$donationID."/validate");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
2023-02-16 20:14:11 +01:00
$headers[] = 'Dolapikey: '.$dolibarrAPIKey;
2023-02-16 19:41:27 +01:00
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
}
2023-02-16 21:03:06 +01:00
function forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey, $BusinesspartnerID){
2023-02-16 21:07:33 +01:00
echo "Sie werden weitergeleitet. Bitte um einen Moment Geduld...";
2023-02-16 21:03:06 +01:00
$donationID = createDonation($amount, $dolibarrURL, $dolibarrAPIKey, $BusinesspartnerID);
2023-02-16 20:14:11 +01:00
validateDonation($donationID, $dolibarrURL, $dolibarrAPIKey);
2023-02-16 19:41:27 +01:00
$payment_link = $dolibarrURL."public/payment/newpayment.php?source=donation&ref=".($donationID)."&securekey=NSmKzZ73HW1zWO8nAlpdq95mDt77Q32o";
2023-02-16 14:36:53 +01:00
header('Location: '.$payment_link);
}
$amount = (float)($_GET["amount"]);
2023-02-16 14:38:15 +01:00
if(is_numeric($amount) && $amount > 0) {
2023-02-16 21:03:06 +01:00
forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey, $BusinesspartnerID);
2023-02-16 14:36:53 +01:00
} else {
2023-02-16 19:29:41 +01:00
$amount = (float)($_POST["amount"]);
if(is_numeric($amount) && $amount > 0) {
2023-02-16 21:03:06 +01:00
forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey, $BusinesspartnerID);
2023-02-16 19:29:41 +01:00
} else {
echo "invalid amount!";
}
2023-02-16 14:36:53 +01:00
}