@unchainedshop/core-payment 3.0.0-alpha7 → 3.0.0-rc10
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/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 +1 -1
- package/lib/payment-settings.js.map +1 -1
- package/package.json +13 -13
- 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 +7 -8
- package/tsconfig.json +5 -7
- package/lib/director/PaymentAdapter.d.ts +0 -3
- package/lib/director/PaymentAdapter.d.ts.map +0 -1
- package/lib/director/PaymentAdapter.js +0 -49
- package/lib/director/PaymentAdapter.js.map +0 -1
- package/lib/director/PaymentDirector.d.ts +0 -3
- package/lib/director/PaymentDirector.d.ts.map +0 -1
- package/lib/director/PaymentDirector.js +0 -84
- package/lib/director/PaymentDirector.js.map +0 -1
- package/lib/director/PaymentError.d.ts +0 -7
- package/lib/director/PaymentError.d.ts.map +0 -1
- package/lib/director/PaymentError.js +0 -8
- package/lib/director/PaymentError.js.map +0 -1
- package/lib/director/PaymentPricingAdapter.d.ts +0 -46
- package/lib/director/PaymentPricingAdapter.d.ts.map +0 -1
- package/lib/director/PaymentPricingAdapter.js +0 -25
- package/lib/director/PaymentPricingAdapter.js.map +0 -1
- package/lib/director/PaymentPricingDirector.d.ts +0 -19
- package/lib/director/PaymentPricingDirector.d.ts.map +0 -1
- package/lib/director/PaymentPricingDirector.js +0 -58
- package/lib/director/PaymentPricingDirector.js.map +0 -1
- package/lib/director/PaymentPricingSheet.d.ts +0 -10
- package/lib/director/PaymentPricingSheet.d.ts.map +0 -1
- package/lib/director/PaymentPricingSheet.js +0 -75
- package/lib/director/PaymentPricingSheet.js.map +0 -1
- package/lib/director/PaymentProviderType.d.ts +0 -6
- package/lib/director/PaymentProviderType.d.ts.map +0 -1
- package/lib/director/PaymentProviderType.js +0 -7
- package/lib/director/PaymentProviderType.js.map +0 -1
- package/lib/types.d.ts +0 -84
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -17
- package/lib/types.js.map +0 -1
- 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
package/lib/types.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -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
|
-
};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IPaymentPricingAdapter,
|
|
3
|
-
IPaymentPricingSheet,
|
|
4
|
-
PaymentPricingAdapterContext,
|
|
5
|
-
PaymentPricingCalculation,
|
|
6
|
-
} from './PaymentPricingAdapter.js';
|
|
7
|
-
import { BasePricingDirector } from '@unchainedshop/utils';
|
|
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
|
-
>;
|
|
35
|
-
|
|
36
|
-
const baseDirector = BasePricingDirector<
|
|
37
|
-
PaymentPricingContext,
|
|
38
|
-
PaymentPricingAdapterContext,
|
|
39
|
-
PaymentPricingCalculation,
|
|
40
|
-
IPaymentPricingAdapter
|
|
41
|
-
>('PaymentPricingDirector');
|
|
42
|
-
|
|
43
|
-
export const PaymentPricingDirector: IPaymentPricingDirector = {
|
|
44
|
-
...baseDirector,
|
|
45
|
-
|
|
46
|
-
async buildPricingContext(context, unchainedAPI) {
|
|
47
|
-
const { modules } = unchainedAPI;
|
|
48
|
-
|
|
49
|
-
if ('item' in context) {
|
|
50
|
-
const { item } = context;
|
|
51
|
-
const order = await modules.orders.findOrder({
|
|
52
|
-
orderId: item.orderId,
|
|
53
|
-
});
|
|
54
|
-
const provider = await modules.payment.paymentProviders.findProvider({
|
|
55
|
-
paymentProviderId: item.paymentProviderId,
|
|
56
|
-
});
|
|
57
|
-
const user = await modules.users.findUserById(order.userId);
|
|
58
|
-
const discounts = await modules.orders.discounts.findOrderDiscounts({
|
|
59
|
-
orderId: item.orderId,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
...unchainedAPI,
|
|
64
|
-
country: order.countryCode,
|
|
65
|
-
currency: order.currency,
|
|
66
|
-
order,
|
|
67
|
-
provider,
|
|
68
|
-
user,
|
|
69
|
-
discounts,
|
|
70
|
-
orderPayment: item,
|
|
71
|
-
providerContext: null,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
...unchainedAPI,
|
|
77
|
-
country: context.country,
|
|
78
|
-
currency: context.currency,
|
|
79
|
-
order: context.order,
|
|
80
|
-
provider: context.provider,
|
|
81
|
-
user: context.user,
|
|
82
|
-
discounts: [],
|
|
83
|
-
orderPayment: null,
|
|
84
|
-
providerContext: context.providerContext,
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
async actions(pricingContext, unchainedAPI) {
|
|
89
|
-
const actions = await baseDirector.actions(pricingContext, unchainedAPI, this.buildPricingContext);
|
|
90
|
-
return {
|
|
91
|
-
...actions,
|
|
92
|
-
calculationSheet() {
|
|
93
|
-
const context = actions.getContext();
|
|
94
|
-
return PaymentPricingSheet({
|
|
95
|
-
calculation: actions.getCalculation(),
|
|
96
|
-
currency: context.currency,
|
|
97
|
-
});
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
},
|
|
101
|
-
};
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { BasePricingSheet } from '@unchainedshop/utils';
|
|
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
|
-
}
|
|
11
|
-
|
|
12
|
-
export const PaymentPricingSheet = (
|
|
13
|
-
params: PricingSheetParams<PaymentPricingCalculation>,
|
|
14
|
-
): IPaymentPricingSheet => {
|
|
15
|
-
const basePricingSheet: IBasePricingSheet<PaymentPricingCalculation> = BasePricingSheet(params);
|
|
16
|
-
|
|
17
|
-
const pricingSheet: IPaymentPricingSheet = {
|
|
18
|
-
...basePricingSheet,
|
|
19
|
-
|
|
20
|
-
addDiscount({ amount, isTaxable, isNetPrice, discountId, meta }) {
|
|
21
|
-
basePricingSheet.calculation.push({
|
|
22
|
-
category: PaymentPricingRowCategory.Discount,
|
|
23
|
-
amount,
|
|
24
|
-
isTaxable,
|
|
25
|
-
isNetPrice,
|
|
26
|
-
discountId,
|
|
27
|
-
meta,
|
|
28
|
-
});
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
addFee({ amount, isTaxable, isNetPrice, meta }) {
|
|
32
|
-
basePricingSheet.calculation.push({
|
|
33
|
-
category: PaymentPricingRowCategory.Payment,
|
|
34
|
-
amount,
|
|
35
|
-
isTaxable,
|
|
36
|
-
isNetPrice,
|
|
37
|
-
meta,
|
|
38
|
-
});
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
addTax({ amount, rate, meta }) {
|
|
42
|
-
basePricingSheet.calculation.push({
|
|
43
|
-
category: PaymentPricingRowCategory.Tax,
|
|
44
|
-
amount,
|
|
45
|
-
isTaxable: false,
|
|
46
|
-
isNetPrice: false,
|
|
47
|
-
rate,
|
|
48
|
-
meta,
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
taxSum(filter) {
|
|
53
|
-
return basePricingSheet.sum({
|
|
54
|
-
category: PaymentPricingRowCategory.Tax,
|
|
55
|
-
...(filter || {}),
|
|
56
|
-
});
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
discountPrices(explicitDiscountId) {
|
|
60
|
-
const discountIds = pricingSheet
|
|
61
|
-
.filterBy({
|
|
62
|
-
category: PaymentPricingRowCategory.Discount,
|
|
63
|
-
discountId: explicitDiscountId,
|
|
64
|
-
})
|
|
65
|
-
.map(({ discountId }) => discountId);
|
|
66
|
-
|
|
67
|
-
return [...new Set(discountIds)]
|
|
68
|
-
.map((discountId) => {
|
|
69
|
-
const amount = basePricingSheet.sum({
|
|
70
|
-
category: PaymentPricingRowCategory.Discount,
|
|
71
|
-
discountId,
|
|
72
|
-
});
|
|
73
|
-
if (!amount) {
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
discountId,
|
|
78
|
-
amount: Math.round(amount),
|
|
79
|
-
currency: basePricingSheet.currency,
|
|
80
|
-
};
|
|
81
|
-
})
|
|
82
|
-
.filter(Boolean);
|
|
83
|
-
},
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
return pricingSheet;
|
|
87
|
-
};
|
package/src/types.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
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
|
-
|
|
7
|
-
export enum PaymentProviderType {
|
|
8
|
-
CARD = 'CARD',
|
|
9
|
-
INVOICE = 'INVOICE',
|
|
10
|
-
GENERIC = 'GENERIC',
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type PaymentConfiguration = Array<{
|
|
14
|
-
key: string;
|
|
15
|
-
value: string | null;
|
|
16
|
-
}>;
|
|
17
|
-
|
|
18
|
-
export type PaymentProvider = {
|
|
19
|
-
_id?: string;
|
|
20
|
-
type: PaymentProviderType;
|
|
21
|
-
adapterKey: string;
|
|
22
|
-
configuration: PaymentConfiguration;
|
|
23
|
-
} & TimestampFields;
|
|
24
|
-
|
|
25
|
-
export type PaymentCredentials = {
|
|
26
|
-
_id?: string;
|
|
27
|
-
paymentProviderId: string;
|
|
28
|
-
userId: string;
|
|
29
|
-
token?: string;
|
|
30
|
-
isPreferred?: boolean;
|
|
31
|
-
meta: any;
|
|
32
|
-
} & TimestampFields;
|
|
33
|
-
|
|
34
|
-
export enum PaymentError {
|
|
35
|
-
ADAPTER_NOT_FOUND = 'ADAPTER_NOT_FOUND',
|
|
36
|
-
NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',
|
|
37
|
-
INCOMPLETE_CONFIGURATION = 'INCOMPLETE_CONFIGURATION',
|
|
38
|
-
WRONG_CREDENTIALS = 'WRONG_CREDENTIALS',
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface PaymentContext {
|
|
42
|
-
userId?: string;
|
|
43
|
-
order?: Order;
|
|
44
|
-
orderPayment?: OrderPayment;
|
|
45
|
-
transactionContext?: any; // User for singing and charging a payment
|
|
46
|
-
token?: any; // Used for validation
|
|
47
|
-
meta?: any;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export type ChargeResult = {
|
|
51
|
-
transactionId?: string;
|
|
52
|
-
[key: string]: any;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export type PaymentChargeActionResult = ChargeResult & {
|
|
56
|
-
credentials?: {
|
|
57
|
-
token: string;
|
|
58
|
-
[key: string]: any;
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
export interface IPaymentActions {
|
|
62
|
-
charge: (transactionContext?: any) => Promise<PaymentChargeActionResult | false>;
|
|
63
|
-
configurationError: (transactionContext?: any) => PaymentError;
|
|
64
|
-
isActive: (transactionContext?: any) => boolean;
|
|
65
|
-
isPayLaterAllowed: (transactionContext?: any) => boolean;
|
|
66
|
-
register: (transactionContext?: any) => Promise<any>;
|
|
67
|
-
sign: (transactionContext?: any) => Promise<string>;
|
|
68
|
-
validate: (token?: any) => Promise<boolean>;
|
|
69
|
-
cancel: (transactionContext?: any) => Promise<boolean>;
|
|
70
|
-
confirm: (transactionContext?: any) => Promise<boolean>;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export type IPaymentAdapter = IBaseAdapter & {
|
|
74
|
-
initialConfiguration: PaymentConfiguration;
|
|
75
|
-
|
|
76
|
-
typeSupported: (type: PaymentProviderType) => boolean;
|
|
77
|
-
|
|
78
|
-
actions: (params: {
|
|
79
|
-
config: PaymentConfiguration;
|
|
80
|
-
paymentContext: PaymentContext & {
|
|
81
|
-
paymentProviderId: string;
|
|
82
|
-
paymentProvider: PaymentProvider;
|
|
83
|
-
};
|
|
84
|
-
context: UnchainedCore;
|
|
85
|
-
}) => IPaymentActions;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export type IPaymentDirector = IBaseDirector<IPaymentAdapter> & {
|
|
89
|
-
actions: (
|
|
90
|
-
paymentProvider: PaymentProvider,
|
|
91
|
-
paymentContext: PaymentContext,
|
|
92
|
-
unchainedAPI: UnchainedCore,
|
|
93
|
-
) => Promise<IPaymentActions>;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
/*
|
|
97
|
-
* Module
|
|
98
|
-
*/
|
|
99
|
-
|
|
100
|
-
export interface PaymentInterface {
|
|
101
|
-
_id: string;
|
|
102
|
-
label: string;
|
|
103
|
-
version: string;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/*
|
|
107
|
-
* Settings
|
|
108
|
-
*/
|