Skip to content

Commit

Permalink
Implement changes in notification integration
Browse files Browse the repository at this point in the history
ADCRSET29I-7
  • Loading branch information
Tamara committed Feb 19, 2025
1 parent fc6778e commit d9d7231
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
8 changes: 8 additions & 0 deletions extension/src/ctp.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ async function setUpClient(config) {
return ctpClient.execute(this.buildRequestOptions(uri.byKey(key).build()))
},

fetchByCustomField(uri, key) {
return ctpClient.execute(
this.buildRequestOptions(
uri.where(`custom(fields(merchantReference="${key}"))`).build(),
),
)
},

fetchBatches(uri, callback, opts = { accumulate: false }) {
return this.process(
this.buildRequestOptions(uri.build()),
Expand Down
21 changes: 17 additions & 4 deletions notification/src/handler/notification/notification.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function processNotification({
let retryCount = 0

const handleWebhook = async () => {
let payment = await getPaymentByMerchantReference(
let payment = await getPaymentByMerchantOrPSPReference(
merchantReference,
originalReference || pspReference,
ctpClient,
Expand Down Expand Up @@ -537,15 +537,28 @@ function getSetMethodInfoNameAction(paymentMethod) {
return null
}

async function getPaymentByMerchantReference(
async function getPaymentByMerchantOrPSPReference(
merchantReference,
pspReference,
ctpClient,
) {
try {
const keys = [merchantReference, pspReference]
const result = await ctpClient.fetchByKeys(ctpClient.builder.payments, keys)
return result.body?.results[0]
const resultByKey = await ctpClient.fetchByKeys(
ctpClient.builder.payments,
keys,
)
const payment = resultByKey.body?.results[0]
if (payment) {
return payment
}

const resultByCustomField = await ctpClient.fetchByCustomField(
ctpClient.builder.payments,
merchantReference,
)

return resultByCustomField.body?.results[0]
} catch (err) {
if (err.statusCode === 404) return null
const errMsg =
Expand Down
8 changes: 8 additions & 0 deletions notification/src/utils/ctp.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ async function setUpClient(config) {
return ctpClient.execute(this.buildRequestOptions(uri.byKey(key).build()))
},

fetchByCustomField(uri, key) {
return ctpClient.execute(
this.buildRequestOptions(
uri.where(`custom(fields(merchantReference="${key}"))`).build(),
),
)
},

fetchByKeys(uri, keys) {
const keyList = keys.map((key) => `"${key}"`)
const keyConditions = `key in (${keyList.join(',')})`
Expand Down

0 comments on commit d9d7231

Please sign in to comment.