genderwatchprotocol/donations/donations.php

80 lines
2.9 KiB
PHP
Raw Normal View History

2023-02-16 14:25:14 +01:00
<?php
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:48:27 +01:00
//$dolibarrAPIKey = mysql_fetch_array($result)["value"];
$dolibarrAPIKey = "eM6A2bQP36Q7dhELq02qnD7sY4do5KRa";
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:48:27 +01:00
//$dolibarrURL = mysql_fetch_array($result)["value"];
$dolibarrURL = "https://dolibarr.romanschenk.ch/";
2023-02-16 20:46:12 +01:00
2023-02-16 20:14:11 +01:00
function createDonation($amount, $dolibarrURL, $dolibarrAPIKey) {
2023-02-16 14:45:36 +01:00
$BusinesspartnerID = "220";
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 20:14:11 +01:00
function forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey){
$donationID = createDonation($amount, $dolibarrURL, $dolibarrAPIKey);
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 20:14:11 +01:00
forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey);
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 20:14:11 +01:00
forwardToDonationPage($amount, $dolibarrURL, $dolibarrAPIKey);
2023-02-16 19:29:41 +01:00
} else {
echo "invalid amount!";
}
2023-02-16 14:36:53 +01:00
}