@unchainedshop/core-payment 3.0.0-alpha2 → 3.0.0-alpha4
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 +1 -1
- package/lib/db/PaymentCredentialsCollection.d.ts.map +1 -1
- package/lib/db/PaymentProvidersCollection.d.ts +1 -1
- package/lib/db/PaymentProvidersCollection.d.ts.map +1 -1
- package/lib/director/PaymentAdapter.d.ts +1 -1
- package/lib/director/PaymentAdapter.d.ts.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 +1 -1
- package/lib/director/PaymentDirector.js.map +1 -1
- package/lib/director/PaymentPricingAdapter.d.ts +44 -2
- package/lib/director/PaymentPricingAdapter.d.ts.map +1 -1
- package/lib/director/PaymentPricingAdapter.js.map +1 -1
- package/lib/director/PaymentPricingDirector.d.ts +17 -1
- package/lib/director/PaymentPricingDirector.d.ts.map +1 -1
- package/lib/director/PaymentPricingDirector.js.map +1 -1
- package/lib/director/PaymentPricingSheet.d.ts +8 -2
- package/lib/director/PaymentPricingSheet.d.ts.map +1 -1
- package/lib/director/PaymentPricingSheet.js +7 -1
- package/lib/director/PaymentPricingSheet.js.map +1 -1
- package/lib/module/configurePaymentCredentialsModule.d.ts +22 -2
- package/lib/module/configurePaymentCredentialsModule.d.ts.map +1 -1
- package/lib/module/configurePaymentCredentialsModule.js.map +1 -1
- package/lib/module/configurePaymentModule.d.ts +11 -2
- package/lib/module/configurePaymentModule.d.ts.map +1 -1
- package/lib/module/configurePaymentModule.js +2 -2
- package/lib/module/configurePaymentModule.js.map +1 -1
- package/lib/module/configurePaymentProvidersModule.d.ts +45 -3
- package/lib/module/configurePaymentProvidersModule.d.ts.map +1 -1
- package/lib/module/configurePaymentProvidersModule.js +15 -13
- package/lib/module/configurePaymentProvidersModule.js.map +1 -1
- package/lib/payment-index.d.ts +8 -6
- package/lib/payment-index.d.ts.map +1 -1
- package/lib/payment-index.js +8 -6
- package/lib/payment-index.js.map +1 -1
- package/lib/payment-settings.d.ts +22 -1
- package/lib/payment-settings.d.ts.map +1 -1
- package/lib/payment-settings.js.map +1 -1
- package/lib/types.d.ts +84 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +17 -0
- package/lib/types.js.map +1 -0
- package/package.json +7 -9
- package/src/db/PaymentCredentialsCollection.ts +1 -1
- package/src/db/PaymentProvidersCollection.ts +1 -1
- package/src/director/PaymentAdapter.ts +1 -1
- package/src/director/PaymentDirector.ts +2 -2
- package/src/director/PaymentPricingAdapter.ts +51 -5
- package/src/director/PaymentPricingDirector.ts +28 -3
- package/src/director/PaymentPricingSheet.ts +9 -6
- package/src/module/configurePaymentCredentialsModule.ts +34 -5
- package/src/module/configurePaymentModule.ts +31 -5
- package/src/module/configurePaymentProvidersModule.ts +124 -22
- package/src/module/payment-index.test.ts +10 -10
- package/src/payment-index.ts +8 -7
- package/src/payment-settings.ts +31 -6
- package/src/types.ts +108 -0
- package/lib/db/PaymentCredentialsSchema.d.ts +0 -3
- package/lib/db/PaymentCredentialsSchema.d.ts.map +0 -1
- package/lib/db/PaymentCredentialsSchema.js +0 -11
- package/lib/db/PaymentCredentialsSchema.js.map +0 -1
- package/lib/db/PaymentProvidersSchema.d.ts +0 -3
- package/lib/db/PaymentProvidersSchema.d.ts.map +0 -1
- package/lib/db/PaymentProvidersSchema.js +0 -12
- package/lib/db/PaymentProvidersSchema.js.map +0 -1
- package/src/db/PaymentCredentialsSchema.ts +0 -14
- package/src/db/PaymentProvidersSchema.ts +0 -15
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { IBaseAdapter, IBaseDirector } from '@unchainedshop/utils';
|
|
2
|
+
import type { TimestampFields } from '@unchainedshop/mongodb';
|
|
3
|
+
import { Order } from '@unchainedshop/core-orders';
|
|
4
|
+
import { OrderPayment } from '@unchainedshop/core-orders';
|
|
5
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
6
|
+
export declare enum PaymentProviderType {
|
|
7
|
+
CARD = "CARD",
|
|
8
|
+
INVOICE = "INVOICE",
|
|
9
|
+
GENERIC = "GENERIC"
|
|
10
|
+
}
|
|
11
|
+
export type PaymentConfiguration = Array<{
|
|
12
|
+
key: string;
|
|
13
|
+
value: string | null;
|
|
14
|
+
}>;
|
|
15
|
+
export type PaymentProvider = {
|
|
16
|
+
_id?: string;
|
|
17
|
+
type: PaymentProviderType;
|
|
18
|
+
adapterKey: string;
|
|
19
|
+
configuration: PaymentConfiguration;
|
|
20
|
+
} & TimestampFields;
|
|
21
|
+
export type PaymentCredentials = {
|
|
22
|
+
_id?: string;
|
|
23
|
+
paymentProviderId: string;
|
|
24
|
+
userId: string;
|
|
25
|
+
token?: string;
|
|
26
|
+
isPreferred?: boolean;
|
|
27
|
+
meta: any;
|
|
28
|
+
} & TimestampFields;
|
|
29
|
+
export declare enum PaymentError {
|
|
30
|
+
ADAPTER_NOT_FOUND = "ADAPTER_NOT_FOUND",
|
|
31
|
+
NOT_IMPLEMENTED = "NOT_IMPLEMENTED",
|
|
32
|
+
INCOMPLETE_CONFIGURATION = "INCOMPLETE_CONFIGURATION",
|
|
33
|
+
WRONG_CREDENTIALS = "WRONG_CREDENTIALS"
|
|
34
|
+
}
|
|
35
|
+
export interface PaymentContext {
|
|
36
|
+
userId?: string;
|
|
37
|
+
order?: Order;
|
|
38
|
+
orderPayment?: OrderPayment;
|
|
39
|
+
transactionContext?: any;
|
|
40
|
+
token?: any;
|
|
41
|
+
meta?: any;
|
|
42
|
+
}
|
|
43
|
+
export type ChargeResult = {
|
|
44
|
+
transactionId?: string;
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
};
|
|
47
|
+
export type PaymentChargeActionResult = ChargeResult & {
|
|
48
|
+
credentials?: {
|
|
49
|
+
token: string;
|
|
50
|
+
[key: string]: any;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export interface IPaymentActions {
|
|
54
|
+
charge: (transactionContext?: any) => Promise<PaymentChargeActionResult | false>;
|
|
55
|
+
configurationError: (transactionContext?: any) => PaymentError;
|
|
56
|
+
isActive: (transactionContext?: any) => boolean;
|
|
57
|
+
isPayLaterAllowed: (transactionContext?: any) => boolean;
|
|
58
|
+
register: (transactionContext?: any) => Promise<any>;
|
|
59
|
+
sign: (transactionContext?: any) => Promise<string>;
|
|
60
|
+
validate: (token?: any) => Promise<boolean>;
|
|
61
|
+
cancel: (transactionContext?: any) => Promise<boolean>;
|
|
62
|
+
confirm: (transactionContext?: any) => Promise<boolean>;
|
|
63
|
+
}
|
|
64
|
+
export type IPaymentAdapter = IBaseAdapter & {
|
|
65
|
+
initialConfiguration: PaymentConfiguration;
|
|
66
|
+
typeSupported: (type: PaymentProviderType) => boolean;
|
|
67
|
+
actions: (params: {
|
|
68
|
+
config: PaymentConfiguration;
|
|
69
|
+
paymentContext: PaymentContext & {
|
|
70
|
+
paymentProviderId: string;
|
|
71
|
+
paymentProvider: PaymentProvider;
|
|
72
|
+
};
|
|
73
|
+
context: UnchainedCore;
|
|
74
|
+
}) => IPaymentActions;
|
|
75
|
+
};
|
|
76
|
+
export type IPaymentDirector = IBaseDirector<IPaymentAdapter> & {
|
|
77
|
+
actions: (paymentProvider: PaymentProvider, paymentContext: PaymentContext, unchainedAPI: UnchainedCore) => Promise<IPaymentActions>;
|
|
78
|
+
};
|
|
79
|
+
export interface PaymentInterface {
|
|
80
|
+
_id: string;
|
|
81
|
+
label: string;
|
|
82
|
+
version: string;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB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AC;QAC7B,cAAc,EAAE,cAAc,GAAG;YAC/B,iBAAiB,EAAE,MAAM,CAAC;YAC1B,eAAe,EAAE,eAAe,CAAC;SAClC,CAAC;QACF,OAAO,EAAE,aAAa,CAAC;KACxB,KAAK,eAAe,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAAC,eAAe,CAAC,GAAG;IAC9D,OAAO,EAAE,CACP,eAAe,EAAE,eAAe,EAChC,cAAc,EAAE,cAAc,EAC9B,YAAY,EAAE,aAAa,KACxB,OAAO,CAAC,eAAe,CAAC,CAAC;CAC/B,CAAC;AAMF,MAAM,WAAW,gBAAgB;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export var PaymentProviderType;
|
|
2
|
+
(function (PaymentProviderType) {
|
|
3
|
+
PaymentProviderType["CARD"] = "CARD";
|
|
4
|
+
PaymentProviderType["INVOICE"] = "INVOICE";
|
|
5
|
+
PaymentProviderType["GENERIC"] = "GENERIC";
|
|
6
|
+
})(PaymentProviderType || (PaymentProviderType = {}));
|
|
7
|
+
export var PaymentError;
|
|
8
|
+
(function (PaymentError) {
|
|
9
|
+
PaymentError["ADAPTER_NOT_FOUND"] = "ADAPTER_NOT_FOUND";
|
|
10
|
+
PaymentError["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
|
|
11
|
+
PaymentError["INCOMPLETE_CONFIGURATION"] = "INCOMPLETE_CONFIGURATION";
|
|
12
|
+
PaymentError["WRONG_CREDENTIALS"] = "WRONG_CREDENTIALS";
|
|
13
|
+
})(PaymentError || (PaymentError = {}));
|
|
14
|
+
/*
|
|
15
|
+
* Settings
|
|
16
|
+
*/
|
|
17
|
+
//# sourceMappingURL=types.js.map
|
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAMA,MAAM,CAAN,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC7B,oCAAa,CAAA;IACb,0CAAmB,CAAA;IACnB,0CAAmB,CAAA;AACrB,CAAC,EAJW,mBAAmB,KAAnB,mBAAmB,QAI9B;AAuBD,MAAM,CAAN,IAAY,YAKX;AALD,WAAY,YAAY;IACtB,uDAAuC,CAAA;IACvC,mDAAmC,CAAA;IACnC,qEAAqD,CAAA;IACrD,uDAAuC,CAAA;AACzC,CAAC,EALW,YAAY,KAAZ,YAAY,QAKvB;AAmED;;GAEG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-payment",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-alpha4",
|
|
4
4
|
"main": "lib/payment-index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/payment-index.js",
|
|
@@ -31,16 +31,14 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/events": "^3.0.0-
|
|
35
|
-
"@unchainedshop/logger": "^3.0.0-
|
|
36
|
-
"@unchainedshop/utils": "^3.0.0-
|
|
37
|
-
"simpl-schema": "^3.4.6"
|
|
34
|
+
"@unchainedshop/events": "^3.0.0-alpha4",
|
|
35
|
+
"@unchainedshop/logger": "^3.0.0-alpha4",
|
|
36
|
+
"@unchainedshop/utils": "^3.0.0-alpha4"
|
|
38
37
|
},
|
|
39
38
|
"devDependencies": {
|
|
40
|
-
"@types/node": "^
|
|
41
|
-
"@unchainedshop/types": "^3.0.0-alpha2",
|
|
39
|
+
"@types/node": "^22.7.8",
|
|
42
40
|
"jest": "^29.7.0",
|
|
43
|
-
"ts-jest": "^29.2.
|
|
44
|
-
"typescript": "^5.
|
|
41
|
+
"ts-jest": "^29.2.5",
|
|
42
|
+
"typescript": "^5.6.3"
|
|
45
43
|
}
|
|
46
44
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { mongodb, buildDbIndexes } from '@unchainedshop/mongodb';
|
|
2
|
-
import { PaymentCredentials as PaymentCredentialsType } from '@unchainedshop/
|
|
2
|
+
import { PaymentCredentials as PaymentCredentialsType } from '@unchainedshop/core-payment';
|
|
3
3
|
|
|
4
4
|
export const PaymentCredentialsCollection = async (db: mongodb.Db) => {
|
|
5
5
|
const PaymentCredentials = db.collection<PaymentCredentialsType>('payment_credentials');
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { mongodb, buildDbIndexes } from '@unchainedshop/mongodb';
|
|
2
|
-
import { PaymentProvider } from '@unchainedshop/
|
|
2
|
+
import { PaymentProvider } from '@unchainedshop/core-payment';
|
|
3
3
|
|
|
4
4
|
export const PaymentProvidersCollection = async (db: mongodb.Db) => {
|
|
5
5
|
const PaymentProviders = db.collection<PaymentProvider>('payment-providers');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IPaymentAdapter, IPaymentDirector, PaymentContext } from '@unchainedshop/
|
|
1
|
+
import { IPaymentAdapter, IPaymentDirector, PaymentContext } from '@unchainedshop/core-payment';
|
|
2
2
|
import { BaseDirector } from '@unchainedshop/utils';
|
|
3
3
|
import { createLogger } from '@unchainedshop/logger';
|
|
4
4
|
import { PaymentError } from './PaymentError.js';
|
|
@@ -47,7 +47,7 @@ export const PaymentDirector: IPaymentDirector = {
|
|
|
47
47
|
try {
|
|
48
48
|
const error = adapter.configurationError();
|
|
49
49
|
return error;
|
|
50
|
-
} catch
|
|
50
|
+
} catch {
|
|
51
51
|
return PaymentError.ADAPTER_NOT_FOUND;
|
|
52
52
|
}
|
|
53
53
|
},
|
|
@@ -1,11 +1,57 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
BasePricingAdapterContext,
|
|
3
|
+
IPricingAdapter,
|
|
4
|
+
IPricingSheet,
|
|
5
|
+
PricingCalculation,
|
|
6
|
+
} from '@unchainedshop/utils';
|
|
7
7
|
import { BasePricingAdapter } from '@unchainedshop/utils';
|
|
8
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
|
+
>;
|
|
9
55
|
|
|
10
56
|
const basePricingAdapter = BasePricingAdapter<PaymentPricingAdapterContext, PaymentPricingCalculation>();
|
|
11
57
|
|
|
@@ -1,12 +1,37 @@
|
|
|
1
1
|
import {
|
|
2
2
|
IPaymentPricingAdapter,
|
|
3
|
-
|
|
3
|
+
IPaymentPricingSheet,
|
|
4
4
|
PaymentPricingAdapterContext,
|
|
5
5
|
PaymentPricingCalculation,
|
|
6
|
-
|
|
7
|
-
} from '@unchainedshop/types/payments.pricing.js';
|
|
6
|
+
} from './PaymentPricingAdapter.js';
|
|
8
7
|
import { BasePricingDirector } from '@unchainedshop/utils';
|
|
9
8
|
import { PaymentPricingSheet } from './PaymentPricingSheet.js';
|
|
9
|
+
import { User } from '@unchainedshop/core-users';
|
|
10
|
+
import { Order } from '@unchainedshop/core-orders';
|
|
11
|
+
import { PaymentProvider } from '../types.js';
|
|
12
|
+
import { OrderPayment } from '@unchainedshop/core-orders';
|
|
13
|
+
import { IPricingDirector } from '@unchainedshop/utils';
|
|
14
|
+
|
|
15
|
+
export type PaymentPricingContext =
|
|
16
|
+
| {
|
|
17
|
+
country?: string;
|
|
18
|
+
currency?: string;
|
|
19
|
+
user: User;
|
|
20
|
+
order: Order;
|
|
21
|
+
provider: PaymentProvider;
|
|
22
|
+
providerContext?: any;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
item: OrderPayment;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type IPaymentPricingDirector<DiscountConfiguration = unknown> = IPricingDirector<
|
|
29
|
+
PaymentPricingContext,
|
|
30
|
+
PaymentPricingCalculation,
|
|
31
|
+
PaymentPricingAdapterContext,
|
|
32
|
+
IPaymentPricingSheet,
|
|
33
|
+
IPaymentPricingAdapter<DiscountConfiguration>
|
|
34
|
+
>;
|
|
10
35
|
|
|
11
36
|
const baseDirector = BasePricingDirector<
|
|
12
37
|
PaymentPricingContext,
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { BasePricingSheet } from '@unchainedshop/utils';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { PaymentPricingCalculation, IPaymentPricingSheet } from './PaymentPricingAdapter.js';
|
|
3
|
+
import { IBasePricingSheet, PricingSheetParams } from '@unchainedshop/utils';
|
|
4
|
+
|
|
5
|
+
export enum PaymentPricingRowCategory {
|
|
6
|
+
Item = 'ITEM',
|
|
7
|
+
Payment = 'PAYMENT',
|
|
8
|
+
Discount = 'DISCOUNT',
|
|
9
|
+
Tax = 'TAX',
|
|
10
|
+
}
|
|
8
11
|
|
|
9
12
|
export const PaymentPricingSheet = (
|
|
10
13
|
params: PricingSheetParams<PaymentPricingCalculation>,
|
|
@@ -1,12 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
PaymentCredentials as PaymentCredentialsType,
|
|
3
|
-
PaymentModule,
|
|
4
|
-
} from '@unchainedshop/types/payments.js';
|
|
5
1
|
import { mongodb, generateDbFilterById, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
2
|
+
import { PaymentCredentials as PaymentCredentialsType } from '../types.js';
|
|
3
|
+
|
|
4
|
+
export type PaymentCredentialsModules = {
|
|
5
|
+
// Queries
|
|
6
|
+
|
|
7
|
+
credentialsExists: (query: { paymentCredentialsId: string }) => Promise<boolean>;
|
|
8
|
+
|
|
9
|
+
findPaymentCredential: (
|
|
10
|
+
query: {
|
|
11
|
+
paymentCredentialsId?: string;
|
|
12
|
+
userId?: string;
|
|
13
|
+
paymentProviderId?: string;
|
|
14
|
+
isPreferred?: boolean;
|
|
15
|
+
},
|
|
16
|
+
options?: mongodb.FindOptions,
|
|
17
|
+
) => Promise<PaymentCredentialsType>;
|
|
18
|
+
|
|
19
|
+
findPaymentCredentials: (
|
|
20
|
+
query: mongodb.Filter<PaymentCredentialsType>,
|
|
21
|
+
options?: mongodb.FindOptions,
|
|
22
|
+
) => Promise<Array<PaymentCredentialsType>>;
|
|
23
|
+
|
|
24
|
+
// Mutations
|
|
25
|
+
markPreferred: (query: { userId: string; paymentCredentialsId: string }) => Promise<void>;
|
|
26
|
+
|
|
27
|
+
upsertCredentials: (
|
|
28
|
+
doc: Pick<PaymentCredentialsType, 'userId' | 'paymentProviderId' | '_id' | 'token'> & {
|
|
29
|
+
[x: string]: any;
|
|
30
|
+
},
|
|
31
|
+
) => Promise<string | null>;
|
|
32
|
+
|
|
33
|
+
removeCredentials: (paymentCredentialsId: string) => Promise<PaymentCredentialsType>;
|
|
34
|
+
};
|
|
6
35
|
|
|
7
36
|
export const configurePaymentCredentialsModule = (
|
|
8
37
|
PaymentCredentials: mongodb.Collection<PaymentCredentialsType>,
|
|
9
|
-
):
|
|
38
|
+
): PaymentCredentialsModules => {
|
|
10
39
|
const markPreferred = async ({ userId, paymentCredentialsId }) => {
|
|
11
40
|
await PaymentCredentials.updateOne(
|
|
12
41
|
{
|
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { PaymentModule, PaymentSettingsOptions } from '@unchainedshop/types/payments.js';
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
3
2
|
import { PaymentCredentialsCollection } from '../db/PaymentCredentialsCollection.js';
|
|
4
3
|
import { PaymentProvidersCollection } from '../db/PaymentProvidersCollection.js';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
import {
|
|
5
|
+
configurePaymentCredentialsModule,
|
|
6
|
+
PaymentCredentialsModules,
|
|
7
|
+
} from './configurePaymentCredentialsModule.js';
|
|
8
|
+
import {
|
|
9
|
+
configurePaymentProvidersModule,
|
|
10
|
+
PaymentProvidersModules,
|
|
11
|
+
} from './configurePaymentProvidersModule.js';
|
|
12
|
+
import { paymentSettings, PaymentSettingsOptions } from '../payment-settings.js';
|
|
13
|
+
import { PaymentContext, PaymentCredentials as PaymentCredentialsType } from '../types.js';
|
|
14
|
+
import { ModuleInput } from '@unchainedshop/mongodb';
|
|
15
|
+
export type PaymentModule = {
|
|
16
|
+
/*
|
|
17
|
+
* Payment Providers Module
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
registerCredentials: (
|
|
21
|
+
paymentProviderId: string,
|
|
22
|
+
paymentContext: PaymentContext,
|
|
23
|
+
unchainedAPI: UnchainedCore,
|
|
24
|
+
) => Promise<PaymentCredentialsType>;
|
|
25
|
+
|
|
26
|
+
paymentProviders: PaymentProvidersModules;
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* Payment Credentials Module
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
paymentCredentials: PaymentCredentialsModules;
|
|
33
|
+
};
|
|
8
34
|
|
|
9
35
|
export const configurePaymentModule = async ({
|
|
10
36
|
db,
|
|
@@ -1,12 +1,108 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
2
|
+
import {
|
|
3
|
+
PaymentChargeActionResult,
|
|
4
|
+
PaymentContext,
|
|
5
|
+
PaymentCredentials,
|
|
6
|
+
PaymentError,
|
|
7
|
+
PaymentInterface,
|
|
8
|
+
PaymentProvider,
|
|
9
|
+
} from '../types.js';
|
|
3
10
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
4
|
-
import { generateDbFilterById,
|
|
5
|
-
import { PaymentPricingDirector } from '../director/PaymentPricingDirector.js';
|
|
11
|
+
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
12
|
+
import { PaymentPricingContext, PaymentPricingDirector } from '../director/PaymentPricingDirector.js';
|
|
6
13
|
import { PaymentPricingSheet } from '../director/PaymentPricingSheet.js';
|
|
7
|
-
import { PaymentProvidersSchema } from '../db/PaymentProvidersSchema.js';
|
|
8
14
|
import { PaymentDirector } from '../director/PaymentDirector.js';
|
|
9
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';
|
|
22
|
+
|
|
23
|
+
export type PaymentProvidersModules = {
|
|
24
|
+
// Queries
|
|
25
|
+
create: (doc: PaymentProvider) => Promise<PaymentProvider>;
|
|
26
|
+
update: (_id: string, doc: PaymentProvider) => Promise<PaymentProvider>;
|
|
27
|
+
delete: (_id: string) => Promise<PaymentProvider>;
|
|
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
|
+
};
|
|
10
106
|
|
|
11
107
|
const PAYMENT_PROVIDER_EVENTS: string[] = [
|
|
12
108
|
'PAYMENT_PROVIDER_CREATE',
|
|
@@ -26,14 +122,9 @@ const asyncFilter = async (arr, predicate) => {
|
|
|
26
122
|
|
|
27
123
|
export const configurePaymentProvidersModule = (
|
|
28
124
|
PaymentProviders: mongodb.Collection<PaymentProvider>,
|
|
29
|
-
):
|
|
125
|
+
): PaymentProvidersModules => {
|
|
30
126
|
registerEvents(PAYMENT_PROVIDER_EVENTS);
|
|
31
127
|
|
|
32
|
-
const mutations = generateDbMutations<PaymentProvider>(
|
|
33
|
-
PaymentProviders,
|
|
34
|
-
PaymentProvidersSchema,
|
|
35
|
-
) as ModuleMutations<PaymentProvider>;
|
|
36
|
-
|
|
37
128
|
const getPaymentAdapter = async (
|
|
38
129
|
paymentProviderId: string,
|
|
39
130
|
paymentContext: PaymentContext,
|
|
@@ -185,7 +276,9 @@ export const configurePaymentProvidersModule = (
|
|
|
185
276
|
const Adapter = PaymentDirector.getAdapter(doc.adapterKey);
|
|
186
277
|
if (!Adapter) return null;
|
|
187
278
|
|
|
188
|
-
const paymentProviderId = await
|
|
279
|
+
const { insertedId: paymentProviderId } = await PaymentProviders.insertOne({
|
|
280
|
+
_id: generateDbObjectId(),
|
|
281
|
+
created: new Date(),
|
|
189
282
|
configuration: Adapter.initialConfiguration,
|
|
190
283
|
...doc,
|
|
191
284
|
});
|
|
@@ -199,23 +292,32 @@ export const configurePaymentProvidersModule = (
|
|
|
199
292
|
},
|
|
200
293
|
|
|
201
294
|
update: async (_id: string, doc: PaymentProvider) => {
|
|
202
|
-
await
|
|
203
|
-
|
|
295
|
+
const paymentProvider = await PaymentProviders.findOneAndUpdate(
|
|
296
|
+
generateDbFilterById(_id),
|
|
297
|
+
{
|
|
298
|
+
$set: {
|
|
299
|
+
updated: new Date(),
|
|
300
|
+
...doc,
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
{ returnDocument: 'after' },
|
|
304
|
+
);
|
|
204
305
|
await emit('PAYMENT_PROVIDER_UPDATE', { paymentProvider });
|
|
205
306
|
return paymentProvider;
|
|
206
307
|
},
|
|
207
308
|
|
|
208
309
|
delete: async (_id) => {
|
|
209
|
-
await
|
|
210
|
-
|
|
310
|
+
const paymentProvider = await PaymentProviders.findOneAndUpdate(
|
|
311
|
+
generateDbFilterById(_id),
|
|
312
|
+
{
|
|
313
|
+
$set: {
|
|
314
|
+
deleted: new Date(),
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
{ returnDocument: 'after' },
|
|
318
|
+
);
|
|
211
319
|
await emit('PAYMENT_PROVIDER_REMOVE', { paymentProvider });
|
|
212
320
|
return paymentProvider;
|
|
213
321
|
},
|
|
214
|
-
|
|
215
|
-
deletePermanently: async (_id) => {
|
|
216
|
-
const deliveryProvider = await PaymentProviders.findOne(generateDbFilterById(_id), {});
|
|
217
|
-
await mutations.deletePermanently(_id);
|
|
218
|
-
return deliveryProvider;
|
|
219
|
-
},
|
|
220
322
|
};
|
|
221
323
|
};
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { PaymentProviderType } from
|
|
2
|
-
import { buildFindSelector } from
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { PaymentProviderType } from '../payment-index.js';
|
|
2
|
+
import { buildFindSelector } from './configurePaymentProvidersModule.js';
|
|
5
3
|
|
|
6
4
|
describe('Payment', () => {
|
|
7
5
|
describe('buildFindSelector', () => {
|
|
8
|
-
it('Return correct filter object when passed no argument',
|
|
9
|
-
expect(buildFindSelector({})).toEqual({ deleted: null })
|
|
6
|
+
it('Return correct filter object when passed no argument', () => {
|
|
7
|
+
expect(buildFindSelector({})).toEqual({ deleted: null });
|
|
10
8
|
});
|
|
11
9
|
|
|
12
|
-
it('Return correct filter object when passed no argument',
|
|
13
|
-
expect(buildFindSelector({type: PaymentProviderType.GENERIC})).toEqual({
|
|
10
|
+
it('Return correct filter object when passed no argument', () => {
|
|
11
|
+
expect(buildFindSelector({ type: PaymentProviderType.GENERIC })).toEqual({
|
|
12
|
+
type: 'GENERIC',
|
|
13
|
+
deleted: null,
|
|
14
|
+
});
|
|
14
15
|
});
|
|
15
|
-
})
|
|
16
|
-
|
|
16
|
+
});
|
|
17
17
|
});
|
package/src/payment-index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './module/configurePaymentModule.js';
|
|
3
|
+
export * from './payment-settings.js';
|
|
2
4
|
|
|
3
|
-
export
|
|
5
|
+
export * from './director/PaymentDirector.js';
|
|
6
|
+
export * from './director/PaymentAdapter.js';
|
|
7
|
+
export * from './director/PaymentPricingAdapter.js';
|
|
8
|
+
export * from './director/PaymentPricingDirector.js';
|
|
9
|
+
export * from './director/PaymentPricingSheet.js';
|
|
4
10
|
|
|
5
|
-
export { PaymentDirector } from './director/PaymentDirector.js';
|
|
6
|
-
export { PaymentAdapter } from './director/PaymentAdapter.js';
|
|
7
11
|
export { PaymentError } from './director/PaymentError.js';
|
|
8
12
|
export { PaymentProviderType } from './director/PaymentProviderType.js';
|
|
9
|
-
|
|
10
|
-
export { PaymentPricingAdapter } from './director/PaymentPricingAdapter.js';
|
|
11
|
-
export { PaymentPricingDirector } from './director/PaymentPricingDirector.js';
|