Skip to content
Snippets Groups Projects
Commit 31065c35 authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

migrated invoices to zammad

parent 7279fec8
No related branches found
No related tags found
No related merge requests found
Pipeline #7694 passed
......@@ -50,12 +50,12 @@ class Zammad {
}
/**
* Creates a notification in the configured Zammad instance
*
* @param {string} message
*
* @returns {Promise<boolean>}
*/
* Creates a refund ticket in the configured Zammad instance
*
* @param {string} message
*
* @returns {Promise<boolean>}
*/
static async CREATE_REFUND_TICKET(message, payment_reference_id) {
// Check if notifications are turned on
try {
......@@ -97,6 +97,61 @@ class Zammad {
return false;
}
}
/**
* Creates a receipt ticket in the configured Zammad instance
*
* @param {string} name
* @param {string} email
* @param {string} message
* @param {string} payment_id
*
* @returns {Promise<boolean>}
*/
static async CREATE_RECEIPT_TICKET(name, email, message, payment_id) {
// Check if notifications are turned on
try {
if (config.get("app.zammad.receipt_enabled")) {
let zammad_url = config.get("app.zammad.url") + "/api/v1/tickets";
let subject = `Rechnung (${payment_id})`;
let zammad_data = {
"title": subject,
"group": "Tokens",
"customer_id": `guess:${email}`,
"preferences": { "channel_id": 3 },
"article": {
"type": "email",
"sender": "Customer",
"from": `${name} <${email}>`,
"reply_to": email,
"to": "support@metager.de",
"subject": subject,
"body": message,
"content_type": "text/html",
"internal": false
}
}
return fetch(zammad_url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Token ${config.get("app.zammad.api_key")}`
},
body: JSON.stringify(zammad_data)
}).then(response => {
return true;
}).catch(e => {
console.error(e);
return false;
})
} else {
return true;
}
} catch (e) {
console.error(e);
return false;
}
}
}
module.exports = Zammad;
\ No newline at end of file
......@@ -25,6 +25,7 @@
"zammad": {
"notification_enabled": false,
"refund_enabled": true,
"receipt_enabled": true,
"url": "<ZAMMAD_URL>",
"api_key": "<ZAMMAD_API_KEY>",
"notification_ticket_id": "0"
......
......@@ -4,6 +4,7 @@ var router = express.Router({ mergeParams: true });
const config = require("config");
const { body, validationResult } = require("express-validator");
const Receipt = require("../../app/Receipt");
const Zammad = require("../../app/Zammad");
router.get("/", (req, res) => {
req.data.order.invoice = {
......@@ -54,6 +55,7 @@ router.post("/", (req, res) => {
let moderation_params = {
order: req.data.order.payment.public_id,
payment_reference: req.data.order.payment_reference.public_id,
company: req.data.order.invoice.params.company,
name: req.data.order.invoice.params.name,
email: req.data.order.invoice.params.email,
address: req.data.order.invoice.params.address,
......@@ -74,33 +76,23 @@ router.post("/", (req, res) => {
let message = ejs.render(template, req.data);
// No validation errors. Try to create a new Ticket
return fetch(`${config.get("app.osticket.url")}/api/tickets.json`, {
method: "post",
headers: {
"X-API-Key": config.get("app.osticket.api_key"),
"Content-Type": "application/json",
},
body: JSON.stringify({
alert: true,
autorespond: false,
source: "API",
name: req.data.order.invoice.params.name,
email: req.data.order.invoice.params.email,
subject: `MetaGer Schlüssel: Rechnung (${req.data.order.payment.public_id})`,
message: `data:text/html;charset=utf-8,${message}`,
topicId: 12, // ToDo change topic for english autoresponder
}),
return Zammad.CREATE_RECEIPT_TICKET(moderation_params.name, moderation_params.email, message, req.data.order.payment.public_id).then(success => {
if (success) {
req.data.order.invoice.success = true;
res.render("key", req.data);
} else {
console.error(response.body);
throw "Fehler beim Erstellen der Benachrichtigung,";
}
}).then((response) => {
if (response.status != 201) {
console.error(response.body);
throw "Fehler beim Erstellen der Benachrichtigung,";
} else {
req.data.order.invoice.success = true;
res.render("key", req.data);
}
})
.then((response) => {
if (response.status != 201) {
console.error(response.body);
throw "Fehler beim Erstellen der Benachrichtigung,";
} else {
req.data.order.invoice.success = true;
res.render("key", req.data);
}
})
.catch((reason) => {
console.log(reason);
req.data.order.invoice.errors["send_email"] =
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>Eine Rechnung für die Bestellung mit der ID <span style="font-weight: bold"><%= order.order %></span> wurde angefragt. Folgende Rechnungsdaten wurden übermittelt:</p>
<div style="display: grid; grid-template-columns: auto; gap: 1rem; align-items: center; justify-content: center;">
<label for="company">Firma: <%= order.invoice.params.company _%></label>
<label for="name">Name: <%= order.invoice.params.name %></label>
<label for="email">E-Mail: <%= order.invoice.params.email %></label>
<label for="address">Anschrift:</label>
<pre><%= order.invoice.params["address"] %></pre>
<a href="<%= order.invoice.moderation_url %>" target="_blank" style="display: block;text-decoration: none;border: 1px solid rgb(168, 84, 0);border-radius: 5px;padding: .2rem .5rem;background-color: rgb(185, 92, 0);color: white!important;font-weight: bold;grid-column: span 2;text-align: center;max-width: 15em;justify-self: center;">Rechnung erstellen</a>
</div>
<p style="margin-top: 1rem">Bitte Rechnungsdaten überprüfen und die Rechnung erstellen. Anschließend die erstellte Rechnung als Antwort auf dieses Ticket an den Nutzer schicken.</p>
</body>
</html>
\ No newline at end of file
Eine Rechnung für die Bestellung mit der ID <%= order.order %> wurde angefragt. Folgende Rechnungsdaten wurden übermittelt:
<%_ if(order.invoice.params.company) { _%>
<div><label>Firma: <%= order.invoice.params.company _%></label></div>
<%_ } _%>
<div><label>Name: <%= order.invoice.params.name %></label></div>
<div><label>E-Mail: <%= order.invoice.params.email %></label></div>
<div><label>Anschrift:</label></div>
<div>
<pre><%= order.invoice.params["address"] %></pre>
</div>
<div><a href="<%= order.invoice.moderation_url %>" target="_blank">Rechnung erstellen</a></div>
<div>Bitte Rechnungsdaten überprüfen und die Rechnung erstellen. Anschließend die erstellte Rechnung als Antwort auf dieses Ticket an den Nutzer schicken.</div>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment