Smaller changes and add wildcard-event

This commit is contained in:
Jan Oberhauser 2019-12-01 20:19:38 +01:00
parent 246429b09d
commit ee8d63f34a
4 changed files with 40 additions and 24 deletions

View file

@ -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<any> { // 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<any> { // 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<any> { // 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<any> { // 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<any> { // 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 (

View file

@ -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)}`);
}

View file

@ -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<INodePropertyOptions[]> {
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;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.9 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB