|
1
|
import { |
|
2
|
AuthorizePaymentInput, |
|
3
|
AuthorizePaymentOutput, |
|
4
|
CancelPaymentInput, |
|
5
|
CancelPaymentOutput, |
|
6
|
CapturePaymentInput, |
|
7
|
CapturePaymentOutput, |
|
8
|
RetrieveAccountHolderInput, |
|
9
|
RetrieveAccountHolderOutput, |
|
10
|
CreateAccountHolderInput, |
|
11
|
CreateAccountHolderOutput, |
|
12
|
DAL, |
|
13
|
DeleteAccountHolderInput, |
|
14
|
DeleteAccountHolderOutput, |
|
15
|
DeletePaymentMethodInput, |
|
16
|
DeletePaymentMethodOutput, |
|
17
|
DeletePaymentInput, |
|
18
|
DeletePaymentOutput, |
|
19
|
GetPaymentStatusInput, |
|
20
|
GetPaymentStatusOutput, |
|
21
|
InitiatePaymentInput, |
|
22
|
InitiatePaymentOutput, |
|
23
|
IPaymentProvider, |
|
24
|
ListPaymentMethodsInput, |
|
25
|
ListPaymentMethodsOutput, |
|
26
|
Logger, |
|
27
|
ProviderWebhookPayload, |
|
28
|
RefundPaymentInput, |
|
29
|
RefundPaymentOutput, |
|
30
|
SavePaymentMethodInput, |
|
31
|
SavePaymentMethodOutput, |
|
32
|
UpdateAccountHolderInput, |
|
33
|
UpdateAccountHolderOutput, |
|
34
|
UpdatePaymentInput, |
|
35
|
UpdatePaymentOutput, |
|
36
|
WebhookActionResult, |
|
37
|
} from "@medusajs/framework/types" |
|
38
|
import { ModulesSdkUtils } from "@medusajs/framework/utils" |
|
39
|
import { PaymentProvider } from "@models" |
|
40
|
|
|
41
|
type InjectedDependencies = { |
|
42
|
logger?: Logger |
|
43
|
paymentProviderRepository: DAL.RepositoryService |
|
44
|
[key: `pp_${string}`]: IPaymentProvider |
|
45
|
} |
|
46
|
|
|
47
|
export default class PaymentProviderService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>( |
|
48
|
PaymentProvider |
|
49
|
) { |
|
50
|
#logger: Logger |
|
51
|
|
|
52
|
constructor(container: InjectedDependencies) { |
|
53
|
super(container) |
|
54
|
this.#logger = container["logger"] |
|
55
|
? container.logger |
|
56
|
: (console as unknown as Logger) |
|
57
|
} |
|
58
|
|
|
59
|
retrieveProvider(providerId: string): IPaymentProvider { |
|
60
|
try { |
|
61
|
return this.__container__[providerId] as IPaymentProvider |
|
62
|
} catch (err) { |
|
63
|
if (err.name === "AwilixResolutionError") { |
|
64
|
const errMessage = ` |
|
65
|
Unable to retrieve the payment provider with id: ${providerId} |
|
66
|
Please make sure that the provider is registered in the container and it is configured correctly in your project configuration file.` |
|
67
|
|
|
68
|
// Log full error for debugging |
|
69
|
this.#logger.error(`AwilixResolutionError: ${err.message}`, err) |
|
70
|
|
|
71
|
throw new Error(errMessage) |
|
72
|
} |
|
73
|
|
|
74
|
const errMessage = `Unable to retrieve the payment provider with id: ${providerId}, the following error occurred: ${err.message}` |
|
75
|
this.#logger.error(errMessage) |
|
76
|
|
|
77
|
throw new Error(errMessage) |
|
78
|
} |
|
79
|
} |
|
80
|
|
|
81
|
async createSession( |
|
82
|
providerId: string, |
|
83
|
sessionInput: InitiatePaymentInput |
|
84
|
): Promise<InitiatePaymentOutput> { |
|
85
|
const provider = this.retrieveProvider(providerId) |
|
86
|
|
|
87
|
return await provider.initiatePayment(sessionInput) |
|
88
|
} |
|
89
|
|
|
90
|
async updateSession( |
|
91
|
providerId: string, |
|
92
|
sessionInput: UpdatePaymentInput |
|
93
|
): Promise<UpdatePaymentOutput> { |
|
94
|
const provider = this.retrieveProvider(providerId) |
|
95
|
|
|
96
|
return await provider.updatePayment(sessionInput) |
|
97
|
} |
|
98
|
|
|
99
|
async deleteSession( |
|
100
|
providerId: string, |
|
101
|
input: DeletePaymentInput |
|
102
|
): Promise<DeletePaymentOutput> { |
|
103
|
const provider = this.retrieveProvider(providerId) |
|
104
|
return await provider.deletePayment(input) |
|
105
|
} |
|
106
|
|
|
107
|
async authorizePayment( |
|
108
|
providerId: string, |
|
109
|
input: AuthorizePaymentInput |
|
110
|
): Promise<AuthorizePaymentOutput> { |
|
111
|
const provider = this.retrieveProvider(providerId) |
|
112
|
return await provider.authorizePayment(input) |
|
113
|
} |
|
114
|
|
|
115
|
async getStatus( |
|
116
|
providerId: string, |
|
117
|
input: GetPaymentStatusInput |
|
118
|
): Promise<GetPaymentStatusOutput> { |
|
119
|
const provider = this.retrieveProvider(providerId) |
|
120
|
return await provider.getPaymentStatus(input) |
|
121
|
} |
|
122
|
|
|
123
|
async capturePayment( |
|
124
|
providerId: string, |
|
125
|
input: CapturePaymentInput |
|
126
|
): Promise<CapturePaymentOutput> { |
|
127
|
const provider = this.retrieveProvider(providerId) |
|
128
|
return await provider.capturePayment(input) |
|
129
|
} |
|
130
|
|
|
131
|
async cancelPayment( |
|
132
|
providerId: string, |
|
133
|
input: CancelPaymentInput |
|
134
|
): Promise<CancelPaymentOutput> { |
|
135
|
const provider = this.retrieveProvider(providerId) |
|
136
|
return await provider.cancelPayment(input) |
|
137
|
} |
|
138
|
|
|
139
|
async refundPayment( |
|
140
|
providerId: string, |
|
141
|
input: RefundPaymentInput |
|
142
|
): Promise<RefundPaymentOutput> { |
|
143
|
const provider = this.retrieveProvider(providerId) |
|
144
|
return await provider.refundPayment(input) |
|
145
|
} |
|
146
|
|
|
147
|
async retrieveAccountHolder( |
|
148
|
providerId: string, |
|
149
|
input: RetrieveAccountHolderInput |
|
150
|
): Promise<RetrieveAccountHolderOutput> { |
|
151
|
const provider = this.retrieveProvider(providerId) |
|
152
|
if (!provider.retrieveAccountHolder) { |
|
153
|
this.#logger.warn( |
|
154
|
`Provider ${providerId} does not support retrieving account holders` |
|
155
|
) |
|
156
|
return {} as unknown as RetrieveAccountHolderOutput |
|
157
|
} |
|
158
|
|
|
159
|
return await provider.retrieveAccountHolder(input) |
|
160
|
} |
|
161
|
|
|
162
|
async createAccountHolder( |
|
163
|
providerId: string, |
|
164
|
input: CreateAccountHolderInput |
|
165
|
): Promise<CreateAccountHolderOutput> { |
|
166
|
const provider = this.retrieveProvider(providerId) |
|
167
|
if (!provider.createAccountHolder) { |
|
168
|
this.#logger.warn( |
|
169
|
`Provider ${providerId} does not support creating account holders` |
|
170
|
) |
|
171
|
return {} as unknown as CreateAccountHolderOutput |
|
172
|
} |
|
173
|
|
|
174
|
return await provider.createAccountHolder(input) |
|
175
|
} |
|
176
|
|
|
177
|
async updateAccountHolder( |
|
178
|
providerId: string, |
|
179
|
input: UpdateAccountHolderInput |
|
180
|
): Promise<UpdateAccountHolderOutput> { |
|
181
|
const provider = this.retrieveProvider(providerId) |
|
182
|
if (!provider.updateAccountHolder) { |
|
183
|
this.#logger.warn( |
|
184
|
`Provider ${providerId} does not support updating account holders` |
|
185
|
) |
|
186
|
return {} as unknown as UpdateAccountHolderOutput |
|
187
|
} |
|
188
|
|
|
189
|
return await provider.updateAccountHolder(input) |
|
190
|
} |
|
191
|
|
|
192
|
async deleteAccountHolder( |
|
193
|
providerId: string, |
|
194
|
input: DeleteAccountHolderInput |
|
195
|
): Promise<DeleteAccountHolderOutput> { |
|
196
|
const provider = this.retrieveProvider(providerId) |
|
197
|
if (!provider.deleteAccountHolder) { |
|
198
|
this.#logger.warn( |
|
199
|
`Provider ${providerId} does not support deleting account holders` |
|
200
|
) |
|
201
|
return {} |
|
202
|
} |
|
203
|
|
|
204
|
return await provider.deleteAccountHolder(input) |
|
205
|
} |
|
206
|
|
|
207
|
async listPaymentMethods( |
|
208
|
providerId: string, |
|
209
|
input: ListPaymentMethodsInput |
|
210
|
): Promise<ListPaymentMethodsOutput> { |
|
211
|
const provider = this.retrieveProvider(providerId) |
|
212
|
if (!provider.listPaymentMethods) { |
|
213
|
this.#logger.warn( |
|
214
|
`Provider ${providerId} does not support listing payment methods` |
|
215
|
) |
|
216
|
return [] |
|
217
|
} |
|
218
|
|
|
219
|
return await provider.listPaymentMethods(input) |
|
220
|
} |
|
221
|
|
|
222
|
async savePaymentMethod( |
|
223
|
providerId: string, |
|
224
|
input: SavePaymentMethodInput |
|
225
|
): Promise<SavePaymentMethodOutput> { |
|
226
|
const provider = this.retrieveProvider(providerId) |
|
227
|
if (!provider.savePaymentMethod) { |
|
228
|
this.#logger.warn( |
|
229
|
`Provider ${providerId} does not support saving payment methods` |
|
230
|
) |
|
231
|
return {} as unknown as SavePaymentMethodOutput |
|
232
|
} |
|
233
|
|
|
234
|
return await provider.savePaymentMethod(input) |
|
235
|
} |
|
236
|
|
|
237
|
async deletePaymentMethod( |
|
238
|
providerId: string, |
|
239
|
input: DeletePaymentMethodInput |
|
240
|
): Promise<DeletePaymentMethodOutput> { |
|
241
|
const provider = this.retrieveProvider(providerId) |
|
242
|
if (!provider.deletePaymentMethod) { |
|
243
|
this.#logger.warn( |
|
244
|
`Provider ${providerId} does not support deleting payment methods` |
|
245
|
) |
|
246
|
return {} |
|
247
|
} |
|
248
|
|
|
249
|
return await provider.deletePaymentMethod(input) |
|
250
|
} |
|
251
|
|
|
252
|
async getWebhookActionAndData( |
|
253
|
providerId: string, |
|
254
|
data: ProviderWebhookPayload["payload"] |
|
255
|
): Promise<WebhookActionResult> { |
|
256
|
const provider = this.retrieveProvider(providerId) |
|
257
|
|
|
258
|
return await provider.getWebhookActionAndData(data) |
|
259
|
} |
|
260
|
} |