Skip to content

Commit

Permalink
fix(payment): CHECKOUT-5295 Hide AmazonPay when multishipping
Browse files Browse the repository at this point in the history
  • Loading branch information
lpschz committed Nov 4, 2020
1 parent 16ccad3 commit cacccdc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
21 changes: 21 additions & 0 deletions src/app/payment/Payment.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getStoreConfig } from '../config/config.mock';
import { getCustomer } from '../customer/customers.mock';
import { createLocaleContext, LocaleContext, LocaleContextType } from '../locale';
import { getOrder } from '../order/orders.mock';
import { getConsignment } from '../shipping/consignment.mock';
import { Button } from '../ui/button';

import { getPaymentMethod } from './payment-methods.mock';
Expand Down Expand Up @@ -112,6 +113,26 @@ describe('Payment', () => {
}));
});

it('does not render amazon if multi-shipping', async () => {
paymentMethods.push({ ...getPaymentMethod(), id: 'amazonpay' });

jest.spyOn(checkoutState.data, 'getConsignments')
.mockReturnValue([
getConsignment(),
getConsignment(),
]);

const container = mount(<PaymentTest { ...defaultProps } />);

await new Promise(resolve => process.nextTick(resolve));
container.update();

paymentMethods.pop();

expect(container.find(PaymentForm).prop('methods'))
.toEqual(paymentMethods);
});

it('does not render bolt if showInCheckout is false', async () => {
const expectedPaymentMethods = paymentMethods.filter(method => method.id !== PaymentMethodId.Bolt);
paymentMethods[2] = { ...getPaymentMethod(), id: 'bolt', initializationData: { showInCheckout: false } };
Expand Down
22 changes: 17 additions & 5 deletions src/app/payment/Payment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export function mapToPaymentProps({
getCheckout,
getConfig,
getCustomer,
getConsignments,
getOrder,
getPaymentMethod,
getPaymentMethods,
Expand All @@ -466,6 +467,7 @@ export function mapToPaymentProps({
const checkout = getCheckout();
const config = getConfig();
const customer = getCustomer();
const consignments = getConsignments();
const { isComplete = false } = getOrder() || {};
const methods = getPaymentMethods() || EMPTY_ARRAY;

Expand All @@ -488,21 +490,31 @@ export function mapToPaymentProps({
let selectedPaymentMethod;
let filteredMethods;

// Prevent a payment method from being rendered
const prefilteredMethods = methods.filter((method: PaymentMethod) => {
filteredMethods = methods.filter((method: PaymentMethod) => {
if (method.id === PaymentMethodId.Bolt && method.initializationData) {
return !!method.initializationData.showInCheckout;
}

return true;
});

if (consignments && consignments.length > 1) {
const multiShippingIncompatibleMethodIds: string[] = [
PaymentMethodId.AmazonPay,
PaymentMethodId.Amazon,
];

filteredMethods = methods.filter((method: PaymentMethod) => {
return multiShippingIncompatibleMethodIds.indexOf(method.id) === -1;
});
}

if (selectedPayment) {
selectedPaymentMethod = getPaymentMethod(selectedPayment.providerId, selectedPayment.gatewayId);
filteredMethods = selectedPaymentMethod ? compact([selectedPaymentMethod]) : prefilteredMethods;
filteredMethods = selectedPaymentMethod ? compact([selectedPaymentMethod]) : filteredMethods;
} else {
selectedPaymentMethod = find(prefilteredMethods, { config: { hasDefaultStoredInstrument: true } });
filteredMethods = prefilteredMethods;
selectedPaymentMethod = find(filteredMethods, { config: { hasDefaultStoredInstrument: true } });
filteredMethods = filteredMethods;
}

return {
Expand Down

0 comments on commit cacccdc

Please sign in to comment.