mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
🚧 Node logic / Genericfunctions setup
This commit is contained in:
parent
b46a29b1a7
commit
4a968ee8ea
|
@ -37,7 +37,6 @@ export const couponOperations = [
|
||||||
] as INodeProperties[];
|
] as INodeProperties[];
|
||||||
|
|
||||||
export const couponFields = [
|
export const couponFields = [
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* coupon:create */
|
/* coupon:create */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
@ -55,7 +54,7 @@ export const couponFields = [
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default: '',
|
default: 'checkout',
|
||||||
description: 'Either product (valid for specified products or subscription plans) or checkout (valid for any checkout).',
|
description: 'Either product (valid for specified products or subscription plans) or checkout (valid for any checkout).',
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
|
@ -277,6 +276,26 @@ export const couponFields = [
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
/* coupon:getAll */
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
{
|
||||||
|
displayName: 'Product ID',
|
||||||
|
name: 'productId',
|
||||||
|
type: 'number',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: [
|
||||||
|
'coupon',
|
||||||
|
],
|
||||||
|
operation: [
|
||||||
|
`getAll`
|
||||||
|
]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
default: '',
|
||||||
|
description: 'The specific product/subscription ID.',
|
||||||
|
},
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
/* coupon:update */
|
/* coupon:update */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
{
|
{
|
||||||
|
@ -391,7 +410,6 @@ export const couponFields = [
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
displayName: 'Discount Amount',
|
displayName: 'Discount Amount',
|
||||||
name: 'discountAmount',
|
name: 'discountAmount',
|
||||||
|
|
|
@ -16,26 +16,24 @@ import {
|
||||||
export async function paddleApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
export async function paddleApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
||||||
const credentials = this.getCredentials('paddleApi');
|
const credentials = this.getCredentials('paddleApi');
|
||||||
|
|
||||||
const options = {
|
if (credentials === undefined) {
|
||||||
|
throw new Error('Could not retrieve credentials!');
|
||||||
|
}
|
||||||
|
|
||||||
|
const options : OptionsWithUri = {
|
||||||
method,
|
method,
|
||||||
qs: query || {},
|
uri: `https://vendors.paddle.com/api${endpoint}` ,
|
||||||
uri: uri || `${env}/v1${endpoint}`,
|
|
||||||
body,
|
body,
|
||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
|
|
||||||
|
body.vendor_id = credentials.vendorId;
|
||||||
|
body.vendor_auth_code = credentials.vendorAuthCode;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await this.helpers.request!(options);
|
return await this.helpers.request!(options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
if (error.response.body) {
|
throw new Error(error);
|
||||||
let errorMessage = error.response.body.message;
|
|
||||||
if (error.response.body.details) {
|
|
||||||
errorMessage += ` - Details: ${JSON.stringify(error.response.body.details)}`;
|
|
||||||
}
|
|
||||||
throw new Error(errorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,21 @@
|
||||||
import {
|
import { IExecuteFunctions } from 'n8n-core';
|
||||||
IExecuteFunctions,
|
|
||||||
} from 'n8n-core';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
ILoadOptionsFunctions,
|
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodePropertyOptions,
|
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
import { couponFields, couponOperations } from './CouponDescription';
|
||||||
|
import { paddleApiRequest } from './GenericFunctions';
|
||||||
|
import { paymentFields, paymentOperations } from './PaymentDescription';
|
||||||
|
import { planFields, planOperations } from './PlanDescription';
|
||||||
|
import { productFields, productOperations } from './ProductDescription';
|
||||||
|
import { userFields, userOperations } from './UserDescription';
|
||||||
|
|
||||||
|
import moment = require('moment');
|
||||||
|
import { response } from 'express';
|
||||||
|
|
||||||
export class Paddle implements INodeType {
|
export class Paddle implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -41,7 +47,6 @@ export class Paddle implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'Coupon',
|
name: 'Coupon',
|
||||||
value: 'coupon',
|
value: 'coupon',
|
||||||
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Payments',
|
name: 'Payments',
|
||||||
|
@ -68,6 +73,22 @@ export class Paddle implements INodeType {
|
||||||
description: 'Resource to consume.',
|
description: 'Resource to consume.',
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// COUPON
|
||||||
|
couponFields,
|
||||||
|
couponOperations,
|
||||||
|
// PAYMENT
|
||||||
|
paymentFields,
|
||||||
|
paymentOperations,
|
||||||
|
// PLAN
|
||||||
|
planFields,
|
||||||
|
planOperations,
|
||||||
|
// PRODUCT
|
||||||
|
productFields,
|
||||||
|
productOperations,
|
||||||
|
// USER
|
||||||
|
userFields,
|
||||||
|
userOperations
|
||||||
|
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -76,10 +97,197 @@ export class Paddle implements INodeType {
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
const length = items.length as unknown as number;
|
const length = items.length as unknown as number;
|
||||||
let responseData;
|
let responseData;
|
||||||
const qs: IDataObject = {};
|
const body: IDataObject = {};
|
||||||
const resource = this.getNodeParameter('resource', 0) as string;
|
const resource = this.getNodeParameter('resource', 0) as string;
|
||||||
const operation = this.getNodeParameter('operation', 0) as string;
|
const operation = this.getNodeParameter('operation', 0) as string;
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
|
if (resource === 'coupon') {
|
||||||
|
if (operation === 'create') {
|
||||||
|
const productIds = this.getNodeParameter('productIds', i) as string;
|
||||||
|
const discountType = this.getNodeParameter('discountType', i) as string;
|
||||||
|
const discountAmount = this.getNodeParameter('discountAmount', i) as number;
|
||||||
|
const currency = this.getNodeParameter('currency', i) as string;
|
||||||
|
|
||||||
|
body.product_ids = productIds;
|
||||||
|
body.discount_type = discountType;
|
||||||
|
body.discount_amount = discountAmount;
|
||||||
|
body.currency = currency;
|
||||||
|
|
||||||
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||||
|
|
||||||
|
if (additionalFields.allowedUses) {
|
||||||
|
body.allowed_uses = additionalFields.allowedUses as number;
|
||||||
|
}
|
||||||
|
if (additionalFields.couponCode) {
|
||||||
|
body.coupon_code = additionalFields.couponCode as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.couponPrefix) {
|
||||||
|
body.coupon_prefix = additionalFields.couponPrefix as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.expires) {
|
||||||
|
body.expires = moment(additionalFields.expires as Date).format('YYYY/MM/DD') as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.group) {
|
||||||
|
body.group = additionalFields.group as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.recurring) {
|
||||||
|
if (additionalFields.recurring === true) {
|
||||||
|
body.recurring = 1;
|
||||||
|
} else {
|
||||||
|
body.recurring = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (additionalFields.numberOfCoupons) {
|
||||||
|
body.num_coupons = additionalFields.numberOfCoupons as number;
|
||||||
|
}
|
||||||
|
if (additionalFields.description) {
|
||||||
|
body.description = additionalFields.description as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const endpoint = '/2.1/product/create_coupon';
|
||||||
|
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (operation === 'getAll') {
|
||||||
|
const productIds = this.getNodeParameter('productId', i) as string;
|
||||||
|
const endpoint = '/2.0/product/list_coupons';
|
||||||
|
|
||||||
|
body.product_ids = productIds as string;
|
||||||
|
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (operation === 'update') {
|
||||||
|
const updateBy = this.getNodeParameter('updateBy', i) as string;
|
||||||
|
|
||||||
|
if (updateBy === 'group') {
|
||||||
|
body.group = this.getNodeParameter('group', i) as string;
|
||||||
|
} else {
|
||||||
|
body.coupon_code = this.getNodeParameter('couponCode', i) as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||||
|
|
||||||
|
if (additionalFields.allowedUses) {
|
||||||
|
body.allowed_uses = additionalFields.allowedUses as number;
|
||||||
|
}
|
||||||
|
if (additionalFields.currency) {
|
||||||
|
body.currency = additionalFields.currency as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.newCouponCode) {
|
||||||
|
body.new_coupon_code = additionalFields.newCouponCode as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.expires) {
|
||||||
|
body.expires = moment(additionalFields.expires as Date).format('YYYY/MM/DD') as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.newGroup) {
|
||||||
|
body.new_group = additionalFields.newGroup as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.recurring) {
|
||||||
|
if (additionalFields.recurring === true) {
|
||||||
|
body.recurring = 1;
|
||||||
|
} else {
|
||||||
|
body.recurring = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (additionalFields.productIds) {
|
||||||
|
body.product_ids = additionalFields.productIds as number;
|
||||||
|
}
|
||||||
|
if (additionalFields.discountAmount) {
|
||||||
|
body.discount_amount = additionalFields.discountAmount as number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const endpoint = '/2.1/product/update_coupon';
|
||||||
|
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (resource === 'payment') {
|
||||||
|
if (operation === 'getAll') {
|
||||||
|
const subscriptionId = this.getNodeParameter('subscription', i) as string;
|
||||||
|
const planId = this.getNodeParameter('planId', i) as string;
|
||||||
|
|
||||||
|
body.subscription_id = subscriptionId;
|
||||||
|
body.plan_id = planId;
|
||||||
|
|
||||||
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||||
|
|
||||||
|
if (additionalFields.state) {
|
||||||
|
body.state = additionalFields.state as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.isPaid) {
|
||||||
|
if (additionalFields.isPaid === true) {
|
||||||
|
body.is_paid = 0;
|
||||||
|
} else {
|
||||||
|
body.is_paid = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (additionalFields.from) {
|
||||||
|
body.from = moment(additionalFields.from as Date).format('YYYY/MM/DD') as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.to) {
|
||||||
|
body.to = moment(additionalFields.to as Date).format('YYYY/MM/DD') as string;
|
||||||
|
}
|
||||||
|
if (additionalFields.isOneOffCharge) {
|
||||||
|
body.is_one_off_charge = additionalFields.isOneOffCharge as boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const endpoint = '/2.0/subscription/payments';
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
if (operation === 'reschedule') {
|
||||||
|
const paymentId = this.getNodeParameter('paymentId', i) as number;
|
||||||
|
const date = this.getNodeParameter('date', i) as Date;
|
||||||
|
|
||||||
|
body.payment_id = paymentId;
|
||||||
|
body.date = body.to = moment(date as Date).format('YYYY/MM/DD') as string;
|
||||||
|
|
||||||
|
const endpoint = '/2.0/subscription/payments_reschedule';
|
||||||
|
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (resource === 'plan') {
|
||||||
|
if (operation === 'getAll') {
|
||||||
|
const planId = this.getNodeParameter('planId', i) as string;
|
||||||
|
|
||||||
|
body.plan = planId;
|
||||||
|
|
||||||
|
const endpoint = '/2.0/subscription/plans';
|
||||||
|
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (resource === 'product') {
|
||||||
|
if (operation === 'getAll') {
|
||||||
|
const endpoint = '/2.0/product/get_products';
|
||||||
|
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resource === 'user') {
|
||||||
|
if (operation === 'getAll') {
|
||||||
|
const subscriptionId = this.getNodeParameter('subscriptionId', i) as string;
|
||||||
|
const planId = this.getNodeParameter('planId', i) as string;
|
||||||
|
const limit = this.getNodeParameter('limit', i) as number;
|
||||||
|
|
||||||
|
body.subscription_id = subscriptionId;
|
||||||
|
body.plan_id = planId;
|
||||||
|
body.results_per_page = limit;
|
||||||
|
|
||||||
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||||
|
|
||||||
|
if (additionalFields.state) {
|
||||||
|
body.state = additionalFields.state as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const endpoint = '/2.0/subscription/users';
|
||||||
|
|
||||||
|
responseData = paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (Array.isArray(responseData)) {
|
if (Array.isArray(responseData)) {
|
||||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
ILoadOptionsFunctions,
|
ILoadOptionsFunctions,
|
||||||
INodePropertyOptions,
|
INodePropertyOptions,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
import { paddleApiRequest } from './GenericFunctions';
|
||||||
|
|
||||||
export class PaddleTrigger implements INodeType {
|
export class PaddleTrigger implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -69,7 +70,7 @@ import {
|
||||||
}
|
}
|
||||||
const endpoint = `/notifications/webhooks/${webhookData.webhookId}`;
|
const endpoint = `/notifications/webhooks/${webhookData.webhookId}`;
|
||||||
try {
|
try {
|
||||||
await payPalApiRequest.call(this, endpoint, 'GET');
|
await paddleApiRequest.call(this, endpoint, 'GET');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.response && err.response.name === 'INVALID_RESOURCE_ID') {
|
if (err.response && err.response.name === 'INVALID_RESOURCE_ID') {
|
||||||
// Webhook does not exist
|
// Webhook does not exist
|
||||||
|
@ -93,7 +94,7 @@ import {
|
||||||
};
|
};
|
||||||
const endpoint = '/notifications/webhooks';
|
const endpoint = '/notifications/webhooks';
|
||||||
try {
|
try {
|
||||||
webhook = await payPalApiRequest.call(this, endpoint, 'POST', body);
|
webhook = await paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
@ -111,7 +112,7 @@ import {
|
||||||
if (webhookData.webhookId !== undefined) {
|
if (webhookData.webhookId !== undefined) {
|
||||||
const endpoint = `/notifications/webhooks/${webhookData.webhookId}`;
|
const endpoint = `/notifications/webhooks/${webhookData.webhookId}`;
|
||||||
try {
|
try {
|
||||||
await payPalApiRequest.call(this, endpoint, 'DELETE', {});
|
await paddleApiRequest.call(this, endpoint, 'DELETE', {});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -145,7 +146,7 @@ import {
|
||||||
webhook_event: bodyData,
|
webhook_event: bodyData,
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
webhook = await payPalApiRequest.call(this, endpoint, 'POST', body);
|
webhook = await paddleApiRequest.call(this, endpoint, 'POST', body);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,6 @@ export const paymentOperations = [
|
||||||
] as INodeProperties[];
|
] as INodeProperties[];
|
||||||
|
|
||||||
export const paymentFields = [
|
export const paymentFields = [
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
/* payment:getAll */
|
/* payment:getAll */
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
@ -72,28 +71,6 @@ export const paymentFields = [
|
||||||
},
|
},
|
||||||
description: 'Filter: The product/plan ID (single or comma-separated values).',
|
description: 'Filter: The product/plan ID (single or comma-separated values).',
|
||||||
},
|
},
|
||||||
{
|
|
||||||
displayName: 'Limit',
|
|
||||||
name: 'limit',
|
|
||||||
type: 'number',
|
|
||||||
default: 1,
|
|
||||||
required: true,
|
|
||||||
typeOptions: {
|
|
||||||
minValue: 1,
|
|
||||||
maxValue: 200
|
|
||||||
},
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
resource: [
|
|
||||||
'user',
|
|
||||||
],
|
|
||||||
operation: [
|
|
||||||
'getAll',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
description: 'Number of subscription records to return per page.',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
displayName: 'Additional Fields',
|
displayName: 'Additional Fields',
|
||||||
name: 'additionalFields',
|
name: 'additionalFields',
|
||||||
|
|
Loading…
Reference in a new issue