1 import {
2 AdminOrderChangesResponse,
3 CreateOrderCreditLineDTO,
4 FindParams,
5 HttpTypes,
6 PaginatedResponse,
7 SelectParams,
8 } from "@medusajs/types"
9
10 import { Client } from "../client.js"
11 import { ClientHeaders } from "../types.js"
12
13 export class Order {
14 /**
15 * @ignore
16 */
17 private client: Client
18 /**
19 * @ignore
20 */
21 constructor(client: Client) {
22 this.client = client
23 }
24
25 /**
26 * This method retrieves an order by its ID. It sends a request to the
27 * [Get Order](https://docs.medusajs.com/api/admin/orders/get-an-order)
28 * API route.
29 *
30 * @param id - The order's ID.
31 * @param query - Configure the fields to retrieve in the order.
32 * @param headers - Headers to pass in the request
33 * @returns The order's details.
34 *
35 * @example
36 * To retrieve an order by its ID:
37 *
38 * ```ts
39 * sdk.admin.order.retrieve("order_123")
40 * .then(({ order }) => {
41 * console.log(order)
42 * })
43 * ```
44 *
45 * To specify the fields and relations to retrieve:
46 *
47 * ```ts
48 * sdk.admin.order.retrieve("order_123", {
49 * fields: "id,*items"
50 * })
51 * .then(({ order }) => {
52 * console.log(order)
53 * })
54 * ```
55 *
56 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
57 */
58 async retrieve(id: string, query?: SelectParams, headers?: ClientHeaders) {
59 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
60 `/admin/orders/${id}`,
61 {
62 query,
63 headers,
64 }
65 )
66 }
67
68 /**
69 * This method updates an order. It sends a request to the
70 * [Update Order Email](https://docs.medusajs.com/api/admin/orders/update-order)
71 * API route.
72 *
73 * @param id - The order's ID.
74 * @param body - The update details.
75 * @param headers - Headers to pass in the request
76 * @returns The order's details.
77 *
78 * @example
79 * sdk.admin.order.update(
80 * "order_123",
81 * {
82 * email: "new_email@example.com",
83 * shipping_address: {
84 * first_name: "John",
85 * last_name: "Doe",
86 * address_1: "123 Main St",
87 * }
88 * }
89 * )
90 * .then(({ order }) => {
91 * console.log(order)
92 * })
93 */
94 async update(
95 id: string,
96 body: HttpTypes.AdminUpdateOrder,
97 headers?: ClientHeaders
98 ) {
99 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
100 `/admin/orders/${id}`,
101 {
102 method: "POST",
103 headers,
104 body,
105 }
106 )
107 }
108
109 /**
110 * This method retrieves the preview of an order based on its last associated change. It sends a request to the
111 * [Get Order Preview](https://docs.medusajs.com/api/admin/orders/get-preview) API route.
112 *
113 * @param id - The order's ID.
114 * @param query - Query parameters.
115 * @param headers - Headers to pass in the request
116 * @returns The order preview's details.
117 *
118 * @example
119 * sdk.admin.order.retrievePreview("order_123")
120 * .then(({ order }) => {
121 * console.log(order)
122 * })
123 */
124 async retrievePreview(
125 id: string,
126 query?: HttpTypes.AdminOrderFilters,
127 headers?: ClientHeaders
128 ) {
129 return await this.client.fetch<HttpTypes.AdminOrderPreviewResponse>(
130 `/admin/orders/${id}/preview`,
131 {
132 query,
133 headers,
134 }
135 )
136 }
137
138 /**
139 * This method retrieves a paginated list of orders. It sends a request to the
140 * [List Orders](https://docs.medusajs.com/api/admin/orders/list-orders) API route.
141 *
142 * @param queryParams - Filters and pagination configurations.
143 * @param headers - Headers to pass in the request.
144 * @returns The paginated list of orders.
145 *
146 * @example
147 * To retrieve the list of orders:
148 *
149 * ```ts
150 * sdk.admin.order.list()
151 * .then(({ orders, count, limit, offset }) => {
152 * console.log(orders)
153 * })
154 * ```
155 *
156 * To configure the pagination, pass the `limit` and `offset` query parameters.
157 *
158 * For example, to retrieve only 10 items and skip 10 items:
159 *
160 * ```ts
161 * sdk.admin.order.list({
162 * limit: 10,
163 * offset: 10
164 * })
165 * .then(({ orders, count, limit, offset }) => {
166 * console.log(orders)
167 * })
168 * ```
169 *
170 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
171 * in each order:
172 *
173 * ```ts
174 * sdk.admin.order.list({
175 * fields: "id,*items"
176 * })
177 * .then(({ orders, count, limit, offset }) => {
178 * console.log(orders)
179 * })
180 * ```
181 *
182 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
183 */
184 async list(
185 queryParams?: HttpTypes.AdminOrderFilters,
186 headers?: ClientHeaders
187 ) {
188 return await this.client.fetch<HttpTypes.AdminOrderListResponse>(
189 `/admin/orders`,
190 {
191 query: queryParams,
192 headers,
193 }
194 )
195 }
196
197 /**
198 * This method archives an order. It sends a request to the
199 * [Archive Order](https://docs.medusajs.com/api/admin/orders/archive-an-order)
200 * API route.
201 *
202 * @param id - The order's ID.
203 * @param queryParams - Configure the fields to retrieve in the order.
204 * @param headers - Headers to pass in the request
205 * @returns The order's details.
206 *
207 * @example
208 * sdk.admin.order.archive("order_123")
209 * .then(({ order }) => {
210 * console.log(order)
211 * })
212 */
213 async archive(
214 id: string,
215 queryParams?: SelectParams,
216 headers?: ClientHeaders
217 ) {
218 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
219 `/admin/orders/${id}/archive`,
220 {
221 method: "POST",
222 query: queryParams,
223 headers,
224 }
225 )
226 }
227
228 /**
229 * This method cancels an order. It sends a request to the
230 * [Cancel Order](https://docs.medusajs.com/api/admin/orders/cancel-order)
231 * API route.
232 *
233 * @param id - The order's ID.
234 * @param headers - Headers to pass in the request.
235 * @returns The order's details.
236 *
237 * @example
238 * sdk.admin.order.cancel("order_123")
239 * .then(({ order }) => {
240 * console.log(order)
241 * })
242 */
243 async cancel(id: string, headers?: ClientHeaders) {
244 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
245 `/admin/orders/${id}/cancel`,
246 {
247 method: "POST",
248 headers,
249 }
250 )
251 }
252
253 /**
254 * This method completes an order. It sends a request to the
255 * [Complete Order](https://docs.medusajs.com/api/admin/orders/complete-order)
256 * API route.
257 *
258 * @param id - The order's ID.
259 * @param headers - Headers to pass in the request.
260 * @returns The order's details.
261 *
262 * @example
263 * sdk.admin.order.complete("order_123")
264 * .then(({ order }) => {
265 * console.log(order)
266 * })
267 */
268 async complete(
269 id: string,
270 body: HttpTypes.AdditionalData,
271 queryParams?: SelectParams,
272 headers?: ClientHeaders
273 ) {
274 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
275 `/admin/orders/${id}/complete`,
276 {
277 method: "POST",
278 body,
279 query: queryParams,
280 headers,
281 }
282 )
283 }
284
285 /**
286 * This method requests an order transfer. It sends a request to the
287 * [Request Order Transfer](https://docs.medusajs.com/api/admin/orders/request-transfer)
288 * API route.
289 *
290 * @param id - The order's ID.
291 * @param headers - Headers to pass in the request.
292 * @param body - The transfer's details - the id of the next owner.
293 * @returns The order's details.
294 *
295 * @example
296 * sdk.admin.order.requestTransfer("order_123", {
297 * customer_id: "cus_123",
298 * internal_note: "Internal note",
299 * })
300 * .then(({ order }) => {
301 * console.log(order)
302 * })
303 */
304 async requestTransfer(
305 id: string,
306 body: HttpTypes.AdminRequestOrderTransfer,
307 headers?: ClientHeaders
308 ) {
309 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
310 `/admin/orders/${id}/transfer`,
311 {
312 method: "POST",
313 headers,
314 body,
315 }
316 )
317 }
318
319 /**
320 * This method transfers an order to a guest customer, identified by email. If no
321 * customer exists with the given email, a guest customer is created. Unlike
322 * {@link requestTransfer}, the transfer is applied immediately without requiring
323 * the recipient to accept it. It sends a request to the
324 * `/admin/orders/:id/transfer/guest` API route.
325 *
326 * @param id - The order's ID.
327 * @param body - The transfer's details - the email of the guest customer.
328 * @param headers - Headers to pass in the request.
329 * @returns The order's details.
330 *
331 * @example
332 * sdk.admin.order.transferToGuest("order_123", {
333 * email: "customer@example.com",
334 * internal_note: "Internal note",
335 * })
336 * .then(({ order }) => {
337 * console.log(order)
338 * })
339 */
340 async transferToGuest(
341 id: string,
342 body: HttpTypes.AdminTransferOrderToGuest,
343 headers?: ClientHeaders
344 ) {
345 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
346 `/admin/orders/${id}/transfer/guest`,
347 {
348 method: "POST",
349 headers,
350 body,
351 }
352 )
353 }
354
355 /**
356 * This method cancels an order transfer request. It sends a request to the
357 * [Cancel Order Transfer Request](https://docs.medusajs.com/api/admin/orders/cancel-transfer)
358 * API route.
359 *
360 * @param id - The order's ID.
361 * @param headers - Headers to pass in the request.
362 * @returns The order's details.
363 *
364 * @example
365 * sdk.admin.order.cancelTransfer("order_123")
366 * .then(({ order }) => {
367 * console.log(order)
368 * })
369 */
370 async cancelTransfer(id: string, headers?: ClientHeaders) {
371 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
372 `/admin/orders/${id}/transfer/cancel`,
373 {
374 method: "POST",
375 headers,
376 }
377 )
378 }
379
380 /**
381 * This method creates a fulfillment for an order. It sends a request to the
382 * [Create Fulfillment](https://docs.medusajs.com/api/admin/orders/create-fulfillment)
383 * API route.
384 *
385 * @param id - The order's ID.
386 * @param body - The fulfillment's details.
387 * @param query - Configure the fields to retrieve in the order.
388 * @param headers - Headers to pass in the request
389 * @returns The order's details.
390 *
391 * @example
392 * sdk.admin.order.createFulfillment("order_123", {
393 * items: [
394 * {
395 * id: "orli_123",
396 * quantity: 1
397 * }
398 * ]
399 * })
400 * .then(({ order }) => {
401 * console.log(order)
402 * })
403 */
404 async createFulfillment(
405 id: string,
406 body: HttpTypes.AdminCreateOrderFulfillment,
407 query?: SelectParams,
408 headers?: ClientHeaders
409 ) {
410 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
411 `/admin/orders/${id}/fulfillments`,
412 {
413 method: "POST",
414 headers,
415 body,
416 query,
417 }
418 )
419 }
420
421 /**
422 * This method cancels an order's fulfillment. It sends a request to the
423 * [Cancel Fulfillment](https://docs.medusajs.com/api/admin/orders/cancel-fulfillment)
424 * API route.
425 *
426 * @param id - The order's ID.
427 * @param fulfillmentId - The ID of the fulfillment to cancel.
428 * @param body - The cancelation's details.
429 * @param headers - Headers to pass in the request
430 * @returns The order's details.
431 *
432 * @example
433 * sdk.admin.order.cancelFulfillment(
434 * "order_123",
435 * "ful_123",
436 * {
437 * no_notification: false
438 * }
439 * )
440 * .then(({ order }) => {
441 * console.log(order)
442 * })
443 */
444 async cancelFulfillment(
445 id: string,
446 fulfillmentId: string,
447 body: HttpTypes.AdminCancelOrderFulfillment,
448 headers?: ClientHeaders
449 ) {
450 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
451 `/admin/orders/${id}/fulfillments/${fulfillmentId}/cancel`,
452 {
453 method: "POST",
454 headers,
455 body,
456 }
457 )
458 }
459
460 /**
461 * This method creates a shipment for an order's fulfillment. It sends a request to the
462 * [Create Shipment](https://docs.medusajs.com/api/admin/orders/create-shipment)
463 * API route.
464 *
465 * @param id - The order's ID.
466 * @param fulfillmentId - The ID of the fulfillment.
467 * @param body - The shipment's details.
468 * @param query - Configure the fields to retrieve in the order.
469 * @param headers - Headers to pass in the request
470 * @returns The order's details.
471 *
472 * @example
473 * sdk.admin.order.createShipment(
474 * "order_123",
475 * "ful_123",
476 * {
477 * items: [
478 * {
479 * id: "fulit_123",
480 * quantity: 1
481 * }
482 * ]
483 * }
484 * )
485 * .then(({ order }) => {
486 * console.log(order)
487 * })
488 */
489 async createShipment(
490 id: string,
491 fulfillmentId: string,
492 body: HttpTypes.AdminCreateOrderShipment,
493 query?: SelectParams,
494 headers?: ClientHeaders
495 ) {
496 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
497 `/admin/orders/${id}/fulfillments/${fulfillmentId}/shipments`,
498 {
499 method: "POST",
500 headers,
501 body,
502 query,
503 }
504 )
505 }
506
507 /**
508 * This method marks an order's fulfillment as delivered. It sends a request to the
509 * [Mark Delivered ](https://docs.medusajs.com/api/admin/orders/mark-delivered)
510 * API route.
511 *
512 * @param id - The order's ID.
513 * @param fulfillmentId - The fulfillment's ID.
514 * @param body - The delivery options.
515 * @param query - Configure the fields to retrieve in the order.
516 * @param headers - Headers to pass in the request
517 * @returns The order's details.
518 *
519 * @example
520 * sdk.admin.order.markAsDelivered(
521 * "order_123",
522 * "ful_123",
523 * )
524 * .then(({ order }) => {
525 * console.log(order)
526 * })
527 */
528 async markAsDelivered(
529 id: string,
530 fulfillmentId: string,
531 body?: HttpTypes.AdminMarkOrderFulfillmentAsDelivered,
532 query?: SelectParams,
533 headers?: ClientHeaders
534 ) {
535 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
536 `/admin/orders/${id}/fulfillments/${fulfillmentId}/mark-as-delivered`,
537 {
538 method: "POST",
539 headers,
540 body,
541 query,
542 }
543 )
544 }
545
546 /**
547 * This method retrieves a list of shipping options for an order based on the order's shipping address.
548 *
549 * This method sends a request to the [List Shipping Options](https://docs.medusajs.com/api/admin/orders/list-shipping-options)
550 * API route.
551 *
552 * @param id - The order's ID.
553 * @param queryParams - Configure the fields to retrieve in each shipping option.
554 * @param headers - Headers to pass in the request
555 * @returns The list of shipping options.
556 *
557 * @example
558 * sdk.admin.order.listShippingOptions("order_123")
559 * .then(({ shipping_options }) => {
560 * console.log(shipping_options)
561 * })
562 */
563 async listShippingOptions(
564 id: string,
565 queryParams?: FindParams & HttpTypes.AdminGetOrderShippingOptionList,
566 headers?: ClientHeaders
567 ) {
568 return await this.client.fetch<{
569 shipping_options: HttpTypes.AdminShippingOption[]
570 }>(`/admin/orders/${id}/shipping-options`, {
571 query: queryParams,
572 headers,
573 })
574 }
575
576 /**
577 * This method retrieves a list of changes made on an order, including returns, exchanges, etc...
578 *
579 * This method sends a request to the [List Changes](https://docs.medusajs.com/api/admin/orders/list-changes)
580 * API route.
581 *
582 * @param id - The order's ID.
583 * @param queryParams - Configure the fields to retrieve in each order change.
584 * @param headers - Headers to pass in the request
585 * @returns The list of order changes.
586 *
587 * @example
588 * sdk.admin.order.listChanges("order_123")
589 * .then(({ order_changes }) => {
590 * console.log(order_changes)
591 * })
592 */
593 async listChanges(
594 id: string,
595 queryParams?: FindParams & HttpTypes.AdminOrderChangesFilters,
596 headers?: ClientHeaders
597 ) {
598 return await this.client.fetch<
599 PaginatedResponse<AdminOrderChangesResponse>
600 >(`/admin/orders/${id}/changes`, {
601 query: queryParams,
602 headers,
603 })
604 }
605
606 /**
607 * This method retrieves the order's line items. It sends a request to the
608 * [List Line Items](https://docs.medusajs.com/api/admin/orders/list-line-items)
609 * API routes.
610 *
611 * @param id - The order's ID.
612 * @param queryParams - Configure the fields to retrieve in each line item.
613 * @param headers - Headers to pass in the request
614 * @returns The list of line items.
615 *
616 * @example
617 * sdk.admin.order.listLineItems("order_123")
618 * .then(({ order_items }) => {
619 * console.log(order_items)
620 * })
621 */
622 async listLineItems(
623 id: string,
624 queryParams?: FindParams & HttpTypes.AdminOrderItemsFilters,
625 headers?: ClientHeaders
626 ) {
627 return await this.client.fetch<HttpTypes.AdminOrderLineItemsListResponse>(
628 `/admin/orders/${id}/line-items`,
629 {
630 query: queryParams,
631 headers,
632 }
633 )
634 }
635
636 /**
637 * This method creates a credit line for an order. It sends a request to the
638 * [Create Credit Line](https://docs.medusajs.com/api/admin/orders/create-credit-line) API route.
639 *
640 * @param orderId - The order's ID.
641 * @param body - The credit line's details.
642 * @param query - Configure the fields to retrieve in the order.
643 * @param headers - Headers to pass in the request
644 * @returns The order's details.
645 *
646 * @example
647 * sdk.admin.order.createCreditLine(
648 * "order_123",
649 * {
650 * amount: 100,
651 * reference: "order",
652 * reference_id: "order_123",
653 * }
654 * )
655 * .then(({ order }) => {
656 * console.log(order)
657 * })
658 */
659 async createCreditLine(
660 orderId: string,
661 body: Omit<CreateOrderCreditLineDTO, "order_id">,
662 query?: SelectParams,
663 headers?: ClientHeaders
664 ) {
665 return await this.client.fetch<HttpTypes.AdminOrderResponse>(
666 `/admin/orders/${orderId}/credit-lines`,
667 {
668 method: "POST",
669 headers,
670 body,
671 query,
672 }
673 )
674 }
675
676 /**
677 * This method authorizes a payment session that is in `pending_authorization` status.
678 * It sends a request to the
679 * [Authorize Payment Session](https://docs.medusajs.com/api/admin/orders/authorize-payment-session)
680 * API route.
681 *
682 * This is used for payment methods where authorization happens asynchronously
683 * (e.g., bank transfers, payment links). The method triggers a re-check with
684 * the payment provider to see if the payment has been completed.
685 *
686 * @param id - The order's ID.
687 * @param paymentSessionId - The payment session's ID.
688 * @param query - Configure the fields to retrieve in the order.
689 * @param headers - Headers to pass in the request.
690 * @returns The order's details, along with whether the session was authorized.
691 *
692 * @example
693 * sdk.admin.order.authorizePaymentSession("order_123", "payses_123")
694 * .then(({ order, is_authorized }) => {
695 * console.log(order, is_authorized)
696 * })
697 */
698 async authorizePaymentSession(
699 id: string,
700 paymentSessionId: string,
701 query?: SelectParams,
702 headers?: ClientHeaders
703 ) {
704 return await this.client.fetch<HttpTypes.AdminAuthorizeOrderPaymentSessionResponse>(
705 `/admin/orders/${id}/payment-sessions/authorize`,
706 {
707 method: "POST",
708 headers,
709 body: { payment_session_id: paymentSessionId },
710 query,
711 }
712 )
713 }
714
715 /**
716 * This method updates an order change. It sends a request to the
717 * [Update Order Change](https://docs.medusajs.com/api/admin/order-changes/update-order-change)
718 * API route.
719 *
720 * @since 2.12.0
721 *
722 * @param id - The order change's ID.
723 * @param body - The update details.
724 * @param query - Configure the fields to retrieve in the order change.
725 * @param headers - Headers to pass in the request
726 * @returns The order change's details.
727 *
728 * @example
729 * sdk.admin.order.updateOrderChange(
730 * "ordch_123",
731 * {
732 * carry_over_promotions: true
733 * }
734 * )
735 * .then(({ order_change }) => {
736 * console.log(order_change)
737 * })
738 */
739 async updateOrderChange(
740 id: string,
741 body: HttpTypes.AdminUpdateOrderChange,
742 query?: HttpTypes.AdminOrderChangesFilters,
743 headers?: ClientHeaders
744 ) {
745 return await this.client.fetch<HttpTypes.AdminOrderChangeResponse>(
746 `/admin/order-changes/${id}`,
747 {
748 method: "POST",
749 headers,
750 body,
751 query,
752 }
753 )
754 }
755
756 /**
757 * This method starts an order export process to retrieve a CSV of exported orders. It sends
758 * a request to the [Export Orders](https://docs.medusajs.com/api/admin/orders/export-orders) API route.
759 *
760 * You'll receive in the response the transaction ID of the workflow generating the CSV file.
761 * To check the status of the execution, send a `GET` request to
762 * `/admin/workflows-executions/export-orders/:transaction-id`.
763 *
764 * Once the execution finishes successfully, a notification is created for the export.
765 * You can retrieve the notifications using the `/admin/notification` API route to
766 * retrieve the file's download URL.
767 *
768 * @since 2.12.3
769 *
770 * @param query - Filters to specify which orders to export.
771 * @param headers - Headers to pass in the request.
772 * @returns The export's details.
773 *
774 * @example
775 * sdk.admin.order.export()
776 * .then(({ transaction_id }) => {
777 * console.log(transaction_id)
778 * })
779 */
780 async export(query?: HttpTypes.AdminOrderFilters, headers?: ClientHeaders) {
781 return await this.client.fetch<HttpTypes.AdminExportOrderResponse>(
782 `/admin/orders/export`,
783 {
784 method: "POST",
785 headers,
786 query,
787 }
788 )
789 }
790 }