mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
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:
parent
9ef339e525
commit
c7a037e9fe
|
@ -28,7 +28,7 @@ export class PayPalApi implements ICredentialType {
|
||||||
default: 'live',
|
default: 'live',
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'Sanbox',
|
name: 'Sandbox',
|
||||||
value: 'sanbox',
|
value: 'sanbox',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -38,8 +38,8 @@ export async function payPalApiRequest(this: IHookFunctions | IExecuteFunctions
|
||||||
function getEnvironment(env: string): string {
|
function getEnvironment(env: string): string {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return {
|
return {
|
||||||
'sanbox': 'https://api.sandbox.paypal.com',
|
'sanbox': 'https://api-m.sandbox.paypal.com',
|
||||||
'live': 'https://api.paypal.com',
|
'live': 'https://api-m.paypal.com',
|
||||||
}[env];
|
}[env];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
|
import {
|
||||||
|
OptionsWithUri
|
||||||
|
} from 'request';
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
import {
|
import {
|
||||||
|
ICredentialsDecrypted,
|
||||||
|
ICredentialTestFunctions,
|
||||||
IDataObject,
|
IDataObject,
|
||||||
|
INodeCredentialTestResult,
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
|
@ -46,6 +52,7 @@ export class PayPal implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'payPalApi',
|
name: 'payPalApi',
|
||||||
required: true,
|
required: true,
|
||||||
|
testedBy: 'payPalApiTest',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
@ -168,5 +227,6 @@ export class PayPal implements INodeType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
return [this.helpers.returnJsonArray(returnData)];
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue