|
1
|
import { asFunction, asValue, Lifetime } from "@medusajs/framework/awilix" |
|
2
|
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk" |
|
3
|
import { |
|
4
|
CreatePaymentProviderDTO, |
|
5
|
LoaderOptions, |
|
6
|
ModuleProvider, |
|
7
|
ModulesSdkTypes, |
|
8
|
} from "@medusajs/framework/types" |
|
9
|
import { MedusaError } from "@medusajs/framework/utils" |
|
10
|
|
|
11
|
import { PaymentProviderService } from "@services" |
|
12
|
import * as providers from "../providers" |
|
13
|
|
|
14
|
const PROVIDER_REGISTRATION_KEY = "payment_providers" |
|
15
|
|
|
16
|
const registrationFn = async (klass, container, pluginOptions) => { |
|
17
|
if (!klass?.identifier) { |
|
18
|
throw new MedusaError( |
|
19
|
MedusaError.Types.INVALID_ARGUMENT, |
|
20
|
`Trying to register a payment provider without a provider identifier.` |
|
21
|
) |
|
22
|
} |
|
23
|
|
|
24
|
const key = `pp_${klass.identifier}${ |
|
25
|
pluginOptions.id ? `_${pluginOptions.id}` : "" |
|
26
|
}` |
|
27
|
|
|
28
|
container.register({ |
|
29
|
[key]: asFunction((cradle) => new klass(cradle, pluginOptions.options), { |
|
30
|
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, |
|
31
|
}), |
|
32
|
}) |
|
33
|
|
|
34
|
container.registerAdd(PROVIDER_REGISTRATION_KEY, asValue(key)) |
|
35
|
} |
|
36
|
|
|
37
|
export default async ({ |
|
38
|
container, |
|
39
|
options, |
|
40
|
}: LoaderOptions< |
|
41
|
( |
|
42
|
| ModulesSdkTypes.ModuleServiceInitializeOptions |
|
43
|
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions |
|
44
|
) & { |
|
45
|
providers: ModuleProvider[] |
|
46
|
cloud: { |
|
47
|
api_key?: string |
|
48
|
endpoint?: string |
|
49
|
environment_handle?: string |
|
50
|
sandbox_handle?: string |
|
51
|
webhook_secret?: string |
|
52
|
payment_accounts?: string[] |
|
53
|
} |
|
54
|
} |
|
55
|
>): Promise<void> => { |
|
56
|
await registrationFn(providers.SystemPaymentProvider, container, { |
|
57
|
id: "default", |
|
58
|
}) |
|
59
|
|
|
60
|
// We only want to register medusa payments if the options for it have been provided. |
|
61
|
const { |
|
62
|
api_key, |
|
63
|
endpoint, |
|
64
|
environment_handle, |
|
65
|
sandbox_handle, |
|
66
|
webhook_secret, |
|
67
|
payment_accounts, |
|
68
|
} = options?.cloud ?? {} |
|
69
|
|
|
70
|
if ( |
|
71
|
api_key && |
|
72
|
endpoint && |
|
73
|
webhook_secret && |
|
74
|
(environment_handle || sandbox_handle) |
|
75
|
) { |
|
76
|
// Each Medusa Payments account (eg. one per region) gets its own provider, so it can be |
|
77
|
// assigned to regions and receives the webhooks for that account. |
|
78
|
for (const payment_account of payment_accounts ?? []) { |
|
79
|
await registrationFn(providers.MedusaPaymentsProvider, container, { |
|
80
|
options: { |
|
81
|
api_key, |
|
82
|
endpoint, |
|
83
|
environment_handle, |
|
84
|
sandbox_handle, |
|
85
|
webhook_secret, |
|
86
|
payment_account, |
|
87
|
}, |
|
88
|
id: payment_account, |
|
89
|
}) |
|
90
|
} |
|
91
|
} |
|
92
|
|
|
93
|
await moduleProviderLoader({ |
|
94
|
container, |
|
95
|
providers: options?.providers || [], |
|
96
|
registerServiceFn: registrationFn, |
|
97
|
}) |
|
98
|
|
|
99
|
await registerProvidersInDb({ container }) |
|
100
|
} |
|
101
|
|
|
102
|
const registerProvidersInDb = async ({ |
|
103
|
container, |
|
104
|
}: LoaderOptions): Promise<void> => { |
|
105
|
const providersToLoad = container.resolve<string[]>(PROVIDER_REGISTRATION_KEY) |
|
106
|
const paymentProviderService = container.resolve<PaymentProviderService>( |
|
107
|
"paymentProviderService" |
|
108
|
) |
|
109
|
|
|
110
|
const existingProviders = await paymentProviderService.list( |
|
111
|
{ id: providersToLoad }, |
|
112
|
{} |
|
113
|
) |
|
114
|
|
|
115
|
const upsertData: CreatePaymentProviderDTO[] = [] |
|
116
|
|
|
117
|
for (const { id } of existingProviders) { |
|
118
|
if (!providersToLoad.includes(id)) { |
|
119
|
upsertData.push({ id, is_enabled: false }) |
|
120
|
} |
|
121
|
} |
|
122
|
|
|
123
|
for (const id of providersToLoad) { |
|
124
|
upsertData.push({ id, is_enabled: true }) |
|
125
|
} |
|
126
|
|
|
127
|
await paymentProviderService.upsert(upsertData) |
|
128
|
} |