@commercetools/checkout-payments-processor-sdk 0.0.6

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 (105) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +42 -0
  3. package/biome.json +38 -0
  4. package/package.json +48 -0
  5. package/src/api/context/request-context.helper.test.ts +98 -0
  6. package/src/api/context/request-context.helper.ts +89 -0
  7. package/src/api/context/request-context.provider.ts +27 -0
  8. package/src/api/context/types/request-context.type.ts +18 -0
  9. package/src/api/handlers/payment-components.handler.test.ts +378 -0
  10. package/src/api/handlers/payment-components.handler.ts +80 -0
  11. package/src/api/handlers/payment-intents.handler.test.ts +475 -0
  12. package/src/api/handlers/payment-intents.handler.ts +169 -0
  13. package/src/api/handlers/status.handler.test.ts +551 -0
  14. package/src/api/handlers/status.handler.ts +216 -0
  15. package/src/api/handlers/transaction.handler.ts +86 -0
  16. package/src/api/handlers/types/handler.type.ts +49 -0
  17. package/src/api/hooks/authorize.hook.test.ts +62 -0
  18. package/src/api/hooks/authorize.hook.ts +51 -0
  19. package/src/api/hooks/jwt-auth.hook.test.ts +54 -0
  20. package/src/api/hooks/jwt-auth.hook.ts +56 -0
  21. package/src/api/hooks/oauth2-auth.hook.test.ts +173 -0
  22. package/src/api/hooks/oauth2-auth.hook.ts +46 -0
  23. package/src/api/hooks/session-header-auth.hook.ts +47 -0
  24. package/src/api/hooks/session-query-param-auth.hook.ts +46 -0
  25. package/src/api/hooks/types/hook.type.ts +12 -0
  26. package/src/api/index.ts +11 -0
  27. package/src/api/types.ts +3 -0
  28. package/src/commercetools/api/base-api.ts +28 -0
  29. package/src/commercetools/api/cart-api.test.ts +95 -0
  30. package/src/commercetools/api/cart-api.ts +55 -0
  31. package/src/commercetools/api/custom-type-api.ts +39 -0
  32. package/src/commercetools/api/order-api.ts +22 -0
  33. package/src/commercetools/api/payment-api.ts +64 -0
  34. package/src/commercetools/api/payment-method-api.ts +118 -0
  35. package/src/commercetools/api/root-api.ts +109 -0
  36. package/src/commercetools/errors/ct-api.error.ts +27 -0
  37. package/src/commercetools/helpers/currency.converter.test.ts +121 -0
  38. package/src/commercetools/helpers/currency.converter.test_data.json +2379 -0
  39. package/src/commercetools/helpers/currency.converter.ts +102 -0
  40. package/src/commercetools/helpers/taxrate.converter.test.ts +48 -0
  41. package/src/commercetools/helpers/taxrate.converter.ts +39 -0
  42. package/src/commercetools/index.ts +22 -0
  43. package/src/commercetools/services/ct-cart.service.test.ts +1616 -0
  44. package/src/commercetools/services/ct-cart.service.ts +316 -0
  45. package/src/commercetools/services/ct-custom-type.service.ts +62 -0
  46. package/src/commercetools/services/ct-order.service.test.ts +136 -0
  47. package/src/commercetools/services/ct-order.service.ts +47 -0
  48. package/src/commercetools/services/ct-payment-method.service.test.ts +878 -0
  49. package/src/commercetools/services/ct-payment-method.service.ts +223 -0
  50. package/src/commercetools/services/ct-payment.service.test.ts +1123 -0
  51. package/src/commercetools/services/ct-payment.service.ts +574 -0
  52. package/src/commercetools/types/api.type.ts +162 -0
  53. package/src/commercetools/types/cart.type.ts +83 -0
  54. package/src/commercetools/types/custom-type.type.ts +21 -0
  55. package/src/commercetools/types/order.type.ts +19 -0
  56. package/src/commercetools/types/payment-method.type.ts +130 -0
  57. package/src/commercetools/types/payment.type.ts +74 -0
  58. package/src/commercetools/types/predefined-custom-types.type.ts +240 -0
  59. package/src/commercetools/types.ts +7 -0
  60. package/src/errorx/errorx.ts +625 -0
  61. package/src/errorx/index.ts +1 -0
  62. package/src/errorx/types.ts +5 -0
  63. package/src/fetch/decorators/base.decorator.ts +13 -0
  64. package/src/fetch/decorators/monitoring.decorator.ts +41 -0
  65. package/src/fetch/fetch.test.ts +57 -0
  66. package/src/fetch/index.ts +3 -0
  67. package/src/fetch/types/fetch.type.ts +11 -0
  68. package/src/fetch/types.ts +1 -0
  69. package/src/index.ts +15 -0
  70. package/src/logger/commercetools-logger.ts +69 -0
  71. package/src/logger/index.ts +1 -0
  72. package/src/logger/types/logger.type.ts +6 -0
  73. package/src/logger/types.ts +1 -0
  74. package/src/mocks/auth.mock.ts +6 -0
  75. package/src/mocks/cart.mock.ts +239 -0
  76. package/src/mocks/ct-api-error.mock.ts +23 -0
  77. package/src/mocks/index.ts +4 -0
  78. package/src/mocks/payment.mock.ts +35 -0
  79. package/src/payment-processor.ts +317 -0
  80. package/src/security/authn/authns.ts +219 -0
  81. package/src/security/authn/bearer-utils.test.ts +35 -0
  82. package/src/security/authn/bearer-utils.ts +28 -0
  83. package/src/security/authn/jwt-authn-manager.ts +69 -0
  84. package/src/security/authn/oauth2-authn-manager.ts +105 -0
  85. package/src/security/authn/session-header-authn-manager.ts +58 -0
  86. package/src/security/authn/session-query-param-authn-manager.ts +53 -0
  87. package/src/security/authn/types/authn.type.ts +50 -0
  88. package/src/security/authz/authorization-manager.ts +39 -0
  89. package/src/security/authz/types/authz.type.ts +13 -0
  90. package/src/security/index.ts +13 -0
  91. package/src/security/services/authorization.service.ts +54 -0
  92. package/src/security/services/jwt.service.test.ts +27 -0
  93. package/src/security/services/jwt.service.ts +45 -0
  94. package/src/security/services/oauth2.service.ts +60 -0
  95. package/src/security/services/session.service.ts +141 -0
  96. package/src/security/services/types/authorization.type.ts +10 -0
  97. package/src/security/services/types/jwt.type.ts +3 -0
  98. package/src/security/services/types/oauth2.type.ts +15 -0
  99. package/src/security/services/types/session.type.ts +38 -0
  100. package/src/security/types.ts +6 -0
  101. package/tsconfig.json +28 -0
  102. package/tsconfig.prod.json +9 -0
  103. package/typedoc.json +6 -0
  104. package/vitest.config.ts +15 -0
  105. package/wiki/getting-started.md +13 -0
@@ -0,0 +1,162 @@
1
+ import type {
2
+ ByProjectKeyRequestBuilder,
3
+ Cart,
4
+ CartPagedQueryResponse,
5
+ OrderPagedQueryResponse,
6
+ Payment,
7
+ PaymentDraft,
8
+ PaymentMethod,
9
+ PaymentMethodDraft,
10
+ PaymentMethodPagedQueryResponse,
11
+ PaymentMethodUpdateAction,
12
+ PaymentPagedQueryResponse,
13
+ PaymentUpdateAction,
14
+ Type,
15
+ TypeDraft,
16
+ TypePagedQueryResponse,
17
+ } from '@commercetools/platform-sdk';
18
+
19
+ export type CommercetoolsClient = ByProjectKeyRequestBuilder;
20
+ export type CommercetoolsSessionClient = ByProjectKeyRequestBuilder;
21
+
22
+ /**
23
+ * API Options
24
+ */
25
+ export interface APIOpts {
26
+ client: CommercetoolsClient;
27
+ }
28
+
29
+ /**
30
+ * Update Resource
31
+ */
32
+ export type UpdateResource = {
33
+ id: string;
34
+ version: number;
35
+ };
36
+
37
+ /**
38
+ * Add Payment
39
+ */
40
+ export type AddPayment = {
41
+ resource: UpdateResource;
42
+ paymentId: string;
43
+ };
44
+
45
+ /**
46
+ * Update Payment
47
+ */
48
+ export type UpdatePayment = {
49
+ resource: UpdateResource;
50
+ actions: PaymentUpdateAction[];
51
+ };
52
+
53
+ /**
54
+ * Oauth Token
55
+ */
56
+ export type OauthToken = {
57
+ accessToken: string;
58
+ expiresIn: number;
59
+ tokenType: string;
60
+ scope: string;
61
+ refreshToken?: string;
62
+ };
63
+
64
+ /**
65
+ * Update Payment Method
66
+ */
67
+ export type UpdatePaymentMethod = {
68
+ resource: UpdateResource;
69
+ actions: PaymentMethodUpdateAction[];
70
+ };
71
+
72
+ /**
73
+ * Find Payment Methods
74
+ */
75
+ export type FindPaymentMethods = {
76
+ queryString: string;
77
+ };
78
+
79
+ /**
80
+ * Get Payment Method
81
+ */
82
+ export type GetPaymentMethod = {
83
+ id: string;
84
+ };
85
+
86
+ /**
87
+ * Delete Payment Method
88
+ */
89
+ export type DeletePaymentMethod = {
90
+ id: string;
91
+ version: number;
92
+ };
93
+
94
+ /**
95
+ * Check If Exists By Predicate
96
+ */
97
+ export type CheckIfExistsByPredicate = {
98
+ queryString: string;
99
+ };
100
+
101
+ /**
102
+ * Authorization API
103
+ */
104
+ export interface AuthorizationAPI {
105
+ getToken(): Promise<OauthToken>;
106
+ }
107
+
108
+ /**
109
+ * Cart API
110
+ */
111
+ export interface CartAPI {
112
+ getCartById(id: string): Promise<Cart>;
113
+ find(queryPredicate: string): Promise<CartPagedQueryResponse>;
114
+ addPayment(opts: AddPayment): Promise<Cart>;
115
+ }
116
+
117
+ /**
118
+ * Payment API
119
+ */
120
+ export interface PaymentAPI {
121
+ getPaymentById(id: string): Promise<Payment>;
122
+ find(queryPredicate: string): Promise<PaymentPagedQueryResponse>;
123
+ createPayment(paymentDraft: PaymentDraft): Promise<Payment>;
124
+ updatePayment(opts: UpdatePayment): Promise<Payment>;
125
+ }
126
+
127
+ /**
128
+ * Payment Method API
129
+ */
130
+ export interface PaymentMethodAPI {
131
+ find(opts: FindPaymentMethods): Promise<PaymentMethodPagedQueryResponse>;
132
+ get(opts: GetPaymentMethod): Promise<PaymentMethod>;
133
+ create(draft: PaymentMethodDraft): Promise<PaymentMethod>;
134
+ update(opts: UpdatePaymentMethod): Promise<PaymentMethod>;
135
+ delete(opts: DeletePaymentMethod): Promise<PaymentMethod>;
136
+ checkIfExistsByPredicate(opts: CheckIfExistsByPredicate): Promise<boolean>;
137
+ }
138
+
139
+ /**
140
+ * Order API
141
+ */
142
+ export interface OrderAPI {
143
+ find(queryPredicate: string): Promise<OrderPagedQueryResponse>;
144
+ }
145
+
146
+ export interface CustomTypeAPI {
147
+ getByKey(key: string): Promise<Type>;
148
+ find(queryPredicate: string): Promise<TypePagedQueryResponse>;
149
+ create(draft: TypeDraft): Promise<Type>;
150
+ }
151
+
152
+ /**
153
+ * Commercetools API is a collection of APIs that can be used to interact with the Commercetools platform.
154
+ * It gives advanced functionality to the Commercetools payment related .
155
+ */
156
+ export interface CommercetoolsAPI {
157
+ cart: CartAPI;
158
+ payment: PaymentAPI;
159
+ order: OrderAPI;
160
+ paymentMethod: PaymentMethodAPI;
161
+ customType: CustomTypeAPI;
162
+ }
@@ -0,0 +1,83 @@
1
+ import type { Address, Cart, ShippingInfo } from '@commercetools/platform-sdk';
2
+ import type {
3
+ ContextProvider,
4
+ RequestContextData,
5
+ } from '@/api/context/types/request-context.type.js';
6
+ import type {
7
+ AddPayment,
8
+ CommercetoolsAPI,
9
+ } from '@/commercetools/types/api.type.js';
10
+ import type { PaymentAmount } from '@/commercetools/types/payment.type.js';
11
+ import type { Logger } from '@/logger/types/logger.type.js';
12
+
13
+ export type GetCart = {
14
+ id: string;
15
+ version?: number;
16
+ };
17
+
18
+ export type GetPaymentAmount = {
19
+ cart: Cart;
20
+ };
21
+
22
+ export type GetOneShippingAddress = {
23
+ cart: Pick<Cart, 'shippingAddress' | 'shipping' | 'shippingMode'>;
24
+ };
25
+
26
+ export type GetNormalizedShipping = {
27
+ cart: Pick<
28
+ Cart,
29
+ 'shippingInfo' | 'shipping' | 'shippingMode' | 'shippingAddress'
30
+ >;
31
+ };
32
+
33
+ export type NormalizedShipping = {
34
+ shippingAddress: Address;
35
+ shippingInfo: ShippingInfo;
36
+ };
37
+
38
+ export type ShippingAmounts = {
39
+ priceCentAmount: number;
40
+ taxRateAmount: number;
41
+ totalNetCentAmount: number;
42
+ totalGrossCentAmount: number;
43
+ totalTaxCentAmount: number;
44
+ };
45
+
46
+ export type CartServiceOptions = {
47
+ ctAPI: CommercetoolsAPI;
48
+ logger: Logger;
49
+ contextProvider: ContextProvider<RequestContextData>;
50
+ };
51
+
52
+ export type GetCartByPaymentIdRequest = {
53
+ paymentId: string;
54
+ };
55
+
56
+ /**
57
+ * Cart service interface exposes methods to interact with the commercetools platform API.
58
+ */
59
+ export interface CartService {
60
+ getCartByPaymentId(opts: GetCartByPaymentIdRequest): Promise<Cart>;
61
+ getCart(opts: GetCart): Promise<Cart>;
62
+ /**
63
+ * Get payment amount for a cart. This method is used to calculate the payment amount for a cart.
64
+ * It takes into account existing successful payments.
65
+ * @param opts
66
+ */
67
+ getPaymentAmount(opts: GetPaymentAmount): Promise<PaymentAmount>;
68
+ /**
69
+ * Get payment amount for a cart. This method is used to calculate the payment amount for a cart.
70
+ * It takes into account existing successful payments and giftcards connectors.
71
+ * This method is specifically used for calculating the payment amount for a cart when a giftcard is applied and the payment session is created upfront.
72
+ * @param opts
73
+ */
74
+ getPlannedPaymentAmount(opts: GetPaymentAmount): Promise<PaymentAmount>;
75
+ addPayment(opts: AddPayment): Promise<Cart>;
76
+ /**
77
+ * Checks if the given cart contains items configured for recurring orders.
78
+ *
79
+ * @param cart - The cart to check
80
+ * @returns true if the cart contains recurring items, false otherwise
81
+ */
82
+ isRecurringCart(cart: Cart): boolean;
83
+ }
@@ -0,0 +1,21 @@
1
+ import type {
2
+ Type,
3
+ TypeDraft,
4
+ TypePagedQueryResponse,
5
+ } from '@commercetools/platform-sdk';
6
+
7
+ export type GetCustomType = {
8
+ key: string;
9
+ };
10
+
11
+ export type FindCustomType = {
12
+ predicate: string;
13
+ };
14
+
15
+ export interface CustomTypeService {
16
+ getCustomType(opts: GetCustomType): Promise<Type>;
17
+ findCustomType(opts: FindCustomType): Promise<TypePagedQueryResponse>;
18
+ createCustomType(draft: TypeDraft): Promise<Type>;
19
+ createPredefinedInterfaceInteractionType(): Promise<Type>;
20
+ createPredefinedPaymentMethodsTypes(): Promise<Type[]>;
21
+ }
@@ -0,0 +1,19 @@
1
+ import type { Order } from '@commercetools/platform-sdk';
2
+ import type { CommercetoolsAPI } from '@/commercetools/types/api.type.js';
3
+ import type { Logger } from '@/logger/types/logger.type.js';
4
+
5
+ export type OrderServiceOptions = {
6
+ ctAPI: CommercetoolsAPI;
7
+ logger: Logger;
8
+ };
9
+
10
+ export type GetOrderByPaymentIdRequest = {
11
+ paymentId: string;
12
+ };
13
+
14
+ /**
15
+ * Order service interface exposes methods to interact with the commercetools platform API.
16
+ */
17
+ export interface OrderService {
18
+ getOrderByPaymentId(opts: GetOrderByPaymentIdRequest): Promise<Order>;
19
+ }
@@ -0,0 +1,130 @@
1
+ import type {
2
+ PaymentMethod,
3
+ PaymentMethodPagedQueryResponse,
4
+ PaymentMethodUpdateAction,
5
+ } from '@commercetools/platform-sdk';
6
+ import type {
7
+ CommercetoolsAPI,
8
+ UpdateResource,
9
+ } from '@/commercetools/types/api.type.js';
10
+ import type { Logger } from '@/logger/types/logger.type.js';
11
+
12
+ export type PaymentMethodServiceOptions = {
13
+ ctAPI: CommercetoolsAPI;
14
+ logger: Logger;
15
+ };
16
+
17
+ export type FindPaymentMethod = {
18
+ customerId: string;
19
+ paymentInterface: string;
20
+ interfaceAccount?: string;
21
+ };
22
+
23
+ export type GetPaymentMethodService = {
24
+ customerId: string;
25
+ id: string;
26
+ paymentInterface: string;
27
+ interfaceAccount?: string;
28
+ };
29
+
30
+ export type GetByTokenValuePaymentMethod = {
31
+ customerId: string;
32
+ tokenValue: string;
33
+ paymentInterface: string;
34
+ interfaceAccount?: string;
35
+ };
36
+
37
+ export type DoesTokenBelongToCustomer = {
38
+ customerId: string;
39
+ tokenValue: string;
40
+ paymentInterface: string;
41
+ interfaceAccount?: string;
42
+ };
43
+
44
+ export type DeletePaymentMethodService = {
45
+ customerId: string;
46
+ id: string;
47
+ version: number;
48
+ };
49
+
50
+ export type UpdatePaymentMethodService = {
51
+ customerId: string;
52
+ resource: UpdateResource;
53
+ actions: PaymentMethodUpdateAction[];
54
+ };
55
+
56
+ export type SavePaymentMethodDraft = {
57
+ customerId: string;
58
+ token: string;
59
+ paymentInterface: string;
60
+ interfaceAccount?: string;
61
+ method: string;
62
+ };
63
+
64
+ /**
65
+ * Payment methods service interface exposes methods to interact with the commercetools platform API.
66
+ */
67
+ export interface PaymentMethodService {
68
+ /**
69
+ * Finds all of the payment-methods that match with the given parameters.
70
+ *
71
+ * @param opts the parameters to search by
72
+ *
73
+ * @returns a paged list of payment-methods
74
+ */
75
+ find(opts: FindPaymentMethod): Promise<PaymentMethodPagedQueryResponse>;
76
+
77
+ /**
78
+ * Returns a payment-method by the given ID which must belong to the given customerId.
79
+ *
80
+ * @param opts
81
+ *
82
+ * @returns the payment-method
83
+ */
84
+ get(opts: GetPaymentMethodService): Promise<PaymentMethod>;
85
+
86
+ /**
87
+ * Returns a payment-method by the given tokenValue which must belong to the given customerId and must be unique.
88
+ *
89
+ * @param opts
90
+ *
91
+ * @returns the payment-method
92
+ */
93
+ getByTokenValue(opts: GetByTokenValuePaymentMethod): Promise<PaymentMethod>;
94
+
95
+ /**
96
+ * Returns true if the given token belongs to the given customer for any payment-method.
97
+ *
98
+ * @param opts
99
+ *
100
+ * @returns true if the given token belongs to the customer for any payment method, false otherwise.
101
+ */
102
+ doesTokenBelongsToCustomer(opts: DoesTokenBelongToCustomer): Promise<boolean>;
103
+
104
+ /**
105
+ * Saves a new payment-method given the draft payload. The customer attribute is mandatory.
106
+ *
107
+ * @param opts the payment-method draft
108
+ *
109
+ * @returns the newly created payment-method
110
+ */
111
+ save(opts: SavePaymentMethodDraft): Promise<PaymentMethod>;
112
+
113
+ /**
114
+ * Updates an existing payment-method with the given list of update actions. The payment-method must belong to the given customerId.
115
+ *
116
+ * @param opts the list of update actions to apply
117
+ *
118
+ * @returns the updated payment-method
119
+ */
120
+ update(opts: UpdatePaymentMethodService): Promise<PaymentMethod>;
121
+
122
+ /**
123
+ * Deletes the given payment-method
124
+ *
125
+ * @param opts the payment-method to delete
126
+ *
127
+ * @returns returns the final state of the payment-method before it's deleted
128
+ */
129
+ delete(opts: DeletePaymentMethodService): Promise<PaymentMethod>;
130
+ }
@@ -0,0 +1,74 @@
1
+ import type {
2
+ CustomFieldsDraft,
3
+ Money,
4
+ Payment,
5
+ PaymentDraft,
6
+ PaymentMethodInfoDraft,
7
+ TransactionState,
8
+ TransactionType,
9
+ } from '@commercetools/platform-sdk';
10
+ import type { CommercetoolsAPI } from '@/commercetools/types/api.type.js';
11
+ import type { Logger } from '@/logger/types/logger.type.js';
12
+
13
+ export type PaymentAmount = {
14
+ centAmount: number;
15
+ currencyCode: string;
16
+ fractionDigits: number;
17
+ };
18
+
19
+ export type PaymentServiceOptions = {
20
+ ctAPI: CommercetoolsAPI;
21
+ logger: Logger;
22
+ };
23
+
24
+ export type GetPayment = {
25
+ id: string;
26
+ version?: number;
27
+ };
28
+
29
+ export type FindPaymentsByInterfaceId = {
30
+ interfaceId: string;
31
+ };
32
+
33
+ export type PSPInteraction = {
34
+ request?: string;
35
+ response?: string;
36
+ };
37
+
38
+ export type TransactionData = {
39
+ type: TransactionType;
40
+ amount: Money;
41
+ interactionId?: string;
42
+ interfaceId?: string;
43
+ state: TransactionState;
44
+ };
45
+
46
+ export type UpdatePaymentInfo = {
47
+ id: string;
48
+ pspReference?: string;
49
+ pspInteractions?: CustomFieldsDraft[];
50
+ transaction?: TransactionData;
51
+ paymentMethod?: string;
52
+ paymentMethodInfo?: PaymentMethodInfoDraft;
53
+ customFields?: CustomFieldsDraft;
54
+ token?: string;
55
+ };
56
+
57
+ export type FindTransaction = {
58
+ payment: Payment;
59
+ transactionType: TransactionType;
60
+ states: TransactionState[];
61
+ };
62
+
63
+ /**
64
+ * Payment service interface exposes methods to interact with the commercetools platform API.
65
+ */
66
+ export interface PaymentService {
67
+ getPayment(opts: GetPayment): Promise<Payment>;
68
+ findPaymentsByInterfaceId(
69
+ opts: FindPaymentsByInterfaceId,
70
+ ): Promise<Payment[]>;
71
+ hasTransactionInState(opts: FindTransaction): boolean;
72
+ createPayment(draft: PaymentDraft): Promise<Payment>;
73
+ updatePayment(opts: UpdatePaymentInfo): Promise<Payment>;
74
+ }