Skip to content

Commit 21e6bbd

Browse files
author
Shangamesh T
committed
Amazon Pay NodeJS 2.1.4
1 parent b261a0f commit 21e6bbd

7 files changed

Lines changed: 99 additions & 7 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#### Version 2.1.4 - May 2021
2+
* Enabled support for environment specific keys (i.e Public key & Private key). The changes are fully backwards-compatible, where merchants can also use non environment specific keys
3+
14
#### Version 2.1.3 - April 2021
25
* Removed deprecated library 'request' which is used to make HTTP/HTTPS calls
36
* Added library 'axios' to make HTTP/HTTPS calls

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ To associate the key with your account, follow the instructions here to
5555
};
5656
```
5757

58+
If you have created environment specific keys (i.e Public Key Starts with LIVE or SANDBOX) in Seller Central, then use those PublicKeyId & PrivateKey. In this case, there is no need to pass the Sandbox parameter to the ApiConfiguration.
59+
60+
``` js
61+
const fs = require('fs');
62+
const config = {
63+
'publicKeyId': 'PUBLIC_KEY_ID', // LIVE-XXXXX or SANDBOX-XXXXX
64+
'privateKey': fs.readFileSync('tst/private.pem'), // Path to RSA Private Key (or a string representation)
65+
'region': 'us', // Must be one of: 'us', 'eu', 'jp'
66+
};
67+
```
68+
5869
# Versioning
5970
The pay-api.amazon.com|eu|jp endpoint uses versioning to allow future updates. The major version of this SDK will stay aligned with the API version of the endpoint.
6071

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@amazonpay/amazon-pay-api-sdk-nodejs",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"description": "Amazon Pay Checkout V2 Integration",
55
"main": "src/client.js",
66
"directories": {},

src/clientHelper.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,24 @@ function prepareOptions(configArgs, options) {
6363
if (!(typeof options.payload === 'string' || options.payload instanceof String)) {
6464
options.payload = JSON.stringify(options.payload);
6565
}
66-
67-
if (configArgs['sandbox'] === true || configArgs['sandbox'] === 'true') {
68-
options.urlFragment = `sandbox/${constants.API_VERSION}/${options.urlFragment}`;
66+
// Condition to validate PublicKeyId is Environment specific
67+
if (isEnvSpecificPublicKeyId(configArgs['publicKeyId'])) {
68+
options.urlFragment = `${constants.API_VERSION}/${options.urlFragment}`;
6969
} else {
70-
options.urlFragment = `live/${constants.API_VERSION}/${options.urlFragment}`;
70+
if (configArgs['sandbox'] === true || configArgs['sandbox'] === 'true') {
71+
options.urlFragment = `sandbox/${constants.API_VERSION}/${options.urlFragment}`;
72+
} else {
73+
options.urlFragment = `live/${constants.API_VERSION}/${options.urlFragment}`;
74+
}
7175
}
7276
return options;
7377
}
7478

79+
// Method used to validate whether PublicKeyId starts with prefix LIVE or SANDBOX
80+
function isEnvSpecificPublicKeyId(publicKeyId) {
81+
return publicKeyId.toUpperCase().startsWith('LIVE') || publicKeyId.toUpperCase().startsWith('SANDBOX')
82+
}
83+
7584
function sign(privateKey, stringToSign) {
7685
const sign = crypto.createSign('RSA-SHA256').update(stringToSign);
7786
return sign.sign({

src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
module.exports = {
4-
SDK_VERSION: '2.1.3',
4+
SDK_VERSION: '2.1.4',
55
API_VERSION: 'v2',
66
RETRIES: 3,
77
API_ENDPOINTS: {

tst/unit/clientHelperTest.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
// Including Required Modules
4+
const helper = require('../../src/clientHelper');
5+
const assert = require('assert');
6+
7+
// Constants
8+
const expectedLiveURI = 'live/v2/serviceName';
9+
const expectedSandboxURI = 'sandbox/v2/serviceName';
10+
const expectedUnfiedURI = 'v2/serviceName';
11+
12+
// Test cases to validate Environment specific URI
13+
describe('Test Environment specific URI Test cases', () => {
14+
15+
// Test to validate URI for Live specific URI
16+
it('Testing Live specific URI', (done) => {
17+
const response = helper.prepareOptions(getPayConfig(false), {urlFragment: 'serviceName'});
18+
assert.strictEqual(response.urlFragment, expectedLiveURI);
19+
done();
20+
});
21+
22+
// Test to validate URI for Sandbox specific URI
23+
it('Testing Sandbox specific URI', (done) => {
24+
const response = helper.prepareOptions(getPayConfig(true), {urlFragment: 'serviceName'});
25+
assert.strictEqual(response.urlFragment, expectedSandboxURI);
26+
done();
27+
});
28+
29+
// Generic method used to create Pay Configuration
30+
function getPayConfig(sandboxFlag){
31+
let payConfig = {
32+
'publicKeyId': 'XXXXXXXXXXXXXXXXXXXXXXXX',
33+
'privateKey':'keys/private.pem',
34+
'sandbox': sandboxFlag,
35+
'region': 'us',
36+
};
37+
return payConfig;
38+
}
39+
});
40+
41+
// Test cases to validate Unified Endpoint specific URI
42+
describe('Test Environment specific URI Test cases', () => {
43+
44+
// Testing Unified endpoint URI by passing Live specific PublicKeyId
45+
it('Testing Unified endpoint URI for Live PublicKeyId', (done) => {
46+
const options = {urlFragment: 'serviceName'};
47+
const response = helper.prepareOptions(getPayConfig('LIVE-XXXXXXXXXXXXXXXXXXXXXXXX'), {urlFragment: 'serviceName'});
48+
assert.strictEqual(response.urlFragment, expectedUnfiedURI);
49+
done();
50+
});
51+
52+
// Testing Unified endpoint URI by passing Sandbox specific PublicKeyId
53+
it('Testing Unified endpoint URI for Sandbox PublicKeyId', (done) => {
54+
const options = {urlFragment: 'serviceName'};
55+
const response = helper.prepareOptions(getPayConfig('SANDBOX-XXXXXXXXXXXXXXXXXXXXXXXX'), {urlFragment: 'serviceName'});
56+
assert.strictEqual(response.urlFragment, expectedUnfiedURI);
57+
done();
58+
});
59+
60+
// Generic method used to create Pay Configuration
61+
function getPayConfig(publicKeyId){
62+
let payConfig = {
63+
'publicKeyId': publicKeyId,
64+
'privateKey':'keys/private.pem',
65+
'region': 'us',
66+
};
67+
return payConfig;
68+
}
69+
});

0 commit comments

Comments
 (0)