Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding additional payment doesn't transition order to PaymentSettled #3235

Open
martijnvdbrug opened this issue Nov 26, 2024 · 1 comment
Open
Labels
P2: important Critical issue which does not affect majority of users type: bug 🐛 Something isn't working

Comments

@martijnvdbrug
Copy link
Collaborator

Describe the bug
When adding payment to an order that is in ArrangingAdditionalPayment, the order doesn't transition to PaymentSettled, but stays in ArrangingAdditionalPayment.

To Reproduce
Steps to reproduce the behavior:

Copyable E2e script can be found below

  1. Create a settled order
  2. Transition to Modifying
  3. Increase quantity with 1
  4. Add payment to order with orderService.addPaymentToOrder(). (Normally this is done via a customization, but for the sake of testing).
  5. The order stays in ArrangingAdditionalPayment instead of transitioning to PaymentSettled

Expected behavior
I would expect the order to transition to PaymentSettled

Environment (please complete the following information):

  • @vendure/core version: 3.0.6
  • Nodejs version 18
  • Database (mysql/postgres etc): Verified on both SQLite and MySQL

Additional context

Show E2E script
import {
assertFound,
ChannelService,
DefaultLogger,
LogLevel,
mergeConfig,
Order,
OrderService,
RequestContext
} from '@vendure/core';

import {
createTestEnvironment,
registerInitializer,
SimpleGraphQLClient,
SqljsInitializer,
testConfig,
TestServer
} from '@vendure/testing';
import gql from 'graphql-tag';
import { beforeAll, expect, it } from 'vitest';
import { initialData } from '../../test/src/initial-data';
import { createSettledOrder } from '../../test/src/shop-utils';
import { testPaymentMethod } from '../../test/src/test-payment-method';

let server: TestServer;
let adminClient: SimpleGraphQLClient;
let shopClient: SimpleGraphQLClient;

beforeAll(async () => {
registerInitializer('sqljs', new SqljsInitializer('__data__'));
const config = mergeConfig(testConfig, {
  logger: new DefaultLogger({ level: LogLevel.Debug }),
  paymentOptions: {
    paymentMethodHandlers: [testPaymentMethod],
  },
  plugins: [],
});
({ server, adminClient, shopClient } = createTestEnvironment(config));
await server.init({
  initialData: {
    ...initialData,
    paymentMethods: [
      {
        name: testPaymentMethod.code,
        handler: { code: testPaymentMethod.code, arguments: [] },
      },
    ],
  },
  productsCsvPath: '../test/src/products-import.csv',
});
}, 60000);

let placedOrder: Order;

it('Create settled order', async () => {
// Local helper, but any placed order will do
const order = await createSettledOrder(shopClient, 1);
placedOrder = order as any;
expect(order.code).toBeDefined();
});

it('Transitions to modifying', async () => {
await adminClient.asSuperAdmin();
const transitionResult = await adminClient.query(
  gql`
    mutation TransitionOrderToModifying($id: ID!) {
      transitionOrderToState(id: $id, state: "Modifying") {
        ... on Order {
          id
          state
        }
        ... on OrderStateTransitionError {
          message
        }
      }
    }
  `,
  { id: 1 }
);
expect(transitionResult.transitionOrderToState.state).toBe('Modifying');
});

it('Increases quantity by 1', async () => {
const { modifyOrder } = await adminClient.query(
  gql`
    mutation ModifyOrder($input: ModifyOrderInput!) {
      modifyOrder(input: $input) {
        ... on Order {
          id
          state
          totalWithTax
          lines {
            id
            quantity
          }
          payments {
            amount
          }
        }
      }
    }
  `,
  {
    input: {
      orderId: placedOrder.id,
      dryRun: false,
      addItems: [
        {
          productVariantId: placedOrder.lines[0].productVariant.id,
          quantity: 1,
        },
      ],
    },
  }
);
const amountPaid = modifyOrder.payments.reduce(
  (total, payment) => total + payment.amount,
  0
);
const additionalAmountToBePaid = modifyOrder.totalWithTax - amountPaid;
console.log(
  `Additional amount to be paid: ${modifyOrder.totalWithTax} - ${amountPaid} = ${additionalAmountToBePaid}`
);
expect(modifyOrder.lines[0].quantity).toBe(placedOrder.lines[0].quantity + 1);
});

it('Transitions to ArrangingAdditionalPayment', async () => {
const transitionResult = await adminClient.query(
  gql`
    mutation TransitionOrderToModifying($id: ID!) {
      transitionOrderToState(id: $id, state: "ArrangingAdditionalPayment") {
        ... on Order {
          id
          state
        }
        ... on OrderStateTransitionError {
          message
        }
      }
    }
  `,
  { id: placedOrder.id }
);
expect(transitionResult.transitionOrderToState.state).toBe(
  'ArrangingAdditionalPayment'
);
});

it('Add payment as customer', async () => {
// This part is normally done in a customization, for example a Mollie payment that allows arranging additional payment
const channel = await server.app.get(ChannelService).getDefaultChannel();
const ctx = new RequestContext({
  apiType: 'shop',
  authorizedAsOwnerOnly: true,
  channel,
  isAuthorized: true,
});
(await server.app.get(OrderService).addPaymentToOrder(ctx, 1, {
  method: 'test-payment-method',
  metadata: {},
})) as Order;

// Refetch with payments
const order = await assertFound(
  server.app.get(OrderService).findOne(ctx, 1, ['payments'])
);

const totalWithTax = order.totalWithTax;
const amountPaid = order.payments.reduce(
  (total, payment) => total + payment.amount,
  0
);
expect(amountPaid).toBe(totalWithTax);
expect(order.state).toBe('PaymentSettled'); // Order will stay in 'ArrangingAdditionalPayment'
});
@martijnvdbrug martijnvdbrug added type: bug 🐛 Something isn't working P2: important Critical issue which does not affect majority of users labels Nov 26, 2024
@martijnvdbrug
Copy link
Collaborator Author

I've labeled this P2, but I might be a bit biased. Feel free to change priority. In our case it's important, because paid orders go unnoticed (they are never marked as placed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2: important Critical issue which does not affect majority of users type: bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant