From 95bff8ffaa2beb5b086e5f0e212da7beee5f976a Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Wed, 27 Oct 2021 14:12:17 +0200 Subject: [PATCH] fix(core): Fix order modification with refund on shipping Fixes #1197 --- .../core/e2e/order-modification.e2e-spec.ts | 49 +++++++++++++++++++ .../order-calculator/order-calculator.ts | 1 + .../helpers/order-modifier/order-modifier.ts | 5 ++ 3 files changed, 55 insertions(+) diff --git a/packages/core/e2e/order-modification.e2e-spec.ts b/packages/core/e2e/order-modification.e2e-spec.ts index f08a43de67..e0e910b276 100644 --- a/packages/core/e2e/order-modification.e2e-spec.ts +++ b/packages/core/e2e/order-modification.e2e-spec.ts @@ -1507,6 +1507,55 @@ describe('Order modification', () => { }); }); + // https://github.com/vendure-ecommerce/vendure/issues/1197 + describe('refund on shipping when change made to shippingAddress', () => { + let order: OrderWithModificationsFragment; + beforeAll(async () => { + const createdOrder = await createOrderAndTransitionToModifyingState([ + { + productVariantId: 'T_1', + quantity: 1, + }, + ]); + const { modifyOrder } = await adminClient.query( + MODIFY_ORDER, + { + input: { + dryRun: false, + orderId: createdOrder.id, + updateShippingAddress: { + countryCode: 'GB', + }, + refund: { + paymentId: createdOrder.payments![0].id, + reason: 'discount', + }, + }, + }, + ); + orderGuard.assertSuccess(modifyOrder); + order = modifyOrder; + }); + + it('creates a Refund with the correct amount', async () => { + expect(order.payments?.[0].refunds[0].total).toBe(SHIPPING_OTHER - SHIPPING_GB); + }); + + it('allows transition to PaymentSettled', async () => { + const { transitionOrderToState } = await adminClient.query< + AdminTransition.Mutation, + AdminTransition.Variables + >(ADMIN_TRANSITION_TO_STATE, { + id: order.id, + state: 'PaymentSettled', + }); + + orderGuard.assertSuccess(transitionOrderToState); + + expect(transitionOrderToState.state).toBe('PaymentSettled'); + }); + }); + async function assertOrderIsUnchanged(order: OrderWithLinesFragment) { const { order: order2 } = await adminClient.query(GET_ORDER, { id: order.id, diff --git a/packages/core/src/service/helpers/order-calculator/order-calculator.ts b/packages/core/src/service/helpers/order-calculator/order-calculator.ts index 2a86c90e9f..7bb777f720 100644 --- a/packages/core/src/service/helpers/order-calculator/order-calculator.ts +++ b/packages/core/src/service/helpers/order-calculator/order-calculator.ts @@ -404,6 +404,7 @@ export class OrderCalculator { shippingLine.listPrice = cheapest.result.price; shippingLine.listPriceIncludesTax = cheapest.result.priceIncludesTax; shippingLine.shippingMethod = cheapest.method; + shippingLine.shippingMethodId = cheapest.method.id; shippingLine.taxLines = [ { description: 'shipping tax', diff --git a/packages/core/src/service/helpers/order-modifier/order-modifier.ts b/packages/core/src/service/helpers/order-modifier/order-modifier.ts index 1f356286f0..fa33a9785e 100644 --- a/packages/core/src/service/helpers/order-modifier/order-modifier.ts +++ b/packages/core/src/service/helpers/order-modifier/order-modifier.ts @@ -224,6 +224,7 @@ export class OrderModifier { surcharges: [], }); const initialTotalWithTax = order.totalWithTax; + const initialShippingWithTax = order.shippingWithTax; if (order.state !== 'Modifying') { return new OrderModificationStateError(); } @@ -408,6 +409,10 @@ export class OrderModifier { if (!input.refund) { return new RefundPaymentIdMissingError(); } + const shippingDelta = order.shippingWithTax - initialShippingWithTax; + if (shippingDelta < 0) { + refundInput.shipping = shippingDelta * -1; + } const existingPayments = await this.getOrderPayments(ctx, order.id); const payment = existingPayments.find(p => idsAreEqual(p.id, input.refund?.paymentId)); if (payment) {