Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N8N-3272 paypal auth test #3084

Merged
merged 7 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)];

}
}