From ee8d63f34a5f35ebfdb1a7597454671d791c9ef8 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sun, 1 Dec 2019 20:19:38 +0100 Subject: [PATCH] :zap: Smaller changes and add wildcard-event --- .../nodes/PayPal/GenericFunctions.ts | 27 ++++++++++++------ .../nodes-base/nodes/PayPal/PayPal.node.ts | 14 ++++----- .../{Paypal => PayPal}/PayPalTrigger.node.ts | 23 +++++++++------ packages/nodes-base/nodes/PayPal/paypal.png | Bin 4979 -> 1227 bytes 4 files changed, 40 insertions(+), 24 deletions(-) rename packages/nodes-base/nodes/{Paypal => PayPal}/PayPalTrigger.node.ts (89%) diff --git a/packages/nodes-base/nodes/PayPal/GenericFunctions.ts b/packages/nodes-base/nodes/PayPal/GenericFunctions.ts index 533046b95..b1b5a2091 100644 --- a/packages/nodes-base/nodes/PayPal/GenericFunctions.ts +++ b/packages/nodes-base/nodes/PayPal/GenericFunctions.ts @@ -13,8 +13,8 @@ import { IDataObject, } from 'n8n-workflow'; -export async function paypalApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, 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 | IWebhookFunctions, 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({ }, @@ -30,7 +30,16 @@ export async function paypalApiRequest(this: IHookFunctions | IExecuteFunctions try { return await this.helpers.request!(options); } catch (error) { - throw error.response.body; + + if (error.response.body) { + 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; } } @@ -38,12 +47,12 @@ function getEnviroment(env: string): string { // @ts-ignore return { 'sanbox': 'https://api.sandbox.paypal.com', - 'live': 'https://api.paypal.com' + 'live': 'https://api.paypal.com', }[env]; } async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions): 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!'); } @@ -66,9 +75,9 @@ async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecut const errorMessage = error.response.body.message || error.response.body.Message; if (errorMessage !== undefined) { - throw errorMessage; + throw new Error(errorMessage); } - throw error.response.body; + throw new Error(error.response.body); } } @@ -76,7 +85,7 @@ async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecut * 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[] = []; @@ -85,7 +94,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 index 8071525f7..2f1f00147 100644 --- a/packages/nodes-base/nodes/PayPal/PayPal.node.ts +++ b/packages/nodes-base/nodes/PayPal/PayPal.node.ts @@ -21,9 +21,9 @@ import { RecipientWallet, } from './PaymentInteface'; import { + payPalApiRequest, + payPalApiRequestAllItems, validateJSON, - paypalApiRequest, - paypalApiRequestAllItems } from './GenericFunctions'; export class PayPal implements INodeType { @@ -129,7 +129,7 @@ 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)}`); } @@ -139,10 +139,10 @@ export class PayPal implements INodeType { 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) { @@ -153,7 +153,7 @@ export class PayPal implements INodeType { if (operation === 'get') { const payoutItemId = this.getNodeParameter('payoutItemId', i) as string; try { - responseData = await paypalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}`, 'GET', {}, qs); + responseData = await payPalApiRequest.call(this,`/payments/payouts-item/${payoutItemId}`, 'GET', {}, qs); } catch (err) { throw new Error(`PayPal Error: ${JSON.stringify(err)}`); } @@ -161,7 +161,7 @@ export class PayPal implements INodeType { if (operation === 'cancel') { 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}/cancel`, 'POST', {}, qs); } catch (err) { throw new Error(`PayPal Error: ${JSON.stringify(err)}`); } diff --git a/packages/nodes-base/nodes/Paypal/PayPalTrigger.node.ts b/packages/nodes-base/nodes/PayPal/PayPalTrigger.node.ts similarity index 89% rename from packages/nodes-base/nodes/Paypal/PayPalTrigger.node.ts rename to packages/nodes-base/nodes/PayPal/PayPalTrigger.node.ts index baa07951b..e12d7dd00 100644 --- a/packages/nodes-base/nodes/Paypal/PayPalTrigger.node.ts +++ b/packages/nodes-base/nodes/PayPal/PayPalTrigger.node.ts @@ -12,7 +12,7 @@ import { INodePropertyOptions, } from 'n8n-workflow'; import { - paypalApiRequest, + payPalApiRequest, upperFist } from './GenericFunctions'; @@ -32,7 +32,7 @@ import { outputs: ['main'], credentials: [ { - name: 'paypalApi', + name: 'payPalApi', required: true, } ], @@ -65,11 +65,17 @@ import { // Get all the events types to display them to user so that he can // select them easily async getEvents(this: ILoadOptionsFunctions): Promise { - const returnData: INodePropertyOptions[] = []; + const returnData: INodePropertyOptions[] = [ + { + name: '*', + value: '*', + description: 'Any time any event is triggered (Wildcard Event).', + } + ]; let events; try { const endpoint = '/notifications/webhooks-event-types'; - events = await paypalApiRequest.call(this, endpoint, 'GET'); + events = await payPalApiRequest.call(this, endpoint, 'GET'); } catch (err) { throw new Error(`PayPal Error: ${err}`); } @@ -88,6 +94,7 @@ import { }, }, }; + // @ts-ignore (because of request) webhookMethods = { default: { @@ -99,7 +106,7 @@ import { } const endpoint = `/notifications/webhooks/${webhookData.webhookId}`; try { - await paypalApiRequest.call(this, endpoint, 'GET'); + await payPalApiRequest.call(this, endpoint, 'GET'); } catch (err) { if (err.response && err.response.name === 'INVALID_RESOURCE_ID') { // Webhook does not exist @@ -123,7 +130,7 @@ import { }; const endpoint = '/notifications/webhooks'; try { - webhook = await paypalApiRequest.call(this, endpoint, 'POST', body); + webhook = await payPalApiRequest.call(this, endpoint, 'POST', body); } catch (e) { throw e; } @@ -141,7 +148,7 @@ import { if (webhookData.webhookId !== undefined) { const endpoint = `/notifications/webhooks/${webhookData.webhookId}`; try { - await paypalApiRequest.call(this, endpoint, 'DELETE', {}); + await payPalApiRequest.call(this, endpoint, 'DELETE', {}); } catch (e) { return false; } @@ -175,7 +182,7 @@ import { webhook_event: bodyData, }; try { - webhook = await paypalApiRequest.call(this, endpoint, 'POST', body); + webhook = await payPalApiRequest.call(this, endpoint, 'POST', body); } catch (e) { throw e; } diff --git a/packages/nodes-base/nodes/PayPal/paypal.png b/packages/nodes-base/nodes/PayPal/paypal.png index 01891251ef715748e96d060b917a35afc8a25300..64e121888023dff594148cf0ec2a606fb8edbe94 100644 GIT binary patch literal 1227 zcmV;+1T_1JP)Y-9mrAk$m7V=~5-JO|!$6iE~#52jRnYG0}>6=Zw+4X+*y?O7= z>@q1Oy-nuOTV^S63Y-F`z$tJF+(nj+a?s$}#jSTRI**?>pwS(kQa~J9h5m(}LBBwc z!{6K5(hoP@JTSnvvI6t~8tZ@~7Q$@?E_m95s{HWx7*r>5ym zZgf(HA}(BX3Rsn-q7hUIVva5}Sub-FBx{GJ9C;xFj(j4T|H?%Go(usntkkoOYr7 zn{8kH8nr2A$PfMR&MAI5Y#--E7@C5t+SRejMfub{T_ za(O$`!tcnUMFmz%vir0SMkw>r9uu$%=jXiuJgfz7X6S4yVM$?1NdNQ#@B~!RWoVi2 zhw@~o!GAz6dI9*5_E_}Lyj%)BtOb5Xt=Ao_S=223HMN#zE1ND@N`|`Y1_$=<#$T5V z=lP2)TU6lRp)WS|JL(BRz^Q&_it1C7i{u9;;OD&pyk(+do=@)F2jqS@BF(NKyUag^wC3gy3NP%6Iv_k7Xx5gohN;96K)_3?{22yu@HxIm;l~Y0%A+?@4yT(b!BB zmCp7DP!UWHWDSGiBXH0o6W~)35zG)44;NvE6j|V)XQ3F4M2JlI!DdKLvJ1kR%>@t! zD18(LNsvJx5O^+~fpaC=Ou?aFX2?K3pMyiA!^6W-;d&@GHvo+_F)=}7bkVxHI*^49 zFOtQlM(D73>XRhXJVby;>fOEivnmJ)~+PADYq1+HrPIMX?2m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