UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

165 lines 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WooCommerceTrigger = void 0; const crypto_1 = require("crypto"); const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class WooCommerceTrigger { description = { displayName: 'WooCommerce Trigger', name: 'wooCommerceTrigger', icon: 'file:wooCommerce.svg', group: ['trigger'], version: 1, description: 'Handle WooCommerce events via webhooks', defaults: { name: 'WooCommerce Trigger', }, inputs: [], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'wooCommerceApi', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Event', name: 'event', type: 'options', required: true, default: '', options: [ { name: 'coupon.created', value: 'coupon.created', }, { name: 'coupon.deleted', value: 'coupon.deleted', }, { name: 'coupon.updated', value: 'coupon.updated', }, { name: 'customer.created', value: 'customer.created', }, { name: 'customer.deleted', value: 'customer.deleted', }, { name: 'customer.updated', value: 'customer.updated', }, { name: 'order.created', value: 'order.created', }, { name: 'order.deleted', value: 'order.deleted', }, { name: 'order.updated', value: 'order.updated', }, { name: 'product.created', value: 'product.created', }, { name: 'product.deleted', value: 'product.deleted', }, { name: 'product.updated', value: 'product.updated', }, ], description: 'Determines which resource events the webhook is triggered for', }, ], }; webhookMethods = { default: { async checkExists() { const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); const currentEvent = this.getNodeParameter('event'); const endpoint = '/webhooks'; const webhooks = await GenericFunctions_1.woocommerceApiRequest.call(this, 'GET', endpoint, {}, { status: 'active', per_page: 100 }); for (const webhook of webhooks) { if (webhook.status === 'active' && webhook.delivery_url === webhookUrl && webhook.topic === currentEvent) { webhookData.webhookId = webhook.id; return true; } } return false; }, async create() { const credentials = await this.getCredentials('wooCommerceApi'); const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); const event = this.getNodeParameter('event'); const secret = (0, GenericFunctions_1.getAutomaticSecret)(credentials); const endpoint = '/webhooks'; const body = { delivery_url: webhookUrl, topic: event, secret, }; const { id } = await GenericFunctions_1.woocommerceApiRequest.call(this, 'POST', endpoint, body); webhookData.webhookId = id; webhookData.secret = secret; return true; }, async delete() { const webhookData = this.getWorkflowStaticData('node'); const endpoint = `/webhooks/${webhookData.webhookId}`; try { await GenericFunctions_1.woocommerceApiRequest.call(this, 'DELETE', endpoint, {}, { force: true }); } catch (error) { return false; } delete webhookData.webhookId; delete webhookData.secret; return true; }, }, }; async webhook() { const req = this.getRequestObject(); const headerData = this.getHeaderData(); const webhookData = this.getWorkflowStaticData('node'); if (headerData['x-wc-webhook-id'] === undefined) { return {}; } const computedSignature = (0, crypto_1.createHmac)('sha256', webhookData.secret) .update(req.rawBody) .digest('base64'); if (headerData['x-wc-webhook-signature'] !== computedSignature) { // Signature is not valid so ignore call return {}; } return { workflowData: [this.helpers.returnJsonArray(req.body)], }; } } exports.WooCommerceTrigger = WooCommerceTrigger; //# sourceMappingURL=WooCommerceTrigger.node.js.map