-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (132 loc) · 4.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const functions = require("@google-cloud/functions-framework");
const axios = require("axios");
const base64url = require("base64url");
const jwt = require("jsonwebtoken");
const getCountryISO2 = require("country-iso-3-to-2");
const WEBHOOK_URL = "https://hooks.slack.com/services/xxxxxxxxxxx/xxxxxx"; // CHANGE HERE
const isDiscord = WEBHOOK_URL.includes("discord.com/"); // DECIDES SLACK OR DISCORD VIA URL
const decodeRawEvent = (signedPayload) => {
if (!signedPayload?.includes(".")) throw new Error("invalid signedPayload");
let event;
try {
event = JSON.parse(base64url.decode(signedPayload.split(".")[1]));
} catch (error) {
throw new Error("decode event" + error.message);
}
if (event?.data?.signedTransactionInfo) {
const transactionInfo = jwt.decode(event.data.signedTransactionInfo);
if (transactionInfo.originalTransactionId) {
event.data.transactionInfo = transactionInfo;
delete event?.data?.signedTransactionInfo;
}
}
if (event?.data?.signedRenewalInfo) {
const renewalInfo = jwt.decode(event.data.signedRenewalInfo);
if (renewalInfo.originalTransactionId) {
event.data.renewalInfo = renewalInfo;
delete event?.data?.signedRenewalInfo;
}
}
return event;
};
const slackMessage = ({title, items = [], subItems = [], appAppleId, bundleId}) => {
const item = ({name, value}) => `*${name}:* ${value || ""}`;
return {
text: title,
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text:
`*<https://apps.apple.com/tr/app/id${appAppleId}|${bundleId}>*\n` +
items
.filter(({value}) => Boolean(value))
.map(item)
.join("\n")
},
accessory: {
type: "image",
image_url:
"https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/App_Store_%28iOS%29.svg/512px-App_Store_%28iOS%29.svg.png",
alt_text: "App Store"
}
},
{
type: "divider"
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: subItems
.filter(({value}) => Boolean(value))
.map(item)
.join("\n")
}
]
},
{
type: "divider"
}
]
};
};
const discordMessage = ({title, items = [], subItems = [], appAppleId, bundleId}) => {
const item = ({name, value}) => `${name}: ${value || ""}`;
return {
username: "IAP Events",
content: title,
embeds: [
{
title: bundleId,
url: `https://apps.apple.com/tr/app/id${appAppleId}`,
color: 15258703,
fields: items.filter((e) => Boolean(e.value)),
thumbnail: {
url: "https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/App_Store_%28iOS%29.svg/512px-App_Store_%28iOS%29.svg.png"
},
footer: {
text: subItems
.filter(({value}) => Boolean(value))
.map(item)
.join("\n")
}
}
]
};
};
functions.http("main", async (req, res) => {
if (req.method !== "POST") {
res.status(405).end("Only POST method is accepted");
return;
}
try {
const {notificationType, subtype, data} = decodeRawEvent(req.body.signedPayload); // https://developer.apple.com/documentation/appstoreservernotifications/responsebodyv2decodedpayload
const {transactionInfo, appAppleId, bundleId} = data;
const flagEmoji = `:flag${isDiscord ? "_" : "-"}${getCountryISO2(transactionInfo?.storefront)?.toLowerCase()}:`;
const price = typeof transactionInfo.price === "number" ? String(transactionInfo.price / 1000) : ""; //https://developer.apple.com/documentation/appstoreservernotifications/price
const priceWithCurrency = `${price} ${transactionInfo.currency}`;
const title = `💵 ${isDiscord ? "" : flagEmoji} ${notificationType || ""} ${priceWithCurrency} 🔔`;
const items = [
{name: "Event", value: notificationType},
{name: "Event Subtype", value: subtype},
{name: "Product", value: transactionInfo?.productId},
{name: "Country", value: `${flagEmoji} ${transactionInfo?.storefront}`},
{name: "Price", value: ":dollar: " + priceWithCurrency}
];
const subItems = [
{name: "Environment", value: transactionInfo?.environment},
{name: "ID", value: transactionInfo?.originalTransactionId},
{name: "Type", value: transactionInfo?.type},
{name: "Expires Date", value: new Date(transactionInfo?.expiresDate).toISOString()}
];
const payload = {title, items, subItems, appAppleId, bundleId};
await axios.post(WEBHOOK_URL, isDiscord ? discordMessage(payload) : slackMessage(payload));
res.status(200).send("Notification sent successfully");
} catch (error) {
console.error("Error sending notification", error);
res.status(500).send("Internal Server Error");
}
});