n8n-nodes-iyzico-unofficial
Version:
N8N community node for Iyzico payment system integration
151 lines (150 loc) • 5.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IyzicoApi = void 0;
class IyzicoApi {
name = 'iyzicoApi';
displayName = 'Iyzico API';
documentationUrl = 'https://docs.iyzico.com/';
properties = [
{
displayName: 'Environment',
name: 'environment',
type: 'options',
options: [
{
name: 'Sandbox',
value: 'sandbox',
},
{
name: 'Live',
value: 'live',
},
],
default: 'live',
description: 'The environment to use',
},
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
typeOptions: { password: true },
default: '',
required: true,
description: 'Your Iyzico API Key',
},
{
displayName: 'Secret Key',
name: 'secretKey',
type: 'string',
typeOptions: { password: true },
default: '',
required: true,
description: 'Your Iyzico Secret Key',
},
];
authenticate = {
type: 'generic',
properties: {
headers: {
Authorization: '={{$self.iyzicoAuth($credentials)}}',
'x-iyzi-rnd': '={{$self.iyzicoRandomKey()}}',
},
},
};
methods = {
credentialTest: {
async iyzicoApiTest(credential) {
const crypto = require('crypto');
const apiKey = credential.apiKey;
const secretKey = credential.secretKey;
const environment = credential.environment;
const baseURL = environment === 'sandbox'
? 'https://sandbox-api.iyzipay.com'
: 'https://api.iyzipay.com';
const endpoint = '/v2/iyzilink/products';
const body = {
page: 1,
count: 1,
locale: 'tr',
conversationId: "123456"
};
const requestBody = JSON.stringify(body);
const queryString = new URLSearchParams(body).toString();
const randomKey = new Date().getTime() + Math.floor(Math.random() * 1000000).toString();
const payload = randomKey + endpoint + requestBody;
const encryptedData = crypto.createHmac('sha256', secretKey)
.update(payload)
.digest('hex');
const authorizationString = `apiKey:${apiKey}&randomKey:${randomKey}&signature:${encryptedData}`;
const base64EncodedAuthorization = Buffer.from(authorizationString).toString('base64');
const authorization = `IYZWSv2 ${base64EncodedAuthorization}`;
const options = {
method: 'GET',
url: `${baseURL}${endpoint}${queryString ? `?${queryString}` : ''}`,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': authorization,
'x-iyzi-rnd': randomKey,
},
json: true,
};
try {
const response = await this.helpers.httpRequest(options);
if (response.status === 'success') {
return true;
}
else {
return false;
}
}
catch (error) {
return false;
}
},
},
loadOptions: {
iyzicoAuth(credential) {
const crypto = require('crypto');
const apiKey = credential.apiKey;
const secretKey = credential.secretKey;
const randomKey = new Date().getTime() + '123456789';
const uriPath = '/v2/iyzilink/products';
const requestBody = '';
const payload = randomKey + uriPath + requestBody;
const encryptedData = crypto.createHmac('sha256', secretKey)
.update(payload)
.digest('hex');
const authorizationString = `apiKey:${apiKey}&randomKey:${randomKey}&signature:${encryptedData}`;
const base64EncodedAuthorization = Buffer.from(authorizationString).toString('base64');
return `IYZWSv2 ${base64EncodedAuthorization}`;
},
iyzicoRandomKey() {
return new Date().getTime() + '123456789';
},
},
};
test = {
request: {
baseURL: '={{$credentials.environment === "sandbox" ? "https://sandbox-api.iyzipay.com" : "https://api.iyzipay.com"}}',
url: '/v2/iyzilink/products',
method: 'POST',
body: {
page: 1,
count: 1,
locale: 'tr'
},
},
rules: [
{
type: 'responseSuccessBody',
properties: {
key: 'status',
value: 'success',
message: ''
},
},
],
};
}
exports.IyzicoApi = IyzicoApi;