publish payment data into bigquery
This commit is contained in:
parent
4972c44a95
commit
608809f16d
3 changed files with 39 additions and 0 deletions
|
@ -47,6 +47,13 @@ spec:
|
||||||
limits:
|
limits:
|
||||||
cpu: 200m
|
cpu: 200m
|
||||||
memory: 128Mi
|
memory: 128Mi
|
||||||
|
volumeMounts:
|
||||||
|
- name: team2-credentials
|
||||||
|
mountPath: /config
|
||||||
|
volumes:
|
||||||
|
- name: team2-credentials
|
||||||
|
configMap:
|
||||||
|
name: team2-credentials
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
|
|
|
@ -15,6 +15,17 @@
|
||||||
const cardValidator = require('simple-card-validator');
|
const cardValidator = require('simple-card-validator');
|
||||||
const uuid = require('uuid/v4');
|
const uuid = require('uuid/v4');
|
||||||
const pino = require('pino');
|
const pino = require('pino');
|
||||||
|
const { BigQuery } = require('@google-cloud/bigquery');
|
||||||
|
|
||||||
|
|
||||||
|
// gcp credentails can be found in /config/team2-bg.json
|
||||||
|
process.env['GOOGLE_APPLICATION_CREDENTIALS'] = '/config/team2-bg.json';
|
||||||
|
const datasetId = 'hipstershop_gcp_1210';
|
||||||
|
const tableId = 'payments';
|
||||||
|
|
||||||
|
// Create a client
|
||||||
|
const bigqueryClient = new BigQuery();
|
||||||
|
|
||||||
|
|
||||||
const logger = pino({
|
const logger = pino({
|
||||||
name: 'paymentservice-charge',
|
name: 'paymentservice-charge',
|
||||||
|
@ -76,6 +87,26 @@ module.exports = function charge (request) {
|
||||||
const { credit_card_expiration_year: year, credit_card_expiration_month: month } = creditCard;
|
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); }
|
if ((currentYear * 12 + currentMonth) > (year * 12 + month)) { throw new ExpiredCreditCard(cardNumber.replace('-', ''), month, year); }
|
||||||
|
|
||||||
|
// publish transaction to our bigquery database - if we have a bigquery client
|
||||||
|
|
||||||
|
if (bigqueryClient) {
|
||||||
|
const rows = [
|
||||||
|
{
|
||||||
|
"amount": amount.units + (amount.nanos / 1000000000),
|
||||||
|
"amount_currency_code": amount.currency_code,
|
||||||
|
"credit_card_number": cardNumber,
|
||||||
|
"credit_card_expiration_month": month,
|
||||||
|
"credit_card_expiration_year": year,
|
||||||
|
"created_at": new Date().getTime() / 1000
|
||||||
|
}]
|
||||||
|
|
||||||
|
|
||||||
|
bigqueryClient
|
||||||
|
.dataset(datasetId)
|
||||||
|
.table(tableId)
|
||||||
|
.insert(rows);
|
||||||
|
}
|
||||||
|
|
||||||
logger.info(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
|
logger.info(`Transaction processed: ${cardType} ending ${cardNumber.substr(-4)} \
|
||||||
Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`);
|
Amount: ${amount.currency_code}${amount.units}.${amount.nanos}`);
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
"@google-cloud/debug-agent": "^4.0.1",
|
"@google-cloud/debug-agent": "^4.0.1",
|
||||||
"@google-cloud/profiler": "^2.0.2",
|
"@google-cloud/profiler": "^2.0.2",
|
||||||
"@google-cloud/trace-agent": "4.0.1",
|
"@google-cloud/trace-agent": "4.0.1",
|
||||||
|
"@google-cloud/bigquery": "^4.5.0",
|
||||||
"@grpc/proto-loader": "^0.1.0",
|
"@grpc/proto-loader": "^0.1.0",
|
||||||
"grpc": "^1.22.2",
|
"grpc": "^1.22.2",
|
||||||
"pino": "^5.6.2",
|
"pino": "^5.6.2",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue