1 import { HttpTypes, SelectParams } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 /**
6 * The main service interface for payment collections in the admin SDK.
7 */
8 export class PaymentCollection {
9 /**
10 * @ignore
11 */
12 private client: Client
13 /**
14 * @ignore
15 */
16 constructor(client: Client) {
17 this.client = client
18 }
19
20 /**
21 * This method creates a payment collection. It sends a request to the
22 * [Create Payment Collection](https://docs.medusajs.com/api/admin/payment-collections/create-payment-collection)
23 * API route.
24 *
25 * @param body - The details of the payment collection to create.
26 * @param query - Configure the fields to retrieve in the payment collection.
27 * @param headers - Headers to pass in the request
28 * @returns The payment collection's details.
29 *
30 * @example
31 * sdk.admin.paymentCollection.create({
32 * order_id: "order_123"
33 * })
34 * .then(({ payment_collection }) => {
35 * console.log(payment_collection)
36 * })
37 */
38 async create(
39 body: HttpTypes.AdminCreatePaymentCollection,
40 query?: SelectParams,
41 headers?: ClientHeaders
42 ) {
43 return await this.client.fetch<HttpTypes.AdminPaymentCollectionResponse>(
44 `/admin/payment-collections`,
45 {
46 method: "POST",
47 headers,
48 body,
49 query,
50 }
51 )
52 }
53
54 /**
55 * This method deletes a payment collection. It sends a request to the
56 * [Delete Payment Collection](https://docs.medusajs.com/api/admin/payment-collections/delete-a-payment-collection)
57 * API route.
58 *
59 * @param id - The payment collection's ID.
60 * @param headers - Headers to pass in the request
61 * @returns The deletion's details.
62 *
63 * @example
64 * sdk.admin.paymentCollection.delete("paycol_123")
65 * .then(({ deleted }) => {
66 * console.log(deleted)
67 * })
68 */
69 async delete(id: string, headers?: ClientHeaders) {
70 return await this.client.fetch<HttpTypes.AdminDeletePaymentCollectionResponse>(
71 `/admin/payment-collections/${id}`,
72 {
73 method: "DELETE",
74 headers,
75 }
76 )
77 }
78
79 /**
80 * This method marks a payment collection as paid. It sends a request to the
81 * [Mark as Paid](https://docs.medusajs.com/api/admin/payment-collections/mark-as-paid)
82 * API route.
83 *
84 * The API route creates and authorizes a payment session, then capture its payment.
85 * It uses the manual payment provider by default, or the provider passed in
86 * `provider_id` to record the captured payment under a specific provider.
87 *
88 * @param id - The payment collection to mark as paid.
89 * @param body - The details to mark the payment collection as paid.
90 * @param query - Configure the fields to retrieve in the payment collection.
91 * @param headers - Headers to pass in the request.
92 * @returns The payment collection's details.
93 *
94 * @example
95 * sdk.admin.paymentCollection.markAsPaid("paycol_123", {
96 * order_id: "order_123",
97 * // optional: record the payment under a specific provider
98 * provider_id: "pp_system_default"
99 * })
100 * .then(({ payment_collection }) => {
101 * console.log(payment_collection)
102 * })
103 */
104 async markAsPaid(
105 id: string,
106 body: HttpTypes.AdminMarkPaymentCollectionAsPaid,
107 query?: SelectParams,
108 headers?: ClientHeaders
109 ) {
110 return await this.client.fetch<HttpTypes.AdminPaymentCollectionResponse>(
111 `/admin/payment-collections/${id}/mark-as-paid`,
112 {
113 method: "POST",
114 headers,
115 body,
116 query,
117 }
118 )
119 }
120
121 /**
122 * This method creates a payment session for a payment collection. It sends a request to the
123 * [Create Payment Session](https://docs.medusajs.com/api/admin/payment-collections/initialize-payment-session)
124 * API route.
125 *
126 * @param id - The payment collection's ID.
127 * @param body - The payment session's details.
128 * @param query - Configure the fields to retrieve in the payment collection.
129 * @param headers - Headers to pass in the request.
130 * @returns The payment collection's details.
131 *
132 * @example
133 * sdk.admin.paymentCollection.createPaymentSession("paycol_123", {
134 * provider_id: "pp_stripe_stripe"
135 * })
136 * .then(({ payment_collection }) => {
137 * console.log(payment_collection)
138 * })
139 *
140 * @since 2.14.2
141 * @tags payment-collections
142 */
143 async createPaymentSession(
144 id: string,
145 body: HttpTypes.AdminInitializePaymentSession,
146 query?: SelectParams,
147 headers?: ClientHeaders
148 ) {
149 return await this.client.fetch<HttpTypes.AdminPaymentCollectionResponse>(
150 `/admin/payment-collections/${id}/payment-sessions`,
151 {
152 method: "POST",
153 headers,
154 body,
155 query,
156 }
157 )
158 }
159 }