Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webhooks #562

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions endpoints/cronjobs/sendnotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@
$webhook['payload'] = $row["payload"];
$webhook['iterator'] = $row["iterator"];
if ($webhook['iterator'] === "") {
$webhook['iterator'] = "subscriptions";
$webhook['iterator'] = "{{subscriptions}}";
} else {
if (strpos($webhook['iterator'], "{{") === false) {
$webhook['iterator'] = "{{" . $webhook['iterator'] . "}}";
}
}
}

Expand Down Expand Up @@ -215,6 +219,8 @@
$notify[$rowSubscription['payer_user_id']][$i]['payer'] = $household[$rowSubscription['payer_user_id']]['name'];
$notify[$rowSubscription['payer_user_id']][$i]['date'] = $rowSubscription['next_payment'];
$notify[$rowSubscription['payer_user_id']][$i]['days'] = $daysToCompare;
$notify[$rowSubscription['payer_user_id']][$i]['url'] = $rowSubscription['url'];
$notify[$rowSubscription['payer_user_id']][$i]['notes'] = $rowSubscription['notes'];
$i++;
}
}
Expand Down Expand Up @@ -532,10 +538,16 @@
if ($webhookNotificationsEnabled) {
// Get webhook payload and turn it into a json object

$payload = str_replace("{{days_until}}", $days, $webhook['payload']); // The default value for all subscriptions
$payload = str_replace("{{days_until}}", $days, $webhook['payload']);

$payload_json = json_decode($payload, true);

$subscription_template = $payload_json["{{subscriptions}}"];
if ($payload_json === null) {
echo "Error parsing payload JSON<br />";
continue;
}

$subscription_template = $payload_json[$webhook['iterator']];
$subscriptions = [];

foreach ($notify as $userId => $perUser) {
Expand All @@ -560,18 +572,24 @@
$temp_subscription[$key] = str_replace("{{subscription_category}}", $subscription['category'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_payer}}", $subscription['payer'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_date}}", $subscription['date'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_days_until_payment}}", $subscription['days'], $temp_subscription[$key]); // The de facto value for this subscription
$temp_subscription[$key] = str_replace("{{subscription_days_until_payment}}", $subscription['days'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_url}}", $subscription['url'], $temp_subscription[$key]);
$temp_subscription[$key] = str_replace("{{subscription_notes}}", $subscription['notes'], $temp_subscription[$key]);
}
}
$subscriptions[] = $temp_subscription;

}
}

// remove {{ and }} from the iterator
$payload_iterator = str_replace("{{", "", $webhook['iterator']);
$payload_iterator = str_replace("}}", "", $payload_iterator);

$payload_json["{{subscriptions}}"] = $subscriptions;
$payload_json[$webhook['iterator']] = $subscriptions;
$payload_json[$payload_iterator] = $subscriptions;
unset($payload_json["{{subscriptions}}"]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $webhook['url']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $webhook['request_method']);
Expand Down
8 changes: 5 additions & 3 deletions endpoints/notifications/savewebhooknotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
$url = $data["webhook_url"];
$headers = $data["headers"];
$payload = $data["payload"];
$iterator = $data["iterator"];

$query = "SELECT COUNT(*) FROM webhook_notifications WHERE user_id = :userId";
$stmt = $db->prepare($query);
Expand All @@ -42,18 +43,19 @@
$row = $result->fetchArray();
$count = $row[0];
if ($count == 0) {
$query = "INSERT INTO webhook_notifications (enabled, url, headers, payload, user_id)
VALUES (:enabled, :url, :headers, :payload, :userId)";
$query = "INSERT INTO webhook_notifications (enabled, url, headers, payload, iterator, user_id)
VALUES (:enabled, :url, :headers, :payload, :iterator, :userId)";
} else {
$query = "UPDATE webhook_notifications
SET enabled = :enabled, url = :url, headers = :headers, payload = :payload WHERE user_id = :userId";
SET enabled = :enabled, url = :url, headers = :headers, payload = :payload, iterator = :iterator WHERE user_id = :userId";
}

$stmt = $db->prepare($query);
$stmt->bindValue(':enabled', $enabled, SQLITE3_INTEGER);
$stmt->bindValue(':url', $url, SQLITE3_TEXT);
$stmt->bindValue(':headers', $headers, SQLITE3_TEXT);
$stmt->bindValue(':payload', $payload, SQLITE3_TEXT);
$stmt->bindValue(':iterator', $iterator, SQLITE3_TEXT);
$stmt->bindValue(':userId', $userId, SQLITE3_INTEGER);

if ($stmt->execute()) {
Expand Down
2 changes: 1 addition & 1 deletion includes/version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
$version = "v2.28.0";
$version = "v2.29.0";
?>
8 changes: 6 additions & 2 deletions scripts/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ function saveNotificationsWebhookButton() {
const webhook_url = document.getElementById("webhookurl").value;
const headers = document.getElementById("webhookcustomheaders").value;
const payload = document.getElementById("webhookpayload").value;
const iterator = document.getElementById("webhookiteratorkey").value;

const data = {
enabled: enabled,
webhook_url: webhook_url,
headers: headers,
payload: payload
payload: payload,
iterator: iterator
};

makeFetchCall('endpoints/notifications/savewebhooknotifications.php', data, button);
Expand All @@ -132,13 +134,15 @@ function testNotificationsWebhookButton() {
const url = document.getElementById("webhookurl").value;
const customheaders = document.getElementById("webhookcustomheaders").value;
const payload = document.getElementById("webhookpayload").value;
const iterator = document.getElementById("webhookiteratorkey").value;

const data = {
enabled: enabled,
requestmethod: requestmethod,
url: url,
customheaders: customheaders,
payload: payload
payload: payload,
iterator: iterator
};

makeFetchCall('endpoints/notifications/testwebhooknotifications.php', data, button);
Expand Down
8 changes: 5 additions & 3 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ class="thin mobile-grow" />
"currency": "{{subscription_currency}}",
"category": "{{subscription_category}}",
"date": "{{subscription_date}}",
"payer": "{{subscription_payer}}"
"days": "{{subscription_days_until_payment}}"
"payer": "{{subscription_payer}}",
"days": "{{subscription_days_until_payment}}",
"notes": "{{subscription_notes}}",
"url": "{{subscription_url}}"
}
]

Expand Down Expand Up @@ -617,7 +619,7 @@ class="capitalize"><?= translate('request_method', $i18n) ?>:</label>
<i class="fa-solid fa-circle-info"></i> <?= translate('variables_available', $i18n) ?>:
{{days_until}}, {{subscription_name}}, {{subscription_price}}, {{subscription_currency}},
{{subscription_category}}, {{subscription_date}}, {{subscription_payer}},
{{subscription_days_until_payment}}
{{subscription_days_until_payment}}, {{subscription_notes}}, {{subscription_url}}
</p>
<p>
</div>
Expand Down