Skip to content

Commit

Permalink
fix: treat received as successful result code
Browse files Browse the repository at this point in the history
  • Loading branch information
amihajlovski committed Nov 10, 2023
1 parent b3ad31e commit 868c21c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
10 changes: 9 additions & 1 deletion adyen/controllers/payments-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ import AdyenCheckoutConfig from './checkout-config'
import Logger from './logger'
import {createErrorResponse} from '../utils/createErrorResponse.mjs'

const errorMessages = {
PAYMENTS_DETAILS_NOT_SUCCESSFUL: 'payments details call not successful'
}

async function sendPaymentDetails(req, res) {
Logger.info('sendPaymentDetails', 'start')
const checkout = AdyenCheckoutConfig.getInstance()
try {
const {data} = req.body
const response = await checkout.instance.paymentsDetails(data)
Logger.info('sendPaymentDetails', `resultCode ${response.resultCode}`)
res.json(createCheckoutResponse(response))
const checkoutResponse = createCheckoutResponse(response)
if (checkoutResponse.isFinal && !checkoutResponse.isSuccessful) {
throw new Error(errorMessages.PAYMENTS_DETAILS_NOT_SUCCESSFUL)
}
res.json(checkoutResponse)
} catch (err) {
Logger.error('sendPaymentDetails', err.message)
res.status(err.statusCode || 500).json(
Expand Down
10 changes: 8 additions & 2 deletions adyen/controllers/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const errorMessages = {
AMOUNT_NOT_CORRECT: 'amount not correct',
INVALID_ORDER: 'order is invalid',
INVALID_PARAMS: 'invalid request params',
INVALID_BASKET: 'invalid basket'
INVALID_BASKET: 'invalid basket',
PAYMENT_NOT_SUCCESSFUL: 'payment not successful'
}

const validateRequestParams = (req) => {
Expand Down Expand Up @@ -150,7 +151,12 @@ async function sendPayments(req, res) {
// }
// })

res.json(createCheckoutResponse(response))
const checkoutResponse = createCheckoutResponse(response)
if (checkoutResponse.isFinal && !checkoutResponse.isSuccessful) {
throw new Error(errorMessages.PAYMENT_NOT_SUCCESSFUL)
}

res.json(checkoutResponse)
} catch (err) {
Logger.error('sendPayments', err.message)
res.status(err.statusCode || 500).json(
Expand Down
9 changes: 2 additions & 7 deletions adyen/utils/createCheckoutResponse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export function createCheckoutResponse(response) {
RESULT_CODES.REFUSED,
RESULT_CODES.ERROR,
RESULT_CODES.CANCELLED,
RESULT_CODES.RECEIVED,
].includes(response.resultCode)
) {
return {
isFinal: true,
isSuccessful:
response.resultCode === RESULT_CODES.AUTHORISED,
response.resultCode === RESULT_CODES.AUTHORISED || response.resultCode === RESULT_CODES.RECEIVED,
merchantReference: response.merchantReference,
};
}
Expand All @@ -32,12 +33,6 @@ export function createCheckoutResponse(response) {
};
}

if (response.resultCode === RESULT_CODES.RECEIVED) {
return {
isFinal: false,
};
}

return {
isFinal: true,
isSuccessful: false,
Expand Down

0 comments on commit 868c21c

Please sign in to comment.