forked from signalfx/microservices-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharge.js
308 lines (269 loc) · 8.92 KB
/
charge.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const cardValidator = require('simple-card-validator');
const uuid = require('uuid/v4');
const pino = require('pino');
const { context, getSpan, setSpan, SpanKind, trace } = require('@opentelemetry/api');
const logger = pino({
name: 'paymentservice',
messageKey: 'message',
changeLevelName: 'severity',
useLevelLabels: true,
timestamp: pino.stdTimeFunctions.unixTime,
mixin() {
const span = getSpan(context.active());
if (!span) {
return {};
}
const { traceId, spanId } = span.context();
return {
trace_id: traceId.slice(-16), // convert to 64-bit format
span_id: spanId,
'service.name': 'paymentservice'
};
},
});
// Demo Data
// Percentage of requests to fail: [0, 1]
const API_TOKEN_FAILURE_RATE = Number.parseFloat(
process.env['API_TOKEN_FAILURE_RATE'] || 0
);
// Percentage of requests to fail for deserialization: [0, 1]
const SERIALIZATION_FAILURE_RATE = Number.parseFloat(
process.env['SERIALIZATION_FAILURE_RATE'] || 0
);
// Success attributes
const SUCCESS_VERSION = 'v350.9';
const SUCCESS_TENANT_LEVEL = ['gold', 'silver', 'bronze'];
const SUCCESS_K8S_POD_UID = ['payment-service-449bc'];
const API_TOKEN_SUCCESS_TOKEN = 'prod-a8cf28f9-1a1a-4994-bafa-cd4b143c3291';
// Failure attributes
const FAILURE_VERSION = 'v350.10';
const FAILURE_K8S_POD_UID = [
'payment-service-3483d',
'payment-service-ab82e',
'payment-service-9aaf3',
'payment-service-6bbaf',
];
const API_TOKEN_FAILURE_TOKEN = 'test-20e26e90-356b-432e-a2c6-956fc03f5609';
// Artificial delay
const SUCCESS_PAYMENT_SERVICE_DURATION_MILLIS = Number.parseInt(
process.env['SUCCESS_PAYMENT_SERVICE_DURATION_MILLIS'] || 200
);
const ERROR_PAYMENT_SERVICE_DURATION_MILLIS = Number.parseInt(
process.env['ERROR_PAYMENT_SERVICE_DURATION_MILLIS'] || 1000
);
/** Return random element from given array */
function random(arr) {
const index = Math.floor(Math.random() * arr.length);
return arr[index];
}
/** Returns random integer between `from` and `to` */
function randomInt(from, to) {
return Math.floor((to - from) * Math.random() + from);
}
/**
* Verifies the credit card number and (pretend) charges the card.
*
* @param {*} request
* @return transaction_id - a random uuid v4.
*/
module.exports = async function charge(request) {
// Get handle to the active span and some random attributes for every request. In a failure
// case some of these might be overwritten to constrain the domain of the error.
const grpcActiveSpan = getSpan(context.active());
grpcActiveSpan.setAttributes({
version: SUCCESS_VERSION,
'tenant.level': random(SUCCESS_TENANT_LEVEL),
kubernetes_pod_uid: random(SUCCESS_K8S_POD_UID),
});
// Pick successful or failing token base on configured api token failure rate
const token =
Math.random() < API_TOKEN_FAILURE_RATE
? API_TOKEN_FAILURE_TOKEN
: API_TOKEN_SUCCESS_TOKEN;
logger.info({ token }, 'Charging through ButtercupPayments');
// Fail due to serialization error based on configured serialization failure rate
if (Math.random() < SERIALIZATION_FAILURE_RATE) {
await serializeRequestDataToProto().catch((err) => {
const kubernetes_pod_uid = random(FAILURE_K8S_POD_UID);
grpcActiveSpan.setAttributes({
version: FAILURE_VERSION,
kubernetes_pod_uid,
error: true,
});
logger
.child({
version: FAILURE_VERSION,
kubernetes_pod_uid,
})
.error(err);
throw new InternalError('Error');
});
}
// Represents an "external service call" to a payment processor. This is the "call" that will
// either succeed or fail due to an API token issue.
const tracer = trace.getTracer('charge')
const externalPaymentProcessorClientSpan = tracer.startSpan(
'buttercup.payments.api',
{
kind: SpanKind.CLIENT,
attributes: {
'peer.service': 'ButtercupPayments',
'http.url': 'https://api.buttercup-payments.com/charge',
'http.method': 'POST',
'http.status_code': '200',
},
},
setSpan(context.active(), grpcActiveSpan),
);
// Call into our "external service" for charge processing
return buttercupPaymentsApiCharge(request, token)
.then(({ transaction_id, cardType, cardNumber, amount }) => {
externalPaymentProcessorClientSpan.setAttributes({
'http.status_code': '200',
});
logger.info(
{
cardType,
version: SUCCESS_VERSION,
cardNumberEnding: cardNumber.substr(-4),
'amount.currency_code': amount.currency_code,
'amount.units': amount.units,
'amount.nanos': amount.nanos,
},
'Transaction processed'
);
return { transaction_id };
})
.catch((err) => {
externalPaymentProcessorClientSpan.setAttributes({
'http.status_code': err.code,
});
if (err.code === 401) {
// Mark error conditions on the root span; we force these for the demo
grpcActiveSpan.setAttributes({
version: FAILURE_VERSION,
kubernetes_pod_uid: random(FAILURE_K8S_POD_UID),
error: true,
});
// Log out error about token
logger.error(
{
token: API_TOKEN_FAILURE_TOKEN,
version: FAILURE_VERSION,
},
`Failed payment processing through ButtercupPayments: Invalid API Token (${API_TOKEN_FAILURE_TOKEN})`
);
} else {
logger.error(`Failed payment processing through ButtercupPayments`);
}
throw err;
})
.finally(() => {
externalPaymentProcessorClientSpan.end();
});
};
/**
* Attempt serialization, but it fails!
*/
async function serializeRequestDataToProto() {
return Promise.reject(new Error('Serialization failure'));
}
/**
* Process the charge request with the given API token. Acts as an external payment processer,
* so the call is asynchronous and has added artificial network delay.
*
* @param {*} request
* @param {*} token
*/
function buttercupPaymentsApiCharge(request, token) {
return new Promise((resolve, reject) => {
// Check for invalid token
if (token === API_TOKEN_FAILURE_TOKEN) {
const timeoutMillis = randomInt(0, ERROR_PAYMENT_SERVICE_DURATION_MILLIS);
setTimeout(() => {
reject(new InvalidRequestError());
}, timeoutMillis);
return;
}
// Process card
const { amount, credit_card: creditCard } = request;
const cardNumber = creditCard.credit_card_number;
const cardInfo = cardValidator(cardNumber);
const { card_type: cardType, valid } = cardInfo.getCardDetails();
if (!valid) {
throw new InvalidCreditCard();
}
// Only VISA and mastercard is accepted, other card types (AMEX, dinersclub) will
// throw UnacceptedCreditCard error.
if (!(cardType === 'visa' || cardType === 'mastercard')) {
throw new UnacceptedCreditCard(cardType);
}
// Also validate expiration is > today.
const currentMonth = new Date().getMonth() + 1;
const currentYear = new Date().getFullYear();
const {
credit_card_expiration_year: year,
credit_card_expiration_month: month,
} = creditCard;
if (currentYear * 12 + currentMonth > year * 12 + month) {
throw new ExpiredCreditCard(cardNumber.replace('-', ''), month, year);
}
const timeoutMillis = randomInt(0, SUCCESS_PAYMENT_SERVICE_DURATION_MILLIS);
setTimeout(() => {
resolve({ transaction_id: uuid(), cardType, cardNumber, amount });
}, timeoutMillis);
});
}
// Error Types
class InternalError extends Error {
constructor(message) {
super(message);
this.code = 500;
}
}
class InvalidRequestError extends Error {
constructor(token) {
super('Invalid request');
this.code = 401; // Authorization error
}
}
class CreditCardError extends Error {
constructor(message) {
super(message);
this.code = 400; // Invalid argument error
}
}
class InvalidCreditCard extends CreditCardError {
constructor(cardType) {
super(`Credit card info is invalid`);
}
}
class UnacceptedCreditCard extends CreditCardError {
constructor(cardType) {
super(
`Sorry, we cannot process ${cardType} credit cards. Only VISA or MasterCard is accepted.`
);
}
}
class ExpiredCreditCard extends CreditCardError {
constructor(number, month, year) {
super(
`Your credit card (ending ${number.substr(
-4
)}) expired on ${month}/${year}`
);
}
}