@reactionary/source 0.0.42 → 0.0.48
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/.claude/settings.local.json +28 -0
- package/.vscode/settings.json +5 -0
- package/core/package.json +3 -1
- package/core/src/cache/cache.interface.ts +14 -18
- package/core/src/cache/memory-cache.ts +56 -0
- package/core/src/cache/noop-cache.ts +5 -23
- package/core/src/cache/redis-cache.ts +28 -38
- package/core/src/client/client-builder.ts +3 -3
- package/core/src/client/client.ts +10 -10
- package/core/src/decorators/reactionary.decorator.ts +80 -8
- package/core/src/index.ts +2 -1
- package/{examples/node/src/test-utils.ts → core/src/initialization.ts} +20 -10
- package/core/src/providers/analytics.provider.ts +1 -1
- package/core/src/providers/base.provider.ts +61 -25
- package/core/src/providers/cart-payment.provider.ts +7 -6
- package/core/src/providers/cart.provider.ts +17 -18
- package/core/src/providers/category.provider.ts +9 -9
- package/core/src/providers/identity.provider.ts +8 -7
- package/core/src/providers/index.ts +2 -0
- package/core/src/providers/inventory.provider.ts +4 -4
- package/core/src/providers/price.provider.ts +6 -6
- package/core/src/providers/product.provider.ts +5 -5
- package/core/src/providers/profile.provider.ts +22 -0
- package/core/src/providers/search.provider.ts +4 -4
- package/core/src/providers/store.provider.ts +14 -0
- package/core/src/schemas/capabilities.schema.ts +2 -1
- package/core/src/schemas/models/analytics.model.ts +1 -1
- package/core/src/schemas/models/identifiers.model.ts +66 -38
- package/core/src/schemas/models/identity.model.ts +16 -20
- package/core/src/schemas/models/index.ts +1 -0
- package/core/src/schemas/models/profile.model.ts +3 -2
- package/core/src/schemas/models/shipping-method.model.ts +2 -2
- package/core/src/schemas/models/store.model.ts +11 -0
- package/core/src/schemas/mutations/cart-payment.mutation.ts +1 -1
- package/core/src/schemas/mutations/cart.mutation.ts +2 -2
- package/core/src/schemas/mutations/identity.mutation.ts +6 -0
- package/core/src/schemas/mutations/index.ts +1 -0
- package/core/src/schemas/mutations/profile.mutation.ts +9 -0
- package/core/src/schemas/queries/cart.query.ts +1 -1
- package/core/src/schemas/queries/identity.query.ts +1 -1
- package/core/src/schemas/queries/index.ts +2 -0
- package/core/src/schemas/queries/inventory.query.ts +4 -12
- package/core/src/schemas/queries/price.query.ts +1 -1
- package/core/src/schemas/queries/profile.query.ts +7 -0
- package/core/src/schemas/queries/search.query.ts +1 -1
- package/core/src/schemas/queries/store.query.ts +11 -0
- package/core/src/schemas/session.schema.ts +31 -6
- package/eslint.config.mjs +7 -0
- package/examples/next/src/app/page.tsx +4 -12
- package/examples/node/package.json +1 -3
- package/examples/node/src/basic/basic-node-provider-model-extension.spec.ts +9 -8
- package/examples/node/src/basic/basic-node-provider-query-extension.spec.ts +4 -3
- package/examples/node/src/basic/basic-node-setup.spec.ts +4 -5
- package/nx.json +1 -0
- package/otel/src/metrics.ts +2 -1
- package/otel/src/provider-instrumentation.ts +2 -1
- package/otel/src/tracer.ts +7 -6
- package/otel/src/trpc-middleware.ts +3 -2
- package/package.json +2 -1
- package/providers/algolia/src/core/initialize.ts +4 -3
- package/providers/algolia/src/providers/product.provider.ts +15 -13
- package/providers/algolia/src/providers/search.provider.ts +9 -9
- package/providers/algolia/src/schema/capabilities.schema.ts +1 -1
- package/providers/algolia/src/test/search.provider.spec.ts +10 -10
- package/providers/algolia/src/test/test-utils.ts +1 -1
- package/providers/commercetools/README.md +11 -0
- package/providers/commercetools/src/core/client.ts +144 -88
- package/providers/commercetools/src/core/initialize.ts +14 -11
- package/providers/commercetools/src/providers/cart-payment.provider.ts +30 -29
- package/providers/commercetools/src/providers/cart.provider.ts +88 -116
- package/providers/commercetools/src/providers/category.provider.ts +34 -37
- package/providers/commercetools/src/providers/identity.provider.ts +22 -82
- package/providers/commercetools/src/providers/index.ts +2 -0
- package/providers/commercetools/src/providers/inventory.provider.ts +67 -48
- package/providers/commercetools/src/providers/price.provider.ts +76 -50
- package/providers/commercetools/src/providers/product.provider.ts +20 -23
- package/providers/commercetools/src/providers/profile.provider.ts +61 -0
- package/providers/commercetools/src/providers/search.provider.ts +12 -15
- package/providers/commercetools/src/providers/store.provider.ts +78 -0
- package/providers/commercetools/src/schema/capabilities.schema.ts +2 -1
- package/providers/commercetools/src/test/cart-payment.provider.spec.ts +13 -17
- package/providers/commercetools/src/test/cart.provider.spec.ts +18 -17
- package/providers/commercetools/src/test/category.provider.spec.ts +18 -17
- package/providers/commercetools/src/test/identity.provider.spec.ts +88 -0
- package/providers/commercetools/src/test/inventory.provider.spec.ts +41 -0
- package/providers/commercetools/src/test/price.provider.spec.ts +9 -8
- package/providers/commercetools/src/test/product.provider.spec.ts +9 -8
- package/providers/commercetools/src/test/profile.provider.spec.ts +49 -0
- package/providers/commercetools/src/test/search.provider.spec.ts +8 -7
- package/providers/commercetools/src/test/store.provider.spec.ts +37 -0
- package/providers/commercetools/src/test/test-utils.ts +0 -39
- package/providers/fake/src/core/initialize.ts +96 -38
- package/providers/fake/src/providers/analytics.provider.ts +6 -5
- package/providers/fake/src/providers/cart.provider.ts +30 -27
- package/providers/fake/src/providers/category.provider.ts +12 -12
- package/providers/fake/src/providers/identity.provider.ts +22 -14
- package/providers/fake/src/providers/index.ts +1 -0
- package/providers/fake/src/providers/inventory.provider.ts +13 -13
- package/providers/fake/src/providers/price.provider.ts +13 -13
- package/providers/fake/src/providers/product.provider.ts +11 -11
- package/providers/fake/src/providers/search.provider.ts +7 -5
- package/providers/fake/src/providers/store.provider.ts +47 -0
- package/providers/fake/src/schema/capabilities.schema.ts +4 -1
- package/providers/fake/src/test/cart.provider.spec.ts +18 -18
- package/providers/fake/src/test/category.provider.spec.ts +55 -37
- package/providers/fake/src/test/price.provider.spec.ts +9 -14
- package/providers/fake/src/test/product.provider.spec.ts +27 -0
- package/providers/fake/src/test/test-utils.ts +2 -33
- package/providers/posthog/src/core/initialize.ts +3 -3
- package/providers/posthog/src/schema/capabilities.schema.ts +1 -1
- package/trpc/src/client.ts +42 -41
- package/trpc/src/index.ts +4 -3
- package/trpc/src/integration.spec.ts +11 -11
- package/trpc/src/server.ts +26 -24
- package/trpc/src/test-utils.ts +1 -1
- package/trpc/src/types.ts +24 -22
- package/core/src/cache/cache-evaluation.interface.ts +0 -19
|
@@ -1,125 +1,179 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ClientBuilder,
|
|
3
|
+
type TokenCache,
|
|
4
|
+
type TokenCacheOptions,
|
|
5
|
+
type TokenStore,
|
|
6
|
+
} from '@commercetools/ts-client';
|
|
2
7
|
import { createApiBuilderFromCtpClient } from '@commercetools/platform-sdk';
|
|
3
|
-
import { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
8
|
+
import type { CommercetoolsConfiguration } from '../schema/configuration.schema';
|
|
4
9
|
import { randomUUID } from 'crypto';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
import { GuestIdentitySchema, IdentitySchema, type RequestContext } from '@reactionary/core';
|
|
11
|
+
import * as crypto from 'crypto';
|
|
12
|
+
|
|
13
|
+
export class RequestContextTokenCache implements TokenCache {
|
|
14
|
+
constructor(protected context: RequestContext) {}
|
|
15
|
+
|
|
16
|
+
public async get(
|
|
17
|
+
tokenCacheOptions?: TokenCacheOptions
|
|
18
|
+
): Promise<TokenStore | undefined> {
|
|
19
|
+
const identity = this.context.identity;
|
|
20
|
+
|
|
21
|
+
if (identity.type !== 'Anonymous') {
|
|
22
|
+
return {
|
|
23
|
+
refreshToken: identity.refresh_token,
|
|
24
|
+
token: identity.token || '',
|
|
25
|
+
expirationTime: identity.expiry.getTime(),
|
|
26
|
+
};
|
|
17
27
|
}
|
|
18
28
|
|
|
19
|
-
return
|
|
29
|
+
return undefined;
|
|
20
30
|
}
|
|
21
31
|
|
|
22
|
-
public async
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
});
|
|
28
|
-
const url = `${this.config.authUrl}/oauth/${
|
|
29
|
-
this.config.projectKey
|
|
30
|
-
}/customers/token?${queryParams.toString()}`;
|
|
31
|
-
const headers = {
|
|
32
|
-
Authorization:
|
|
33
|
-
'Basic ' + btoa(this.config.clientId + ':' + this.config.clientSecret),
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const remote = await fetch(url, { method: 'POST', headers });
|
|
37
|
-
const json = await remote.json();
|
|
32
|
+
public async set(
|
|
33
|
+
cache: TokenStore,
|
|
34
|
+
tokenCacheOptions?: TokenCacheOptions
|
|
35
|
+
): Promise<void> {
|
|
36
|
+
const identity = this.context.identity;
|
|
38
37
|
|
|
39
|
-
|
|
38
|
+
identity.refresh_token = cache.refreshToken;
|
|
39
|
+
identity.token = cache.token;
|
|
40
|
+
identity.expiry = new Date(cache.expirationTime);
|
|
40
41
|
}
|
|
42
|
+
}
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
grant_type: 'client_credentials',
|
|
45
|
-
});
|
|
46
|
-
const url = `${this.config.authUrl}/oauth/${
|
|
47
|
-
this.config.projectKey
|
|
48
|
-
}/anonymous/token?${queryParams.toString()}`;
|
|
49
|
-
const headers = {
|
|
50
|
-
Authorization:
|
|
51
|
-
'Basic ' + btoa(this.config.clientId + ':' + this.config.clientSecret),
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const remote = await fetch(url, { method: 'POST', headers });
|
|
55
|
-
const json = await remote.json();
|
|
44
|
+
export class CommercetoolsClient {
|
|
45
|
+
protected config: CommercetoolsConfiguration;
|
|
56
46
|
|
|
57
|
-
|
|
47
|
+
constructor(config: CommercetoolsConfiguration) {
|
|
48
|
+
this.config = config;
|
|
58
49
|
}
|
|
59
50
|
|
|
60
|
-
public async
|
|
61
|
-
|
|
62
|
-
token: token,
|
|
63
|
-
token_type_hint: 'access_token',
|
|
64
|
-
});
|
|
65
|
-
const url = `${
|
|
66
|
-
this.config.authUrl
|
|
67
|
-
}/oauth/token/revoke?${queryParams.toString()}`;
|
|
68
|
-
const headers = {
|
|
69
|
-
Authorization:
|
|
70
|
-
'Basic ' + btoa(this.config.clientId + ':' + this.config.clientSecret),
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const remote = await fetch(url, { method: 'POST', headers });
|
|
74
|
-
|
|
75
|
-
return remote;
|
|
51
|
+
public async getClient(reqCtx: RequestContext) {
|
|
52
|
+
return this.createClient(reqCtx);
|
|
76
53
|
}
|
|
77
54
|
|
|
78
|
-
public async
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
55
|
+
public async register(
|
|
56
|
+
username: string,
|
|
57
|
+
password: string,
|
|
58
|
+
reqCtx: RequestContext
|
|
59
|
+
) {
|
|
60
|
+
const cache = new RequestContextTokenCache(reqCtx);
|
|
61
|
+
|
|
62
|
+
const registrationBuilder =
|
|
63
|
+
this.createBaseClientBuilder().withAnonymousSessionFlow({
|
|
64
|
+
host: this.config.authUrl,
|
|
65
|
+
projectKey: this.config.projectKey,
|
|
66
|
+
credentials: {
|
|
67
|
+
clientId: this.config.clientId,
|
|
68
|
+
clientSecret: this.config.clientSecret,
|
|
69
|
+
},
|
|
70
|
+
scopes: this.config.scopes,
|
|
71
|
+
tokenCache: cache,
|
|
72
|
+
});
|
|
87
73
|
|
|
88
|
-
const
|
|
89
|
-
|
|
74
|
+
const registrationClient = createApiBuilderFromCtpClient(
|
|
75
|
+
registrationBuilder.build()
|
|
76
|
+
);
|
|
77
|
+
const registration = await registrationClient
|
|
78
|
+
.withProjectKey({ projectKey: this.config.projectKey })
|
|
79
|
+
.me()
|
|
80
|
+
.signup()
|
|
81
|
+
.post({
|
|
82
|
+
body: {
|
|
83
|
+
email: username,
|
|
84
|
+
password: password,
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
.execute();
|
|
90
88
|
|
|
91
|
-
|
|
89
|
+
const login = await this.login(username, password, reqCtx);
|
|
92
90
|
}
|
|
93
91
|
|
|
94
|
-
public
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
public async login(
|
|
93
|
+
username: string,
|
|
94
|
+
password: string,
|
|
95
|
+
reqCtx: RequestContext
|
|
96
|
+
) {
|
|
97
|
+
const cache = new RequestContextTokenCache(reqCtx);
|
|
98
|
+
const identity = reqCtx.identity;
|
|
99
|
+
|
|
100
|
+
const loginBuilder = this.createBaseClientBuilder().withPasswordFlow({
|
|
97
101
|
host: this.config.authUrl,
|
|
98
102
|
projectKey: this.config.projectKey,
|
|
99
103
|
credentials: {
|
|
100
104
|
clientId: this.config.clientId,
|
|
101
105
|
clientSecret: this.config.clientSecret,
|
|
106
|
+
user: { username, password },
|
|
102
107
|
},
|
|
103
|
-
|
|
108
|
+
tokenCache: cache,
|
|
109
|
+
scopes: this.config.scopes,
|
|
104
110
|
});
|
|
105
111
|
|
|
106
|
-
|
|
107
|
-
}
|
|
112
|
+
const loginClient = createApiBuilderFromCtpClient(loginBuilder.build());
|
|
108
113
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
const login = await loginClient
|
|
115
|
+
.withProjectKey({ projectKey: this.config.projectKey })
|
|
116
|
+
.me()
|
|
117
|
+
.get()
|
|
118
|
+
.execute();
|
|
114
119
|
|
|
115
|
-
|
|
120
|
+
identity.type = 'Registered';
|
|
121
|
+
identity.logonId = username;
|
|
122
|
+
identity.id = {
|
|
123
|
+
userId: login.body.id
|
|
124
|
+
};
|
|
116
125
|
}
|
|
117
126
|
|
|
127
|
+
public async logout(reqCtx: RequestContext) {
|
|
128
|
+
const cache = new RequestContextTokenCache(reqCtx);
|
|
129
|
+
await cache.set({ token: '', refreshToken: '', expirationTime: 0 });
|
|
118
130
|
|
|
131
|
+
reqCtx.identity = IdentitySchema.parse({});
|
|
119
132
|
|
|
133
|
+
// TODO: We could do token revocation here, if we wanted to. The above simply whacks the session.
|
|
134
|
+
}
|
|
120
135
|
|
|
136
|
+
protected createClient(reqCtx: RequestContext) {
|
|
137
|
+
const cache = new RequestContextTokenCache(reqCtx);
|
|
138
|
+
|
|
139
|
+
if (reqCtx.identity.type === 'Anonymous') {
|
|
140
|
+
reqCtx.identity = GuestIdentitySchema.parse({
|
|
141
|
+
id: {
|
|
142
|
+
userId: crypto.randomUUID().toString(),
|
|
143
|
+
},
|
|
144
|
+
type: 'Guest'
|
|
145
|
+
});
|
|
146
|
+
}
|
|
121
147
|
|
|
148
|
+
const identity = reqCtx.identity;
|
|
149
|
+
let builder = this.createBaseClientBuilder();
|
|
150
|
+
|
|
151
|
+
if (!identity.token || !identity.refresh_token) {
|
|
152
|
+
builder = builder.withAnonymousSessionFlow({
|
|
153
|
+
host: this.config.authUrl,
|
|
154
|
+
projectKey: this.config.projectKey,
|
|
155
|
+
credentials: {
|
|
156
|
+
clientId: this.config.clientId,
|
|
157
|
+
clientSecret: this.config.clientSecret,
|
|
158
|
+
anonymousId: identity.id.userId,
|
|
159
|
+
},
|
|
160
|
+
tokenCache: cache,
|
|
161
|
+
});
|
|
162
|
+
} else {
|
|
163
|
+
builder = builder.withRefreshTokenFlow({
|
|
164
|
+
credentials: {
|
|
165
|
+
clientId: this.config.clientId,
|
|
166
|
+
clientSecret: this.config.clientSecret,
|
|
167
|
+
},
|
|
168
|
+
host: this.config.authUrl,
|
|
169
|
+
projectKey: this.config.projectKey,
|
|
170
|
+
refreshToken: identity.refresh_token || '',
|
|
171
|
+
tokenCache: cache,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
122
174
|
|
|
175
|
+
return createApiBuilderFromCtpClient(builder.build());
|
|
176
|
+
}
|
|
123
177
|
|
|
124
178
|
protected createBaseClientBuilder() {
|
|
125
179
|
const builder = new ClientBuilder()
|
|
@@ -133,7 +187,9 @@ export class CommercetoolsClient {
|
|
|
133
187
|
// And yes, ideally the frontend would handle this, but as the customer is not really in a position to DO anything about it,
|
|
134
188
|
// we might as well just deal with it here.....
|
|
135
189
|
|
|
136
|
-
console.log(
|
|
190
|
+
console.log(
|
|
191
|
+
`Concurrent modification error, retry with version ${version}`
|
|
192
|
+
);
|
|
137
193
|
const body = request.body as Record<string, any>;
|
|
138
194
|
body['version'] = version;
|
|
139
195
|
return Promise.resolve(body);
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CartSchema,
|
|
3
|
-
IdentitySchema,
|
|
4
|
-
InventorySchema,
|
|
5
|
-
PriceSchema,
|
|
6
|
-
ProductSchema,
|
|
7
|
-
SearchResultSchema,
|
|
1
|
+
import type {
|
|
8
2
|
Cache,
|
|
9
|
-
CategorySchema,
|
|
10
3
|
ProductProvider,
|
|
11
4
|
SearchProvider,
|
|
12
5
|
IdentityProvider,
|
|
@@ -14,12 +7,21 @@ import {
|
|
|
14
7
|
InventoryProvider,
|
|
15
8
|
PriceProvider,
|
|
16
9
|
CategoryProvider,
|
|
10
|
+
StoreProvider} from "@reactionary/core";
|
|
11
|
+
import {
|
|
12
|
+
CartSchema,
|
|
13
|
+
IdentitySchema,
|
|
14
|
+
InventorySchema,
|
|
15
|
+
PriceSchema,
|
|
16
|
+
ProductSchema,
|
|
17
|
+
SearchResultSchema,
|
|
18
|
+
CategorySchema,
|
|
17
19
|
CartPaymentInstructionSchema
|
|
18
20
|
} from "@reactionary/core";
|
|
19
|
-
import { CommercetoolsCapabilities } from "../schema/capabilities.schema";
|
|
21
|
+
import type { CommercetoolsCapabilities } from "../schema/capabilities.schema";
|
|
20
22
|
import { CommercetoolsSearchProvider } from "../providers/search.provider";
|
|
21
23
|
import { CommercetoolsProductProvider } from '../providers/product.provider';
|
|
22
|
-
import { CommercetoolsConfiguration } from "../schema/configuration.schema";
|
|
24
|
+
import type { CommercetoolsConfiguration } from "../schema/configuration.schema";
|
|
23
25
|
import { CommercetoolsIdentityProvider } from "../providers/identity.provider";
|
|
24
26
|
import { CommercetoolsCartProvider } from "../providers/cart.provider";
|
|
25
27
|
import { CommercetoolsInventoryProvider } from "../providers/inventory.provider";
|
|
@@ -34,7 +36,8 @@ type CommercetoolsClient<T extends CommercetoolsCapabilities> =
|
|
|
34
36
|
(T['identity'] extends true ? { identity: IdentityProvider } : object) &
|
|
35
37
|
(T['category'] extends true ? { category: CategoryProvider } : object) &
|
|
36
38
|
(T['inventory'] extends true ? { inventory: InventoryProvider } : object) &
|
|
37
|
-
(T['price'] extends true ? { price: PriceProvider } : object)
|
|
39
|
+
(T['price'] extends true ? { price: PriceProvider } : object) &
|
|
40
|
+
(T['store'] extends true ? { store: StoreProvider } : object);
|
|
38
41
|
|
|
39
42
|
export function withCommercetoolsCapabilities<T extends CommercetoolsCapabilities>(
|
|
40
43
|
configuration: CommercetoolsConfiguration,
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
|
|
2
|
-
import { CommercetoolsConfiguration } from "../schema/configuration.schema";
|
|
2
|
+
import type { CommercetoolsConfiguration } from "../schema/configuration.schema";
|
|
3
3
|
import { CommercetoolsClient } from "../core/client";
|
|
4
|
-
import { Payment as CTPayment
|
|
4
|
+
import type { Payment as CTPayment} from "@commercetools/platform-sdk";
|
|
5
|
+
import { PaymentStatus } from "@commercetools/platform-sdk";
|
|
5
6
|
import { traced } from "@reactionary/otel";
|
|
6
|
-
import { CommercetoolsCartIdentifier
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import
|
|
7
|
+
import type { CommercetoolsCartIdentifier} from "../schema/commercetools.schema";
|
|
8
|
+
import { CommercetoolsCartIdentifierSchema, CommercetoolsCartPaymentInstructionIdentifierSchema } from "../schema/commercetools.schema";
|
|
9
|
+
import { CartPaymentProvider, PaymentMethodIdentifierSchema, } from "@reactionary/core";
|
|
10
|
+
import type { CartPaymentQueryByCart, CartPaymentMutationAddPayment, CartPaymentMutationCancelPayment, Session, RequestContext , Cache, CartPaymentInstruction, Currency} from "@reactionary/core";
|
|
11
|
+
import type z from "zod";
|
|
10
12
|
|
|
11
13
|
export class CommercetoolsCartPaymentProvider<
|
|
12
14
|
T extends CartPaymentInstruction = CartPaymentInstruction
|
|
@@ -19,11 +21,9 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
19
21
|
this.config = config;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
protected getClient(
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
token
|
|
26
|
-
);
|
|
24
|
+
protected async getClient(reqCtx: RequestContext) {
|
|
25
|
+
const client = await new CommercetoolsClient(this.config).getClient(reqCtx);
|
|
26
|
+
|
|
27
27
|
return {
|
|
28
28
|
payments: client.withProjectKey({ projectKey: this.config.projectKey }).me().payments(),
|
|
29
29
|
carts: client.withProjectKey({ projectKey: this.config.projectKey }).me().carts()
|
|
@@ -33,8 +33,8 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
@traced()
|
|
36
|
-
public override async getByCartIdentifier(payload: CartPaymentQueryByCart,
|
|
37
|
-
const client = this.getClient(
|
|
36
|
+
public override async getByCartIdentifier(payload: CartPaymentQueryByCart, reqCtx: RequestContext): Promise<T[]> {
|
|
37
|
+
const client = await this.getClient(reqCtx);
|
|
38
38
|
|
|
39
39
|
const ctId = payload.cart as CommercetoolsCartIdentifier;
|
|
40
40
|
const ctVersion = ctId.version || 0;
|
|
@@ -53,7 +53,7 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// Map over the payments and parse each one
|
|
56
|
-
const parsedPayments = payments.map(payment => this.parseSingle(payment,
|
|
56
|
+
const parsedPayments = payments.map(payment => this.parseSingle(payment, reqCtx));
|
|
57
57
|
|
|
58
58
|
// Commercetools does not link carts to payments, but the other way around, so for this we have to synthesize the link.
|
|
59
59
|
const returnPayments = parsedPayments.map(x => {
|
|
@@ -65,8 +65,8 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
65
65
|
|
|
66
66
|
|
|
67
67
|
|
|
68
|
-
public override async initiatePaymentForCart(payload: CartPaymentMutationAddPayment,
|
|
69
|
-
const client = this.getClient(
|
|
68
|
+
public override async initiatePaymentForCart(payload: CartPaymentMutationAddPayment, reqCtx: RequestContext): Promise<T> {
|
|
69
|
+
const client = await this.getClient(reqCtx);
|
|
70
70
|
const cartId = payload.cart as CommercetoolsCartIdentifier;
|
|
71
71
|
const response = await client.payments.post({
|
|
72
72
|
body: {
|
|
@@ -78,19 +78,20 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
78
78
|
paymentMethodInfo: {
|
|
79
79
|
method: payload.paymentInstruction.paymentMethod.method,
|
|
80
80
|
name: {
|
|
81
|
-
[
|
|
81
|
+
[reqCtx.languageContext.locale]: payload.paymentInstruction.paymentMethod.name
|
|
82
82
|
},
|
|
83
83
|
paymentInterface: payload.paymentInstruction.paymentMethod.paymentProcessor
|
|
84
84
|
},
|
|
85
85
|
custom:{
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
type: {
|
|
87
|
+
typeId: 'type',
|
|
88
|
+
key: 'reactionaryPaymentCustomFields',
|
|
89
|
+
},
|
|
90
|
+
fields: {
|
|
91
|
+
cartId: cartId.key,
|
|
92
|
+
cartVersion: cartId.version + '',
|
|
93
|
+
}
|
|
89
94
|
},
|
|
90
|
-
fields: {
|
|
91
|
-
cartId: cartId.key,
|
|
92
|
-
cartVersion: cartId.version + '',
|
|
93
|
-
}},
|
|
94
95
|
|
|
95
96
|
},
|
|
96
97
|
}).execute();
|
|
@@ -112,7 +113,7 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
112
113
|
}
|
|
113
114
|
}).execute();
|
|
114
115
|
|
|
115
|
-
const payment = this.parseSingle(response.body,
|
|
116
|
+
const payment = this.parseSingle(response.body, reqCtx);
|
|
116
117
|
|
|
117
118
|
// we return the newest cart version so caller can update their cart reference, if they want to.
|
|
118
119
|
// hopefully this wont cause excessive confusion
|
|
@@ -125,8 +126,8 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
125
126
|
|
|
126
127
|
|
|
127
128
|
@traced()
|
|
128
|
-
public override async cancelPaymentInstruction(payload: CartPaymentMutationCancelPayment,
|
|
129
|
-
const client = this.getClient(
|
|
129
|
+
public override async cancelPaymentInstruction(payload: CartPaymentMutationCancelPayment, reqCtx: RequestContext): Promise<T> {
|
|
130
|
+
const client = await this.getClient(reqCtx);
|
|
130
131
|
|
|
131
132
|
// get newest version
|
|
132
133
|
const newestVersion = await client.payments.withId({ ID: payload.paymentInstruction.key }).get().execute();
|
|
@@ -153,7 +154,7 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
153
154
|
}
|
|
154
155
|
}).execute();
|
|
155
156
|
|
|
156
|
-
const payment = this.parseSingle(response.body,
|
|
157
|
+
const payment = this.parseSingle(response.body, reqCtx);
|
|
157
158
|
payment.cart = payload.cart;
|
|
158
159
|
return payment;
|
|
159
160
|
}
|
|
@@ -161,7 +162,7 @@ export class CommercetoolsCartPaymentProvider<
|
|
|
161
162
|
|
|
162
163
|
|
|
163
164
|
@traced()
|
|
164
|
-
protected override parseSingle(_body: unknown,
|
|
165
|
+
protected override parseSingle(_body: unknown, reqCtx: RequestContext): T {
|
|
165
166
|
const body = _body as CTPayment;
|
|
166
167
|
|
|
167
168
|
const base = this.newModel();
|