Skip to content

Commit

Permalink
Implement changes in extension integration
Browse files Browse the repository at this point in the history
ADCRSET29I-6
  • Loading branch information
Tamara committed Feb 18, 2025
1 parent ea14aa8 commit fc6778e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
12 changes: 12 additions & 0 deletions extension/resources/web-components-payment-type.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@
},
"inputHint": "MultiLine",
"required": false
},
{
"name": "merchantReference",
"label": {
"en": "merchantReference"
},
"type": {
"name": "String"
},
"inputHint": "SingleLine",
"required": false,
"searchable": true
}
]
}
1 change: 1 addition & 0 deletions extension/src/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
'submitAdditionalPaymentDetailsResponse',
CTP_INTERACTION_TYPE_MANUAL_CAPTURE: 'manualCapture',
CTP_INTERACTION_TYPE_REFUND: 'refund',
CTP_CUSTOM_FIELD_MERCHANT_REFERENCE: 'merchantReference',

PAYMENT_METHOD_TYPE_KLARNA_METHODS: [
'klarna',
Expand Down
10 changes: 10 additions & 0 deletions extension/src/paymentHandler/make-payment.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createSetMethodInfoNameAction,
createAddTransactionActionByResponse,
getPaymentKeyUpdateAction,
getMerchantReferenceCustomFieldUpdateAction,
} from './payment-utils.js'
import c from '../config/constants.js'
import { makePayment } from '../service/web-component-service.js'
Expand Down Expand Up @@ -55,9 +56,18 @@ async function execute(paymentObject) {
const updatePaymentAction = getPaymentKeyUpdateAction(
paymentObject.key,
request,
response,
)
if (updatePaymentAction) actions.push(updatePaymentAction)

const updateMerchantReferenceCustomFieldAction =
getMerchantReferenceCustomFieldUpdateAction(
request,
c.CTP_CUSTOM_FIELD_MERCHANT_REFERENCE,
)
if (updateMerchantReferenceCustomFieldAction)
actions.push(updateMerchantReferenceCustomFieldAction)

const addTransactionAction = createAddTransactionActionByResponse(
paymentObject.amountPlanned.centAmount,
paymentObject.amountPlanned.currencyCode,
Expand Down
32 changes: 26 additions & 6 deletions extension/src/paymentHandler/payment-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import c from '../config/constants.js'
import config from '../config/config.js'

const { getAdyenPaymentMethodsToNames } = config
const unsuccessfulResponseCodes = [400, 401, 403, 422, 500]

function getAuthorizationTransactionSuccess(paymentObject) {
return getTransactionWithTypesAndStates(
Expand Down Expand Up @@ -198,22 +199,40 @@ function getIdempotencyKey(transaction) {
return idempotencyKey
}

function getPaymentKeyUpdateAction(paymentKey, request) {
const requestBodyJson = JSON.parse(request.body)
let newReference = requestBodyJson.reference?.toString()

function getPaymentKeyUpdateAction(paymentKey, request, response) {
let paymentKeyUpdateAction
const pspReference = response.pspReference?.toString()
// ensure the key and new reference is different, otherwise the error with
// "code": "InvalidOperation", "message": "'key' has no changes." will return by commercetools API.
if (newReference && newReference !== paymentKey) {
if (
!unsuccessfulResponseCodes.includes(response.status) &&
pspReference &&
pspReference !== paymentKey
) {
paymentKeyUpdateAction = {
action: 'setKey',
key: newReference,
key: pspReference,
}
}

return paymentKeyUpdateAction
}

function getMerchantReferenceCustomFieldUpdateAction(request, name) {
let customFieldAction
const requestBodyJson = JSON.parse(request.body)
const reference = requestBodyJson.reference?.toString()
if (reference) {
customFieldAction = {
action: 'setCustomField',
name,
value: reference,
}
}

return customFieldAction
}

export {
getChargeTransactionInitial,
getChargeTransactionPending,
Expand All @@ -234,4 +253,5 @@ export {
isValidMetadata,
getIdempotencyKey,
getPaymentKeyUpdateAction,
getMerchantReferenceCustomFieldUpdateAction,
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createSetCustomFieldAction,
createAddTransactionActionByResponse,
getPaymentKeyUpdateAction,
getMerchantReferenceCustomFieldUpdateAction,
} from './payment-utils.js'
import c from '../config/constants.js'

Expand Down Expand Up @@ -57,8 +58,16 @@ async function execute(paymentObject) {
const updatePaymentAction = getPaymentKeyUpdateAction(
paymentObject.key,
request,
response,
)
if (updatePaymentAction) actions.push(updatePaymentAction)
const updateMerchantReferenceCustomFieldAction =
getMerchantReferenceCustomFieldUpdateAction(
request,
c.CTP_CUSTOM_FIELD_MERCHANT_REFERENCE,
)
if (updateMerchantReferenceCustomFieldAction)
actions.push(updateMerchantReferenceCustomFieldAction)
}
return {
actions,
Expand Down

0 comments on commit fc6778e

Please sign in to comment.