Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[paymentservice] Fix transactionId in JSON response #230

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/paymentservice/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ const tracer = opentelemetry.trace.getTracer('paymentservice')
module.exports.charge = request => {
const span = tracer.startSpan('charge')

const { amount, creditCard } = request
const cardNumber = creditCard.creditCardNumber
const card = cardValidator(cardNumber)
const { creditCardNumber: number,
creditCardExpirationYear: year,
creditCardExpirationMonth: month
} = request.creditCard
const { units, nanos, currencyCode } = request.amount
const currentMonth = new Date().getMonth() + 1
const currentYear = new Date().getFullYear()
const lastFourDigits = number.substr(-4)
const transactionId = uuidv4()

const card = cardValidator(number)
const {card_type: cardType, valid } = card.getCardDetails()

span.setAttributes({
'app.payment.charge.cardType': cardType,
'app.payment.charge.valid': valid
Expand All @@ -40,18 +49,14 @@ module.exports.charge = request => {

if (!['visa', 'mastercard'].includes(cardType))
throw new Error(`Sorry, we cannot process ${cardType} credit cards. Only VISA or MasterCard is accepted.`)

const currentMonth = new Date().getMonth() + 1
const currentYear = new Date().getFullYear()
const { credit_card_expiration_year: year, credit_card_expiration_month: month } = creditCard
const lastFourDigits = cardNumber.substr(-4)

if ((currentYear * 12 + currentMonth) > (year * 12 + month))
throw new Error(`The credit card (ending ${lastFourDigits}) expired on ${month}/${year}.`)

span.setAttribute('app.payment.charged', true)
span.end()

logger.info(`Transaction processed: ${cardType} ending ${lastFourDigits} | Amount: ${amount.units}.${amount.nanos} ${amount.currencyCode}`)
logger.info(`Transaction ${transactionId}: ${cardType} ending ${lastFourDigits} | Amount: ${units}.${nanos} ${currencyCode}`)

return { transaction_id: uuidv4() }
return { transactionId }
}