Skip to content

Commit

Permalink
fix: address issue where errors were not returned to the agent, resul…
Browse files Browse the repository at this point in the history
…ting in a stuck payment request app
  • Loading branch information
hendrickson-tyler committed Aug 27, 2024
1 parent e681e48 commit abb74d8
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions lib/lambda/c3-submit-payment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ import { stripTagsFromText } from '/opt/nodejs/formatting.js';
export async function handler(event) {
console.log(`EVENT: ${JSON.stringify(event)}`);

try {
// Get the contact attributes.
let contactAttributes = {};
contactAttributes = event.Details.ContactData.Attributes;
if (!contactAttributes) {
throw new Error('Contact attributes not found.');
}
if (!contactAttributes.PaymentToken) {
throw new Error('PaymentToken contact attribute not found.');
}
if (!contactAttributes.PaymentMethod) {
throw new Error('PaymentMethod contact attribute not found.');
}
const paymentMethod = contactAttributes.PaymentMethod;
// Get the contact attributes.
let contactAttributes = {};
contactAttributes = event.Details.ContactData.Attributes;
if (!contactAttributes) {
throw new Error('Contact attributes not found.');
}
if (!contactAttributes.PaymentToken) {
throw new Error('PaymentToken contact attribute not found.');
}
if (!contactAttributes.PaymentMethod) {
throw new Error('PaymentMethod contact attribute not found.');
}
const paymentMethod = contactAttributes.PaymentMethod;

let response;
try {
// Post the transaction.
let paymentInfo = {
name: contactAttributes.ContactName,
Expand All @@ -47,41 +48,46 @@ export async function handler(event) {
paymentInfo = { ...paymentInfo, ...bankAccountPaymentInfo };
}

const response = await postTransaction(
response = await postTransaction(
contactAttributes.PaymentRequestId,
paymentInfo,
);
console.log('Post transaction response:', response);
} catch (error) {
// Only return Zift errors to the contact flow.
if (error.message.includes('Zift Error:')) {
return {
Error: stripTagsFromText(error.message),
};
}
throw error;
}

// Get the last 4 digits of the card number or the bank account number.
const maskedNumber =
paymentMethod === 'Card'
? response.transaction.cardNumber
: response.transaction.bankAccountNumber;
const endingDigits = maskedNumber.slice(-4);
// Get the last 4 digits of the card number or the bank account number.
const maskedNumber =
paymentMethod === 'Card'
? response.transaction.cardNumber
: response.transaction.bankAccountNumber;
const endingDigits = maskedNumber.slice(-4);

// Update the list of all transaction IDs.
const previousTransactionIds = contactAttributes.TransactionIds
? JSON.parse(contactAttributes.TransactionIds)
: [];
const newTransactionIds = [
...previousTransactionIds,
response.transaction.id,
];
// Update the list of all transaction IDs.
const previousTransactionIds = contactAttributes.TransactionIds
? JSON.parse(contactAttributes.TransactionIds)
: [];
console.debug('previousTransactionIds:', previousTransactionIds);
const newTransactionIds = [
...previousTransactionIds,
response.transaction.id,
];
console.debug('newTransactionIds:', newTransactionIds);

return {
TransactionApproved: response.transactionApproved,
TransactionId: response.transaction.id,
TransactionIds: JSON.stringify(newTransactionIds),
TransactionMeta: response.transactionMeta,
PaymentMethodEndingDigits: endingDigits,
Error: 'NULL',
};
} catch (error) {
return {
Error: stripTagsFromText(error.message),
};
}
return {
TransactionApproved: response.transactionApproved,
TransactionId: response.transaction.id,
TransactionIds: JSON.stringify(newTransactionIds),
TransactionMeta: response.transactionMeta,
PaymentMethodEndingDigits: endingDigits,
Error: 'NULL',
};
}

/**
Expand Down Expand Up @@ -112,6 +118,7 @@ async function postTransaction(paymentRequestId, paymentInfo) {
);
if (!postTransactionResponse.ok) {
const response = await postTransactionResponse.json();
console.log('Post transaction response:', response);
const metaInfo = JSON.parse(response.transactionMeta.metaInfo);
throw new Error(
`Zift Error: ${metaInfo.responseMessage ?? 'An unknown error occurred.'}`,
Expand Down

0 comments on commit abb74d8

Please sign in to comment.