@unchainedshop/core-payment 3.0.0-alpha7 → 3.0.0-rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/db/PaymentCredentialsCollection.d.ts +10 -3
- package/lib/db/PaymentCredentialsCollection.d.ts.map +1 -1
- package/lib/db/PaymentCredentialsCollection.js +3 -3
- package/lib/db/PaymentCredentialsCollection.js.map +1 -1
- package/lib/db/PaymentProvidersCollection.d.ts +16 -2
- package/lib/db/PaymentProvidersCollection.d.ts.map +1 -1
- package/lib/db/PaymentProvidersCollection.js +6 -0
- package/lib/db/PaymentProvidersCollection.js.map +1 -1
- package/lib/director/PaymentAdapter.d.ts +1 -1
- package/lib/director/PaymentAdapter.d.ts.map +1 -1
- package/lib/director/PaymentAdapter.js.map +1 -1
- package/lib/director/PaymentDirector.d.ts +1 -1
- package/lib/director/PaymentDirector.d.ts.map +1 -1
- package/lib/director/PaymentDirector.js +5 -8
- package/lib/director/PaymentDirector.js.map +1 -1
- package/lib/director/PaymentPricingAdapter.d.ts +2 -4
- package/lib/director/PaymentPricingAdapter.d.ts.map +1 -1
- package/lib/director/PaymentPricingAdapter.js.map +1 -1
- package/lib/director/PaymentPricingDirector.d.ts +3 -4
- package/lib/director/PaymentPricingDirector.d.ts.map +1 -1
- package/lib/director/PaymentPricingDirector.js.map +1 -1
- package/lib/module/configurePaymentCredentialsModule.d.ts +11 -12
- package/lib/module/configurePaymentCredentialsModule.d.ts.map +1 -1
- package/lib/module/configurePaymentCredentialsModule.js +3 -3
- package/lib/module/configurePaymentCredentialsModule.js.map +1 -1
- package/lib/module/configurePaymentModule.d.ts +35 -10
- package/lib/module/configurePaymentModule.d.ts.map +1 -1
- package/lib/module/configurePaymentModule.js +2 -18
- package/lib/module/configurePaymentModule.js.map +1 -1
- package/lib/module/configurePaymentProvidersModule.d.ts +17 -41
- package/lib/module/configurePaymentProvidersModule.d.ts.map +1 -1
- package/lib/module/configurePaymentProvidersModule.js +0 -101
- package/lib/module/configurePaymentProvidersModule.js.map +1 -1
- package/lib/payment-index.d.ts +2 -8
- package/lib/payment-index.d.ts.map +1 -1
- package/lib/payment-index.js +2 -8
- package/lib/payment-index.js.map +1 -1
- package/lib/payment-settings.d.ts +6 -7
- package/lib/payment-settings.d.ts.map +1 -1
- package/lib/payment-settings.js.map +1 -1
- package/lib/types.d.ts +8 -22
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js.map +1 -1
- package/package.json +9 -12
- package/src/db/PaymentCredentialsCollection.ts +13 -5
- package/src/db/PaymentProvidersCollection.ts +19 -2
- package/src/module/configurePaymentCredentialsModule.ts +42 -40
- package/src/module/configurePaymentModule.ts +5 -57
- package/src/module/configurePaymentProvidersModule.ts +27 -239
- package/src/module/payment-index.test.ts +1 -1
- package/src/payment-index.ts +2 -10
- package/src/payment-settings.ts +6 -7
- package/tsconfig.json +5 -7
- package/src/director/PaymentAdapter.ts +0 -63
- package/src/director/PaymentDirector.ts +0 -99
- package/src/director/PaymentError.ts +0 -6
- package/src/director/PaymentPricingAdapter.ts +0 -87
- package/src/director/PaymentPricingDirector.ts +0 -101
- package/src/director/PaymentPricingSheet.ts +0 -87
- package/src/director/PaymentProviderType.ts +0 -5
- package/src/types.ts +0 -108
|
@@ -1,108 +1,12 @@
|
|
|
1
|
-
import { UnchainedCore } from '@unchainedshop/core';
|
|
2
|
-
import {
|
|
3
|
-
PaymentChargeActionResult,
|
|
4
|
-
PaymentContext,
|
|
5
|
-
PaymentCredentials,
|
|
6
|
-
PaymentError,
|
|
7
|
-
PaymentInterface,
|
|
8
|
-
PaymentProvider,
|
|
9
|
-
} from '../types.js';
|
|
10
1
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
11
2
|
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
12
|
-
import {
|
|
13
|
-
import { PaymentPricingSheet } from '../director/PaymentPricingSheet.js';
|
|
14
|
-
import { PaymentDirector } from '../director/PaymentDirector.js';
|
|
15
|
-
import { paymentSettings } from '../payment-settings.js';
|
|
16
|
-
import { Order } from '@unchainedshop/core-orders';
|
|
17
|
-
import {
|
|
18
|
-
IPaymentPricingSheet,
|
|
19
|
-
PaymentPricingCalculation,
|
|
20
|
-
PaymentProviderType,
|
|
21
|
-
} from '../payment-index.js';
|
|
3
|
+
import { PaymentProvider } from '../db/PaymentProvidersCollection.js';
|
|
22
4
|
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
count: (query: mongodb.Filter<PaymentProvider>) => Promise<number>;
|
|
29
|
-
findProvider: (
|
|
30
|
-
query: mongodb.Filter<PaymentProvider> & {
|
|
31
|
-
paymentProviderId: string;
|
|
32
|
-
},
|
|
33
|
-
options?: mongodb.FindOptions,
|
|
34
|
-
) => Promise<PaymentProvider>;
|
|
35
|
-
findProviders: (
|
|
36
|
-
query: mongodb.Filter<PaymentProvider>,
|
|
37
|
-
options?: mongodb.FindOptions,
|
|
38
|
-
) => Promise<Array<PaymentProvider>>;
|
|
39
|
-
|
|
40
|
-
providerExists: (query: { paymentProviderId: string }) => Promise<boolean>;
|
|
41
|
-
|
|
42
|
-
// Payment adapter
|
|
43
|
-
findSupported: (
|
|
44
|
-
query: { order: Order },
|
|
45
|
-
unchainedAPI: UnchainedCore,
|
|
46
|
-
) => Promise<Array<PaymentProvider>>;
|
|
47
|
-
determineDefault: (
|
|
48
|
-
paymentProviders: Array<PaymentProvider>,
|
|
49
|
-
params: { order: Order; paymentCredentials?: Array<PaymentCredentials> },
|
|
50
|
-
unchainedAPI: UnchainedCore,
|
|
51
|
-
) => Promise<PaymentProvider>;
|
|
52
|
-
|
|
53
|
-
findInterface: (query: PaymentProvider) => PaymentInterface;
|
|
54
|
-
findInterfaces: (query: { type: PaymentProviderType }) => Array<PaymentInterface>;
|
|
55
|
-
|
|
56
|
-
pricingSheet: (params: {
|
|
57
|
-
calculation: Array<PaymentPricingCalculation>;
|
|
58
|
-
currency: string;
|
|
59
|
-
}) => IPaymentPricingSheet;
|
|
60
|
-
|
|
61
|
-
configurationError: (
|
|
62
|
-
paymentProvider: PaymentProvider,
|
|
63
|
-
unchainedAPI: UnchainedCore,
|
|
64
|
-
) => Promise<PaymentError>;
|
|
65
|
-
|
|
66
|
-
isActive: (paymentProvider: PaymentProvider, unchainedAPI: UnchainedCore) => Promise<boolean>;
|
|
67
|
-
|
|
68
|
-
isPayLaterAllowed: (paymentProvider: PaymentProvider, unchainedAPI: UnchainedCore) => Promise<boolean>;
|
|
69
|
-
|
|
70
|
-
calculate: (
|
|
71
|
-
pricingContext: PaymentPricingContext,
|
|
72
|
-
unchainedAPI: UnchainedCore,
|
|
73
|
-
) => Promise<Array<PaymentPricingCalculation>>;
|
|
74
|
-
|
|
75
|
-
charge: (
|
|
76
|
-
paymentProviderId: string,
|
|
77
|
-
paymentContext: PaymentContext,
|
|
78
|
-
unchainedAPI: UnchainedCore,
|
|
79
|
-
) => Promise<PaymentChargeActionResult | false>;
|
|
80
|
-
register: (
|
|
81
|
-
paymentProviderId: string,
|
|
82
|
-
paymentContext: PaymentContext,
|
|
83
|
-
unchainedAPI: UnchainedCore,
|
|
84
|
-
) => Promise<any>;
|
|
85
|
-
sign: (
|
|
86
|
-
paymentProviderId: string,
|
|
87
|
-
paymentContext: PaymentContext,
|
|
88
|
-
unchainedAPI: UnchainedCore,
|
|
89
|
-
) => Promise<string>;
|
|
90
|
-
validate: (
|
|
91
|
-
paymentProviderId: string,
|
|
92
|
-
paymentContext: PaymentContext,
|
|
93
|
-
unchainedAPI: UnchainedCore,
|
|
94
|
-
) => Promise<boolean>;
|
|
95
|
-
cancel: (
|
|
96
|
-
paymentProviderId: string,
|
|
97
|
-
paymentContext: PaymentContext,
|
|
98
|
-
unchainedAPI: UnchainedCore,
|
|
99
|
-
) => Promise<boolean>;
|
|
100
|
-
confirm: (
|
|
101
|
-
paymentProviderId: string,
|
|
102
|
-
paymentContext: PaymentContext,
|
|
103
|
-
unchainedAPI: UnchainedCore,
|
|
104
|
-
) => Promise<boolean>;
|
|
105
|
-
};
|
|
5
|
+
export interface PaymentInterface {
|
|
6
|
+
_id: string;
|
|
7
|
+
label: string;
|
|
8
|
+
version: string;
|
|
9
|
+
}
|
|
106
10
|
|
|
107
11
|
const PAYMENT_PROVIDER_EVENTS: string[] = [
|
|
108
12
|
'PAYMENT_PROVIDER_CREATE',
|
|
@@ -114,47 +18,42 @@ export const buildFindSelector = ({ type }: mongodb.Filter<PaymentProvider> = {}
|
|
|
114
18
|
return { ...(type ? { type } : {}), deleted: null };
|
|
115
19
|
};
|
|
116
20
|
|
|
117
|
-
const asyncFilter = async (arr, predicate) => {
|
|
118
|
-
const results = await Promise.all(arr.map(predicate));
|
|
119
|
-
|
|
120
|
-
return arr.filter((_v, index) => results[index]);
|
|
121
|
-
};
|
|
122
|
-
|
|
123
21
|
export const configurePaymentProvidersModule = (
|
|
124
22
|
PaymentProviders: mongodb.Collection<PaymentProvider>,
|
|
125
|
-
)
|
|
23
|
+
) => {
|
|
126
24
|
registerEvents(PAYMENT_PROVIDER_EVENTS);
|
|
127
25
|
|
|
128
|
-
const getPaymentAdapter = async (
|
|
129
|
-
paymentProviderId: string,
|
|
130
|
-
paymentContext: PaymentContext,
|
|
131
|
-
unchainedAPI: UnchainedCore,
|
|
132
|
-
) => {
|
|
133
|
-
const provider = await PaymentProviders.findOne(generateDbFilterById(paymentProviderId), {});
|
|
134
|
-
|
|
135
|
-
return PaymentDirector.actions(provider, paymentContext, unchainedAPI);
|
|
136
|
-
};
|
|
137
|
-
|
|
138
26
|
return {
|
|
139
27
|
// Queries
|
|
140
|
-
count: async (query) => {
|
|
28
|
+
count: async (query: mongodb.Filter<PaymentProvider>): Promise<number> => {
|
|
141
29
|
const providerCount = await PaymentProviders.countDocuments(buildFindSelector(query));
|
|
142
30
|
return providerCount;
|
|
143
31
|
},
|
|
144
32
|
|
|
145
|
-
findProvider: async (
|
|
33
|
+
findProvider: async (
|
|
34
|
+
{
|
|
35
|
+
paymentProviderId,
|
|
36
|
+
...query
|
|
37
|
+
}: mongodb.Filter<PaymentProvider> & {
|
|
38
|
+
paymentProviderId: string;
|
|
39
|
+
},
|
|
40
|
+
options?: mongodb.FindOptions,
|
|
41
|
+
): Promise<PaymentProvider> => {
|
|
146
42
|
return PaymentProviders.findOne(
|
|
147
43
|
paymentProviderId ? generateDbFilterById(paymentProviderId) : query,
|
|
148
44
|
options,
|
|
149
45
|
);
|
|
150
46
|
},
|
|
151
47
|
|
|
152
|
-
findProviders: async (
|
|
48
|
+
findProviders: async (
|
|
49
|
+
query: mongodb.Filter<PaymentProvider>,
|
|
50
|
+
options: mongodb.FindOptions = { sort: { created: 1 } },
|
|
51
|
+
): Promise<Array<PaymentProvider>> => {
|
|
153
52
|
const providers = PaymentProviders.find(buildFindSelector(query), options);
|
|
154
53
|
return providers.toArray();
|
|
155
54
|
},
|
|
156
55
|
|
|
157
|
-
providerExists: async ({ paymentProviderId }) => {
|
|
56
|
+
providerExists: async ({ paymentProviderId }: { paymentProviderId: string }): Promise<boolean> => {
|
|
158
57
|
const providerCount = await PaymentProviders.countDocuments(
|
|
159
58
|
generateDbFilterById(paymentProviderId, { deleted: null }),
|
|
160
59
|
{ limit: 1 },
|
|
@@ -162,124 +61,11 @@ export const configurePaymentProvidersModule = (
|
|
|
162
61
|
return !!providerCount;
|
|
163
62
|
},
|
|
164
63
|
|
|
165
|
-
findInterface: (paymentProvider) => {
|
|
166
|
-
const Adapter = PaymentDirector.getAdapter(paymentProvider.adapterKey);
|
|
167
|
-
if (!Adapter) return null;
|
|
168
|
-
return {
|
|
169
|
-
_id: Adapter.key,
|
|
170
|
-
label: Adapter.label,
|
|
171
|
-
version: Adapter.version,
|
|
172
|
-
};
|
|
173
|
-
},
|
|
174
|
-
|
|
175
|
-
pricingSheet: (params) => {
|
|
176
|
-
return PaymentPricingSheet(params);
|
|
177
|
-
},
|
|
178
|
-
|
|
179
|
-
findInterfaces: ({ type }) => {
|
|
180
|
-
return PaymentDirector.getAdapters()
|
|
181
|
-
.filter((Adapter) => Adapter.typeSupported(type))
|
|
182
|
-
.map((Adapter) => ({
|
|
183
|
-
_id: Adapter.key,
|
|
184
|
-
label: Adapter.label,
|
|
185
|
-
version: Adapter.version,
|
|
186
|
-
}));
|
|
187
|
-
},
|
|
188
|
-
|
|
189
|
-
findSupported: async (paymentContext, unchainedAPI) => {
|
|
190
|
-
const allProviders = await PaymentProviders.find({ deleted: null }).toArray();
|
|
191
|
-
const providers: PaymentProvider[] = await asyncFilter(
|
|
192
|
-
allProviders,
|
|
193
|
-
async (provider: PaymentProvider) => {
|
|
194
|
-
try {
|
|
195
|
-
const director = await PaymentDirector.actions(provider, paymentContext, unchainedAPI);
|
|
196
|
-
return director.isActive();
|
|
197
|
-
} catch {
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
return paymentSettings.filterSupportedProviders(
|
|
204
|
-
{
|
|
205
|
-
providers,
|
|
206
|
-
order: paymentContext.order,
|
|
207
|
-
},
|
|
208
|
-
unchainedAPI,
|
|
209
|
-
);
|
|
210
|
-
},
|
|
211
|
-
|
|
212
|
-
determineDefault: async (paymentProviders, deliveryContext, unchainedAPI) => {
|
|
213
|
-
return paymentSettings.determineDefaultProvider(
|
|
214
|
-
{
|
|
215
|
-
providers: paymentProviders,
|
|
216
|
-
...deliveryContext,
|
|
217
|
-
},
|
|
218
|
-
unchainedAPI,
|
|
219
|
-
);
|
|
220
|
-
},
|
|
221
|
-
|
|
222
|
-
// Payment Adapter
|
|
223
|
-
|
|
224
|
-
configurationError: async (paymentProvider, unchainedAPI) => {
|
|
225
|
-
const actions = await PaymentDirector.actions(paymentProvider, {}, unchainedAPI);
|
|
226
|
-
return actions.configurationError();
|
|
227
|
-
},
|
|
228
|
-
|
|
229
|
-
calculate: async (pricingContext, unchainedAPI) => {
|
|
230
|
-
const pricing = await PaymentPricingDirector.actions(pricingContext, unchainedAPI);
|
|
231
|
-
return pricing.calculate();
|
|
232
|
-
},
|
|
233
|
-
|
|
234
|
-
isActive: async (paymentProvider, unchainedAPI) => {
|
|
235
|
-
const actions = await PaymentDirector.actions(paymentProvider, {}, unchainedAPI);
|
|
236
|
-
return actions.isActive();
|
|
237
|
-
},
|
|
238
|
-
|
|
239
|
-
isPayLaterAllowed: async (paymentProvider, unchainedAPI) => {
|
|
240
|
-
const actions = await PaymentDirector.actions(paymentProvider, {}, unchainedAPI);
|
|
241
|
-
return actions.isPayLaterAllowed();
|
|
242
|
-
},
|
|
243
|
-
|
|
244
|
-
charge: async (paymentProviderId, paymentContext, unchainedAPI) => {
|
|
245
|
-
const adapter = await getPaymentAdapter(paymentProviderId, paymentContext, unchainedAPI);
|
|
246
|
-
return adapter.charge();
|
|
247
|
-
},
|
|
248
|
-
|
|
249
|
-
register: async (paymentProviderId, paymentContext, unchainedAPI) => {
|
|
250
|
-
const adapter = await getPaymentAdapter(paymentProviderId, paymentContext, unchainedAPI);
|
|
251
|
-
return adapter.register();
|
|
252
|
-
},
|
|
253
|
-
|
|
254
|
-
sign: async (paymentProviderId, paymentContext, unchainedAPI) => {
|
|
255
|
-
const adapter = await getPaymentAdapter(paymentProviderId, paymentContext, unchainedAPI);
|
|
256
|
-
return adapter.sign();
|
|
257
|
-
},
|
|
258
|
-
|
|
259
|
-
validate: async (paymentProviderId, paymentContext, unchainedAPI) => {
|
|
260
|
-
const adapter = await getPaymentAdapter(paymentProviderId, paymentContext, unchainedAPI);
|
|
261
|
-
return adapter.validate();
|
|
262
|
-
},
|
|
263
|
-
|
|
264
|
-
cancel: async (paymentProviderId, paymentContext, unchainedAPI) => {
|
|
265
|
-
const adapter = await getPaymentAdapter(paymentProviderId, paymentContext, unchainedAPI);
|
|
266
|
-
return adapter.cancel();
|
|
267
|
-
},
|
|
268
|
-
|
|
269
|
-
confirm: async (paymentProviderId, paymentContext, unchainedAPI) => {
|
|
270
|
-
const adapter = await getPaymentAdapter(paymentProviderId, paymentContext, unchainedAPI);
|
|
271
|
-
return adapter.confirm();
|
|
272
|
-
},
|
|
273
|
-
|
|
274
64
|
// Mutations
|
|
275
|
-
create: async (doc) => {
|
|
276
|
-
const Adapter = PaymentDirector.getAdapter(doc.adapterKey);
|
|
277
|
-
if (!Adapter) return null;
|
|
278
|
-
|
|
65
|
+
create: async (doc: PaymentProvider): Promise<PaymentProvider> => {
|
|
279
66
|
const { insertedId: paymentProviderId } = await PaymentProviders.insertOne({
|
|
280
67
|
_id: generateDbObjectId(),
|
|
281
68
|
created: new Date(),
|
|
282
|
-
configuration: Adapter.initialConfiguration,
|
|
283
69
|
...doc,
|
|
284
70
|
});
|
|
285
71
|
|
|
@@ -291,7 +77,7 @@ export const configurePaymentProvidersModule = (
|
|
|
291
77
|
return paymentProvider;
|
|
292
78
|
},
|
|
293
79
|
|
|
294
|
-
update: async (_id: string, doc: PaymentProvider) => {
|
|
80
|
+
update: async (_id: string, doc: PaymentProvider): Promise<PaymentProvider> => {
|
|
295
81
|
const paymentProvider = await PaymentProviders.findOneAndUpdate(
|
|
296
82
|
generateDbFilterById(_id),
|
|
297
83
|
{
|
|
@@ -306,7 +92,7 @@ export const configurePaymentProvidersModule = (
|
|
|
306
92
|
return paymentProvider;
|
|
307
93
|
},
|
|
308
94
|
|
|
309
|
-
delete: async (_id) => {
|
|
95
|
+
delete: async (_id: string): Promise<PaymentProvider> => {
|
|
310
96
|
const paymentProvider = await PaymentProviders.findOneAndUpdate(
|
|
311
97
|
generateDbFilterById(_id),
|
|
312
98
|
{
|
|
@@ -321,3 +107,5 @@ export const configurePaymentProvidersModule = (
|
|
|
321
107
|
},
|
|
322
108
|
};
|
|
323
109
|
};
|
|
110
|
+
|
|
111
|
+
export type PaymentProvidersModule = ReturnType<typeof configurePaymentProvidersModule>;
|
package/src/payment-index.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
export * from './types.js';
|
|
2
1
|
export * from './module/configurePaymentModule.js';
|
|
3
2
|
export * from './payment-settings.js';
|
|
4
|
-
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './director/PaymentAdapter.js';
|
|
7
|
-
export * from './director/PaymentPricingAdapter.js';
|
|
8
|
-
export * from './director/PaymentPricingDirector.js';
|
|
9
|
-
export * from './director/PaymentPricingSheet.js';
|
|
10
|
-
|
|
11
|
-
export { PaymentError } from './director/PaymentError.js';
|
|
12
|
-
export { PaymentProviderType } from './director/PaymentProviderType.js';
|
|
3
|
+
export * from './db/PaymentCredentialsCollection.js';
|
|
4
|
+
export * from './db/PaymentProvidersCollection.js';
|
package/src/payment-settings.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { UnchainedCore } from '@unchainedshop/core';
|
|
1
|
+
import { PaymentCredentials } from './db/PaymentCredentialsCollection.js';
|
|
2
|
+
import { PaymentProvider } from './db/PaymentProvidersCollection.js';
|
|
4
3
|
|
|
5
|
-
export type FilterProviders = (
|
|
4
|
+
export type FilterProviders<Order = unknown, UnchainedAPI = unknown> = (
|
|
6
5
|
params: {
|
|
7
6
|
providers: Array<PaymentProvider>;
|
|
8
7
|
order: Order;
|
|
9
8
|
},
|
|
10
|
-
|
|
9
|
+
unchainedAPI: UnchainedAPI,
|
|
11
10
|
) => Promise<Array<PaymentProvider>>;
|
|
12
11
|
|
|
13
|
-
export type DetermineDefaultProvider = (
|
|
12
|
+
export type DetermineDefaultProvider<Order = unknown, UnchainedAPI = unknown> = (
|
|
14
13
|
params: {
|
|
15
14
|
providers: Array<PaymentProvider>;
|
|
16
15
|
order: Order;
|
|
17
16
|
paymentCredentials?: Array<PaymentCredentials>;
|
|
18
17
|
},
|
|
19
|
-
|
|
18
|
+
unchainedAPI: UnchainedAPI,
|
|
20
19
|
) => Promise<PaymentProvider>;
|
|
21
20
|
export interface PaymentSettingsOptions {
|
|
22
21
|
sortProviders?: (a: PaymentProvider, b: PaymentProvider) => number;
|
package/tsconfig.json
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../shared/base.tsconfig.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"declarationDir": "./lib",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./lib"
|
|
6
7
|
},
|
|
7
|
-
"exclude": ["**/*.test.ts", "**/*.test.js", "tests"]
|
|
8
|
-
|
|
9
|
-
"./src"
|
|
10
|
-
]
|
|
11
|
-
}
|
|
8
|
+
"exclude": ["**/*.test.ts", "**/*.test.js", "tests", "lib"]
|
|
9
|
+
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { IPaymentAdapter } from '@unchainedshop/core-payment';
|
|
2
|
-
import { log, LogLevel } from '@unchainedshop/logger';
|
|
3
|
-
import { PaymentError } from './PaymentError.js';
|
|
4
|
-
|
|
5
|
-
export const PaymentAdapter: Omit<IPaymentAdapter, 'key' | 'label' | 'version'> = {
|
|
6
|
-
initialConfiguration: [],
|
|
7
|
-
|
|
8
|
-
typeSupported: () => {
|
|
9
|
-
return false;
|
|
10
|
-
},
|
|
11
|
-
|
|
12
|
-
actions: () => {
|
|
13
|
-
return {
|
|
14
|
-
configurationError: () => {
|
|
15
|
-
return PaymentError.NOT_IMPLEMENTED;
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
isActive: () => {
|
|
19
|
-
return false;
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
isPayLaterAllowed: () => {
|
|
23
|
-
return false;
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
charge: async () => {
|
|
27
|
-
// if you return true, the status will be changed to PAID
|
|
28
|
-
|
|
29
|
-
// if you return false, the order payment status stays the
|
|
30
|
-
// same but the order status might change
|
|
31
|
-
|
|
32
|
-
// if you throw an error, you cancel the checkout process
|
|
33
|
-
return false;
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
register: async () => {
|
|
37
|
-
return {
|
|
38
|
-
token: '',
|
|
39
|
-
};
|
|
40
|
-
},
|
|
41
|
-
|
|
42
|
-
sign: async () => {
|
|
43
|
-
return null;
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
validate: async () => {
|
|
47
|
-
return false;
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
cancel: async () => {
|
|
51
|
-
return false;
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
confirm: async () => {
|
|
55
|
-
return false;
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
log(message: string, { level = LogLevel.Debug, ...options } = {}) {
|
|
61
|
-
return log(message, { level, ...options });
|
|
62
|
-
},
|
|
63
|
-
};
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { IPaymentAdapter, IPaymentDirector, PaymentContext } from '@unchainedshop/core-payment';
|
|
2
|
-
import { BaseDirector } from '@unchainedshop/utils';
|
|
3
|
-
import { createLogger } from '@unchainedshop/logger';
|
|
4
|
-
import { PaymentError } from './PaymentError.js';
|
|
5
|
-
|
|
6
|
-
const logger = createLogger('unchained:core-payment');
|
|
7
|
-
const baseDirector = BaseDirector<IPaymentAdapter>('PaymentDirector');
|
|
8
|
-
|
|
9
|
-
export const PaymentDirector: IPaymentDirector = {
|
|
10
|
-
...baseDirector,
|
|
11
|
-
|
|
12
|
-
actions: async (paymentProvider, paymentContext, unchainedAPI) => {
|
|
13
|
-
const Adapter = baseDirector.getAdapter(paymentProvider.adapterKey) as IPaymentAdapter;
|
|
14
|
-
|
|
15
|
-
if (!Adapter) {
|
|
16
|
-
throw new Error(`Payment Plugin ${paymentProvider.adapterKey} not available`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const newPaymentContext: PaymentContext = {
|
|
20
|
-
...paymentContext,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
if (paymentContext?.orderPayment) {
|
|
24
|
-
const { modules } = unchainedAPI;
|
|
25
|
-
const { orderId } = paymentContext.orderPayment;
|
|
26
|
-
const order =
|
|
27
|
-
paymentContext?.order ||
|
|
28
|
-
(await modules.orders.findOrder({
|
|
29
|
-
orderId,
|
|
30
|
-
}));
|
|
31
|
-
|
|
32
|
-
newPaymentContext.order = order;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const adapter = Adapter.actions({
|
|
36
|
-
config: paymentProvider.configuration,
|
|
37
|
-
paymentContext: {
|
|
38
|
-
paymentProvider,
|
|
39
|
-
paymentProviderId: paymentProvider._id,
|
|
40
|
-
...newPaymentContext,
|
|
41
|
-
},
|
|
42
|
-
context: unchainedAPI,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
configurationError: () => {
|
|
47
|
-
try {
|
|
48
|
-
const error = adapter.configurationError();
|
|
49
|
-
return error;
|
|
50
|
-
} catch {
|
|
51
|
-
return PaymentError.ADAPTER_NOT_FOUND;
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
isActive: () => {
|
|
56
|
-
try {
|
|
57
|
-
return adapter.isActive(paymentContext.transactionContext);
|
|
58
|
-
} catch (error) {
|
|
59
|
-
logger.error(error.message);
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
isPayLaterAllowed: () => {
|
|
65
|
-
try {
|
|
66
|
-
return adapter.isPayLaterAllowed(paymentContext.transactionContext);
|
|
67
|
-
} catch (error) {
|
|
68
|
-
logger.error(error.message);
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
charge: async () => {
|
|
74
|
-
return adapter.charge(paymentContext.transactionContext);
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
register: async () => {
|
|
78
|
-
return adapter.register(paymentContext.transactionContext);
|
|
79
|
-
},
|
|
80
|
-
|
|
81
|
-
sign: async () => {
|
|
82
|
-
return adapter.sign(paymentContext.transactionContext);
|
|
83
|
-
},
|
|
84
|
-
|
|
85
|
-
validate: async () => {
|
|
86
|
-
const validated = await adapter.validate(paymentContext.token);
|
|
87
|
-
return !!validated;
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
cancel: async () => {
|
|
91
|
-
return adapter.cancel(paymentContext.transactionContext);
|
|
92
|
-
},
|
|
93
|
-
|
|
94
|
-
confirm: async () => {
|
|
95
|
-
return adapter.confirm(paymentContext.transactionContext);
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
},
|
|
99
|
-
};
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BasePricingAdapterContext,
|
|
3
|
-
IPricingAdapter,
|
|
4
|
-
IPricingSheet,
|
|
5
|
-
PricingCalculation,
|
|
6
|
-
} from '@unchainedshop/utils';
|
|
7
|
-
import { BasePricingAdapter } from '@unchainedshop/utils';
|
|
8
|
-
import { PaymentPricingSheet } from './PaymentPricingSheet.js';
|
|
9
|
-
import { User } from '@unchainedshop/core-users';
|
|
10
|
-
import { OrderPayment } from '@unchainedshop/core-orders';
|
|
11
|
-
import { Order } from '@unchainedshop/core-orders';
|
|
12
|
-
import { PaymentProvider } from '../types.js';
|
|
13
|
-
import { OrderDiscount } from '@unchainedshop/core-orders';
|
|
14
|
-
|
|
15
|
-
export interface PaymentPricingCalculation extends PricingCalculation {
|
|
16
|
-
discountId?: string;
|
|
17
|
-
isTaxable: boolean;
|
|
18
|
-
isNetPrice: boolean;
|
|
19
|
-
rate?: number;
|
|
20
|
-
}
|
|
21
|
-
export interface PaymentPricingAdapterContext extends BasePricingAdapterContext {
|
|
22
|
-
country?: string;
|
|
23
|
-
currency?: string;
|
|
24
|
-
user: User;
|
|
25
|
-
orderPayment: OrderPayment;
|
|
26
|
-
order: Order;
|
|
27
|
-
provider: PaymentProvider;
|
|
28
|
-
discounts: Array<OrderDiscount>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type IPaymentPricingSheet = IPricingSheet<PaymentPricingCalculation> & {
|
|
32
|
-
addFee: (params: { amount: number; isTaxable: boolean; isNetPrice: boolean; meta?: any }) => void;
|
|
33
|
-
addTax: (params: {
|
|
34
|
-
amount: number;
|
|
35
|
-
rate: number;
|
|
36
|
-
baseCategory?: string;
|
|
37
|
-
discountId?: string;
|
|
38
|
-
meta?: any;
|
|
39
|
-
}) => void;
|
|
40
|
-
addDiscount: (params: {
|
|
41
|
-
amount: number;
|
|
42
|
-
isTaxable: boolean;
|
|
43
|
-
isNetPrice: boolean;
|
|
44
|
-
discountId: string;
|
|
45
|
-
meta?: any;
|
|
46
|
-
}) => void;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export type IPaymentPricingAdapter<DiscountConfiguration = unknown> = IPricingAdapter<
|
|
50
|
-
PaymentPricingAdapterContext,
|
|
51
|
-
PaymentPricingCalculation,
|
|
52
|
-
IPaymentPricingSheet,
|
|
53
|
-
DiscountConfiguration
|
|
54
|
-
>;
|
|
55
|
-
|
|
56
|
-
const basePricingAdapter = BasePricingAdapter<PaymentPricingAdapterContext, PaymentPricingCalculation>();
|
|
57
|
-
|
|
58
|
-
export const PaymentPricingAdapter: IPricingAdapter<
|
|
59
|
-
PaymentPricingAdapterContext,
|
|
60
|
-
PaymentPricingCalculation,
|
|
61
|
-
IPaymentPricingSheet
|
|
62
|
-
> = {
|
|
63
|
-
...basePricingAdapter,
|
|
64
|
-
|
|
65
|
-
isActivatedFor: () => {
|
|
66
|
-
return false;
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
actions: (params) => {
|
|
70
|
-
const { context } = params;
|
|
71
|
-
const { currency } = context.order;
|
|
72
|
-
const baseActions = basePricingAdapter.actions(params);
|
|
73
|
-
const resultSheet = PaymentPricingSheet({ currency });
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
...baseActions,
|
|
77
|
-
calculate: async () => {
|
|
78
|
-
const resultRaw = resultSheet.getRawPricingSheet();
|
|
79
|
-
resultRaw.forEach(({ amount, category }) =>
|
|
80
|
-
basePricingAdapter.log(`Payment Calculation -> ${category} ${amount}`),
|
|
81
|
-
);
|
|
82
|
-
return resultRaw;
|
|
83
|
-
},
|
|
84
|
-
resultSheet: () => resultSheet,
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
};
|