@unchainedshop/core-payment 3.0.0-alpha7 → 3.0.0-rc2

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.
Files changed (61) hide show
  1. package/lib/db/PaymentCredentialsCollection.d.ts +10 -3
  2. package/lib/db/PaymentCredentialsCollection.d.ts.map +1 -1
  3. package/lib/db/PaymentCredentialsCollection.js +3 -3
  4. package/lib/db/PaymentCredentialsCollection.js.map +1 -1
  5. package/lib/db/PaymentProvidersCollection.d.ts +16 -2
  6. package/lib/db/PaymentProvidersCollection.d.ts.map +1 -1
  7. package/lib/db/PaymentProvidersCollection.js +6 -0
  8. package/lib/db/PaymentProvidersCollection.js.map +1 -1
  9. package/lib/director/PaymentAdapter.d.ts +1 -1
  10. package/lib/director/PaymentAdapter.d.ts.map +1 -1
  11. package/lib/director/PaymentAdapter.js.map +1 -1
  12. package/lib/director/PaymentDirector.d.ts +1 -1
  13. package/lib/director/PaymentDirector.d.ts.map +1 -1
  14. package/lib/director/PaymentDirector.js +5 -8
  15. package/lib/director/PaymentDirector.js.map +1 -1
  16. package/lib/director/PaymentPricingAdapter.d.ts +2 -4
  17. package/lib/director/PaymentPricingAdapter.d.ts.map +1 -1
  18. package/lib/director/PaymentPricingAdapter.js.map +1 -1
  19. package/lib/director/PaymentPricingDirector.d.ts +3 -4
  20. package/lib/director/PaymentPricingDirector.d.ts.map +1 -1
  21. package/lib/director/PaymentPricingDirector.js.map +1 -1
  22. package/lib/module/configurePaymentCredentialsModule.d.ts +11 -12
  23. package/lib/module/configurePaymentCredentialsModule.d.ts.map +1 -1
  24. package/lib/module/configurePaymentCredentialsModule.js +3 -3
  25. package/lib/module/configurePaymentCredentialsModule.js.map +1 -1
  26. package/lib/module/configurePaymentModule.d.ts +35 -10
  27. package/lib/module/configurePaymentModule.d.ts.map +1 -1
  28. package/lib/module/configurePaymentModule.js +2 -18
  29. package/lib/module/configurePaymentModule.js.map +1 -1
  30. package/lib/module/configurePaymentProvidersModule.d.ts +17 -41
  31. package/lib/module/configurePaymentProvidersModule.d.ts.map +1 -1
  32. package/lib/module/configurePaymentProvidersModule.js +0 -101
  33. package/lib/module/configurePaymentProvidersModule.js.map +1 -1
  34. package/lib/payment-index.d.ts +2 -8
  35. package/lib/payment-index.d.ts.map +1 -1
  36. package/lib/payment-index.js +2 -8
  37. package/lib/payment-index.js.map +1 -1
  38. package/lib/payment-settings.d.ts +6 -7
  39. package/lib/payment-settings.d.ts.map +1 -1
  40. package/lib/payment-settings.js.map +1 -1
  41. package/lib/types.d.ts +8 -22
  42. package/lib/types.d.ts.map +1 -1
  43. package/lib/types.js.map +1 -1
  44. package/package.json +9 -12
  45. package/src/db/PaymentCredentialsCollection.ts +13 -5
  46. package/src/db/PaymentProvidersCollection.ts +19 -2
  47. package/src/module/configurePaymentCredentialsModule.ts +42 -40
  48. package/src/module/configurePaymentModule.ts +5 -57
  49. package/src/module/configurePaymentProvidersModule.ts +27 -239
  50. package/src/module/payment-index.test.ts +1 -1
  51. package/src/payment-index.ts +2 -10
  52. package/src/payment-settings.ts +6 -7
  53. package/tsconfig.json +5 -7
  54. package/src/director/PaymentAdapter.ts +0 -63
  55. package/src/director/PaymentDirector.ts +0 -99
  56. package/src/director/PaymentError.ts +0 -6
  57. package/src/director/PaymentPricingAdapter.ts +0 -87
  58. package/src/director/PaymentPricingDirector.ts +0 -101
  59. package/src/director/PaymentPricingSheet.ts +0 -87
  60. package/src/director/PaymentProviderType.ts +0 -5
  61. package/src/types.ts +0 -108
@@ -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
- };
@@ -1,5 +0,0 @@
1
- export enum PaymentProviderType {
2
- CARD = 'CARD',
3
- INVOICE = 'INVOICE',
4
- GENERIC = 'GENERIC',
5
- }
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
- */