Adyen commercetools API v2 -> v3
In update, we decided to:
- cancel partial payment process if cart's totals changed during the process, as Adyen doesn't allow to update total amount of payment after initializing partial payment.
If you want to migrate, firstly make sure version of packages, extension, and notification module matches specified on the Compatibility page.
Then let's adjust codebase.
Extend order custom type in commercetools
We added adyenOrderTotalAmountValue, and adyenOrderTotalAmountCurrency custom fields to the commercetools cart/order type.
Add them to your existing type using the following field definitions:
{
"name": "adyenOrderTotalAmountValue",
"label": {
"en": "adyenOrderTotalAmountValue"
},
"type": {
"name": "Number"
},
"inputHint": "SingleLine",
"required": false
},
{
"name": "adyenOrderTotalAmountCurrency",
"label": {
"en": "adyenOrderTotalAmountCurrency"
},
"type": {
"name": "String"
},
"inputHint": "SingleLine",
"required": false
}Setup commercetools API Extension for cart
Register commercetools API Extension that triggers on cart's update. It's URL should match the following pattern https://your-shop-url.local/api/adyen/webhookOnCartUpdate.
The endpoint is public, so commercetools must authenticate every call to it. Choose a username and a long random password, and get the Base64 form of username:password:
printf '%s' 'my-username:my-long-random-password' | base64Register the API Extension with that value in the Authorization header:
{
"destination" : {
"type" : "HTTP",
"url" : "https://your-shop-url.local/api/adyen/webhookOnCartUpdate",
"authentication" : {
"type" : "AuthorizationHeader",
"headerValue" : "Basic <ADYEN_CART_EXTENSION_CREDENTIALS>"
}
},
"triggers" : [ {
"resourceTypeId" : "cart",
"actions" : [ "Update" ]
} ],
"key" : "ct-adyen-partial-payments-cart-ext"
}For an API Extension that already exists, set the same destination with the Change Destination update action.
The integration doesn't read this header, so check it in a middleware extension that rejects every webhookOnCartUpdate call without the credentials:
import { createHash, timingSafeEqual } from 'node:crypto';
import { createHttpError, HttpStatusCode } from '@alokai/connect/middleware';
import type { ApiClientExtension } from '@alokai/connect/middleware';
export const cartExtensionAuth: ApiClientExtension = {
name: 'adyen-cart-extension-auth',
hooks(req) {
return {
beforeCall({ args, callName }) {
if (callName === 'webhookOnCartUpdate' && !hasCartExtensionCredentials(req.headers.authorization)) {
throw createHttpError({ statusCode: HttpStatusCode.UNAUTHORIZED, message: 'Unauthorized' });
}
return args;
},
};
},
};
function hasCartExtensionCredentials(authorization: string | undefined) {
const credentials = process.env.ADYEN_CART_EXTENSION_CREDENTIALS;
if (!credentials || !authorization) {
return false;
}
return timingSafeEqual(digest(authorization), digest(`Basic ${credentials}`));
}
function digest(value: string) {
return createHash('sha256').update(value).digest();
}Register the extension in the Adyen integration config, and set the ADYEN_CART_EXTENSION_CREDENTIALS environment variable of the middleware to the Base64 value:
import { cartExtensionAuth } from './extensions/cart-extension-auth';
export const config = {
location: '@vsf-enterprise/adyen-commercetools-api/server',
configuration: {
// ...
},
extensions: (extensions) => [...extensions, cartExtensionAuth],
} satisfies Integration<MiddlewareConfig>;Add the header in commercetools before you deploy the middleware extension. Without the header, the middleware answers 401 and commercetools rejects the cart update.
Adjust frontend part of integration to match current boilerplate
Base on the example presented at quickstart guide.