-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
139 lines (121 loc) · 4.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const BigNumber = require('bignumber.js')
const request = require('request')
const aws4 = require('aws4')
const helpers = require('./lib/helpers')
module.exports = class {
constructor(cfg = {}) {
this.config = Object.assign({}, cfg);
}
createGiftCard(region, amount, currencyCode, cb) {
this._checkRegion(region)
const sequentialId = this._getNewId()
const requestBody = this._getCreateGiftCardRequestBody(sequentialId, amount, currencyCode)
const signedRequest = this._getSignedRequest(region, 'CreateGiftCard', requestBody)
const req = this._doRequest(signedRequest, cb)
return {req, sequentialId, requestBody, signedRequest}
}
createGiftCardAgain(region, amount, currencyCode, sequentialId, cb) {
this._checkRegion(region)
const requestBody = this._getCreateGiftCardRequestBody(sequentialId, amount, currencyCode)
const signedRequest = this._getSignedRequest(region, 'CreateGiftCard', requestBody)
const req = this._doRequest(signedRequest, cb)
return {req, sequentialId, requestBody, signedRequest}
}
cancelGiftCard(region, sequentialId, gcId, cb) {
this._checkRegion(region)
const requestBody = this._getCancelGiftCardRequestBody(sequentialId, gcId)
const signedRequest = this._getSignedRequest(region, 'CancelGiftCard', requestBody)
const req = this._doRequest(signedRequest, cb)
return {req, requestBody, signedRequest}
}
/**
* Throws when region is not NA, EU or FE
*/
_checkRegion(region) {
if (['NA', 'EU', 'FE'].indexOf(region) === -1 ) {
throw new Error(`First argument must be string NA, EU or FE`)
}
}
/**
* Builds the request body to be POSTed for creating a gift card
* @returns {Object}
*/
_getCreateGiftCardRequestBody(sequentialId, amount, currencyCode) {
return helpers.CreateGiftCardRequest(
this.config.partnerId,
sequentialId, amount, currencyCode
)
}
/**
* Builds the request body to be POSTed for cancelling a gift card
* @returns {Object}
*/
_getCancelGiftCardRequestBody(sequentialId, gcId) {
return helpers.CancelGiftCardRequest(
this.config.partnerId,
sequentialId, gcId
)
}
/**
* Builds an object with all the specifics for a new https request
* it includes headers with a version 4 signing authentication header
* @param {string} region - 'NA' for US/CA, 'EU' for IT/ES/DE/FR/UK, 'FE' for JP
* @param {string} action - 'CreateGiftCard' or 'CancelGiftCard'
* @param {Object} requestBody - generated by _getCreateGiftCardRequestBody or _getCancelGiftCardRequestBody
* @returns {Object}
*/
_getSignedRequest(region, action, requestBody) {
const credentials = this.config.credentials;
const endpoint = this.config.endpoint[region]
const opts = {
region: endpoint.region,
host: endpoint.host,
path: `/${action}`,
body: JSON.stringify(requestBody),
// defaults
service: 'AGCODService',
headers: Object.assign({
'accept': `application/json`,
'content-type': `application/json`,
'x-amz-target': `com.amazonaws.agcod.AGCODService.${action}`
}, this.config.extraHeaders),
method: 'POST',
securityOptions: 'SSL_OP_NO_SSLv3'
}
return aws4.sign(opts, credentials)
}
/**
* Makes the https based on the object created _getSignedRequest
* @param {Object} signedRequest - signed re'NA' for US/CA, 'EU' for IT/ES/DE/FR/UK, 'FE' for JP
* @param {Function} cb - Callback function
* @returns {Object} - whatever node-request returns
*/
_doRequest(signedRequest, cb) {
const params = {
method: 'POST',
url: `https://${signedRequest.host}${signedRequest.path}`,
headers: signedRequest.headers,
body: signedRequest.body
}
return request(params, (error, response, result) => {
if (error) return cb(error)
if (response.statusCode !== 200) {
const err = Object.assign({
request: params,
statusCode: response.statusCode
}, JSON.parse(result))
return cb(err)
}
return cb(null, JSON.parse(result))
})
}
/**
* Generates a unique sequential base-36 string based on processor time
* @returns string with length of 10
*/
_getNewId() {
let hrTime = process.hrtime()
let id = new BigNumber(hrTime[0]).times('1e9').plus(hrTime[1]).toString(36)
return id
}
}