2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2019-11-23 16:36:47 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
JsonObject,
|
|
|
|
IDataObject,
|
2019-11-23 16:36:47 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2019-12-01 07:38:54 -08:00
|
|
|
IWebhookFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
import { BINARY_ENCODING, NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2021-04-16 09:33:36 -07:00
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
function getEnvironment(env: string) {
|
|
|
|
return {
|
|
|
|
sanbox: 'https://api-m.sandbox.paypal.com',
|
|
|
|
live: 'https://api-m.paypal.com',
|
|
|
|
}[env];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAccessToken(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
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);
|
2023-01-13 09:11:56 -08:00
|
|
|
const data = Buffer.from(`${credentials.clientId}:${credentials.secret}`).toString(
|
|
|
|
BINARY_ENCODING,
|
|
|
|
);
|
2022-08-17 08:50:24 -07:00
|
|
|
const headerWithAuthentication = Object.assign(
|
|
|
|
{},
|
2023-01-13 09:11:56 -08:00
|
|
|
{ Authorization: `Basic ${data}`, 'Content-Type': 'application/x-www-form-urlencoded' },
|
2022-08-17 08:50:24 -07:00
|
|
|
);
|
2023-01-13 09:11:56 -08:00
|
|
|
const options: OptionsWithUri = {
|
2019-11-26 08:59:27 -08:00
|
|
|
headers: headerWithAuthentication,
|
2023-01-13 09:11:56 -08:00
|
|
|
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 {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2019-11-23 16:36:47 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeOperationError(this.getNode(), error as 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
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
export async function payPalApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions | IWebhookFunctions,
|
2023-01-13 09:11:56 -08:00
|
|
|
endpoint: string,
|
|
|
|
method: string,
|
|
|
|
|
|
|
|
body: any = {},
|
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
2022-08-17 08:50:24 -07:00
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('payPalApi');
|
2022-12-02 12:54:28 -08:00
|
|
|
const env = getEnvironment(credentials.env as string);
|
2023-01-13 09:11:56 -08:00
|
|
|
const tokenInfo = await getAccessToken.call(this);
|
2022-08-17 08:50:24 -07:00
|
|
|
const headerWithAuthentication = Object.assign(
|
|
|
|
{},
|
2023-01-13 09:11:56 -08:00
|
|
|
{ Authorization: `Bearer ${tokenInfo.access_token}`, 'Content-Type': 'application/json' },
|
2022-08-17 08:50:24 -07:00
|
|
|
);
|
2023-01-13 09:11:56 -08:00
|
|
|
const options = {
|
2022-08-17 08:50:24 -07:00
|
|
|
headers: headerWithAuthentication,
|
2023-01-13 09:11:56 -08:00
|
|
|
method,
|
2023-01-19 04:37:19 -08:00
|
|
|
qs: query || {},
|
|
|
|
uri: uri || `${env}/v1${endpoint}`,
|
2023-01-13 09:11:56 -08:00
|
|
|
body,
|
2022-08-17 08:50:24 -07:00
|
|
|
json: true,
|
|
|
|
};
|
2019-11-23 16:36:47 -08:00
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2019-11-23 16:36:47 -08:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2023-01-13 09:11:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNext(links: IDataObject[]): string | undefined {
|
|
|
|
for (const link of links) {
|
|
|
|
if (link.rel === 'next') {
|
|
|
|
return link.href as string;
|
|
|
|
}
|
2019-11-23 16:36:47 -08:00
|
|
|
}
|
2023-01-13 09:11:56 -08:00
|
|
|
return undefined;
|
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
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function payPalApiRequestAllItems(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-17 08:50:24 -07:00
|
|
|
propertyName: string,
|
|
|
|
endpoint: string,
|
|
|
|
method: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
|
|
|
): Promise<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);
|
2023-02-27 19:39:43 -08:00
|
|
|
uri = getNext(responseData.links as IDataObject[]);
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
|
|
|
} while (getNext(responseData.links as IDataObject[]) !== undefined);
|
2019-11-23 16:36:47 -08:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function validateJSON(json: string | undefined): any {
|
2019-11-23 16:36:47 -08:00
|
|
|
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 {
|
2022-08-17 08:50:24 -07:00
|
|
|
return s
|
|
|
|
.split('.')
|
|
|
|
.map((e) => {
|
|
|
|
return e.toLowerCase().charAt(0).toUpperCase() + e.toLowerCase().slice(1);
|
|
|
|
})
|
|
|
|
.join(' ');
|
2019-12-01 07:38:54 -08:00
|
|
|
}
|