Skip to content

Commit

Permalink
feat(PayPal Node): Add auth test, fix typo and update API URL (#3084)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Joffcom and paolo-rechia authored Apr 8, 2022
1 parent 9ef339e commit c7a037e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/nodes-base/credentials/PayPalApi.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PayPalApi implements ICredentialType {
default: 'live',
options: [
{
name: 'Sanbox',
name: 'Sandbox',
value: 'sanbox',
},
{
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/PayPal/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
60 changes: 60 additions & 0 deletions packages/nodes-base/nodes/PayPal/PayPal.node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {
OptionsWithUri
} from 'request';
import {
IExecuteFunctions,
} from 'n8n-core';
import {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
INodeCredentialTestResult,
INodeExecutionData,
INodeType,
INodeTypeDescription,
Expand Down Expand Up @@ -46,6 +52,7 @@ export class PayPal implements INodeType {
{
name: 'payPalApi',
required: true,
testedBy: 'payPalApiTest',
},
],
properties: [
Expand Down Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -168,5 +227,6 @@ export class PayPal implements INodeType {
}
}
return [this.helpers.returnJsonArray(returnData)];

}
}

0 comments on commit c7a037e

Please sign in to comment.