2019-11-23 16:36:47 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
|
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
BINARY_ENCODING,
|
2019-11-23 16:36:47 -08:00
|
|
|
IExecuteFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
IExecuteSingleFunctions,
|
2019-11-23 16:36:47 -08:00
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2019-12-01 07:38:54 -08:00
|
|
|
IWebhookFunctions,
|
2019-11-23 16:36:47 -08:00
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2019-11-23 16:36:47 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2019-12-01 11:19:38 -08:00
|
|
|
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
|
2021-04-16 09:33:36 -07:00
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('payPalApi');
|
2022-04-14 23:00:47 -07:00
|
|
|
const env = getEnvironment(credentials.env as string);
|
2019-11-26 08:59:27 -08:00
|
|
|
const tokenInfo = await getAccessToken.call(this);
|
|
|
|
const headerWithAuthentication = Object.assign({ },
|
|
|
|
{ Authorization: `Bearer ${tokenInfo.access_token}`, 'Content-Type': 'application/json' });
|
|
|
|
const options = {
|
|
|
|
headers: headerWithAuthentication,
|
|
|
|
method,
|
|
|
|
qs: query || {},
|
|
|
|
uri: uri || `${env}/v1${endpoint}`,
|
|
|
|
body,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-26 08:59:27 -08:00
|
|
|
};
|
2019-11-23 16:36:47 -08:00
|
|
|
try {
|
2019-11-26 08:59:27 -08:00
|
|
|
return await this.helpers.request!(options);
|
2019-11-23 16:36:47 -08:00
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-11-23 16:36:47 -08:00
|
|
|
}
|
2019-11-26 08:59:27 -08:00
|
|
|
}
|
2019-11-23 16:36:47 -08:00
|
|
|
|
2021-01-07 04:43:42 -08:00
|
|
|
function getEnvironment(env: string): string {
|
2019-11-26 08:59:27 -08:00
|
|
|
// @ts-ignore
|
|
|
|
return {
|
2022-04-08 02:49:07 -07:00
|
|
|
'sanbox': 'https://api-m.sandbox.paypal.com',
|
|
|
|
'live': 'https://api-m.paypal.com',
|
2019-11-26 08:59:27 -08:00
|
|
|
}[env];
|
|
|
|
}
|
2019-11-23 16:36:47 -08:00
|
|
|
|
2019-12-01 07:38:54 -08:00
|
|
|
async function getAccessToken(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('payPalApi');
|
2021-01-07 04:43:42 -08:00
|
|
|
const env = getEnvironment(credentials!.env as string);
|
2019-11-26 08:59:27 -08:00
|
|
|
const data = Buffer.from(`${credentials!.clientId}:${credentials!.secret}`).toString(BINARY_ENCODING);
|
|
|
|
const headerWithAuthentication = Object.assign({},
|
|
|
|
{ Authorization: `Basic ${data}`, 'Content-Type': 'application/x-www-form-urlencoded' });
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: headerWithAuthentication,
|
|
|
|
method: 'POST',
|
|
|
|
form: {
|
|
|
|
grant_type: 'client_credentials',
|
|
|
|
},
|
|
|
|
uri: `${env}/v1/oauth2/token`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-11-26 08:59:27 -08:00
|
|
|
};
|
2019-11-23 16:36:47 -08:00
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), error);
|
2019-11-23 16:36:47 -08:00
|
|
|
}
|
|
|
|
}
|
2019-12-01 07:15:02 -08:00
|
|
|
|
2019-11-23 16:36:47 -08:00
|
|
|
/**
|
2019-11-26 08:59:27 -08:00
|
|
|
* Make an API request to paginated paypal endpoint
|
2019-11-23 16:36:47 -08:00
|
|
|
* and return all results
|
|
|
|
*/
|
2019-12-01 11:19:38 -08:00
|
|
|
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
|
2019-11-23 16:36:47 -08:00
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
2019-11-26 08:59:27 -08:00
|
|
|
query!.page_size = 1000;
|
2019-11-23 16:36:47 -08:00
|
|
|
|
|
|
|
do {
|
2019-12-01 11:19:38 -08:00
|
|
|
responseData = await payPalApiRequest.call(this, endpoint, method, body, query, uri);
|
2019-11-26 08:59:27 -08:00
|
|
|
uri = getNext(responseData.links);
|
2019-11-23 16:36:47 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
} while (
|
2019-11-26 08:59:27 -08:00
|
|
|
getNext(responseData.links) !== undefined
|
2019-11-23 16:36:47 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2019-11-26 08:59:27 -08:00
|
|
|
function getNext(links: IDataObject[]): string | undefined {
|
|
|
|
for (const link of links) {
|
|
|
|
if (link.rel === 'next') {
|
|
|
|
return link.href as string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-11-23 16:36:47 -08:00
|
|
|
|
|
|
|
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(json!);
|
|
|
|
} catch (exception) {
|
|
|
|
result = '';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-12-01 07:38:54 -08:00
|
|
|
|
|
|
|
export function upperFist(s: string): string {
|
|
|
|
return s.split('.').map(e => {
|
|
|
|
return e.toLowerCase().charAt(0).toUpperCase() + e.toLowerCase().slice(1);
|
|
|
|
}).join(' ');
|
|
|
|
}
|