2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2019-11-22 09:38:08 -08:00
|
|
|
import {
|
2022-04-08 02:49:07 -07:00
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialTestFunctions,
|
2019-11-22 09:38:08 -08:00
|
|
|
IDataObject,
|
2022-04-08 02:49:07 -07:00
|
|
|
INodeCredentialTestResult,
|
2019-11-22 09:38:08 -08:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodeTypeDescription,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2019-11-22 09:38:08 -08:00
|
|
|
} from 'n8n-workflow';
|
2019-11-23 16:36:47 -08:00
|
|
|
import {
|
|
|
|
payoutFields,
|
2019-12-01 07:15:02 -08:00
|
|
|
payoutItemFields,
|
2020-10-01 05:01:39 -07:00
|
|
|
payoutItemOperations,
|
|
|
|
payoutOperations,
|
2019-11-23 16:36:47 -08:00
|
|
|
} from './PaymentDescription';
|
|
|
|
import {
|
2020-10-01 05:01:39 -07:00
|
|
|
IAmount,
|
|
|
|
IItem,
|
2019-11-23 16:36:47 -08:00
|
|
|
IPaymentBatch,
|
|
|
|
ISenderBatchHeader,
|
|
|
|
RecipientType,
|
|
|
|
RecipientWallet,
|
2022-08-17 08:50:24 -07:00
|
|
|
} from './PaymentInteface';
|
|
|
|
import { payPalApiRequest, payPalApiRequestAllItems, validateJSON } from './GenericFunctions';
|
2019-11-22 09:38:08 -08:00
|
|
|
|
2019-11-23 16:36:47 -08:00
|
|
|
export class PayPal implements INodeType {
|
2019-11-22 09:38:08 -08:00
|
|
|
description: INodeTypeDescription = {
|
2019-11-23 16:36:47 -08:00
|
|
|
displayName: 'PayPal',
|
2019-12-01 07:15:02 -08:00
|
|
|
name: 'payPal',
|
2021-03-25 09:10:02 -07:00
|
|
|
icon: 'file:paypal.svg',
|
2019-11-22 09:38:08 -08:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
2019-11-23 16:36:47 -08:00
|
|
|
description: 'Consume PayPal API',
|
2019-11-22 09:38:08 -08:00
|
|
|
defaults: {
|
2019-11-23 16:36:47 -08:00
|
|
|
name: 'PayPal',
|
2019-11-22 09:38:08 -08:00
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
2019-12-01 07:15:02 -08:00
|
|
|
name: 'payPalApi',
|
2019-11-22 09:38:08 -08:00
|
|
|
required: true,
|
2022-04-08 02:49:07 -07:00
|
|
|
testedBy: 'payPalApiTest',
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-11-22 09:38:08 -08:00
|
|
|
],
|
2019-11-23 16:36:47 -08:00
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2019-11-23 16:36:47 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Payout',
|
|
|
|
value: 'payout',
|
2019-12-01 07:15:02 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Payout Item',
|
|
|
|
value: 'payoutItem',
|
2019-11-23 16:36:47 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'payout',
|
|
|
|
},
|
2019-12-01 07:15:02 -08:00
|
|
|
|
|
|
|
// Payout
|
|
|
|
...payoutOperations,
|
|
|
|
...payoutItemOperations,
|
2019-11-23 16:36:47 -08:00
|
|
|
...payoutFields,
|
2019-12-01 07:15:02 -08:00
|
|
|
...payoutItemFields,
|
2019-11-23 16:36:47 -08:00
|
|
|
],
|
2019-11-22 09:38:08 -08:00
|
|
|
};
|
|
|
|
|
2022-04-08 02:49:07 -07:00
|
|
|
methods = {
|
|
|
|
credentialTest: {
|
2022-08-17 08:50:24 -07:00
|
|
|
async payPalApiTest(
|
|
|
|
this: ICredentialTestFunctions,
|
|
|
|
credential: ICredentialsDecrypted,
|
|
|
|
): Promise<INodeCredentialTestResult> {
|
2022-04-08 02:49:07 -07:00
|
|
|
const credentials = credential.data;
|
|
|
|
const clientId = credentials!.clientId;
|
|
|
|
const clientSecret = credentials!.secret;
|
|
|
|
const environment = credentials!.env;
|
|
|
|
|
|
|
|
if (!clientId || !clientSecret || !environment) {
|
|
|
|
return {
|
|
|
|
status: 'Error',
|
2022-12-29 03:20:43 -08:00
|
|
|
message: 'Connection details not valid: missing credentials',
|
2022-04-08 02:49:07 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let baseUrl = '';
|
|
|
|
if (environment !== 'live') {
|
|
|
|
baseUrl = 'https://api-m.sandbox.paypal.com';
|
|
|
|
} else {
|
|
|
|
baseUrl = 'https://api-m.paypal.com';
|
|
|
|
}
|
|
|
|
|
|
|
|
const base64Key = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-17 08:50:24 -07:00
|
|
|
Authorization: `Basic ${base64Key}`,
|
2022-04-08 02:49:07 -07:00
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
uri: `${baseUrl}/v1/oauth2/token`,
|
|
|
|
form: {
|
|
|
|
grant_type: 'client_credentials',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2022-12-23 10:09:52 -08:00
|
|
|
await this.helpers.request(options);
|
2022-04-08 02:49:07 -07:00
|
|
|
return {
|
|
|
|
status: 'OK',
|
|
|
|
message: 'Authentication successful!',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
} catch (error) {
|
2022-04-08 02:49:07 -07:00
|
|
|
return {
|
|
|
|
status: 'Error',
|
|
|
|
message: `Connection details not valid: ${error.message}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-22 09:38:08 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2019-11-23 16:36:47 -08:00
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2019-11-23 16:36:47 -08:00
|
|
|
let responseData;
|
2019-12-01 07:15:02 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2019-12-01 07:15:02 -08:00
|
|
|
|
2019-11-23 16:36:47 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'payout') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const body: IPaymentBatch = {};
|
|
|
|
const header: ISenderBatchHeader = {};
|
2022-11-18 05:31:38 -08:00
|
|
|
const jsonActive = this.getNodeParameter('jsonParameters', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
const senderBatchId = this.getNodeParameter('senderBatchId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
header.sender_batch_id = senderBatchId;
|
|
|
|
if (additionalFields.emailSubject) {
|
|
|
|
header.email_subject = additionalFields.emailSubject as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.emailMessage) {
|
|
|
|
header.email_message = additionalFields.emailMessage as string;
|
|
|
|
}
|
|
|
|
if (additionalFields.note) {
|
|
|
|
header.note = additionalFields.note as string;
|
|
|
|
}
|
|
|
|
body.sender_batch_header = header;
|
|
|
|
if (!jsonActive) {
|
|
|
|
const payoutItems: IItem[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const itemsValues = (this.getNodeParameter('itemsUi', i) as IDataObject)
|
|
|
|
.itemsValues as IDataObject[];
|
2021-07-19 23:58:54 -07:00
|
|
|
if (itemsValues && itemsValues.length > 0) {
|
2022-08-17 08:50:24 -07:00
|
|
|
itemsValues.forEach((o) => {
|
2021-07-19 23:58:54 -07:00
|
|
|
const payoutItem: IItem = {};
|
|
|
|
const amount: IAmount = {};
|
|
|
|
amount.currency = o.currency as string;
|
|
|
|
amount.value = parseFloat(o.amount as string);
|
|
|
|
payoutItem.amount = amount;
|
2022-08-17 08:50:24 -07:00
|
|
|
payoutItem.note = (o.note as string) || '';
|
2021-07-19 23:58:54 -07:00
|
|
|
payoutItem.receiver = o.receiverValue as string;
|
|
|
|
payoutItem.recipient_type = o.recipientType as RecipientType;
|
|
|
|
payoutItem.recipient_wallet = o.recipientWallet as RecipientWallet;
|
2022-08-17 08:50:24 -07:00
|
|
|
payoutItem.sender_item_id = (o.senderItemId as string) || '';
|
2021-07-19 23:58:54 -07:00
|
|
|
payoutItems.push(payoutItem);
|
|
|
|
});
|
|
|
|
body.items = payoutItems;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'You must have at least one item.', {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const itemsJson = validateJSON(this.getNodeParameter('itemsJson', i) as string);
|
|
|
|
body.items = itemsJson;
|
|
|
|
}
|
|
|
|
responseData = await payPalApiRequest.call(this, '/payments/payouts', 'POST', body);
|
2019-11-23 16:36:47 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'get') {
|
|
|
|
const payoutBatchId = this.getNodeParameter('payoutBatchId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2022-12-02 12:54:28 -08:00
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await payPalApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'items',
|
|
|
|
`/payments/payouts/${payoutBatchId}`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2019-11-23 16:36:47 -08:00
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.page_size = this.getNodeParameter('limit', i);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await payPalApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/payments/payouts/${payoutBatchId}`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.items;
|
2019-11-23 16:36:47 -08:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
} else if (resource === 'payoutItem') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const payoutItemId = this.getNodeParameter('payoutItemId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await payPalApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/payments/payouts-item/${payoutItemId}`,
|
|
|
|
'GET',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
if (operation === 'cancel') {
|
|
|
|
const payoutItemId = this.getNodeParameter('payoutItemId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await payPalApiRequest.call(
|
|
|
|
this,
|
|
|
|
`/payments/payouts-item/${payoutItemId}/cancel`,
|
|
|
|
'POST',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2019-12-01 07:15:02 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
2019-11-23 16:36:47 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
2019-11-23 16:36:47 -08:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2019-11-22 09:38:08 -08:00
|
|
|
}
|
|
|
|
}
|