@reactionary/provider-commercetools 0.0.55 → 0.0.58
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/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-commercetools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.58",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@reactionary/core": "0.0.
|
|
7
|
+
"@reactionary/core": "0.0.58",
|
|
8
8
|
"debug": "^4.4.3",
|
|
9
9
|
"zod": "4.1.9",
|
|
10
10
|
"@commercetools/ts-client": "^4.2.1",
|
|
@@ -1,9 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
AddressSchema,
|
|
3
|
+
CheckoutItemSchema,
|
|
4
|
+
CheckoutProvider,
|
|
5
|
+
PaymentInstructionIdentifierSchema,
|
|
6
|
+
PaymentInstructionSchema,
|
|
7
|
+
PaymentMethodIdentifierSchema,
|
|
8
|
+
ShippingInstructionSchema,
|
|
9
|
+
ShippingMethodSchema
|
|
10
|
+
} from "@reactionary/core";
|
|
2
11
|
import { CommercetoolsClient } from "../core/client.js";
|
|
3
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
CommercetoolsCartIdentifierSchema,
|
|
14
|
+
CommercetoolsCheckoutIdentifierSchema,
|
|
15
|
+
CommercetoolsOrderIdentifierSchema
|
|
16
|
+
} from "../schema/commercetools.schema.js";
|
|
4
17
|
class CheckoutNotReadyForFinalizationError extends Error {
|
|
5
18
|
constructor(checkoutIdentifier) {
|
|
6
|
-
super(
|
|
19
|
+
super(
|
|
20
|
+
"Checkout is not ready for finalization. Ensure all required fields are set and valid. " + (checkoutIdentifier ? `Checkout ID: ${JSON.stringify(checkoutIdentifier)}` : "")
|
|
21
|
+
);
|
|
7
22
|
this.checkoutIdentifier = checkoutIdentifier;
|
|
8
23
|
this.name = "CheckoutNotReadyForFinalizationError";
|
|
9
24
|
}
|
|
@@ -76,9 +91,7 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
76
91
|
const checkoutResponse = await client.carts.withId({ ID: replicationResponse.body.id }).post({
|
|
77
92
|
body: {
|
|
78
93
|
version: replicationResponse.body.version || 0,
|
|
79
|
-
actions: [
|
|
80
|
-
...actions
|
|
81
|
-
]
|
|
94
|
+
actions: [...actions]
|
|
82
95
|
}
|
|
83
96
|
}).execute();
|
|
84
97
|
return this.parseSingle(checkoutResponse.body, reqCtx);
|
|
@@ -90,7 +103,18 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
90
103
|
expand: ["paymentInfo.payments[*]", "shippingInfo.shippingMethod"]
|
|
91
104
|
}
|
|
92
105
|
}).execute();
|
|
93
|
-
|
|
106
|
+
const checkout = this.parseSingle(checkoutResponse.body, reqCtx);
|
|
107
|
+
if (checkoutResponse.body.cartState === "Ordered") {
|
|
108
|
+
const order = await client.orders.get({
|
|
109
|
+
queryArgs: {
|
|
110
|
+
where: `cart(id="${checkout.identifier.key}")`
|
|
111
|
+
}
|
|
112
|
+
}).execute();
|
|
113
|
+
checkout.resultingOrder = {
|
|
114
|
+
key: order.body.results[0].id
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return checkout;
|
|
94
118
|
}
|
|
95
119
|
async setShippingAddress(payload, reqCtx) {
|
|
96
120
|
const client = await this.getClient(reqCtx);
|
|
@@ -142,7 +166,10 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
142
166
|
return result;
|
|
143
167
|
}
|
|
144
168
|
async getAvailablePaymentMethods(payload, reqCtx) {
|
|
145
|
-
const staticMethods = this.getStaticPaymentMethods(
|
|
169
|
+
const staticMethods = this.getStaticPaymentMethods(
|
|
170
|
+
payload.checkout,
|
|
171
|
+
reqCtx
|
|
172
|
+
);
|
|
146
173
|
const dynamicMethods = [];
|
|
147
174
|
return [...staticMethods, ...dynamicMethods];
|
|
148
175
|
}
|
|
@@ -151,7 +178,9 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
151
178
|
const response = await client.payments.post({
|
|
152
179
|
body: {
|
|
153
180
|
amountPlanned: {
|
|
154
|
-
centAmount: Math.round(
|
|
181
|
+
centAmount: Math.round(
|
|
182
|
+
payload.paymentInstruction.amount.value * 100
|
|
183
|
+
),
|
|
155
184
|
currencyCode: payload.paymentInstruction.amount.currency
|
|
156
185
|
},
|
|
157
186
|
paymentMethodInfo: {
|
|
@@ -167,7 +196,7 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
167
196
|
key: "reactionaryPaymentCustomFields"
|
|
168
197
|
},
|
|
169
198
|
fields: {
|
|
170
|
-
|
|
199
|
+
commerceToolsCartId: payload.checkout.key
|
|
171
200
|
}
|
|
172
201
|
}
|
|
173
202
|
}
|
|
@@ -182,11 +211,18 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
182
211
|
}
|
|
183
212
|
}
|
|
184
213
|
];
|
|
185
|
-
return this.applyActions(
|
|
214
|
+
return this.applyActions(
|
|
215
|
+
payload.checkout,
|
|
216
|
+
actions,
|
|
217
|
+
reqCtx
|
|
218
|
+
);
|
|
186
219
|
}
|
|
187
220
|
async removePaymentInstruction(payload, reqCtx) {
|
|
188
221
|
const client = await this.getClient(reqCtx);
|
|
189
|
-
const checkout = await this.getById(
|
|
222
|
+
const checkout = await this.getById(
|
|
223
|
+
{ identifier: payload.checkout },
|
|
224
|
+
reqCtx
|
|
225
|
+
);
|
|
190
226
|
return checkout;
|
|
191
227
|
}
|
|
192
228
|
async setShippingInstruction(payload, reqCtx) {
|
|
@@ -215,10 +251,17 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
215
251
|
name: "pickupPointId",
|
|
216
252
|
value: payload.shippingInstruction.pickupPoint
|
|
217
253
|
});
|
|
218
|
-
return this.applyActions(
|
|
254
|
+
return this.applyActions(
|
|
255
|
+
payload.checkout,
|
|
256
|
+
actions,
|
|
257
|
+
reqCtx
|
|
258
|
+
);
|
|
219
259
|
}
|
|
220
260
|
async finalizeCheckout(payload, reqCtx) {
|
|
221
|
-
const checkout = await this.getById(
|
|
261
|
+
const checkout = await this.getById(
|
|
262
|
+
{ identifier: payload.checkout },
|
|
263
|
+
reqCtx
|
|
264
|
+
);
|
|
222
265
|
if (!checkout || !checkout.readyForFinalization) {
|
|
223
266
|
throw new CheckoutNotReadyForFinalizationError(payload.checkout);
|
|
224
267
|
}
|
|
@@ -230,13 +273,10 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
230
273
|
version: ctId.version
|
|
231
274
|
}
|
|
232
275
|
}).execute();
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
value: orderResponse.body.id
|
|
238
|
-
});
|
|
239
|
-
return this.applyActions(payload.checkout, actions, reqCtx);
|
|
276
|
+
return this.getById(
|
|
277
|
+
{ identifier: payload.checkout },
|
|
278
|
+
reqCtx
|
|
279
|
+
);
|
|
240
280
|
}
|
|
241
281
|
async applyActions(checkout, actions, reqCtx) {
|
|
242
282
|
const client = await this.getClient(reqCtx);
|
|
@@ -285,15 +325,11 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
285
325
|
key: remote.custom?.fields["commerceToolsCartId"] || "",
|
|
286
326
|
version: 0
|
|
287
327
|
});
|
|
288
|
-
const orderId = remote.custom?.fields["commerceToolsOrderId"];
|
|
289
|
-
if (orderId) {
|
|
290
|
-
result.resultingOrder = CommercetoolsOrderIdentifierSchema.parse({
|
|
291
|
-
key: orderId,
|
|
292
|
-
version: 0
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
328
|
if (remote.shippingAddress) {
|
|
296
|
-
result.shippingAddress = this.parseAddress(
|
|
329
|
+
result.shippingAddress = this.parseAddress(
|
|
330
|
+
remote.shippingAddress,
|
|
331
|
+
reqCtx
|
|
332
|
+
);
|
|
297
333
|
}
|
|
298
334
|
if (remote.billingAddress) {
|
|
299
335
|
result.billingAddress = this.parseAddress(remote.billingAddress, reqCtx);
|
|
@@ -301,7 +337,9 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
301
337
|
result.shippingInstruction = this.parseShippingInstruction(remote);
|
|
302
338
|
for (const p of remote.paymentInfo?.payments || []) {
|
|
303
339
|
if (p.obj) {
|
|
304
|
-
result.paymentInstructions.push(
|
|
340
|
+
result.paymentInstructions.push(
|
|
341
|
+
this.parsePaymentInstruction(p.obj, reqCtx)
|
|
342
|
+
);
|
|
305
343
|
}
|
|
306
344
|
}
|
|
307
345
|
const grandTotal = remote.totalPrice.centAmount || 0;
|
|
@@ -392,7 +430,9 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
392
430
|
}
|
|
393
431
|
parsePaymentInstruction(remote, reqCtx) {
|
|
394
432
|
const newModel = PaymentInstructionSchema.parse({});
|
|
395
|
-
newModel.identifier = PaymentInstructionIdentifierSchema.parse({
|
|
433
|
+
newModel.identifier = PaymentInstructionIdentifierSchema.parse({
|
|
434
|
+
key: remote.id || ""
|
|
435
|
+
});
|
|
396
436
|
newModel.amount = {
|
|
397
437
|
value: remote.amountPlanned.centAmount / 100,
|
|
398
438
|
currency: remote.amountPlanned.currencyCode
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Cache, Checkout, RequestContext, PaymentMethod, ShippingMethod, CheckoutMutationInitiateCheckout, CheckoutMutationSetShippingAddress, CheckoutMutationFinalizeCheckout, CheckoutMutationAddPaymentInstruction, CheckoutMutationRemovePaymentInstruction, CheckoutMutationSetShippingInstruction, CheckoutQueryById, CheckoutQueryForAvailablePaymentMethods, CheckoutQueryForAvailableShippingMethods, CheckoutIdentifier, ShippingInstruction, PaymentInstruction } from
|
|
2
|
-
import { CheckoutProvider } from
|
|
3
|
-
import type z from
|
|
4
|
-
import type { CommercetoolsConfiguration } from
|
|
5
|
-
import type { MyCartUpdateAction } from
|
|
6
|
-
import type { Address as CTAddress, Payment as CTPayment, Cart as CTCart } from
|
|
1
|
+
import type { Cache, Checkout, RequestContext, PaymentMethod, ShippingMethod, CheckoutMutationInitiateCheckout, CheckoutMutationSetShippingAddress, CheckoutMutationFinalizeCheckout, CheckoutMutationAddPaymentInstruction, CheckoutMutationRemovePaymentInstruction, CheckoutMutationSetShippingInstruction, CheckoutQueryById, CheckoutQueryForAvailablePaymentMethods, CheckoutQueryForAvailableShippingMethods, CheckoutIdentifier, ShippingInstruction, PaymentInstruction } from '@reactionary/core';
|
|
2
|
+
import { CheckoutProvider } from '@reactionary/core';
|
|
3
|
+
import type z from 'zod';
|
|
4
|
+
import type { CommercetoolsConfiguration } from '../schema/configuration.schema.js';
|
|
5
|
+
import type { MyCartUpdateAction } from '@commercetools/platform-sdk';
|
|
6
|
+
import type { Address as CTAddress, Payment as CTPayment, Cart as CTCart } from '@commercetools/platform-sdk';
|
|
7
7
|
export declare class CheckoutNotReadyForFinalizationError extends Error {
|
|
8
8
|
checkoutIdentifier: CheckoutIdentifier;
|
|
9
9
|
constructor(checkoutIdentifier: CheckoutIdentifier);
|