genderwatchprotocol/donations/donations.php

80 lines
2.9 KiB
PHP

<?php
require $_SERVER['DOCUMENT_ROOT'] . '/databaseConnection/databaseConnection.php';
$conn = getDatabaseConnection();
$sql = "SELECT `value` FROM `dolibarrInformation` WHERE `information` = 'apiKey'";
$result = $conn->query($sql);
$dolibarrAPIKey = $result->fetch_assoc()["value"];
//$dolibarrAPIKey = "eM6A2bQP36Q7dhELq02qnD7sY4do5KRa";
$conn = getDatabaseConnection();
$sql = "SELECT `value` FROM `dolibarrInformation` WHERE `information` = 'url'";
$result = $conn->query($sql);
$dolibarrURL = $result->fetch_assoc()["value"];
//$dolibarrURL = "https://dolibarr.romanschenk.ch/";
function createDonation($amount, $dolibarrURL, $dolibarrAPIKey) {
$BusinesspartnerID = "220";
$pulicNote = "Automatisch generiert von genderwatchprotocol";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dolibarrURL."api/index.php/donations");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"amount\": ".$amount.", \"date\": ".time().", \"socid\": ".$BusinesspartnerID.", \"note_public\": \"".$pulicNote."\"}");
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'Dolapikey: '.$dolibarrAPIKey;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
}
function validateDonation($donationID, $dolibarrURL, $dolibarrAPIKey) {
$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';
$headers[] = 'Dolapikey: '.$dolibarrAPIKey;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
}
function forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey){
$donationID = createDonation($amount, $dolibarrURL, $dolibarrAPIKey);
validateDonation($donationID, $dolibarrURL, $dolibarrAPIKey);
$payment_link = $dolibarrURL."public/payment/newpayment.php?source=donation&ref=".($donationID)."&securekey=NSmKzZ73HW1zWO8nAlpdq95mDt77Q32o";
header('Location: '.$payment_link);
}
$amount = (float)($_GET["amount"]);
if(is_numeric($amount) && $amount > 0) {
forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey);
} else {
$amount = (float)($_POST["amount"]);
if(is_numeric($amount) && $amount > 0) {
forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey);
} else {
echo "invalid amount!";
}
}