Skip to content

Commit

Permalink
Amazon Pay NodeJS 2.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Shangamesh T committed Jun 1, 2021
1 parent b261a0f commit 21e6bbd
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### Version 2.1.4 - May 2021
* 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

#### Version 2.1.3 - April 2021
* Removed deprecated library 'request' which is used to make HTTP/HTTPS calls
* Added library 'axios' to make HTTP/HTTPS calls
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ To associate the key with your account, follow the instructions here to
};
```

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.

``` js
const fs = require('fs');
const config = {
'publicKeyId': 'PUBLIC_KEY_ID', // LIVE-XXXXX or SANDBOX-XXXXX
'privateKey': fs.readFileSync('tst/private.pem'), // Path to RSA Private Key (or a string representation)
'region': 'us', // Must be one of: 'us', 'eu', 'jp'
};
```

# Versioning
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.

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@amazonpay/amazon-pay-api-sdk-nodejs",
"version": "2.1.3",
"version": "2.1.4",
"description": "Amazon Pay Checkout V2 Integration",
"main": "src/client.js",
"directories": {},
Expand Down
17 changes: 13 additions & 4 deletions src/clientHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,24 @@ function prepareOptions(configArgs, options) {
if (!(typeof options.payload === 'string' || options.payload instanceof String)) {
options.payload = JSON.stringify(options.payload);
}

if (configArgs['sandbox'] === true || configArgs['sandbox'] === 'true') {
options.urlFragment = `sandbox/${constants.API_VERSION}/${options.urlFragment}`;
// Condition to validate PublicKeyId is Environment specific
if (isEnvSpecificPublicKeyId(configArgs['publicKeyId'])) {
options.urlFragment = `${constants.API_VERSION}/${options.urlFragment}`;
} else {
options.urlFragment = `live/${constants.API_VERSION}/${options.urlFragment}`;
if (configArgs['sandbox'] === true || configArgs['sandbox'] === 'true') {
options.urlFragment = `sandbox/${constants.API_VERSION}/${options.urlFragment}`;
} else {
options.urlFragment = `live/${constants.API_VERSION}/${options.urlFragment}`;
}
}
return options;
}

// Method used to validate whether PublicKeyId starts with prefix LIVE or SANDBOX
function isEnvSpecificPublicKeyId(publicKeyId) {
return publicKeyId.toUpperCase().startsWith('LIVE') || publicKeyId.toUpperCase().startsWith('SANDBOX')
}

function sign(privateKey, stringToSign) {
const sign = crypto.createSign('RSA-SHA256').update(stringToSign);
return sign.sign({
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = {
SDK_VERSION: '2.1.3',
SDK_VERSION: '2.1.4',
API_VERSION: 'v2',
RETRIES: 3,
API_ENDPOINTS: {
Expand Down
69 changes: 69 additions & 0 deletions tst/unit/clientHelperTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

// Including Required Modules
const helper = require('../../src/clientHelper');
const assert = require('assert');

// Constants
const expectedLiveURI = 'live/v2/serviceName';
const expectedSandboxURI = 'sandbox/v2/serviceName';
const expectedUnfiedURI = 'v2/serviceName';

// Test cases to validate Environment specific URI
describe('Test Environment specific URI Test cases', () => {

// Test to validate URI for Live specific URI
it('Testing Live specific URI', (done) => {
const response = helper.prepareOptions(getPayConfig(false), {urlFragment: 'serviceName'});
assert.strictEqual(response.urlFragment, expectedLiveURI);
done();
});

// Test to validate URI for Sandbox specific URI
it('Testing Sandbox specific URI', (done) => {
const response = helper.prepareOptions(getPayConfig(true), {urlFragment: 'serviceName'});
assert.strictEqual(response.urlFragment, expectedSandboxURI);
done();
});

// Generic method used to create Pay Configuration
function getPayConfig(sandboxFlag){
let payConfig = {
'publicKeyId': 'XXXXXXXXXXXXXXXXXXXXXXXX',
'privateKey':'keys/private.pem',
'sandbox': sandboxFlag,
'region': 'us',
};
return payConfig;
}
});

// Test cases to validate Unified Endpoint specific URI
describe('Test Environment specific URI Test cases', () => {

// Testing Unified endpoint URI by passing Live specific PublicKeyId
it('Testing Unified endpoint URI for Live PublicKeyId', (done) => {
const options = {urlFragment: 'serviceName'};
const response = helper.prepareOptions(getPayConfig('LIVE-XXXXXXXXXXXXXXXXXXXXXXXX'), {urlFragment: 'serviceName'});
assert.strictEqual(response.urlFragment, expectedUnfiedURI);
done();
});

// Testing Unified endpoint URI by passing Sandbox specific PublicKeyId
it('Testing Unified endpoint URI for Sandbox PublicKeyId', (done) => {
const options = {urlFragment: 'serviceName'};
const response = helper.prepareOptions(getPayConfig('SANDBOX-XXXXXXXXXXXXXXXXXXXXXXXX'), {urlFragment: 'serviceName'});
assert.strictEqual(response.urlFragment, expectedUnfiedURI);
done();
});

// Generic method used to create Pay Configuration
function getPayConfig(publicKeyId){
let payConfig = {
'publicKeyId': publicKeyId,
'privateKey':'keys/private.pem',
'region': 'us',
};
return payConfig;
}
});

0 comments on commit 21e6bbd

Please sign in to comment.