Skip to content

Commit

Permalink
feat(cart): support for multiple shipping
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeliziact committed Dec 27, 2024
1 parent 1497f84 commit c641312
Show file tree
Hide file tree
Showing 10 changed files with 829 additions and 200 deletions.
172 changes: 39 additions & 133 deletions processor/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@adyen/api-library": "22.1.0",
"@commercetools-backend/loggers": "22.37.0",
"@commercetools/connect-payments-sdk": "0.12.0",
"@commercetools/connect-payments-sdk": "0.13.0",
"@fastify/autoload": "6.0.3",
"@fastify/cors": "10.0.1",
"@fastify/formbody": "8.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Cart, Payment } from '@commercetools/connect-payments-sdk';
import { buildReturnUrl, mapCoCoCartItemsToAdyenLineItems, populateCartAddress } from './helper.converter';
import { CreatePaymentRequestDTO } from '../../dtos/adyen-payment.dto';
import { getFutureOrderNumberFromContext } from '../../libs/fastify/context/context';
import { paymentSDK } from '../../payment-sdk';

export class CreatePaymentConverter {
public convertRequest(opts: { data: CreatePaymentRequestDTO; cart: Cart; payment: Payment }): PaymentRequest {
Expand All @@ -26,7 +27,7 @@ export class CreatePaymentConverter {
billingAddress: populateCartAddress(opts.cart.billingAddress),
}),
...(opts.cart.shippingAddress && {
deliveryAddress: populateCartAddress(opts.cart.shippingAddress),
deliveryAddress: populateCartAddress(paymentSDK.ctCartService.getOneShippingAddress({ cart: opts.cart })),
}),
...(futureOrderNumber && { merchantOrderReference: futureOrderNumber }),
...this.populateAddionalPaymentMethodData(opts.data, opts.cart),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { CreateSessionRequestDTO } from '../../dtos/adyen-payment.dto';
import { Cart, Payment } from '@commercetools/connect-payments-sdk';
import { getFutureOrderNumberFromContext } from '../../libs/fastify/context/context';
import { paymentSDK } from '../../payment-sdk';

export class CreateSessionConverter {
public convertRequest(opts: {
Expand All @@ -35,7 +36,7 @@ export class CreateSessionConverter {
billingAddress: populateCartAddress(opts.cart.billingAddress),
}),
...(opts.cart.shippingAddress && {
deliveryAddress: populateCartAddress(opts.cart.shippingAddress),
deliveryAddress: populateCartAddress(paymentSDK.ctCartService.getOneShippingAddress({ cart: opts.cart })),
}),
shopperEmail: opts.cart.customerEmail,
...(futureOrderNumber && { merchantOrderReference: futureOrderNumber }),
Expand Down
47 changes: 32 additions & 15 deletions processor/src/services/converters/helper.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
LineItem as CoCoLineItem,
CustomLineItem,
Address as CartAddress,
ShippingInfo,
Order,
} from '@commercetools/connect-payments-sdk';
import {
getAllowedPaymentMethodsFromContext,
getCtSessionIdFromContext,
getProcessorUrlFromContext,
} from '../../libs/fastify/context/context';
import { NormalizedShipping } from '@commercetools/connect-payments-sdk/dist/commercetools/types/cart.type';
import { paymentSDK } from '../../payment-sdk';

export const mapCoCoLineItemToAdyenLineItem = (lineItem: CoCoLineItem): LineItem => {
return {
Expand All @@ -38,15 +39,15 @@ export const mapCoCoCustomLineItemToAdyenLineItem = (customLineItem: CustomLineI
};
};

export const mapCoCoShippingInfoToAdyenLineItem = (shippingInfo: ShippingInfo): LineItem => {
return {
description: 'Shipping',
export const mapCoCoShippingInfoToAdyenLineItem = (shippingInfo: NormalizedShipping[]): LineItem[] => {
return shippingInfo.map((shipping) => ({
description: `Shipping - ${shipping.shippingInfo.shippingMethodName}`,
quantity: 1,
amountExcludingTax: shippingInfo.taxedPrice?.totalNet.centAmount || 0,
amountIncludingTax: shippingInfo.taxedPrice?.totalGross.centAmount || 0,
taxAmount: shippingInfo.taxedPrice?.totalTax?.centAmount || 0,
taxPercentage: convertTaxPercentageToCentAmount(shippingInfo.taxRate?.amount),
};
amountExcludingTax: shipping.shippingInfo.taxedPrice?.totalNet.centAmount || 0,
amountIncludingTax: shipping.shippingInfo.taxedPrice?.totalGross.centAmount || 0,
taxAmount: shipping.shippingInfo.taxedPrice?.totalTax?.centAmount || 0,
taxPercentage: convertTaxPercentageToCentAmount(shipping.shippingInfo.taxRate?.amount),
}));
};

export const mapCoCoDiscountOnTotalPriceToAdyenLineItem = (
Expand Down Expand Up @@ -74,7 +75,16 @@ export const mapCoCoDiscountOnTotalPriceToAdyenLineItem = (
* @returns List of lineitems to be send to Adyen
*/
export const mapCoCoOrderItemsToAdyenLineItems = (
order: Pick<Order, 'lineItems' | 'customLineItems' | 'shippingInfo' | 'discountOnTotalPrice'>,
order: Pick<
Order,
| 'lineItems'
| 'customLineItems'
| 'shippingMode'
| 'shippingAddress'
| 'shippingInfo'
| 'shipping'
| 'discountOnTotalPrice'
>,
): LineItem[] => {
// CoCo model between these attributes is shared between a Cart and Order hence we can re-use the existing mapping logic.
return mapCoCoCartItemsToAdyenLineItems(order);
Expand All @@ -89,7 +99,16 @@ export const mapCoCoOrderItemsToAdyenLineItems = (
* @returns List of lineitems to be send to Adyen
*/
export const mapCoCoCartItemsToAdyenLineItems = (
cart: Pick<Cart, 'lineItems' | 'customLineItems' | 'shippingInfo' | 'discountOnTotalPrice'>,
cart: Pick<
Cart,
| 'lineItems'
| 'customLineItems'
| 'shippingMode'
| 'shippingAddress'
| 'shippingInfo'
| 'shipping'
| 'discountOnTotalPrice'
>,
): LineItem[] => {
const aydenLineItems: LineItem[] = [];

Expand All @@ -99,9 +118,7 @@ export const mapCoCoCartItemsToAdyenLineItems = (
aydenLineItems.push(mapCoCoCustomLineItemToAdyenLineItem(customLineItem)),
);

if (cart.shippingInfo) {
aydenLineItems.push(mapCoCoShippingInfoToAdyenLineItem(cart.shippingInfo));
}
aydenLineItems.push(...mapCoCoShippingInfoToAdyenLineItem(paymentSDK.ctCartService.getNormalizedShipping({ cart })));

if (cart.discountOnTotalPrice) {
aydenLineItems.push(
Expand All @@ -112,7 +129,7 @@ export const mapCoCoCartItemsToAdyenLineItems = (
return aydenLineItems;
};

export const populateCartAddress = (address: CartAddress): Address => {
export const populateCartAddress = (address?: CartAddress): Address => {
return {
country: address?.country || '',
city: address?.city || '',
Expand Down
Loading

0 comments on commit c641312

Please sign in to comment.