-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
40 changed files
with
2,437 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 13. Add support fro advanced checkout flow | ||
|
||
Date: 2023-09-15 | ||
|
||
## Status | ||
|
||
[Accepted] | ||
|
||
## Context | ||
|
||
In Adyen web component version 5, new endpoint `/sessions` is introduced. It allows merchant to create payment session | ||
before shopper selects payment methods and drastically simplify the checkout flow. | ||
|
||
For details, please refer to [Adyen Documentation](https://docs.adyen.com/online-payments/web-components). | ||
|
||
On the other hand, API calls for web component v4 can be used for advanced checkout flow, for details, | ||
please refer to [Adyen Documentation](https://docs.adyen.com/online-payments/build-your-integration/additional-use-cases/advanced-flow-integration/?platform=Web&integration=Components&version=5.39.1). | ||
|
||
## Decision | ||
|
||
- We add all required API payment calls to be able to support advanced checkout flow. | ||
|
||
## Consequences | ||
- The commercetools extension can be utilized for advanced checkout flow implementation. | ||
- The migration to the Checkout web components V5 should be easier since advanced flow support keeps backward compatibility. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import _ from 'lodash' | ||
import ctpClientBuilder from '../ctp.js' | ||
import config from '../config/config.js' | ||
|
||
const ADYEN_PERCENTAGE_MINOR_UNIT = 10000 | ||
const KLARNA_DEFAULT_LINE_ITEM_NAME = 'item' | ||
const KLARNA_DEFAULT_SHIPPING_METHOD_DESCRIPTION = 'shipping' | ||
|
||
async function fetchMatchingCart(paymentObject, ctpProjectKey) { | ||
const ctpConfig = config.getCtpConfig(ctpProjectKey) | ||
const ctpClient = await ctpClientBuilder.get(ctpConfig) | ||
const { body } = await ctpClient.fetch( | ||
ctpClient.builder.carts | ||
.where(`paymentInfo(payments(id="${paymentObject.id}"))`) | ||
.expand('shippingInfo.shippingMethod') | ||
) | ||
return body.results[0] | ||
} | ||
|
||
function createLineItems(payment, cart) { | ||
const lineItems = [] | ||
const locales = _getLocales(cart, payment) | ||
|
||
cart.lineItems.forEach((item) => { | ||
if (item.taxRate) | ||
lineItems.push(_createAdyenLineItemFromLineItem(item, locales)) | ||
}) | ||
|
||
cart.customLineItems.forEach((item) => { | ||
if (item.taxRate) | ||
lineItems.push(_createAdyenLineItemFromCustomLineItem(item, locales)) | ||
}) | ||
|
||
const { shippingInfo } = cart | ||
if (shippingInfo && shippingInfo.taxRate) | ||
lineItems.push(_createShippingInfoAdyenLineItem(shippingInfo, locales)) | ||
|
||
return lineItems | ||
} | ||
|
||
function _getLocales(cart, payment) { | ||
const locales = [] | ||
let paymentLanguage = payment.custom && payment.custom.fields['languageCode'] | ||
if (!paymentLanguage) paymentLanguage = cart.locale | ||
if (paymentLanguage) locales.push(paymentLanguage) | ||
return locales | ||
} | ||
|
||
function _createAdyenLineItemFromLineItem(ctpLineItem, locales) { | ||
return { | ||
id: ctpLineItem.variant.sku, | ||
quantity: ctpLineItem.quantity, | ||
description: _localizeOrFallback( | ||
ctpLineItem.name, | ||
locales, | ||
KLARNA_DEFAULT_LINE_ITEM_NAME | ||
), | ||
amountIncludingTax: ctpLineItem.price.value.centAmount, | ||
taxPercentage: ctpLineItem.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT, | ||
} | ||
} | ||
|
||
function _createAdyenLineItemFromCustomLineItem(ctpLineItem, locales) { | ||
return { | ||
id: ctpLineItem.id, | ||
quantity: ctpLineItem.quantity, | ||
description: _localizeOrFallback( | ||
ctpLineItem.name, | ||
locales, | ||
KLARNA_DEFAULT_LINE_ITEM_NAME | ||
), | ||
amountIncludingTax: ctpLineItem.money.centAmount, | ||
taxPercentage: ctpLineItem.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT, | ||
} | ||
} | ||
|
||
function _createShippingInfoAdyenLineItem(shippingInfo, locales) { | ||
return { | ||
id: `${shippingInfo.shippingMethodName}`, | ||
quantity: 1, // always one shipment item so far | ||
description: | ||
_getShippingMethodDescription(shippingInfo, locales) || | ||
KLARNA_DEFAULT_SHIPPING_METHOD_DESCRIPTION, | ||
amountIncludingTax: shippingInfo.price.centAmount, | ||
taxPercentage: shippingInfo.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT, | ||
} | ||
} | ||
|
||
function _getShippingMethodDescription(shippingInfo, locales) { | ||
const shippingMethod = shippingInfo.shippingMethod?.obj | ||
if (shippingMethod) { | ||
return _localizeOrFallback( | ||
shippingMethod.localizedDescription, | ||
locales, | ||
shippingMethod.description | ||
) | ||
} | ||
return shippingInfo.shippingMethodName | ||
} | ||
|
||
function _localizeOrFallback(localizedString, locales, fallback) { | ||
let result | ||
if (_.size(localizedString) > 0) { | ||
const locale = locales?.find((l) => localizedString[l]) | ||
result = localizedString[locale] || Object.values(localizedString)[0] | ||
} else result = fallback | ||
return result | ||
} | ||
|
||
export default { fetchMatchingCart, createLineItems } |
28 changes: 28 additions & 0 deletions
28
extension/src/paymentHandler/make-lineitems-payment.handler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import makePaymentHandler from './make-payment.handler.js' | ||
import lineItemsUtils from './line-items-utils.js' | ||
|
||
async function execute(paymentObject) { | ||
const makePaymentRequestObj = JSON.parse( | ||
paymentObject.custom.fields.makePaymentRequest | ||
) | ||
const commercetoolsProjectKey = | ||
paymentObject.custom.fields.commercetoolsProjectKey | ||
if (!makePaymentRequestObj.lineItems) { | ||
const ctpCart = await lineItemsUtils.fetchMatchingCart( | ||
paymentObject, | ||
commercetoolsProjectKey | ||
) | ||
if (ctpCart) { | ||
makePaymentRequestObj.lineItems = lineItemsUtils.createLineItems( | ||
paymentObject, | ||
ctpCart | ||
) | ||
paymentObject.custom.fields.makePaymentRequest = JSON.stringify( | ||
makePaymentRequestObj | ||
) | ||
} | ||
} | ||
|
||
return makePaymentHandler.execute(paymentObject) | ||
} | ||
export default { execute } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
createAddInterfaceInteractionAction, | ||
createSetCustomFieldAction, | ||
createSetMethodInfoMethodAction, | ||
createSetMethodInfoNameAction, | ||
createAddTransactionActionByResponse, | ||
getPaymentKeyUpdateAction, | ||
} from './payment-utils.js' | ||
import c from '../config/constants.js' | ||
import { makePayment } from '../service/web-component-service.js' | ||
|
||
async function execute(paymentObject) { | ||
const makePaymentRequestObj = JSON.parse( | ||
paymentObject.custom.fields.makePaymentRequest | ||
) | ||
const adyenMerchantAccount = paymentObject.custom.fields.adyenMerchantAccount | ||
const commercetoolsProjectKey = | ||
paymentObject.custom.fields.commercetoolsProjectKey | ||
const { request, response } = await makePayment( | ||
adyenMerchantAccount, | ||
commercetoolsProjectKey, | ||
makePaymentRequestObj | ||
) | ||
const actions = [ | ||
createAddInterfaceInteractionAction({ | ||
request, | ||
response, | ||
type: c.CTP_INTERACTION_TYPE_MAKE_PAYMENT, | ||
}), | ||
createSetCustomFieldAction( | ||
c.CTP_CUSTOM_FIELD_MAKE_PAYMENT_RESPONSE, | ||
response | ||
), | ||
] | ||
|
||
const requestBodyJson = JSON.parse(request.body) | ||
const paymentMethod = requestBodyJson?.paymentMethod?.type | ||
if (paymentMethod) { | ||
actions.push(createSetMethodInfoMethodAction(paymentMethod)) | ||
const action = createSetMethodInfoNameAction(paymentMethod) | ||
if (action) actions.push(action) | ||
} | ||
|
||
const updatePaymentAction = getPaymentKeyUpdateAction( | ||
paymentObject.key, | ||
request, | ||
response | ||
) | ||
if (updatePaymentAction) actions.push(updatePaymentAction) | ||
|
||
const addTransactionAction = createAddTransactionActionByResponse( | ||
paymentObject.amountPlanned.centAmount, | ||
paymentObject.amountPlanned.currencyCode, | ||
response | ||
) | ||
|
||
if (addTransactionAction) actions.push(addTransactionAction) | ||
|
||
return { | ||
actions, | ||
} | ||
} | ||
|
||
export default { execute } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.