1 import { HttpTypes, SelectParams } from "@medusajs/types"
2 import { Client } from "../client.js"
3 import { ClientHeaders } from "../types.js"
4
5 export class Payment {
6 /**
7 * @ignore
8 */
9 private client: Client
10 /**
11 * @ignore
12 */
13 constructor(client: Client) {
14 this.client = client
15 }
16
17 /**
18 * This method retrieves a paginated list of payments. It sends a request to the
19 * [List Payments](https://docs.medusajs.com/api/admin/payments/list-payments) API route.
20 *
21 * @param query - Filters and pagination configurations.
22 * @param headers - Headers to pass in the request.
23 * @returns The paginated list of payments.
24 *
25 * @example
26 * To retrieve the list of payments:
27 *
28 * ```ts
29 * sdk.admin.payment.list()
30 * .then(({ payments, count, limit, offset }) => {
31 * console.log(payments)
32 * })
33 * ```
34 *
35 * To configure the pagination, pass the `limit` and `offset` query parameters.
36 *
37 * For example, to retrieve only 10 items and skip 10 items:
38 *
39 * ```ts
40 * sdk.admin.payment.list({
41 * limit: 10,
42 * offset: 10
43 * })
44 * .then(({ payments, count, limit, offset }) => {
45 * console.log(payments)
46 * })
47 * ```
48 *
49 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
50 * in each payment:
51 *
52 * ```ts
53 * sdk.admin.payment.list({
54 * fields: "id,*payment_collection"
55 * })
56 * .then(({ payments, count, limit, offset }) => {
57 * console.log(payments)
58 * })
59 * ```
60 *
61 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
62 */
63 async list(query?: HttpTypes.AdminPaymentFilters, headers?: ClientHeaders) {
64 return await this.client.fetch<HttpTypes.AdminPaymentsResponse>(
65 `/admin/payments`,
66 {
67 query,
68 headers,
69 }
70 )
71 }
72
73 /**
74 * This method retrieves a paginated list of payment providers. It sends a request to the
75 * [List Payment Providers](https://docs.medusajs.com/api/admin/payments/list-payment-providers) API route.
76 *
77 * @param query - Filters and pagination configurations.
78 * @param headers - Headers to pass in the request.
79 * @returns The paginated list of payment providers.
80 *
81 * @example
82 * To retrieve the list of payment providers:
83 *
84 * ```ts
85 * sdk.admin.payment.listPaymentProviders()
86 * .then(({ payment_providers, count, limit, offset }) => {
87 * console.log(payment_providers)
88 * })
89 * ```
90 *
91 * To configure the pagination, pass the `limit` and `offset` query parameters.
92 *
93 * For example, to retrieve only 10 items and skip 10 items:
94 *
95 * ```ts
96 * sdk.admin.payment.listPaymentProviders({
97 * limit: 10,
98 * offset: 10
99 * })
100 * .then(({ payment_providers, count, limit, offset }) => {
101 * console.log(payment_providers)
102 * })
103 * ```
104 *
105 * Using the `fields` query parameter, you can specify the fields and relations to retrieve
106 * in each payment provider:
107 *
108 * ```ts
109 * sdk.admin.payment.listPaymentProviders({
110 * fields: "id,is_enabled"
111 * })
112 * .then(({ payment_providers, count, limit, offset }) => {
113 * console.log(payment_providers)
114 * })
115 * ```
116 *
117 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
118 */
119 async listPaymentProviders(
120 query?: HttpTypes.AdminGetPaymentProvidersParams,
121 headers?: ClientHeaders
122 ) {
123 return await this.client.fetch<HttpTypes.AdminPaymentProviderListResponse>(
124 `/admin/payments/payment-providers`,
125 {
126 query,
127 headers,
128 }
129 )
130 }
131
132 /**
133 * This method retrieves a payment's details. It sends a request to the
134 * [Get Payment](https://docs.medusajs.com/api/admin/payments/get-a-payment)
135 * API route.
136 *
137 * @param id - The payment's ID.
138 * @param query - Configure the fields to retrieve in the payment.
139 * @param headers - Headers to pass in the request
140 * @returns The payment's details.
141 *
142 * @example
143 * To retrieve a payment by its ID:
144 *
145 * ```ts
146 * sdk.admin.payment.retrieve("pay_123")
147 * .then(({ payment }) => {
148 * console.log(payment)
149 * })
150 * ```
151 *
152 * To specify the fields and relations to retrieve:
153 *
154 * ```ts
155 * sdk.admin.payment.retrieve("pay_123", {
156 * fields: "id,*payment_collection"
157 * })
158 * .then(({ payment }) => {
159 * console.log(payment)
160 * })
161 * ```
162 *
163 * Learn more about the `fields` property in the [API reference](https://docs.medusajs.com/api/store/select-fields-and-relations).
164 */
165 async retrieve(
166 id: string,
167 query?: SelectParams,
168 headers?: ClientHeaders
169 ) {
170 return await this.client.fetch<HttpTypes.AdminPaymentResponse>(
171 `/admin/payments/${id}`,
172 {
173 query,
174 headers,
175 }
176 )
177 }
178
179 /**
180 * This method captures a payment. It sends a request to the
181 * [Capture Payment](https://docs.medusajs.com/api/admin/payments/capture-payment) API route.
182 *
183 * The API route uses the `capturePayment` method of the payment provider associated with the payment's collection.
184 *
185 * @param id - The payment's ID.
186 * @param body - The capture's details.
187 * @param query - Configure the fields to retrieve in the payment.
188 * @param headers - Headers to pass in the request
189 * @returns The payment's details.
190 *
191 * @example
192 * sdk.admin.payment.capture("paycol_123", {})
193 * .then(({ payment }) => {
194 * console.log(payment)
195 * })
196 */
197 async capture(
198 id: string,
199 body: HttpTypes.AdminCapturePayment,
200 query?: SelectParams,
201 headers?: ClientHeaders
202 ) {
203 return await this.client.fetch<HttpTypes.AdminPaymentResponse>(
204 `/admin/payments/${id}/capture`,
205 {
206 method: "POST",
207 headers,
208 body,
209 query,
210 }
211 )
212 }
213
214 /**
215 * This method refunds a payment. It sends a request to the
216 * [Refund Payment](https://docs.medusajs.com/api/admin/payments/refund-payment) API route.
217 *
218 * The API route uses the `refundPayment` method of the payment provider associated with the payment's collection.
219 *
220 * @param id - The payment's ID.
221 * @param body - The refund's details.
222 * @param query - Configure the fields to retrieve in the payment.
223 * @param headers - Headers to pass in the request
224 * @returns The payment's details.
225 *
226 * @example
227 * sdk.admin.payment.refund("paycol_123", {})
228 * .then(({ payment }) => {
229 * console.log(payment)
230 * })
231 */
232 async refund(
233 id: string,
234 body: HttpTypes.AdminRefundPayment,
235 query?: SelectParams,
236 headers?: ClientHeaders
237 ) {
238 return await this.client.fetch<HttpTypes.AdminPaymentResponse>(
239 `/admin/payments/${id}/refund`,
240 {
241 method: "POST",
242 headers,
243 body,
244 query,
245 }
246 )
247 }
248 }