Skip to content

Commit

Permalink
fix(core): Fix order state change from default payment process
Browse files Browse the repository at this point in the history
Relates to #2204
  • Loading branch information
michaelbromley committed Jun 12, 2023
1 parent dcae0d9 commit 0e5129e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 31 deletions.
53 changes: 53 additions & 0 deletions packages/core/e2e/order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
import gql from 'graphql-tag';
import path from 'path';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { ad } from 'vitest/dist/types-94cfe4b4';

import { initialData } from '../../../e2e-common/e2e-initial-data';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
import { OrderHistoryEntry } from '../src/entity/history-entry/order-history-entry.entity';

import {
failsToCancelPaymentMethod,
Expand All @@ -35,6 +37,8 @@ import {
CanceledOrderFragment,
ErrorCode,
FulfillmentFragment,
GetOrderDocument,
GetOrderHistoryDocument,
GlobalFlag,
HistoryEntryType,
LanguageCode,
Expand Down Expand Up @@ -81,6 +85,7 @@ import {
SET_SHIPPING_ADDRESS,
SET_SHIPPING_METHOD,
} from './graphql/shop-definitions';
import { ADD_MANUAL_PAYMENT } from './payment-process.e2e-spec';
import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
import { addPaymentToOrder, proceedToArrangingPayment, sortById } from './utils/test-order-utils';

Expand Down Expand Up @@ -2515,6 +2520,54 @@ describe('Orders resolver', () => {
.price,
).toBe(108720);
});

// https://github.com/vendure-ecommerce/vendure/issues/2204
it('creates correct history entries and results in correct state when manually adding payment to order', async () => {
await shopClient.asUserWithCredentials(customers[0].emailAddress, password);
const { addItemToOrder } = await shopClient.query<
CodegenShop.AddItemToOrderMutation,
CodegenShop.AddItemToOrderMutationVariables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 2,
});
await proceedToArrangingPayment(shopClient);
orderGuard.assertSuccess(addItemToOrder);

const { addManualPaymentToOrder } = await adminClient.query<
Codegen.AddManualPayment2Mutation,
Codegen.AddManualPayment2MutationVariables
>(ADD_MANUAL_PAYMENT, {
input: {
orderId: addItemToOrder.id,
metadata: {},
method: twoStagePaymentMethod.code,
transactionId: '12345',
},
});

orderGuard.assertSuccess(addManualPaymentToOrder);

const { order: orderWithHistory } = await adminClient.query(GetOrderHistoryDocument, {
id: addManualPaymentToOrder.id,
});

const stateTransitionHistory = orderWithHistory!.history.items
.filter(i => i.type === HistoryEntryType.ORDER_STATE_TRANSITION)
.map(i => i.data);

expect(stateTransitionHistory).toEqual([
{ from: 'Created', to: 'AddingItems' },
{ from: 'AddingItems', to: 'ArrangingPayment' },
{ from: 'ArrangingPayment', to: 'PaymentSettled' },
]);

const { order } = await adminClient.query(GetOrderDocument, {
id: addManualPaymentToOrder.id,
});

expect(order!.state).toBe('PaymentSettled');
});
});
});

Expand Down
27 changes: 1 addition & 26 deletions packages/core/e2e/payment-process.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,35 +369,10 @@ describe('Payment process', () => {
});

orderGuard.assertSuccess(addManualPaymentToOrder);
expect(addManualPaymentToOrder.state).toBe('ArrangingAdditionalPayment');
expect(addManualPaymentToOrder.state).toBe('PaymentSettled');
expect(addManualPaymentToOrder.payments![1].state).toBe('Settled');
expect(addManualPaymentToOrder.payments![1].amount).toBe(addManualPaymentToOrder.totalWithTax);
});

it('transitions Order to PaymentSettled', async () => {
const { transitionOrderToState } = await adminClient.query<
Codegen.AdminTransitionMutation,
Codegen.AdminTransitionMutationVariables
>(ADMIN_TRANSITION_TO_STATE, {
id: order2Id,
state: 'PaymentSettled',
});

orderGuard.assertSuccess(transitionOrderToState);
expect(transitionOrderToState.state).toBe('PaymentSettled');

const { order } = await adminClient.query<Codegen.GetOrderQuery, Codegen.GetOrderQueryVariables>(
GET_ORDER,
{
id: order2Id,
},
);
const settledPaymentAmount = order?.payments
?.filter(p => p.state === 'Settled')
.reduce((sum, p) => sum + p.amount, 0);

expect(settledPaymentAmount).toBe(order?.totalWithTax);
});
});
});

Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/config/payment/default-payment-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,17 @@ export const defaultPaymentProcess: PaymentProcess<PaymentState> = {
},
});

if (orderTotalIsCovered(order, 'Settled') && order.state !== 'PaymentSettled') {
if (
orderTotalIsCovered(order, 'Settled') &&
order.state !== 'PaymentSettled' &&
order.state !== 'ArrangingAdditionalPayment'
) {
await orderService.transitionToState(ctx, order.id, 'PaymentSettled');
}
if (orderTotalIsCovered(order, ['Authorized', 'Settled']) && order.state !== 'PaymentAuthorized') {
} else if (
orderTotalIsCovered(order, ['Authorized', 'Settled']) &&
order.state !== 'PaymentAuthorized' &&
order.state !== 'ArrangingAdditionalPayment'
) {
await orderService.transitionToState(ctx, order.id, 'PaymentAuthorized');
}
},
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,8 +1129,12 @@ export class OrderService {
}

const payment = await this.paymentService.createManualPayment(ctx, order, amount, input);
order.payments.push(payment);
await this.connection.getRepository(ctx, Order).save(order, { reload: false });
await this.connection
.getRepository(ctx, Order)
.createQueryBuilder('order')
.relation('payments')
.of(order)
.add(payment);
for (const modification of unsettledModifications) {
modification.payment = payment;
await this.connection.getRepository(ctx, OrderModification).save(modification);
Expand Down

0 comments on commit 0e5129e

Please sign in to comment.