Skip to content

Commit 5e915c8

Browse files
Hash Transition (#131)
* All services switched to Authorization V2 * Signature verification function implemented * typo fix * Included missing tests & signature verification samples * project dependencies updated * request replaced with postman-request * Update IyzipayResource.js Signed-off-by: Bünyamin Yaşar <[email protected]> --------- Signed-off-by: Bünyamin Yaşar <[email protected]> Co-authored-by: Osman Keser <[email protected]>
1 parent a324a43 commit 5e915c8

7 files changed

+3710
-42
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ com_crashlytics_export_strings.xml
8080
crashlytics.properties
8181
crashlytics-build.properties
8282

83-
84-
83+
# Nyc
84+
.nyc_output

lib/IyzipayResource.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

3-
var request = require('request'),
4-
crypto = require('crypto'),
5-
utils = require('./utils');
3+
const request = require('postman-request');
4+
const utils = require('./utils');
65

76
function IyzipayResource() {
87
}
@@ -11,36 +10,42 @@ IyzipayResource.RANDOM_STRING_SIZE = 8;
1110
IyzipayResource.RANDOM_HEADER_NAME = 'x-iyzi-rnd';
1211
IyzipayResource.CLIENT_VERSION = 'x-iyzi-client-version';
1312
IyzipayResource.AUTHORIZATION = 'Authorization';
13+
IyzipayResource.AUTHORIZATION_FALLBACK_HEADER = 'Authorization_Fallback';
1414
IyzipayResource.IYZI_WS_HEADER_NAME = 'IYZWS';
1515
IyzipayResource.IYZI_WS_HEADER_NAME_V2 = 'IYZWSv2';
1616
IyzipayResource.SEPARATOR = ':';
1717

1818
IyzipayResource.prototype._getHttpHeaders = function (method) {
19-
var headers = {};
20-
var randomString = utils.generateRandomString(IyzipayResource.RANDOM_STRING_SIZE);
21-
var v2AuthUrlRegex = RegExp(/\/v2\//);
19+
const headers = {};
20+
const randomString = utils.generateRandomString(IyzipayResource.RANDOM_STRING_SIZE);
2221
headers[IyzipayResource.RANDOM_HEADER_NAME] = randomString;
2322
headers[IyzipayResource.CLIENT_VERSION] = "iyzipay-node-2.0.61";
24-
if (v2AuthUrlRegex.test(this._api[method].path)) {
25-
headers[IyzipayResource.AUTHORIZATION] = utils.generateAuthorizationHeaderV2(
26-
IyzipayResource.IYZI_WS_HEADER_NAME_V2,
23+
24+
const generateHttpHeadersV1 = () => {
25+
return utils.generateAuthorizationHeader(
26+
IyzipayResource.IYZI_WS_HEADER_NAME,
2727
this._config.apiKey,
2828
IyzipayResource.SEPARATOR,
2929
this._config.secretKey,
30-
this._api[method].generatedPath,
31-
this._getBody(method),
30+
this._getPkiString(method),
3231
randomString
3332
);
34-
} else {
35-
headers[IyzipayResource.AUTHORIZATION] = utils.generateAuthorizationHeader(
36-
IyzipayResource.IYZI_WS_HEADER_NAME,
33+
};
34+
35+
const generateHttpHeadersV2 = () => {
36+
return utils.generateAuthorizationHeaderV2(
37+
IyzipayResource.IYZI_WS_HEADER_NAME_V2,
3738
this._config.apiKey,
3839
IyzipayResource.SEPARATOR,
3940
this._config.secretKey,
40-
this._getPkiString(method),
41+
this._api[method].generatedPath,
42+
this._getBody(method),
4143
randomString
4244
);
43-
}
45+
};
46+
47+
headers[IyzipayResource.AUTHORIZATION] = generateHttpHeadersV2();
48+
headers[IyzipayResource.AUTHORIZATION_FALLBACK_HEADER] = generateHttpHeadersV1();
4449
return headers;
4550
};
4651

lib/resources/ThreedsInitialize.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var IyzipayResource = require('../IyzipayResource');
44

5-
function ThreedsInitializ() {
5+
function ThreedsInitialize() {
66
this._config = arguments[0];
77
this._api = {
88
create: {
@@ -13,6 +13,6 @@ function ThreedsInitializ() {
1313
};
1414
}
1515

16-
ThreedsInitializ.prototype = new IyzipayResource();
16+
ThreedsInitialize.prototype = new IyzipayResource();
1717

18-
module.exports = ThreedsInitializ;
18+
module.exports = ThreedsInitialize;

lib/utils.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var utils = module.exports = {
3232
'randomKey' + separator + randomString,
3333
'signature' + separator + signature
3434
];
35-
return new Buffer(authorizationParams.join('&')).toString('base64');
35+
return new Buffer.from(authorizationParams.join('&')).toString('base64');
3636
},
3737
generateRandomString: function (size) {
3838
return process.hrtime()[0] + Math.random().toString(size).slice(2);
@@ -60,5 +60,13 @@ var utils = module.exports = {
6060
}
6161
}
6262
return mergedObject;
63+
},
64+
calculateHmacSHA256Signature: (params, secretKey) => {
65+
const dataToCheck = params.join(':');
66+
67+
return crypto
68+
.createHmac('sha256', secretKey)
69+
.update(dataToCheck)
70+
.digest('hex');
6371
}
6472
};

0 commit comments

Comments
 (0)