Skip to content

Commit

Permalink
fix(core): Fix createRefund amount on cancelled OrderLines
Browse files Browse the repository at this point in the history
Fixes #2302
  • Loading branch information
michaelbromley committed Jul 25, 2023
1 parent 5f898b4 commit 2b49edf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/e2e/fixtures/test-payment-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const singleStageRefundablePaymentMethod = new PaymentMethodHandler({
return {
state: 'Settled',
transactionId: 'abc123',
metadata: { amount },
};
},
});
Expand Down
44 changes: 44 additions & 0 deletions packages/core/e2e/order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,50 @@ describe('Orders resolver', () => {
expect(refund2.state).toBe('Settled');
expect(refund2.total).toBe(order.totalWithTax);
});

// https://github.com/vendure-ecommerce/vendure/issues/2302
it('passes correct amount to createRefund function after cancellation', async () => {
const orderResult = await createTestOrder(
adminClient,
shopClient,
customers[0].emailAddress,
password,
);
await proceedToArrangingPayment(shopClient);
const order = await addPaymentToOrder(shopClient, singleStageRefundablePaymentMethod);
orderGuard.assertSuccess(order);

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

const { cancelOrder } = await adminClient.query<
Codegen.CancelOrderMutation,
Codegen.CancelOrderMutationVariables
>(CANCEL_ORDER, {
input: {
orderId: order.id,
lines: order.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })),
reason: 'cancel reason 1',
},
});
orderGuard.assertSuccess(cancelOrder);

const { refundOrder } = await adminClient.query<
Codegen.RefundOrderMutation,
Codegen.RefundOrderMutationVariables
>(REFUND_ORDER, {
input: {
lines: order.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })),
shipping: order.shipping,
adjustment: 0,
reason: 'foo',
paymentId: order.payments![0].id,
},
});
refundGuard.assertSuccess(refundOrder);
expect(refundOrder.state).toBe('Settled');
expect(refundOrder.total).toBe(order.totalWithTax);
expect(refundOrder.metadata.amount).toBe(order.totalWithTax);
});
});

describe('payment cancellation', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/services/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class PaymentService {
.find({ where: { id: In(input.lines.map(l => l.orderLineId)) } });
for (const line of input.lines) {
const orderLine = orderLines.find(l => idsAreEqual(l.id, line.orderLineId));
if (orderLine && 0 < orderLine.quantity) {
if (orderLine && 0 < orderLine.orderPlacedQuantity) {
refundOrderLinesTotal += line.quantity * orderLine.proratedUnitPriceWithTax;
}
}
Expand Down

0 comments on commit 2b49edf

Please sign in to comment.