feat(PayPal Node): Add auth test, fix typo and update API URL (#3084)

* Implements PayPal Auth API Test

* Deletes unit tests

* 🚨 Fixed lint issues

* Added changes from PR#2568

* Moved methods to above execute

Co-authored-by: paolo-rechia <paolo@e-bot7.com>
This commit is contained in:
Jonathan Bennetts 2022-04-08 10:49:07 +01:00 committed by GitHub
parent 9ef339e525
commit c7a037e9fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 3 deletions

View file

@ -28,7 +28,7 @@ export class PayPalApi implements ICredentialType {
default: 'live',
options: [
{
name: 'Sanbox',
name: 'Sandbox',
value: 'sanbox',
},
{

View file

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

View file

@ -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<INodeCredentialTestResult> {
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<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
@ -168,5 +227,6 @@ export class PayPal implements INodeType {
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}