1 import crypto from "crypto"
2 import {
3 AuthorizePaymentInput,
4 AuthorizePaymentOutput,
5 CancelPaymentInput,
6 CancelPaymentOutput,
7 CapturePaymentInput,
8 CapturePaymentOutput,
9 DeletePaymentInput,
10 DeletePaymentOutput,
11 GetPaymentStatusInput,
12 GetPaymentStatusOutput,
13 InitiatePaymentInput,
14 InitiatePaymentOutput,
15 ProviderWebhookPayload,
16 RefundPaymentInput,
17 RefundPaymentOutput,
18 RetrievePaymentInput,
19 RetrievePaymentOutput,
20 UpdatePaymentInput,
21 UpdatePaymentOutput,
22 WebhookActionResult,
23 } from "@medusajs/framework/types"
24 import {
25 AbstractPaymentProvider,
26 PaymentActions,
27 PaymentSessionStatus,
28 } from "@medusajs/framework/utils"
29
30 /**
31 * A test payment provider that simulates async payment methods.
32 *
33 * - First `authorizePayment` call returns `pending_authorization`
34 * - Subsequent calls return `authorized` if `data.payment_received` is truthy
35 * (simulating the payment arriving), otherwise stays `pending_authorization`
36 */
37 export class PendingAuthorizationPaymentProvider extends AbstractPaymentProvider {
38 static identifier = "pending-auth"
39
40 constructor(cradle: Record<string, unknown>, config = {}) {
41 // @ts-ignore - AbstractPaymentProvider has protected constructor
42 super(cradle, config)
43 }
44
45 async initiatePayment(
46 input: InitiatePaymentInput
47 ): Promise<InitiatePaymentOutput> {
48 return { data: {}, id: crypto.randomUUID() }
49 }
50
51 async authorizePayment(
52 input: AuthorizePaymentInput
53 ): Promise<AuthorizePaymentOutput> {
54 if (input.data?.payment_received) {
55 return {
56 data: { ...input.data, authorized: true },
57 status: PaymentSessionStatus.AUTHORIZED,
58 }
59 }
60
61 return {
62 data: { ...input.data },
63 status: PaymentSessionStatus.PENDING_AUTHORIZATION,
64 }
65 }
66
67 async capturePayment(
68 input: CapturePaymentInput
69 ): Promise<CapturePaymentOutput> {
70 return { data: {} }
71 }
72
73 async refundPayment(
74 input: RefundPaymentInput
75 ): Promise<RefundPaymentOutput> {
76 return { data: {} }
77 }
78
79 async cancelPayment(
80 input: CancelPaymentInput
81 ): Promise<CancelPaymentOutput> {
82 return { data: {} }
83 }
84
85 async deletePayment(
86 input: DeletePaymentInput
87 ): Promise<DeletePaymentOutput> {
88 return { data: {} }
89 }
90
91 async getPaymentStatus(
92 input: GetPaymentStatusInput
93 ): Promise<GetPaymentStatusOutput> {
94 if (input.data?.authorized) {
95 return { status: PaymentSessionStatus.AUTHORIZED }
96 }
97 return { status: PaymentSessionStatus.PENDING_AUTHORIZATION }
98 }
99
100 async retrievePayment(
101 input: RetrievePaymentInput
102 ): Promise<RetrievePaymentOutput> {
103 return {}
104 }
105
106 async updatePayment(
107 input: UpdatePaymentInput
108 ): Promise<UpdatePaymentOutput> {
109 return { data: input.data ?? {} }
110 }
111
112 async getWebhookActionAndData(
113 data: ProviderWebhookPayload["payload"]
114 ): Promise<WebhookActionResult> {
115 const body = data.data as Record<string, unknown>
116
117 if (!body?.action || !body?.session_id) {
118 return { action: PaymentActions.NOT_SUPPORTED }
119 }
120
121 return {
122 action: body.action as PaymentActions,
123 data: {
124 session_id: body.session_id as string,
125 amount: body.amount as number,
126 },
127 }
128 }
129 }
130
131 export default PendingAuthorizationPaymentProvider