-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathstatus_change.mjs
More file actions
87 lines (75 loc) · 2.52 KB
/
status_change.mjs
File metadata and controls
87 lines (75 loc) · 2.52 KB
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
import fraudlabsProApp from "../../fraudlabs_pro.app.mjs";
export default {
name: "FraudLabs Pro Status Change Trigger",
description: "Trigger to receive data from FraudLabs Pro Screen Order API if the status changed match to your setting.",
version: "0.0.1",
key: "fraudlabspro_status_change",
type: "source",
props: {
fraudlabsProApp,
db: "$.service.db",
http: {
type: "$.interface.http",
customResponse: true,
},
},
hooks: {
async activate() {
// 1. Check if a webhook already exists in the DB to prevent duplicates
const existingHookId = this.db.get("hookId");
if (existingHookId) {
console.log(`Webhook already active with ID: ${existingHookId}`);
return;
}
const urlWithAuth = `https://www.fraudlabspro.com/pipedream-webhook-subscribe?key=${encodeURIComponent(this.$auth.api_key)}`;
const response = await fetch(urlWithAuth, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
url: this.http.endpoint,
event: "status_changed",
description: "Pipedream Workflow Trigger"
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Activation failed (${response.status}): ${errorText}`);
}
const data = await response.json();
// Store the ID returned by FraudLabs Pro
if (data && data.id) {
this.db.set("hookId", data.id);
}
},
async deactivate() {
const hookId = this.db.get("hookId");
if (!hookId) return;
const baseUrl = `https://www.fraudlabspro.com/pipedream-webhook-unsubscribe`;
const params = new URLSearchParams({
key: this.$auth.api_key,
id: hookId
});
await fetch(`${baseUrl}?${params.toString()}`, {
method: "GET",
});
// Clear the DB so it can be re-activated later
this.db.set("hookId", null);
},
},
async run(event) {
// 2. Capture the incoming POST data from the API
const body = event.body;
// Emit the data so it can be used in workflow steps
// We use fields like order_id as the primary ID for deduplication in Pipedream
this.$emit(body, {
summary: `New Status: ${body.flp_status} for Order #${body.order_id}`,
id: body.order_id || Date.now(),
ts: Date.now(),
});
// Acknowledge the receipt to FraudLabs Pro
this.http.respond({
status: 200,
body: { message: "Success" },
});
},
};