---
displayed_sidebar: servicesSidebar
---

import ParameterTypes from "@site/src/components/ParameterTypes"

# SwapService

Handles swaps

## constructor

### Parameters

<ParameterTypes parameters={[{"name":"__namedParameters","type":"`InjectedProps`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

___

## Properties

<ParameterTypes parameters={[{"name":"__container__","type":"`any`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"cartService_","type":"[CartService](services.CartService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"customShippingOptionService_","type":"[CustomShippingOptionService](services.CustomShippingOptionService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"eventBus_","type":"[EventBusService](services.EventBusService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillmentService_","type":"[FulfillmentService](services.FulfillmentService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"lineItemAdjustmentService_","type":"[LineItemAdjustmentService](services.LineItemAdjustmentService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"lineItemService_","type":"[LineItemService](services.LineItemService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"manager_","type":"`EntityManager`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"orderService_","type":"[OrderService](services.OrderService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"paymentProviderService_","type":"[PaymentProviderService](services.PaymentProviderService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"productVariantInventoryService_","type":"[ProductVariantInventoryService](services.ProductVariantInventoryService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"returnService_","type":"[ReturnService](services.ReturnService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"shippingOptionService_","type":"[ShippingOptionService](services.ShippingOptionService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"swapRepository_","type":"Repository&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"totalsService_","type":"[TotalsService](services.TotalsService.mdx)","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"transactionManager_","type":"`undefined` \\| `EntityManager`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"Events","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"Events.CREATED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.created\"","expandable":false,"children":[]},{"name":"Events.FULFILLMENT_CREATED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.fulfillment_created\"","expandable":false,"children":[]},{"name":"Events.PAYMENT_CAPTURED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.payment_captured\"","expandable":false,"children":[]},{"name":"Events.PAYMENT_CAPTURE_FAILED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.payment_capture_failed\"","expandable":false,"children":[]},{"name":"Events.PAYMENT_COMPLETED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.payment_completed\"","expandable":false,"children":[]},{"name":"Events.PROCESS_REFUND_FAILED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.process_refund_failed\"","expandable":false,"children":[]},{"name":"Events.RECEIVED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.received\"","expandable":false,"children":[]},{"name":"Events.REFUND_PROCESSED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.refund_processed\"","expandable":false,"children":[]},{"name":"Events.SHIPMENT_CREATED","type":"`string`","description":"","optional":false,"defaultValue":"\"swap.shipment_created\"","expandable":false,"children":[]},{"name":"__configModule__","type":"`Record<string, unknown>`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"__moduleDeclaration__","type":"`Record<string, unknown>`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />

___

## Accessors

### activeManager\_

#### Returns

<ParameterTypes parameters={[{"name":"EntityManager","type":"`EntityManager`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />

___

## Methods

### areReturnItemsValid

#### Parameters

<ParameterTypes parameters={[{"name":"returnItems","type":"[WithRequiredProperty](../../medusa/types/medusa.WithRequiredProperty.mdx)&#60;Partial&#60;[ReturnItem](../../entities/classes/entities.ReturnItem.mdx)&#62;, \"item_id\"&#62;[]","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;boolean&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />

___

### atomicPhase\_

Wraps some work within a transactional block. If the service already has
a transaction manager attached this will be reused, otherwise a new
transaction manager is created.

#### Type Parameters

<ParameterTypes parameters={[{"name":"TResult","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"TError","type":"`object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Parameters

<ParameterTypes parameters={[{"name":"work","type":"(`transactionManager`: `EntityManager`) => Promise&#60;TResult&#62;","description":"the transactional work to be done","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"isolationOrErrorHandler","type":"`IsolationLevel` \\| (`error`: `TError`) => Promise&#60;void \\| TResult&#62;","description":"the isolation level to be used for the work.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"maybeErrorHandlerOrDontFail","type":"(`error`: `TError`) => Promise&#60;void \\| TResult&#62;","description":"Potential error handler","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;TResult&#62;","optional":false,"defaultValue":"","description":"the result of the transactional work","expandable":false,"children":[]}]} />

___

### cancel

Cancels a given swap if possible. A swap can only be canceled if all
related returns, fulfillments, and payments have been canceled. If a swap
is associated with a refund, it cannot be canceled.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"the id of the swap to cancel.","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the canceled swap.","expandable":false,"children":[]}]} />

___

### cancelFulfillment

Cancels a fulfillment (if related to a swap)

#### Parameters

<ParameterTypes parameters={[{"name":"fulfillmentId","type":"`string`","description":"the ID of the fulfillment to cancel","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"updated swap","expandable":false,"children":[]}]} />

___

### create

Creates a swap from an order, with given return items, additional items
and an optional return shipping method.

#### Parameters

<ParameterTypes parameters={[{"name":"order","type":"[Order](../../entities/classes/entities.Order.mdx)","description":"the order to base the swap off","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"returnItems","type":"[WithRequiredProperty](../../medusa/types/medusa.WithRequiredProperty.mdx)&#60;Partial&#60;[ReturnItem](../../entities/classes/entities.ReturnItem.mdx)&#62;, \"item_id\"&#62;[]","description":"the items to return in the swap","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"returnShipping.option_id","type":"`string`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"custom","type":"`object`","description":"contains relevant custom information. This object may\n include no\\_notification which will disable sending notification when creating\n swap. If set, it overrules the attribute inherited from the order","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"additionalItems","type":"Pick&#60;[LineItem](../../entities/classes/entities.LineItem.mdx), \"variant_id\" \\| \"quantity\"&#62;[]","description":"the items to send to the customer","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"returnShipping","type":"`object`","description":"an optional shipping method for returning the returnItems","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"returnShipping.price","type":"`number`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"custom.allow_backorder","type":"`boolean`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"custom.idempotency_key","type":"`string`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"custom.location_id","type":"`string`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"custom.no_notification","type":"`boolean`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the newly created swap","expandable":false,"children":[]}]} />

___

### createCart

Creates a cart from the given swap. The cart can be used to pay
for differences associated with the swap. The swap represented by the
swapId must belong to the order. Fails if there is already a cart on the
swap.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"the id of the swap to create the cart from","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"customShippingOptions","type":"`object`[]","description":"the shipping options","optional":false,"defaultValue":"[]","expandable":false,"children":[]},{"name":"context","type":"`object`","description":"","optional":false,"defaultValue":"{}","expandable":false,"children":[]},{"name":"context.sales_channel_id","type":"`string`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the swap with its cart\\_id prop set to the id of the new cart.","expandable":false,"children":[]}]} />

___

### createFulfillment

Fulfills the additional items associated with the swap. Will call the
fulfillment providers associated with the shipping methods.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"the id of the swap to fulfill,","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"`CreateShipmentConfig`","description":"optional configurations, includes optional metadata to attach to the shipment, and a no\\_notification flag.","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the updated swap with new status and fulfillments.","expandable":false,"children":[]}]} />

___

### createShipment

Marks a fulfillment as shipped and attaches tracking numbers.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"the id of the swap that has been shipped.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"fulfillmentId","type":"`string`","description":"the id of the specific fulfillment that has been shipped","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"`CreateShipmentConfig`","description":"optional configurations, includes optional metadata to attach to the shipment, and a noNotification flag.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"trackingLinks","type":"`object`[]","description":"the tracking numbers associated with the shipment","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the updated swap with new fulfillments and status.","expandable":false,"children":[]}]} />

___

### deleteMetadata

Dedicated method to delete metadata for a swap.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"the order to delete metadata from.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"key","type":"`string`","description":"key for metadata field","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"resolves to the updated result.","expandable":false,"children":[]}]} />

___

### list

List swaps.

#### Parameters

<ParameterTypes parameters={[{"name":"selector","type":"[Selector](../../medusa/types/medusa.Selector.mdx)&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","description":"the query object for find","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","description":"the configuration used to find the objects. contains relations, skip, and take.","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)[]&#62;","optional":false,"defaultValue":"","description":"the result of the find operation","expandable":false,"children":[]}]} />

___

### listAndCount

List swaps.

#### Parameters

<ParameterTypes parameters={[{"name":"selector","type":"[Selector](../../medusa/types/medusa.Selector.mdx)&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","description":"the query object for find","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","description":"the configuration used to find the objects. contains relations, skip, and take.","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[[Swap](../../entities/classes/entities.Swap.mdx)[], number]&#62;","optional":false,"defaultValue":"","description":"the result of the find operation","expandable":false,"children":[]}]} />

___

### processDifference

Process difference for the requested swap.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"id of a swap being processed","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"processed swap","expandable":false,"children":[]}]} />

___

### registerCartCompletion

Register a cart completion

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"The id of the swap","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"swap related to the cart","expandable":false,"children":[]}]} />

___

### registerReceived

Registers the swap return items as received so that they cannot be used
as a part of other swaps/returns.

#### Parameters

<ParameterTypes parameters={[{"name":"id","type":"`any`","description":"the id of the order with the swap.","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the resulting order","expandable":false,"children":[]}]} />

___

### retrieve

Retrieves a swap with the given id.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"the id of the swap to retrieve","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"config","type":"Omit&#60;[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;, \"select\"&#62; & `object`","description":"the configuration to retrieve the swap","optional":false,"defaultValue":"{}","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the swap","expandable":false,"children":[]}]} />

___

### retrieveByCartId

Retrieves a swap based on its associated cart id

#### Parameters

<ParameterTypes parameters={[{"name":"cartId","type":"`string`","description":"the cart id that the swap's cart has","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"relations","type":"`undefined` \\| `string`[]","description":"the relations to retrieve swap","optional":false,"defaultValue":"[]","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"the swap","expandable":false,"children":[]}]} />

___

### shouldRetryTransaction\_

#### Parameters

<ParameterTypes parameters={[{"name":"err","type":"`Record<string, unknown>` \\| `object`","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"boolean","type":"`boolean`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]} />

___

### transformQueryForCart

Transform find config object for retrieval.

#### Parameters

<ParameterTypes parameters={[{"name":"config","type":"Omit&#60;[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;, \"select\"&#62; & `object`","description":"parsed swap find config","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Omit","type":"Omit&#60;[FindConfig](../../medusa/interfaces/medusa.FindConfig.mdx)&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;, \"select\"&#62;","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]},{"name":"cartRelations","type":"`undefined` \\| `string`[]","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"cartSelects","type":"`undefined` \\| keyof [Cart](../../entities/classes/entities.Cart.mdx)[]","description":"","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"select","type":"`string`[]","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />

___

### update

Update the swap record.

#### Parameters

<ParameterTypes parameters={[{"name":"swapId","type":"`string`","description":"id of a swap to update","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"update","type":"Partial&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","description":"new data","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"Promise","type":"Promise&#60;[Swap](../../entities/classes/entities.Swap.mdx)&#62;","optional":false,"defaultValue":"","description":"updated swap record","expandable":false,"children":[]}]} />

___

### withTransaction

#### Parameters

<ParameterTypes parameters={[{"name":"transactionManager","type":"`EntityManager`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]} />

#### Returns

<ParameterTypes parameters={[{"name":"SwapService","type":"`object`","description":"Handles swaps","optional":false,"defaultValue":"","expandable":false,"children":[]}]} />
