From 20eab17b234840d59a9df33721f9507cbaafd256 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sun, 1 Dec 2019 16:15:02 +0100 Subject: [PATCH] :zap: Fixed build and made some smaller changes --- ...redentials.ts => PayPalApi.credentials.ts} | 2 +- .../{Paypal => PayPal}/GenericFunctions.ts | 11 +- .../Paypal.node.ts => PayPal/PayPal.node.ts} | 89 +++--- .../{Paypal => PayPal}/PaymentDescription.ts | 256 ++++++++++-------- .../{Paypal => PayPal}/PaymentInteface.ts | 0 .../nodes/{Paypal => PayPal}/paypal.png | Bin packages/nodes-base/package.json | 8 +- 7 files changed, 202 insertions(+), 164 deletions(-) rename packages/nodes-base/credentials/{PaypalApi.credentials.ts => PayPalApi.credentials.ts} (96%) rename packages/nodes-base/nodes/{Paypal => PayPal}/GenericFunctions.ts (91%) rename packages/nodes-base/nodes/{Paypal/Paypal.node.ts => PayPal/PayPal.node.ts} (65%) rename packages/nodes-base/nodes/{Paypal => PayPal}/PaymentDescription.ts (62%) rename packages/nodes-base/nodes/{Paypal => PayPal}/PaymentInteface.ts (100%) rename packages/nodes-base/nodes/{Paypal => PayPal}/paypal.png (100%) diff --git a/packages/nodes-base/credentials/PaypalApi.credentials.ts b/packages/nodes-base/credentials/PayPalApi.credentials.ts similarity index 96% rename from packages/nodes-base/credentials/PaypalApi.credentials.ts rename to packages/nodes-base/credentials/PayPalApi.credentials.ts index ae04c5ef87..96674b7370 100644 --- a/packages/nodes-base/credentials/PaypalApi.credentials.ts +++ b/packages/nodes-base/credentials/PayPalApi.credentials.ts @@ -5,7 +5,7 @@ import { export class PayPalApi implements ICredentialType { - name = 'paypalApi'; + name = 'payPalApi'; displayName = 'PayPal API'; properties = [ { diff --git a/packages/nodes-base/nodes/Paypal/GenericFunctions.ts b/packages/nodes-base/nodes/PayPal/GenericFunctions.ts similarity index 91% rename from packages/nodes-base/nodes/Paypal/GenericFunctions.ts rename to packages/nodes-base/nodes/PayPal/GenericFunctions.ts index c70b1de23c..28dc39e01a 100644 --- a/packages/nodes-base/nodes/Paypal/GenericFunctions.ts +++ b/packages/nodes-base/nodes/PayPal/GenericFunctions.ts @@ -12,8 +12,8 @@ import { IDataObject, } from 'n8n-workflow'; -export async function paypalApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('paypalApi'); +export async function payPalApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise { // tslint:disable-line:no-any + const credentials = this.getCredentials('payPalApi'); const env = getEnviroment(credentials!.env as string); const tokenInfo = await getAccessToken.call(this); const headerWithAuthentication = Object.assign({ }, @@ -47,7 +47,7 @@ function getEnviroment(env: string): string { } async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions): Promise { // tslint:disable-line:no-any - const credentials = this.getCredentials('paypalApi'); + const credentials = this.getCredentials('payPalApi'); if (credentials === undefined) { throw new Error('No credentials got returned!'); } @@ -75,11 +75,12 @@ async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecut throw error.response.body; } } + /** * Make an API request to paginated paypal endpoint * and return all results */ -export async function paypalApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise { // tslint:disable-line:no-any +export async function payPalApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise { // tslint:disable-line:no-any const returnData: IDataObject[] = []; @@ -88,7 +89,7 @@ export async function paypalApiRequestAllItems(this: IHookFunctions | IExecuteFu query!.page_size = 1000; do { - responseData = await paypalApiRequest.call(this, endpoint, method, body, query, uri); + responseData = await payPalApiRequest.call(this, endpoint, method, body, query, uri); uri = getNext(responseData.links); returnData.push.apply(returnData, responseData[propertyName]); } while ( diff --git a/packages/nodes-base/nodes/Paypal/Paypal.node.ts b/packages/nodes-base/nodes/PayPal/PayPal.node.ts similarity index 65% rename from packages/nodes-base/nodes/Paypal/Paypal.node.ts rename to packages/nodes-base/nodes/PayPal/PayPal.node.ts index 3f18788a91..9f664a46ee 100644 --- a/packages/nodes-base/nodes/Paypal/Paypal.node.ts +++ b/packages/nodes-base/nodes/PayPal/PayPal.node.ts @@ -1,7 +1,6 @@ import { IExecuteFunctions, } from 'n8n-core'; - import { IDataObject, INodeTypeDescription, @@ -9,8 +8,10 @@ import { INodeType, } from 'n8n-workflow'; import { - payoutOpeations, + payoutOperations, + payoutItemOperations, payoutFields, + payoutItemFields, } from './PaymentDescription'; import { IPaymentBatch, @@ -21,14 +22,14 @@ import { } from './PaymentInteface'; import { validateJSON, - paypalApiRequest, - paypalApiRequestAllItems + payPalApiRequest, + payPalApiRequestAllItems } from './GenericFunctions'; export class PayPal implements INodeType { description: INodeTypeDescription = { displayName: 'PayPal', - name: 'paypal', + name: 'payPal', icon: 'file:paypal.png', group: ['output'], version: 1, @@ -42,7 +43,7 @@ export class PayPal implements INodeType { outputs: ['main'], credentials: [ { - name: 'paypalApi', + name: 'payPalApi', required: true, } ], @@ -55,14 +56,21 @@ export class PayPal implements INodeType { { name: 'Payout', value: 'payout', - description: 'Use the Payouts API to make payments to multiple PayPal or Venmo recipients. The Payouts API is a fast, convenient way to send commissions, rebates, rewards, and general disbursements. You can send up to 15,000 payments per call. If you integrated the Payouts API before September 1, 2017, you receive transaction reports through Mass Payments Reporting.', + }, + { + name: 'Payout Item', + value: 'payoutItem', }, ], default: 'payout', description: 'Resource to consume.', }, - ...payoutOpeations, + + // Payout + ...payoutOperations, + ...payoutItemOperations, ...payoutFields, + ...payoutItemFields, ], }; @@ -71,10 +79,12 @@ export class PayPal implements INodeType { const returnData: IDataObject[] = []; const length = items.length as unknown as number; let responseData; - let qs: IDataObject = {}; + const qs: IDataObject = {}; + + const resource = this.getNodeParameter('resource', 0) as string; + const operation = this.getNodeParameter('operation', 0) as string; + for (let i = 0; i < length; i++) { - const resource = this.getNodeParameter('resource', 0) as string; - const operation = this.getNodeParameter('operation', 0) as string; if (resource === 'payout') { if (operation === 'create') { const body: IPaymentBatch = {}; @@ -97,18 +107,18 @@ export class PayPal implements INodeType { const payoutItems: IItem[] = []; const itemsValues = (this.getNodeParameter('itemsUi', i) as IDataObject).itemsValues as IDataObject[]; if (itemsValues && itemsValues.length > 0) { - itemsValues.forEach( o => { - const payoutItem: IItem = {}; - const amount: IAmount = {}; - amount.currency = o.currency as string; - amount.value = parseFloat(o.amount as string); - payoutItem.amount = amount; - payoutItem.note = o.note as string || ''; - payoutItem.receiver = o.receiverValue as string; - payoutItem.recipient_type = o.recipientType as RecipientType; - payoutItem.recipient_wallet = o.recipientWallet as RecipientWallet; - payoutItem.sender_item_id = o.senderItemId as string || ''; - payoutItems.push(payoutItem); + itemsValues.forEach(o => { + const payoutItem: IItem = {}; + const amount: IAmount = {}; + amount.currency = o.currency as string; + amount.value = parseFloat(o.amount as string); + payoutItem.amount = amount; + payoutItem.note = o.note as string || ''; + payoutItem.receiver = o.receiverValue as string; + payoutItem.recipient_type = o.recipientType as RecipientType; + payoutItem.recipient_wallet = o.recipientWallet as RecipientWallet; + payoutItem.sender_item_id = o.senderItemId as string || ''; + payoutItems.push(payoutItem); }); body.items = payoutItems; } else { @@ -119,40 +129,41 @@ export class PayPal implements INodeType { body.items = itemsJson; } try { - responseData = await paypalApiRequest.call(this, '/payments/payouts', 'POST', body); + responseData = await payPalApiRequest.call(this, '/payments/payouts', 'POST', body); } catch (err) { - throw new Error(`Paypal Error: ${JSON.stringify(err)}`); + throw new Error(`PayPal Error: ${JSON.stringify(err)}`); } } if (operation === 'get') { - const payoutItemId = this.getNodeParameter('payoutItemId', i) as string; - try { - responseData = await paypalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}`, 'GET', {}, qs); - } catch (err) { - throw new Error(`Paypal Error: ${JSON.stringify(err)}`); - } - } - if (operation === 'getAll') { const payoutBatchId = this.getNodeParameter('payoutBatchId', i) as string; const returnAll = this.getNodeParameter('returnAll', 0) as boolean; try { if (returnAll === true) { - responseData = await paypalApiRequestAllItems.call(this, 'items', `/payments/payouts/${payoutBatchId}`, 'GET', {}, qs); + responseData = await payPalApiRequestAllItems.call(this, 'items', `/payments/payouts/${payoutBatchId}`, 'GET', {}, qs); } else { qs.page_size = this.getNodeParameter('limit', i) as number; - responseData = await paypalApiRequest.call(this,`/payments/payouts/${payoutBatchId}`, 'GET', {}, qs); + responseData = await payPalApiRequest.call(this, `/payments/payouts/${payoutBatchId}`, 'GET', {}, qs); responseData = responseData.items; } } catch (err) { - throw new Error(`Paypal Error: ${JSON.stringify(err)}`); + throw new Error(`PayPal Error: ${JSON.stringify(err)}`); } } - if (operation === 'delete') { + } else if (resource === 'payoutItem') { + if (operation === 'get') { const payoutItemId = this.getNodeParameter('payoutItemId', i) as string; try { - responseData = await paypalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}/cancel`, 'POST', {}, qs); + responseData = await payPalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}`, 'GET', {}, qs); } catch (err) { - throw new Error(`Paypal Error: ${JSON.stringify(err)}`); + throw new Error(`PayPal Error: ${JSON.stringify(err)}`); + } + } + if (operation === 'cancel') { + const payoutItemId = this.getNodeParameter('payoutItemId', i) as string; + try { + responseData = await payPalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}/cancel`, 'POST', {}, qs); + } catch (err) { + throw new Error(`PayPal Error: ${JSON.stringify(err)}`); } } } diff --git a/packages/nodes-base/nodes/Paypal/PaymentDescription.ts b/packages/nodes-base/nodes/PayPal/PaymentDescription.ts similarity index 62% rename from packages/nodes-base/nodes/Paypal/PaymentDescription.ts rename to packages/nodes-base/nodes/PayPal/PaymentDescription.ts index 00c994ddeb..13a74cea62 100644 --- a/packages/nodes-base/nodes/Paypal/PaymentDescription.ts +++ b/packages/nodes-base/nodes/PayPal/PaymentDescription.ts @@ -1,6 +1,6 @@ import { INodeProperties } from "n8n-workflow"; -export const payoutOpeations = [ +export const payoutOperations = [ { displayName: 'Operation', name: 'operation', @@ -21,17 +21,7 @@ export const payoutOpeations = [ { name: 'Get', value: 'get', - description: 'Show payout item details', - }, - { - name: 'Get All', - value: 'getAll', - description: 'Show payout batch details', - }, - { - name: 'Delete', - value: 'delete', - description: 'Cancels an unclaimed payout item, by ID.', + description: 'Show batch payout details', }, ], default: 'create', @@ -120,7 +110,7 @@ export const payoutFields = [ { name: 'Email', value: 'email', - description: 'The unencrypted email. Value is a string of up to 127 single-byte characters.', + description: 'The unencrypted email.', }, { name: 'PayPal ID', @@ -137,7 +127,7 @@ export const payoutFields = [ type: 'string', required: true, default: '', - description: 'The receiver of the payment. Corresponds to the recipient_type value in the request. Max value of up to 127 single-byte characters.', + description: 'The receiver of the payment. Corresponds to the recipient_type value
in the request. Max length: 127 characters.', }, { displayName: 'Currency', @@ -190,7 +180,7 @@ export const payoutFields = [ type: 'string', required: false, default: '', - description: 'The sender-specified note for notifications. Supports up to 4000 ASCII characters and 1000 non-ASCII characters.', + description: 'The sender-specified note for notifications. Supports up to
4000 ASCII characters and 1000 non-ASCII characters.', }, { displayName: 'Sender Item ID', @@ -266,128 +256,164 @@ export const payoutFields = [ name: 'emailSubject', type: 'string', default: '', - description: 'The subject line for the email that PayPal sends when payment for a payout item completes. The subject line is the same for all recipients. Value is an alphanumeric string of up to 255 single-byte characters.', + description: 'The subject line for the email that PayPal sends when payment
for a payout item completes. The subject line is the same for all
recipients. Max length: 255 characters.', }, { displayName: 'Email Message', name: 'emailMessage', type: 'string', default: '', - description: 'The email message that PayPal sends when the payout item completes. The message is the same for all recipients.', + description: 'The email message that PayPal sends when the payout item completes.
The message is the same for all recipients.', }, { displayName: 'Note', name: 'note', type: 'string', default: '', - description: 'The payouts and item-level notes are concatenated in the email. The maximum combined length of the notes is 1000 characters.', + description: 'The payouts and item-level notes are concatenated in the email.
Max length: 1000 characters.', }, ], }, -/* -------------------------------------------------------------------------- */ -/* payout:getAll */ -/* -------------------------------------------------------------------------- */ + /* -------------------------------------------------------------------------- */ + /* payout:get */ + /* -------------------------------------------------------------------------- */ -{ - displayName: 'Payout Batch Id', - name: 'payoutBatchId', - type: 'string', - required: true, - displayOptions: { - show: { - resource: [ - 'payout', - ], - operation: [ - 'getAll', - ], + { + displayName: 'Payout Batch Id', + name: 'payoutBatchId', + type: 'string', + required: true, + displayOptions: { + show: { + resource: [ + 'payout', + ], + operation: [ + 'get', + ], + }, }, + description: 'The ID of the payout for which to show details.', }, - description: 'The ID of the payout for which to show details.', -}, -{ - displayName: 'Return All', - name: 'returnAll', - type: 'boolean', - default: false, - displayOptions: { - show: { - resource: [ - 'payout', - ], - operation: [ - 'getAll', - ], + { + displayName: 'Return All', + name: 'returnAll', + type: 'boolean', + default: false, + displayOptions: { + show: { + resource: [ + 'payout', + ], + operation: [ + 'get', + ], + }, }, + description: 'If all results should be returned or only up to a given limit.', }, - description: 'If all results should be returned or only up to a given limit.', -}, -{ - displayName: 'Limit', - name: 'limit', - type: 'number', - typeOptions: { - maxValue: 1000, - minValue: 1 - }, - default: 100, - displayOptions: { - show: { - resource: [ - 'payout', - ], - operation: [ - 'getAll', - ], - returnAll: [ - false, - ], + { + displayName: 'Limit', + name: 'limit', + type: 'number', + typeOptions: { + maxValue: 1000, + minValue: 1 }, + default: 100, + displayOptions: { + show: { + resource: [ + 'payout', + ], + operation: [ + 'get', + ], + returnAll: [ + false, + ], + }, + }, + description: 'If all results should be returned or only up to a given limit.', }, - description: 'If all results should be returned or only up to a given limit.', -}, -/* -------------------------------------------------------------------------- */ -/* payout:get */ -/* -------------------------------------------------------------------------- */ -{ - displayName: 'Payout Item Id', - name: 'payoutItemId', - type: 'string', - required: true, - displayOptions: { - show: { - resource: [ - 'payout', - ], - operation: [ - 'get', - ], - }, - }, - description: 'The ID of the payout item for which to show details.', -}, - -/* -------------------------------------------------------------------------- */ -/* payout:delete */ -/* -------------------------------------------------------------------------- */ - -{ - displayName: 'Payout Item Id', - name: 'payoutItemId', - type: 'string', - required: true, - displayOptions: { - show: { - resource: [ - 'payout', - ], - operation: [ - 'delete', - ], - }, - }, - description: 'The ID of the payout item to cancel.', -}, +] as INodeProperties[]; + + +export const payoutItemOperations = [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'payoutItem', + ], + }, + }, + options: [ + { + name: 'Cancel', + value: 'cancel', + description: 'Cancels an unclaimed payout item', + }, + { + name: 'Get', + value: 'get', + description: 'Show payout item details', + }, + ], + default: 'get', + description: 'The operation to perform.', + }, +] as INodeProperties[]; + +export const payoutItemFields = [ + + /* -------------------------------------------------------------------------- */ + /* payoutItem:get */ + /* -------------------------------------------------------------------------- */ + { + displayName: 'Payout Item Id', + name: 'payoutItemId', + type: 'string', + default: '', + required: true, + displayOptions: { + show: { + resource: [ + 'payoutItem', + ], + operation: [ + 'get', + ], + }, + }, + description: 'The ID of the payout item for which to show details.', + }, + + /* -------------------------------------------------------------------------- */ + /* payoutItem:cancel */ + /* -------------------------------------------------------------------------- */ + + { + displayName: 'Payout Item Id', + name: 'payoutItemId', + type: 'string', + default: '', + required: true, + displayOptions: { + show: { + resource: [ + 'payoutItem', + ], + operation: [ + 'cancel', + ], + }, + }, + description: 'The ID of the payout item to cancel.', + }, ] as INodeProperties[]; diff --git a/packages/nodes-base/nodes/Paypal/PaymentInteface.ts b/packages/nodes-base/nodes/PayPal/PaymentInteface.ts similarity index 100% rename from packages/nodes-base/nodes/Paypal/PaymentInteface.ts rename to packages/nodes-base/nodes/PayPal/PaymentInteface.ts diff --git a/packages/nodes-base/nodes/Paypal/paypal.png b/packages/nodes-base/nodes/PayPal/paypal.png similarity index 100% rename from packages/nodes-base/nodes/Paypal/paypal.png rename to packages/nodes-base/nodes/PayPal/paypal.png diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 8500467f80..92a4f0e63d 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -52,8 +52,8 @@ "dist/credentials/NextCloudApi.credentials.js", "dist/credentials/OpenWeatherMapApi.credentials.js", "dist/credentials/PipedriveApi.credentials.js", - "dist/credentials/Postgres.credentials.js", - "dist/credentials/PayPalApi.credentials.js", + "dist/credentials/Postgres.credentials.js", + "dist/credentials/PayPalApi.credentials.js", "dist/credentials/Redis.credentials.js", "dist/credentials/RocketchatApi.credentials.js", "dist/credentials/SlackApi.credentials.js", @@ -115,8 +115,8 @@ "dist/nodes/OpenWeatherMap.node.js", "dist/nodes/Pipedrive/Pipedrive.node.js", "dist/nodes/Pipedrive/PipedriveTrigger.node.js", - "dist/nodes/Postgres/Postgres.node.js", - "dist/nodes/PayPal/PayPal.node.js", + "dist/nodes/Postgres/Postgres.node.js", + "dist/nodes/PayPal/PayPal.node.js", "dist/nodes/Rocketchat/Rocketchat.node.js", "dist/nodes/ReadBinaryFile.node.js", "dist/nodes/ReadBinaryFiles.node.js",