-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathidevaffiliate-marketplace.js
99 lines (91 loc) · 2.95 KB
/
idevaffiliate-marketplace.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
const FoxyWebhook = require("../../foxy/FoxyWebhook.js");
const dotenv = require("dotenv");
const { URLSearchParams } = require("url");
const { config } = require("../../../config.js");
const fetch = require("node-fetch");
dotenv.config();
function getIdevApiUrl() {
return config.idevAffiliate.apiUrl || "";
}
const idevSecretKey = config.idevAffiliate.secretKey || "";
const foxyWebhookEncryptionKey = config.foxy.webhook.encryptionKey || "";
const getAffiliateIdFromProduct = (productCode) => {
const m = productCode.match(/-a(\d+)$/i)
return m ? m[1] : null;
};
// TODO: Use this method instead of just referencing the `price`,
// as `price` doesn't include discounts, modifiers, coupons, etc.
const getProductNetPrice = (productCode, webhook) => {};
/**
* Push a single item to Idev affiliate
*
* @param {Object} item to be pushed
* @param {string|number} webhookId the id of the webhook.
* @returns {Promise} that resolves to the response of the posting to Idev.
*/
function pushToIdev (item, webhookId) {
if (!item.name || !item.code || !item.price) {
return Promise.resolve(false);
}
const params = new URLSearchParams();
params.append("affiliate_id", getAffiliateIdFromProduct(item.code));
params.append("idev_saleamt", item.price);
params.append("idev_ordernum", webhookId);
// TODO: Check an existing attribute to see if this has already been done.
// Upsert a Foxy API attribute on the product after pushing so it's not duplicated
// with a re-run of the webhook.
return fetch(getIdevApiUrl(), {
body: params,
method: "POST",
});
}
/**
* Processes all transactions within a message.
*
* @param {Object} message to be processed
* @returns {Promise} that resolves to the completion of the processes of all
* transactions.
*/
async function processTransaction (message) {
return Promise.all(message._embedded["fx:items"]
.map(i => pushToIdev(i, message.id))
);
}
/**
* Verifies the request as a valid Foxy webhook.
* @param (string) - The message payload, described here: https://wiki.foxycart.com/v/2.0/webhooks#example_payload
*/
async function handler (requestEvent) {
const err = FoxyWebhook.validFoxyRequest(requestEvent);
if (err) {
return FoxyWebhook.response(err, 400);
}
const payload = JSON.parse(requestEvent.body);
// Make sure everything looks ok
if (requestEvent.headers["foxy-webhook-event"] !== "transaction/created") {
return FoxyWebhook.response('Unsupported event.', 501);
}
if (
!payload._embedded ||
!payload._embedded["fx:items"] ||
!payload._embedded["fx:items"].length > 0
) {
return FoxyWebhook.response("Invalid payload.", 400);
}
try {
await processTransaction(payload);
return {
body: JSON.stringify({ message: "success." }),
statusCode: 200,
};
} catch (e) {
console.error("ERROR:", err);
return {
body: JSON.stringify({ message: "error. check logs." }),
statusCode: 400,
};
}
}
module.exports = {
handler
}