|
1
|
import { |
|
2
|
IPaymentProvider, |
|
3
|
ProviderWebhookPayload, |
|
4
|
WebhookActionResult, |
|
5
|
CapturePaymentInput, |
|
6
|
CapturePaymentOutput, |
|
7
|
AuthorizePaymentInput, |
|
8
|
AuthorizePaymentOutput, |
|
9
|
CancelPaymentInput, |
|
10
|
CancelPaymentOutput, |
|
11
|
InitiatePaymentInput, |
|
12
|
InitiatePaymentOutput, |
|
13
|
DeletePaymentInput, |
|
14
|
DeletePaymentOutput, |
|
15
|
GetPaymentStatusInput, |
|
16
|
GetPaymentStatusOutput, |
|
17
|
RefundPaymentInput, |
|
18
|
RefundPaymentOutput, |
|
19
|
RetrievePaymentInput, |
|
20
|
RetrievePaymentOutput, |
|
21
|
UpdatePaymentInput, |
|
22
|
UpdatePaymentOutput, |
|
23
|
} from "@medusajs/types" |
|
24
|
|
|
25
|
export abstract class AbstractPaymentProvider<TConfig = Record<string, unknown>> |
|
26
|
implements IPaymentProvider |
|
27
|
{ |
|
28
|
/** |
|
29
|
* @ignore |
|
30
|
*/ |
|
31
|
protected readonly container: Record<string, unknown> |
|
32
|
/** |
|
33
|
* This method validates the options of the provider set in `medusa-config.ts`. |
|
34
|
* Implementing this method is optional, but it's useful to ensure that the required |
|
35
|
* options are passed to the provider, or if you have any custom validation logic. |
|
36
|
* |
|
37
|
* If the options aren't valid, throw an error. |
|
38
|
* |
|
39
|
* @param options - The provider's options passed in `medusa-config.ts`. |
|
40
|
* |
|
41
|
* @example |
|
42
|
* class MyPaymentProviderService extends AbstractPaymentProvider<Options> { |
|
43
|
* static validateOptions(options: Record<any, any>) { |
|
44
|
* if (!options.apiKey) { |
|
45
|
* throw new MedusaError( |
|
46
|
* MedusaError.Types.INVALID_DATA, |
|
47
|
* "API key is required in the provider's options." |
|
48
|
* ) |
|
49
|
* } |
|
50
|
* } |
|
51
|
* } |
|
52
|
*/ |
|
53
|
static validateOptions(options: Record<any, any>): void | never {} |
|
54
|
|
|
55
|
/** |
|
56
|
* The constructor allows you to access resources from the [module's container](https://docs.medusajs.com/learn/fundamentals/modules/container) |
|
57
|
* using the first parameter, and the module's options using the second parameter. |
|
58
|
* |
|
59
|
* If you're creating a client or establishing a connection with a third-party service, do it in the constructor. |
|
60
|
* |
|
61
|
* :::note |
|
62
|
* |
|
63
|
* A module's options are passed when you register it in the Medusa application. |
|
64
|
* |
|
65
|
* ::: |
|
66
|
* |
|
67
|
* @param {Record<string, unknown>} cradle - The module's container used to resolve resources. |
|
68
|
* @param {Record<string, unknown>} config - The options passed to the Payment Module provider. |
|
69
|
* @typeParam TConfig - The type of the provider's options passed as a second parameter. |
|
70
|
* |
|
71
|
* @example |
|
72
|
* import { AbstractPaymentProvider } from "@medusajs/framework/utils" |
|
73
|
* import { Logger } from "@medusajs/framework/types" |
|
74
|
* |
|
75
|
* type Options = { |
|
76
|
* apiKey: string |
|
77
|
* } |
|
78
|
* |
|
79
|
* type InjectedDependencies = { |
|
80
|
* logger: Logger |
|
81
|
* } |
|
82
|
* |
|
83
|
* class MyPaymentProviderService extends AbstractPaymentProvider<Options> { |
|
84
|
* protected logger_: Logger |
|
85
|
* protected options_: Options |
|
86
|
* // assuming you're initializing a client |
|
87
|
* protected client |
|
88
|
* |
|
89
|
* constructor( |
|
90
|
* container: InjectedDependencies, |
|
91
|
* options: Options |
|
92
|
* ) { |
|
93
|
* super(container, options) |
|
94
|
* |
|
95
|
* this.logger_ = container.logger |
|
96
|
* this.options_ = options |
|
97
|
* |
|
98
|
* // TODO initialize your client |
|
99
|
* } |
|
100
|
* // ... |
|
101
|
* } |
|
102
|
* |
|
103
|
* export default MyPaymentProviderService |
|
104
|
*/ |
|
105
|
protected constructor( |
|
106
|
cradle: Record<string, unknown>, |
|
107
|
/** |
|
108
|
* @ignore |
|
109
|
*/ |
|
110
|
protected readonly config: TConfig = {} as TConfig // eslint-disable-next-line @typescript-eslint/no-empty-function |
|
111
|
) { |
|
112
|
this.container = cradle |
|
113
|
} |
|
114
|
|
|
115
|
/** |
|
116
|
* @ignore |
|
117
|
*/ |
|
118
|
static _isPaymentProvider = true |
|
119
|
|
|
120
|
/** |
|
121
|
* @ignore |
|
122
|
*/ |
|
123
|
static isPaymentProvider(object): boolean { |
|
124
|
return object?.constructor?._isPaymentProvider |
|
125
|
} |
|
126
|
|
|
127
|
/** |
|
128
|
* Each payment provider has a unique identifier defined in its class. The provider's ID |
|
129
|
* will be stored as `pp_{identifier}_{id}`, where `{id}` is the provider's `id` |
|
130
|
* property in the `medusa-config.ts`. |
|
131
|
* |
|
132
|
* @example |
|
133
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
134
|
* Options |
|
135
|
* > { |
|
136
|
* static identifier = "my-payment" |
|
137
|
* // ... |
|
138
|
* } |
|
139
|
*/ |
|
140
|
public static identifier: string |
|
141
|
|
|
142
|
/** |
|
143
|
* @ignore |
|
144
|
* |
|
145
|
* Return a unique identifier to retrieve the payment plugin provider |
|
146
|
*/ |
|
147
|
public getIdentifier(): string { |
|
148
|
const ctr = this.constructor as typeof AbstractPaymentProvider |
|
149
|
|
|
150
|
if (!ctr.identifier) { |
|
151
|
throw new Error(`Missing static property "identifier".`) |
|
152
|
} |
|
153
|
|
|
154
|
return ctr.identifier |
|
155
|
} |
|
156
|
|
|
157
|
/** |
|
158
|
* This method captures a payment using the third-party provider. In this method, use the third-party provider to capture the payment. |
|
159
|
* |
|
160
|
* When an order is placed, the payment is authorized using the {@link authorizePayment} method. Then, the admin |
|
161
|
* user can capture the payment, which triggers this method. |
|
162
|
* |
|
163
|
*  |
|
164
|
* |
|
165
|
* This method can also be triggered by a webhook event if the {@link getWebhookActionAndData} method returns the action `captured`. |
|
166
|
* |
|
167
|
* #### Understanding `data` property |
|
168
|
* |
|
169
|
* The `data` property of the input parameter contains data that was previously stored in the Payment record's `data` property, which was |
|
170
|
* returned by the {@link authorizePayment} method. |
|
171
|
* |
|
172
|
* The `data` property returned by this method is then stored in the `Payment` record. You can store data relevant to later refund or process the payment. |
|
173
|
* For example, you can store the ID of the payment in the third-party provider to reference it later. |
|
174
|
* |
|
175
|
*  |
|
176
|
* |
|
177
|
* @param input - The input to capture the payment. The `data` field should contain the data from the payment provider. when the payment was created. |
|
178
|
* @returns The new data to store in the payment's `data` property. Throws in case of an error. |
|
179
|
* |
|
180
|
* @example |
|
181
|
* // other imports... |
|
182
|
* import { |
|
183
|
* CapturePaymentInput, |
|
184
|
* CapturePaymentOutput, |
|
185
|
* } from "@medusajs/framework/types" |
|
186
|
* |
|
187
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
188
|
* Options |
|
189
|
* > { |
|
190
|
* async capturePayment( |
|
191
|
* input: CapturePaymentInput |
|
192
|
* ): Promise<CapturePaymentOutput> { |
|
193
|
* const externalId = input.data?.id |
|
194
|
* |
|
195
|
* // assuming you have a client that captures the payment |
|
196
|
* const newData = await this.client.capturePayment(externalId) |
|
197
|
* return { |
|
198
|
* data: { |
|
199
|
* ...newData, |
|
200
|
* id: externalId, |
|
201
|
* } |
|
202
|
* } |
|
203
|
* } |
|
204
|
* // ... |
|
205
|
* } |
|
206
|
*/ |
|
207
|
abstract capturePayment( |
|
208
|
input: CapturePaymentInput |
|
209
|
): Promise<CapturePaymentOutput> |
|
210
|
|
|
211
|
/** |
|
212
|
* This method authorizes a payment session using the third-party payment provider. |
|
213
|
* |
|
214
|
* During checkout, the customer may need to perform actions required by the payment provider, |
|
215
|
* such as entering their card details or confirming the payment. Once that is done, |
|
216
|
* the customer can place their order. |
|
217
|
* |
|
218
|
* During cart-completion before placing the order, this method is used to authorize the cart's payment session with the |
|
219
|
* third-party payment provider. The payment can later be captured |
|
220
|
* using the {@link capturePayment} method. |
|
221
|
* |
|
222
|
*  |
|
223
|
* |
|
224
|
* When authorized successfully, a `Payment` is created by the Payment |
|
225
|
* Module, and it's associated with the order. |
|
226
|
* |
|
227
|
* #### Deferred / Asynchronous Authorization |
|
228
|
* |
|
229
|
* For payment methods where authorization does not happen immediately (e.g., bank transfers, |
|
230
|
* payment links, vouchers like OXXO or boleto), return `status: "pending_authorization"` instead |
|
231
|
* of `"authorized"`. This signals that: |
|
232
|
* |
|
233
|
* - No `Payment` record will be created yet. |
|
234
|
* - The cart can still be completed and an order will be created with an "awaiting" payment status. |
|
235
|
* - This method may be called again later (via a webhook or manual admin action) to check if the |
|
236
|
* payment has been completed. When the payment is confirmed, return `"authorized"` or `"captured"`. |
|
237
|
* |
|
238
|
* #### Understanding `data` property |
|
239
|
* |
|
240
|
* The `data` property of the method's parameter contains the `PaymentSession` record's `data` property, which was |
|
241
|
* returned by the {@link initiatePayment} method. |
|
242
|
* |
|
243
|
* The `data` property returned by this method is then stored in the created `Payment` record. You can store data relevant to later capture or process the payment. |
|
244
|
* For example, you can store the ID of the payment in the third-party provider to reference it later. |
|
245
|
* |
|
246
|
*  |
|
247
|
* |
|
248
|
* @param input - The input to authorize the payment. The `data` field should contain the data from the payment provider. when the payment was created. |
|
249
|
* @returns The status of the authorization, along with the `data` field about the payment. Return `"pending_authorization"` for async payment methods. Throws in case of an error. |
|
250
|
* |
|
251
|
* @example |
|
252
|
* // other imports... |
|
253
|
* import { |
|
254
|
* AuthorizePaymentInput, |
|
255
|
* AuthorizePaymentOutput, |
|
256
|
* PaymentSessionStatus |
|
257
|
* } from "@medusajs/framework/types" |
|
258
|
* |
|
259
|
* |
|
260
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
261
|
* Options |
|
262
|
* > { |
|
263
|
* async authorizePayment( |
|
264
|
* input: AuthorizePaymentInput |
|
265
|
* ): Promise<AuthorizePaymentOutput> { |
|
266
|
* const externalId = input.data?.id |
|
267
|
* |
|
268
|
* // assuming you have a client that authorizes the payment |
|
269
|
* const paymentData = await this.client.authorizePayment(externalId) |
|
270
|
* |
|
271
|
* return { |
|
272
|
* data: paymentData, |
|
273
|
* status: "authorized" |
|
274
|
* } |
|
275
|
* } |
|
276
|
* |
|
277
|
* // ... |
|
278
|
* } |
|
279
|
*/ |
|
280
|
abstract authorizePayment( |
|
281
|
input: AuthorizePaymentInput |
|
282
|
): Promise<AuthorizePaymentOutput> |
|
283
|
|
|
284
|
/** |
|
285
|
* This method cancels a payment in the third-party payment provider. It's used when |
|
286
|
* the admin user cancels an order. The order can only be canceled if the payment |
|
287
|
* is not captured yet. |
|
288
|
* |
|
289
|
* #### Understanding `data` property |
|
290
|
* |
|
291
|
* The `data` property of the method's parameter contains the `Payment` record's `data` property, which was |
|
292
|
* returned by the {@link authorizePayment} method. |
|
293
|
* |
|
294
|
* The `data` property returned by this method is then stored in the `Payment` record. You can store data relevant for any further processing of the payment. |
|
295
|
* |
|
296
|
*  |
|
297
|
* |
|
298
|
* @param input - The input to cancel the payment. The `data` field should contain the data from the payment provider. when the payment was created. |
|
299
|
* @returns The new data to store in the payment's `data` property, if any. Throws in case of an error. |
|
300
|
* |
|
301
|
* @example |
|
302
|
* // other imports... |
|
303
|
* import { |
|
304
|
* CancelPaymentInput, |
|
305
|
* CancelPaymentOutput, |
|
306
|
* } from "@medusajs/framework/types" |
|
307
|
* |
|
308
|
* |
|
309
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
310
|
* Options |
|
311
|
* > { |
|
312
|
* async cancelPayment( |
|
313
|
* input: CancelPaymentInput |
|
314
|
* ): Promise<CancelPaymentOutput> { |
|
315
|
* const externalId = input.data?.id |
|
316
|
* |
|
317
|
* // assuming you have a client that cancels the payment |
|
318
|
* const paymentData = await this.client.cancelPayment(externalId) |
|
319
|
* return { data: paymentData } |
|
320
|
* } |
|
321
|
* |
|
322
|
* // ... |
|
323
|
* } |
|
324
|
*/ |
|
325
|
abstract cancelPayment( |
|
326
|
input: CancelPaymentInput |
|
327
|
): Promise<CancelPaymentOutput> |
|
328
|
|
|
329
|
/** |
|
330
|
* This method initializes a payment session with the third-party payment provider. |
|
331
|
* |
|
332
|
* When a customer chooses a payment method during checkout, this method is triggered to |
|
333
|
* perform any initialization action with the third-party provider, such as creating a payment session. |
|
334
|
* |
|
335
|
*  |
|
336
|
* |
|
337
|
* #### Understanding `data` property |
|
338
|
* |
|
339
|
* The `data` property returned by this method will be stored in the created `PaymentSession` record. You can store data relevant to later authorize or process the payment. |
|
340
|
* For example, you can store the ID of the payment session in the third-party provider to reference it later. |
|
341
|
* |
|
342
|
* The `data` property is also available to storefronts, allowing you to store data necessary for the storefront to integrate |
|
343
|
* the payment provider in the checkout flow. For example, you can store the client token to use with the payment provider's SDK. |
|
344
|
* |
|
345
|
* :::note |
|
346
|
* |
|
347
|
* This also means you shouldn't store sensitive data and tokens in the `data` property, as it's publicly accessible. |
|
348
|
* |
|
349
|
* ::: |
|
350
|
* |
|
351
|
*  |
|
352
|
* |
|
353
|
* @param input - The input to create the payment session. |
|
354
|
* @returns The new data to store in the payment's `data` property. Throws in case of an error. |
|
355
|
* |
|
356
|
* @example |
|
357
|
* // other imports... |
|
358
|
* import { |
|
359
|
* InitiatePaymentInput, |
|
360
|
* InitiatePaymentOutput, |
|
361
|
* } from "@medusajs/framework/types" |
|
362
|
* |
|
363
|
* |
|
364
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
365
|
* Options |
|
366
|
* > { |
|
367
|
* async initiatePayment( |
|
368
|
* input: InitiatePaymentInput |
|
369
|
* ): Promise<InitiatePaymentOutput> { |
|
370
|
* const { |
|
371
|
* amount, |
|
372
|
* currency_code, |
|
373
|
* context: customerDetails |
|
374
|
* } = input |
|
375
|
* |
|
376
|
* // assuming you have a client that initializes the payment |
|
377
|
* const response = await this.client.init( |
|
378
|
* amount, currency_code, customerDetails |
|
379
|
* ) |
|
380
|
* |
|
381
|
* return { |
|
382
|
* id: response.id, |
|
383
|
* data: response, |
|
384
|
* } |
|
385
|
* } |
|
386
|
* |
|
387
|
* // ... |
|
388
|
* } |
|
389
|
*/ |
|
390
|
abstract initiatePayment( |
|
391
|
input: InitiatePaymentInput |
|
392
|
): Promise<InitiatePaymentOutput> |
|
393
|
|
|
394
|
/** |
|
395
|
* This method deletes a payment session in the third-party payment provider. |
|
396
|
* |
|
397
|
* When a customer chooses a payment method during checkout, then chooses a different one, |
|
398
|
* this method is triggered to delete the previous payment session. |
|
399
|
* |
|
400
|
* If your provider doesn't support deleting a payment session, you can just return an empty object or |
|
401
|
* an object that contains the same received `data` property. |
|
402
|
* |
|
403
|
*  |
|
404
|
* |
|
405
|
* #### Understanding `data` property |
|
406
|
* |
|
407
|
* The `data` property of the method's parameter contains the `PaymentSession` record's `data` property, which was |
|
408
|
* returned by the {@link initiatePayment} method. |
|
409
|
* |
|
410
|
*  |
|
411
|
* |
|
412
|
* @param input - The input to delete the payment session. The `data` field should contain the data from the payment provider. when the payment was created. |
|
413
|
* @returns The new data to store in the payment's `data` property, if any. Throws in case of an error. |
|
414
|
* |
|
415
|
* @example |
|
416
|
* // other imports... |
|
417
|
* import { |
|
418
|
* DeletePaymentInput, |
|
419
|
* DeletePaymentOutput, |
|
420
|
* } from "@medusajs/framework/types" |
|
421
|
* |
|
422
|
* |
|
423
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
424
|
* Options |
|
425
|
* > { |
|
426
|
* async deletePayment( |
|
427
|
* input: DeletePaymentInput |
|
428
|
* ): Promise<DeletePaymentOutput> { |
|
429
|
* const externalId = input.data?.id |
|
430
|
* |
|
431
|
* // assuming you have a client that cancels the payment |
|
432
|
* await this.client.cancelPayment(externalId) |
|
433
|
* return { |
|
434
|
* data: input.data |
|
435
|
* } |
|
436
|
* } |
|
437
|
* |
|
438
|
* // ... |
|
439
|
* } |
|
440
|
*/ |
|
441
|
abstract deletePayment( |
|
442
|
input: DeletePaymentInput |
|
443
|
): Promise<DeletePaymentOutput> |
|
444
|
|
|
445
|
/** |
|
446
|
* This method gets the status of a payment session based on the status in the third-party integration. |
|
447
|
* |
|
448
|
* @param input - The input to get the payment status. The `data` field should contain the data from the payment provider. when the payment was created. |
|
449
|
* @returns The payment session's status. It can also return additional `data` from the payment provider. |
|
450
|
* |
|
451
|
* @example |
|
452
|
* // other imports... |
|
453
|
* import { |
|
454
|
* GetPaymentStatusInput, |
|
455
|
* GetPaymentStatusOutput, |
|
456
|
* PaymentSessionStatus |
|
457
|
* } from "@medusajs/framework/types" |
|
458
|
* |
|
459
|
* |
|
460
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
461
|
* Options |
|
462
|
* > { |
|
463
|
* async getPaymentStatus( |
|
464
|
* input: GetPaymentStatusInput |
|
465
|
* ): Promise<GetPaymentStatusOutput> { |
|
466
|
* const externalId = input.data?.id |
|
467
|
* |
|
468
|
* // assuming you have a client that retrieves the payment status |
|
469
|
* const status = await this.client.getStatus(externalId) |
|
470
|
* |
|
471
|
* switch (status) { |
|
472
|
* case "requires_capture": |
|
473
|
* return {status: "authorized"} |
|
474
|
* case "success": |
|
475
|
* return {status: "captured"} |
|
476
|
* case "canceled": |
|
477
|
* return {status: "canceled"} |
|
478
|
* default: |
|
479
|
* return {status: "pending"} |
|
480
|
* } |
|
481
|
* } |
|
482
|
* |
|
483
|
* // ... |
|
484
|
* } |
|
485
|
*/ |
|
486
|
abstract getPaymentStatus( |
|
487
|
input: GetPaymentStatusInput |
|
488
|
): Promise<GetPaymentStatusOutput> |
|
489
|
|
|
490
|
/** |
|
491
|
* This method refunds an amount using the third-party payment provider. This method |
|
492
|
* is triggered when the admin user refunds a payment of an order. |
|
493
|
* |
|
494
|
* #### Understanding `data` property |
|
495
|
* |
|
496
|
* The `data` property of the method's parameter contains the `Payment` record's `data` property, which was |
|
497
|
* returned by the {@link capturePayment} or {@link refundPayment} method. |
|
498
|
* |
|
499
|
* The `data` property returned by this method is then stored in the `Payment` record. You can store data relevant to later refund or process the payment. |
|
500
|
* For example, you can store the ID of the payment in the third-party provider to reference it later. |
|
501
|
* |
|
502
|
* :::note |
|
503
|
* |
|
504
|
* A payment may be refunded multiple times with different amounts. In this case, the `data` property |
|
505
|
* of the input parameter contains the data from the last refund. |
|
506
|
* |
|
507
|
* ::: |
|
508
|
* |
|
509
|
*  |
|
510
|
* |
|
511
|
* @param input - The input to refund the payment. The `data` field should contain the data from the payment provider. when the payment was created. |
|
512
|
* @returns The new data to store in the payment's `data` property, or an error object. |
|
513
|
* |
|
514
|
* @example |
|
515
|
* // other imports... |
|
516
|
* import { |
|
517
|
* RefundPaymentInput, |
|
518
|
* RefundPaymentOutput, |
|
519
|
* } from "@medusajs/framework/types" |
|
520
|
* |
|
521
|
* |
|
522
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
523
|
* Options |
|
524
|
* > { |
|
525
|
* async refundPayment( |
|
526
|
* input: RefundPaymentInput |
|
527
|
* ): Promise<RefundPaymentOutput> { |
|
528
|
* const externalId = input.data?.id |
|
529
|
* |
|
530
|
* // assuming you have a client that refunds the payment |
|
531
|
* const newData = await this.client.refund( |
|
532
|
* externalId, |
|
533
|
* input.amount |
|
534
|
* ) |
|
535
|
* |
|
536
|
* return { |
|
537
|
* data: input.data, |
|
538
|
* } |
|
539
|
* } |
|
540
|
* // ... |
|
541
|
* } |
|
542
|
*/ |
|
543
|
abstract refundPayment( |
|
544
|
input: RefundPaymentInput |
|
545
|
): Promise<RefundPaymentOutput> |
|
546
|
|
|
547
|
/** |
|
548
|
* This method retrieves the payment's data from the third-party payment provider. |
|
549
|
* |
|
550
|
* @param input - The input to retrieve the payment. The `data` field should contain the data from the payment provider when the payment was created. |
|
551
|
* @returns The payment's data as found in the payment provider. |
|
552
|
* |
|
553
|
* @example |
|
554
|
* // other imports... |
|
555
|
* import { |
|
556
|
* RetrievePaymentInput, |
|
557
|
* RetrievePaymentOutput, |
|
558
|
* } from "@medusajs/framework/types" |
|
559
|
* |
|
560
|
* |
|
561
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
562
|
* Options |
|
563
|
* > { |
|
564
|
* async retrievePayment( |
|
565
|
* input: RetrievePaymentInput |
|
566
|
* ): Promise<RetrievePaymentOutput> { |
|
567
|
* const externalId = input.data?.id |
|
568
|
* |
|
569
|
* // assuming you have a client that retrieves the payment |
|
570
|
* return await this.client.retrieve(externalId) |
|
571
|
* } |
|
572
|
* // ... |
|
573
|
* } |
|
574
|
*/ |
|
575
|
abstract retrievePayment( |
|
576
|
input: RetrievePaymentInput |
|
577
|
): Promise<RetrievePaymentOutput> |
|
578
|
|
|
579
|
/** |
|
580
|
* This method updates a payment in the third-party service that was previously initiated with the {@link initiatePayment} method. |
|
581
|
* |
|
582
|
* @param input - The input to update the payment. The `data` field should contain the data from the payment provider. when the payment was created. |
|
583
|
* @returns The new data to store in the payment's `data` property. Throws in case of an error. |
|
584
|
* |
|
585
|
* @example |
|
586
|
* // other imports... |
|
587
|
* import { |
|
588
|
* UpdatePaymentInput, |
|
589
|
* UpdatePaymentOutput, |
|
590
|
* } from "@medusajs/framework/types" |
|
591
|
* |
|
592
|
* |
|
593
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
594
|
* Options |
|
595
|
* > { |
|
596
|
* async updatePayment( |
|
597
|
* input: UpdatePaymentInput |
|
598
|
* ): Promise<UpdatePaymentOutput> { |
|
599
|
* const { amount, currency_code, context } = input |
|
600
|
* const externalId = input.data?.id |
|
601
|
* |
|
602
|
* // Validate context.customer |
|
603
|
* if (!context || !context.customer) { |
|
604
|
* throw new Error("Context must include a valid customer."); |
|
605
|
* } |
|
606
|
* |
|
607
|
* // assuming you have a client that updates the payment |
|
608
|
* const response = await this.client.update( |
|
609
|
* externalId, |
|
610
|
* { |
|
611
|
* amount, |
|
612
|
* currency_code, |
|
613
|
* customer: context.customer |
|
614
|
* } |
|
615
|
* ) |
|
616
|
* |
|
617
|
* return response |
|
618
|
* } |
|
619
|
* |
|
620
|
* // ... |
|
621
|
* } |
|
622
|
*/ |
|
623
|
abstract updatePayment( |
|
624
|
input: UpdatePaymentInput |
|
625
|
): Promise<UpdatePaymentOutput> |
|
626
|
|
|
627
|
/** |
|
628
|
* This method is executed when a webhook event is received from the third-party payment provider. Medusa uses |
|
629
|
* the data returned by this method to perform actions in the Medusa application, such as completing the associated cart |
|
630
|
* if the payment was authorized successfully. |
|
631
|
* |
|
632
|
* Learn more in the [Webhook Events](https://docs.medusajs.com/resources/commerce-modules/payment/webhook-events) documentation. |
|
633
|
* |
|
634
|
* @param data - The webhook event's data |
|
635
|
* @returns The webhook result. If the `action`'s value is `captured`, the payment is captured within Medusa as well. |
|
636
|
* If the `action`'s value is `authorized`, the associated payment session is authorized within Medusa and the associated cart |
|
637
|
* will be completed to create an order. |
|
638
|
* |
|
639
|
* @example |
|
640
|
* // other imports... |
|
641
|
* import { |
|
642
|
* BigNumber |
|
643
|
* } from "@medusajs/framework/utils" |
|
644
|
* import { |
|
645
|
* ProviderWebhookPayload, |
|
646
|
* WebhookActionResult |
|
647
|
* } from "@medusajs/framework/types" |
|
648
|
* |
|
649
|
* |
|
650
|
* class MyPaymentProviderService extends AbstractPaymentProvider< |
|
651
|
* Options |
|
652
|
* > { |
|
653
|
* async getWebhookActionAndData( |
|
654
|
* payload: ProviderWebhookPayload["payload"] |
|
655
|
* ): Promise<WebhookActionResult> { |
|
656
|
* const { |
|
657
|
* data, |
|
658
|
* rawData, |
|
659
|
* headers |
|
660
|
* } = payload |
|
661
|
* |
|
662
|
* try { |
|
663
|
* switch(data.event_type) { |
|
664
|
* case "authorized_amount": |
|
665
|
* return { |
|
666
|
* action: "authorized", |
|
667
|
* data: { |
|
668
|
* // assuming the session_id is stored in the metadata of the payment |
|
669
|
* // in the third-party provider |
|
670
|
* session_id: (data.metadata as Record<string, any>).session_id, |
|
671
|
* amount: new BigNumber(data.amount as number) |
|
672
|
* } |
|
673
|
* } |
|
674
|
* case "success": |
|
675
|
* return { |
|
676
|
* action: "captured", |
|
677
|
* data: { |
|
678
|
* // assuming the session_id is stored in the metadata of the payment |
|
679
|
* // in the third-party provider |
|
680
|
* session_id: (data.metadata as Record<string, any>).session_id, |
|
681
|
* amount: new BigNumber(data.amount as number) |
|
682
|
* } |
|
683
|
* } |
|
684
|
* default: |
|
685
|
* return { |
|
686
|
* action: "not_supported", |
|
687
|
* data: { |
|
688
|
* session_id: "", |
|
689
|
* amount: new BigNumber(0) |
|
690
|
* } |
|
691
|
* } |
|
692
|
* } |
|
693
|
* } catch (e) { |
|
694
|
* return { |
|
695
|
* action: "failed", |
|
696
|
* data: { |
|
697
|
* // assuming the session_id is stored in the metadata of the payment |
|
698
|
* // in the third-party provider |
|
699
|
* session_id: (data.metadata as Record<string, any>).session_id, |
|
700
|
* amount: new BigNumber(data.amount as number) |
|
701
|
* } |
|
702
|
* } |
|
703
|
* } |
|
704
|
* } |
|
705
|
* |
|
706
|
* // ... |
|
707
|
* } |
|
708
|
*/ |
|
709
|
abstract getWebhookActionAndData( |
|
710
|
data: ProviderWebhookPayload["payload"] |
|
711
|
): Promise<WebhookActionResult> |
|
712
|
} |