diff --git a/packages/core/src/event-bus/events/refund-event.ts b/packages/core/src/event-bus/events/refund-event.ts new file mode 100644 index 0000000000..b02fac2af4 --- /dev/null +++ b/packages/core/src/event-bus/events/refund-event.ts @@ -0,0 +1,21 @@ +import { RequestContext } from '../../api/common/request-context'; +import { Order, Refund } from '../../entity'; +import { VendureEvent } from '../vendure-event'; + +/** + * @description + * This event is fired whenever a {@link Refund} is created + * + * @docsCategory events + * @docsPage Event Types + */ +export class RefundEvent extends VendureEvent { + constructor( + public ctx: RequestContext, + public order: Order, + public refund: Refund, + public type: 'created', + ) { + super(); + } +} diff --git a/packages/core/src/event-bus/index.ts b/packages/core/src/event-bus/index.ts index fe739c6107..a3063737e3 100644 --- a/packages/core/src/event-bus/index.ts +++ b/packages/core/src/event-bus/index.ts @@ -47,6 +47,7 @@ export * from './events/product-variant-channel-event'; export * from './events/product-variant-event'; export * from './events/product-variant-price-event'; export * from './events/promotion-event'; +export * from './events/refund-event'; export * from './events/refund-state-transition-event'; export * from './events/role-change-event'; export * from './events/role-event'; diff --git a/packages/core/src/service/services/order.service.ts b/packages/core/src/service/services/order.service.ts index 46dd621ce9..9386e1ae2c 100644 --- a/packages/core/src/service/services/order.service.ts +++ b/packages/core/src/service/services/order.service.ts @@ -98,6 +98,7 @@ import { CouponCodeEvent } from '../../event-bus/events/coupon-code-event'; import { OrderEvent } from '../../event-bus/events/order-event'; import { OrderLineEvent } from '../../event-bus/events/order-line-event'; import { OrderStateTransitionEvent } from '../../event-bus/events/order-state-transition-event'; +import { RefundEvent } from '../../event-bus/events/refund-event'; import { RefundStateTransitionEvent } from '../../event-bus/events/refund-state-transition-event'; import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service'; import { FulfillmentState } from '../helpers/fulfillment-state-machine/fulfillment-state'; @@ -1419,7 +1420,12 @@ export class OrderService { return new RefundOrderStateError({ orderState: order.state }); } - return await this.paymentService.createRefund(ctx, input, order, payment); + const createdRefund = await this.paymentService.createRefund(ctx, input, order, payment); + + if (createdRefund instanceof Refund) { + await this.eventBus.publish(new RefundEvent(ctx, order, createdRefund, 'created')); + } + return createdRefund; } /**