n8n/packages/nodes-base/nodes/Shopify/ShopifyTrigger.node.ts

394 lines
9 KiB
TypeScript
Raw Normal View History

2019-12-02 13:21:55 -08:00
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeType,
INodeTypeDescription,
2019-12-02 13:21:55 -08:00
IWebhookResponseData,
} from 'n8n-workflow';
import {
shopifyApiRequest,
} from './GenericFunctions';
2020-04-23 17:56:53 -07:00
import {
createHmac,
} from 'crypto';
2019-12-02 13:21:55 -08:00
export class ShopifyTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Shopify Trigger',
2020-05-12 06:08:19 -07:00
name: 'shopifyTrigger',
2019-12-02 13:21:55 -08:00
icon: 'file:shopify.png',
group: ['trigger'],
version: 1,
description: 'Handle Shopify events via webhooks',
defaults: {
name: 'Shopify Trigger',
color: '#559922',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'shopifyApi',
required: true,
}
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Topic',
name: 'topic',
type: 'options',
default: '',
options:
[
{
name: 'App uninstalled',
value: 'app/uninstalled',
2019-12-02 13:21:55 -08:00
},
{
name: 'Carts create',
value: 'carts/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Carts update',
value: 'carts/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Checkouts create',
value: 'checkouts/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Checkouts delete',
value: 'checkouts/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Checkouts update',
value: 'checkouts/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Collection listings add',
value: 'collection_listings/add',
2019-12-02 13:21:55 -08:00
},
{
name: 'Collection listings remove',
value: 'collection_listings/remove',
2019-12-02 13:21:55 -08:00
},
{
name: 'Collection listings update',
value: 'collection_listings/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Collections create',
value: 'collections/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Collections delete',
value: 'collections/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Collections update',
value: 'collections/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customer groups create',
value: 'customer_groups/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customer groups delete',
value: 'customer_groups/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customer groups update',
value: 'customer_groups/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customers create',
value: 'customers/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customers delete',
value: 'customers/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customers disable',
value: 'customers/disable',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customers enable',
value: 'customers/enable',
2019-12-02 13:21:55 -08:00
},
{
name: 'Customers update',
value: 'customers/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Draft orders create',
value: 'draft_orders/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Draft orders delete',
value: 'draft_orders/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Draft orders update',
value: 'draft_orders/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Fulfillment events create',
value: 'fulfillment_events/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Fulfillment events delete',
value: 'fulfillment_events/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Fulfillments create',
value: 'fulfillments/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Fulfillments update',
value: 'fulfillments/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Inventory_items create',
value: 'inventory_items/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Inventory_items delete',
value: 'inventory_items/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Inventory_items update',
value: 'inventory_items/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Inventory_levels connect',
value: 'inventory_levels/connect',
2019-12-02 13:21:55 -08:00
},
{
name: 'Inventory_levels disconnect',
value: 'inventory_levels/disconnect',
2019-12-02 13:21:55 -08:00
},
{
name: 'Inventory_levels update',
value: 'inventory_levels/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Locales create',
value: 'locales/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Locales update',
value: 'locales/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Locations create',
value: 'locations/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Locations delete',
value: 'locations/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Locations update',
value: 'locations/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Order transactions create',
value: 'order_transactions/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Orders cancelled',
value: 'orders/cancelled',
2019-12-02 13:21:55 -08:00
},
{
name: 'Orders create',
value: 'orders/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Orders delete',
value: 'orders/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Orders fulfilled',
value: 'orders/fulfilled',
2019-12-02 13:21:55 -08:00
},
{
name: 'Orders paid',
value: 'orders/paid',
2019-12-02 13:21:55 -08:00
},
{
name: 'Orders partially fulfilled',
value: 'orders/partially_fulfilled',
2019-12-02 13:21:55 -08:00
},
{
name: 'Orders updated',
value: 'orders/updated',
2019-12-02 13:21:55 -08:00
},
{
name: 'Product listings add',
value: 'product_listings/add',
2019-12-02 13:21:55 -08:00
},
{
name: 'Product listings remove',
value: 'product_listings/remove',
2019-12-02 13:21:55 -08:00
},
{
name: 'Product listings update',
value: 'product_listings/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Products create',
value: 'products/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Products delete',
value: 'products/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Products update',
value: 'products/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Refunds create',
value: 'refunds/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Shop update',
value: 'shop/update',
2019-12-02 13:21:55 -08:00
},
{
name: 'Tender transactions create',
value: 'tender_transactions/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Themes create',
value: 'themes/create',
2019-12-02 13:21:55 -08:00
},
{
name: 'Themes delete',
value: 'themes/delete',
2019-12-02 13:21:55 -08:00
},
{
name: 'Themes publish',
value: 'themes/publish',
2019-12-02 13:21:55 -08:00
},
{
name: 'Themes update',
value: 'themes/update',
},
2019-12-02 13:21:55 -08:00
],
description: 'Event that triggers the webhook',
},
],
};
// @ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/webhooks/${webhookData.webhookId}.json`;
try {
await shopifyApiRequest.call(this, 'GET', endpoint, {});
} catch (e) {
if (e.statusCode === 404) {
delete webhookData.webhookId;
return false;
}
throw e;
}
return true;
},
async create(this: IHookFunctions): Promise<boolean> {
const credentials = this.getCredentials('shopifyApi');
const webhookUrl = this.getNodeWebhookUrl('default');
const topic = this.getNodeParameter('topic') as string;
const endpoint = `/webhooks.json`;
const body = {
webhook: {
topic,
address: webhookUrl,
format: 'json',
}
};
let responseData;
try {
responseData = await shopifyApiRequest.call(this, 'POST', endpoint, body);
} catch (error) {
2019-12-02 13:21:55 -08:00
return false;
}
if (responseData.webhook === undefined || responseData.webhook.id === undefined) {
// Required data is missing so was not successful
return false;
}
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = responseData.webhook.id as string;
webhookData.sharedSecret = credentials!.sharedSecret as string;
webhookData.topic = topic as string;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId !== undefined) {
const endpoint = `/webhooks/${webhookData.webhookId}.json`;
try {
await shopifyApiRequest.call(this, 'DELETE', endpoint, {});
} catch (e) {
return false;
}
delete webhookData.webhookId;
delete webhookData.sharedSecret;
delete webhookData.topic;
}
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const headerData = this.getHeaderData() as IDataObject;
const req = this.getRequestObject();
const webhookData = this.getWorkflowStaticData('node') as IDataObject;
if (headerData['x-shopify-topic'] !== undefined
&& headerData['x-shopify-hmac-sha256'] !== undefined
&& headerData['x-shopify-shop-domain'] !== undefined
&& headerData['x-shopify-api-version'] !== undefined) {
2019-12-02 13:21:55 -08:00
// @ts-ignore
const computedSignature = createHmac('sha256', webhookData.sharedSecret as string).update(req.rawBody).digest('base64');
if (headerData['x-shopify-hmac-sha256'] !== computedSignature) {
return {};
}
if (webhookData.topic !== headerData['x-shopify-topic']) {
return {};
}
} else {
return {};
}
return {
workflowData: [
this.helpers.returnJsonArray(req.body)
],
};
}
}