diff --git a/packages/nodes-base/credentials/PayPalApi.credentials.ts b/packages/nodes-base/credentials/PayPalApi.credentials.ts index 897a66b627e98..9b2514efffe28 100644 --- a/packages/nodes-base/credentials/PayPalApi.credentials.ts +++ b/packages/nodes-base/credentials/PayPalApi.credentials.ts @@ -28,7 +28,7 @@ export class PayPalApi implements ICredentialType { default: 'live', options: [ { - name: 'Sanbox', + name: 'Sandbox', value: 'sanbox', }, { diff --git a/packages/nodes-base/nodes/PayPal/GenericFunctions.ts b/packages/nodes-base/nodes/PayPal/GenericFunctions.ts index a7859b335c8ec..1e8b84b4ebb3b 100644 --- a/packages/nodes-base/nodes/PayPal/GenericFunctions.ts +++ b/packages/nodes-base/nodes/PayPal/GenericFunctions.ts @@ -38,8 +38,8 @@ export async function payPalApiRequest(this: IHookFunctions | IExecuteFunctions function getEnvironment(env: string): string { // @ts-ignore return { - 'sanbox': 'https://api.sandbox.paypal.com', - 'live': 'https://api.paypal.com', + 'sanbox': 'https://api-m.sandbox.paypal.com', + 'live': 'https://api-m.paypal.com', }[env]; } diff --git a/packages/nodes-base/nodes/PayPal/PayPal.node.ts b/packages/nodes-base/nodes/PayPal/PayPal.node.ts index f72cffde6648b..36e4d6ee3499a 100644 --- a/packages/nodes-base/nodes/PayPal/PayPal.node.ts +++ b/packages/nodes-base/nodes/PayPal/PayPal.node.ts @@ -1,8 +1,14 @@ +import { + OptionsWithUri +} from 'request'; import { IExecuteFunctions, } from 'n8n-core'; import { + ICredentialsDecrypted, + ICredentialTestFunctions, IDataObject, + INodeCredentialTestResult, INodeExecutionData, INodeType, INodeTypeDescription, @@ -46,6 +52,7 @@ export class PayPal implements INodeType { { name: 'payPalApi', required: true, + testedBy: 'payPalApiTest', }, ], properties: [ @@ -75,6 +82,58 @@ export class PayPal implements INodeType { ], }; + methods = { + credentialTest: { + async payPalApiTest(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise { + const credentials = credential.data; + const clientId = credentials!.clientId; + const clientSecret = credentials!.secret; + const environment = credentials!.env; + + if (!clientId || !clientSecret || !environment) { + return { + status: 'Error', + message: `Connection details not valid: missing credentials`, + }; + } + + 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: { + 'Authorization': `Basic ${base64Key}`, + }, + method: 'POST', + uri: `${baseUrl}/v1/oauth2/token`, + form: { + grant_type: 'client_credentials', + }, + }; + + try { + await this.helpers.request!(options); + return { + status: 'OK', + message: 'Authentication successful!', + }; + } + catch (error) { + return { + status: 'Error', + message: `Connection details not valid: ${error.message}`, + }; + } + }, + }, + }; + async execute(this: IExecuteFunctions): Promise { const items = this.getInputData(); const returnData: IDataObject[] = []; @@ -168,5 +227,6 @@ export class PayPal implements INodeType { } } return [this.helpers.returnJsonArray(returnData)]; + } }