浏览代码

fix(core): Fix createRefund amount on cancelled OrderLines

Fixes #2302
Michael Bromley 2 年之前
父节点
当前提交
2b49edfba4

+ 1 - 0
packages/core/e2e/fixtures/test-payment-methods.ts

@@ -104,6 +104,7 @@ export const singleStageRefundablePaymentMethod = new PaymentMethodHandler({
         return {
             state: 'Settled',
             transactionId: 'abc123',
+            metadata: { amount },
         };
     },
 });

+ 44 - 0
packages/core/e2e/order.e2e-spec.ts

@@ -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', () => {

+ 1 - 1
packages/core/src/service/services/payment.service.ts

@@ -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;
             }
         }