@reactionary/medusa 0.6.4 → 0.6.5
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/capabilities/cart.capability.js +7 -99
- package/capabilities/category.capability.js +29 -47
- package/capabilities/checkout.capability.js +19 -209
- package/capabilities/inventory.capability.js +10 -54
- package/capabilities/order-search.capability.js +2 -76
- package/capabilities/order.capability.js +2 -130
- package/capabilities/price.capability.js +3 -38
- package/capabilities/product-associations.capability.js +19 -54
- package/capabilities/product-search.capability.js +15 -67
- package/capabilities/product.capability.js +18 -131
- package/capabilities/profile.capability.js +14 -63
- package/factories/cart/cart.factory.js +91 -2
- package/factories/category/category.factory.js +33 -3
- package/factories/checkout/checkout.factory.js +187 -4
- package/factories/inventory/inventory.factory.js +19 -1
- package/factories/order/order.factory.js +128 -2
- package/factories/order-search/order-search.factory.js +75 -2
- package/factories/price/price.factory.js +31 -2
- package/factories/product/product.factory.js +155 -2
- package/factories/product-associations/product-associations.factory.js +38 -2
- package/factories/product-search/product-search.factory.js +51 -2
- package/factories/profile/profile.factory.js +54 -2
- package/package.json +2 -2
- package/src/capabilities/cart.capability.d.ts +2 -28
- package/src/capabilities/category.capability.d.ts +5 -7
- package/src/capabilities/checkout.capability.d.ts +3 -35
- package/src/capabilities/inventory.capability.d.ts +2 -12
- package/src/capabilities/order-search.capability.d.ts +2 -42
- package/src/capabilities/order.capability.d.ts +2 -39
- package/src/capabilities/price.capability.d.ts +1 -4
- package/src/capabilities/product-associations.capability.d.ts +3 -7
- package/src/capabilities/product-search.capability.d.ts +4 -34
- package/src/capabilities/product.capability.d.ts +1 -6
- package/src/capabilities/profile.capability.d.ts +3 -5
- package/src/factories/cart/cart.factory.d.ts +23 -2
- package/src/factories/category/category.factory.d.ts +4 -3
- package/src/factories/checkout/checkout.factory.d.ts +31 -4
- package/src/factories/inventory/inventory.factory.d.ts +7 -1
- package/src/factories/order/order.factory.d.ts +38 -2
- package/src/factories/order-search/order-search.factory.d.ts +20 -2
- package/src/factories/price/price.factory.d.ts +6 -1
- package/src/factories/product/product.factory.d.ts +6 -2
- package/src/factories/product-associations/product-associations.factory.d.ts +8 -2
- package/src/factories/product-search/product-search.factory.d.ts +5 -2
- package/src/factories/profile/profile.factory.d.ts +4 -2
|
@@ -62,7 +62,7 @@ class MedusaCartCapability extends CartCapability {
|
|
|
62
62
|
debug("Received cart response:", cartResponse);
|
|
63
63
|
}
|
|
64
64
|
if (cartResponse.cart) {
|
|
65
|
-
return success(this.
|
|
65
|
+
return success(this.factory.parseCart(this.context, cartResponse.cart));
|
|
66
66
|
}
|
|
67
67
|
return error({
|
|
68
68
|
type: "NotFound",
|
|
@@ -119,7 +119,7 @@ class MedusaCartCapability extends CartCapability {
|
|
|
119
119
|
debug("Received add item response:", response);
|
|
120
120
|
}
|
|
121
121
|
if (response.cart) {
|
|
122
|
-
return success(this.
|
|
122
|
+
return success(this.factory.parseCart(this.context, response.cart));
|
|
123
123
|
}
|
|
124
124
|
throw new Error("Failed to add item to cart");
|
|
125
125
|
} catch (error2) {
|
|
@@ -138,7 +138,7 @@ class MedusaCartCapability extends CartCapability {
|
|
|
138
138
|
}
|
|
139
139
|
);
|
|
140
140
|
if (response.parent) {
|
|
141
|
-
return success(this.
|
|
141
|
+
return success(this.factory.parseCart(this.context, response.parent));
|
|
142
142
|
}
|
|
143
143
|
throw new Error("Failed to remove item from cart");
|
|
144
144
|
} catch (error2) {
|
|
@@ -173,7 +173,7 @@ class MedusaCartCapability extends CartCapability {
|
|
|
173
173
|
}
|
|
174
174
|
);
|
|
175
175
|
if (response.cart) {
|
|
176
|
-
return success(this.
|
|
176
|
+
return success(this.factory.parseCart(this.context, response.cart));
|
|
177
177
|
}
|
|
178
178
|
throw new Error("Failed to change item quantity");
|
|
179
179
|
} catch (error2) {
|
|
@@ -257,7 +257,7 @@ class MedusaCartCapability extends CartCapability {
|
|
|
257
257
|
}
|
|
258
258
|
);
|
|
259
259
|
if (response.cart) {
|
|
260
|
-
return success(this.
|
|
260
|
+
return success(this.factory.parseCart(this.context, response.cart));
|
|
261
261
|
}
|
|
262
262
|
throw new Error("Failed to apply coupon code");
|
|
263
263
|
} catch (error2) {
|
|
@@ -289,7 +289,7 @@ class MedusaCartCapability extends CartCapability {
|
|
|
289
289
|
}
|
|
290
290
|
);
|
|
291
291
|
if (response.cart) {
|
|
292
|
-
return success(this.
|
|
292
|
+
return success(this.factory.parseCart(this.context, response.cart));
|
|
293
293
|
}
|
|
294
294
|
throw new Error("Failed to remove coupon code");
|
|
295
295
|
} catch (error2) {
|
|
@@ -322,7 +322,7 @@ class MedusaCartCapability extends CartCapability {
|
|
|
322
322
|
this.medusaApi.setSessionData({
|
|
323
323
|
activeCartId: updatedCartResponse.cart.id
|
|
324
324
|
});
|
|
325
|
-
return success(this.
|
|
325
|
+
return success(this.factory.parseCart(this.context, updatedCartResponse.cart));
|
|
326
326
|
}
|
|
327
327
|
throw new Error("Failed to change currency");
|
|
328
328
|
} catch (error2) {
|
|
@@ -366,98 +366,6 @@ class MedusaCartCapability extends CartCapability {
|
|
|
366
366
|
async getClient() {
|
|
367
367
|
return this.medusaApi.getClient();
|
|
368
368
|
}
|
|
369
|
-
/**
|
|
370
|
-
* Extension point to control the parsing of a single cart item price
|
|
371
|
-
* @param remoteItem
|
|
372
|
-
* @param currency
|
|
373
|
-
* @returns
|
|
374
|
-
*/
|
|
375
|
-
parseItemPrice(remoteItem, currency) {
|
|
376
|
-
return parseMedusaItemPrice(remoteItem, currency);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Extension point to control the parsing of the cost breakdown of a cart
|
|
380
|
-
* @param remote
|
|
381
|
-
* @returns
|
|
382
|
-
*/
|
|
383
|
-
parseCostBreakdown(remote) {
|
|
384
|
-
return parseMedusaCostBreakdown(remote);
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Extension point to control the parsing of a single cart item
|
|
388
|
-
* @param remoteItem
|
|
389
|
-
* @param currency
|
|
390
|
-
* @returns
|
|
391
|
-
*/
|
|
392
|
-
parseCartItem(remoteItem, currency) {
|
|
393
|
-
const item = {
|
|
394
|
-
identifier: {
|
|
395
|
-
key: remoteItem.id
|
|
396
|
-
},
|
|
397
|
-
product: {
|
|
398
|
-
key: remoteItem.product_id || ""
|
|
399
|
-
},
|
|
400
|
-
variant: ProductVariantIdentifierSchema.parse({
|
|
401
|
-
sku: remoteItem.variant_sku || ""
|
|
402
|
-
}),
|
|
403
|
-
quantity: remoteItem.quantity || 1,
|
|
404
|
-
price: parseMedusaItemPrice(remoteItem, currency)
|
|
405
|
-
};
|
|
406
|
-
return item;
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Extension point to control the parsing of a single cart
|
|
410
|
-
* @param remote
|
|
411
|
-
* @returns
|
|
412
|
-
*/
|
|
413
|
-
parseSingle(remote) {
|
|
414
|
-
const identifier = MedusaCartIdentifierSchema.parse({
|
|
415
|
-
key: remote.id,
|
|
416
|
-
region_id: remote.region_id
|
|
417
|
-
});
|
|
418
|
-
const name = "" + (remote.metadata?.["name"] || "");
|
|
419
|
-
const description = "" + (remote.metadata?.["description"] || "");
|
|
420
|
-
const price = this.parseCostBreakdown(remote);
|
|
421
|
-
const items = new Array();
|
|
422
|
-
const allItems = remote.items || [];
|
|
423
|
-
allItems.sort(
|
|
424
|
-
(a, b) => a.created_at && b.created_at ? new Date(a.created_at).getTime() - new Date(b.created_at).getTime() : 0
|
|
425
|
-
);
|
|
426
|
-
for (const remoteItem of allItems) {
|
|
427
|
-
items.push(this.parseCartItem(remoteItem, price.grandTotal.currency));
|
|
428
|
-
}
|
|
429
|
-
const appliedPromotions = [];
|
|
430
|
-
if (remote.promotions) {
|
|
431
|
-
for (const promo of remote.promotions) {
|
|
432
|
-
const promotionName = promo.code;
|
|
433
|
-
let promoDescription = "";
|
|
434
|
-
if (promo.application_method?.type === "percentage") {
|
|
435
|
-
promoDescription = `-${promo.application_method.value}%`;
|
|
436
|
-
}
|
|
437
|
-
if (promo.application_method?.type === "fixed") {
|
|
438
|
-
promoDescription = `-${promo.application_method.value} ${price.grandTotal.currency}`;
|
|
439
|
-
}
|
|
440
|
-
appliedPromotions.push({
|
|
441
|
-
code: promo.code || "",
|
|
442
|
-
isCouponCode: promo.is_automatic ? false : true,
|
|
443
|
-
name: promotionName || promoDescription,
|
|
444
|
-
description: promoDescription
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
const result = {
|
|
449
|
-
identifier,
|
|
450
|
-
name,
|
|
451
|
-
description,
|
|
452
|
-
price,
|
|
453
|
-
items,
|
|
454
|
-
appliedPromotions,
|
|
455
|
-
userId: {
|
|
456
|
-
userId: "???"
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
return this.factory.parseCart(this.context, result);
|
|
460
|
-
}
|
|
461
369
|
}
|
|
462
370
|
__decorateClass([
|
|
463
371
|
Reactionary({
|
|
@@ -10,19 +10,17 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
10
10
|
return result;
|
|
11
11
|
};
|
|
12
12
|
import {
|
|
13
|
-
CategoryIdentifierSchema,
|
|
14
13
|
CategoryCapability,
|
|
15
|
-
|
|
14
|
+
CategoryPaginatedResultSchema,
|
|
16
15
|
CategoryQueryByIdSchema,
|
|
17
16
|
CategoryQueryBySlugSchema,
|
|
18
17
|
CategoryQueryForBreadcrumbSchema,
|
|
19
18
|
CategoryQueryForChildCategoriesSchema,
|
|
20
19
|
CategoryQueryForTopCategoriesSchema,
|
|
21
|
-
|
|
22
|
-
Reactionary,
|
|
23
|
-
success,
|
|
20
|
+
CategorySchema,
|
|
24
21
|
error,
|
|
25
|
-
|
|
22
|
+
Reactionary,
|
|
23
|
+
success
|
|
26
24
|
} from "@reactionary/core";
|
|
27
25
|
import * as z from "zod";
|
|
28
26
|
class MedusaCategoryCapability extends CategoryCapability {
|
|
@@ -72,7 +70,7 @@ class MedusaCategoryCapability extends CategoryCapability {
|
|
|
72
70
|
identifier: payload
|
|
73
71
|
});
|
|
74
72
|
}
|
|
75
|
-
return success(this.
|
|
73
|
+
return success(this.factory.parseCategory(this.context, candidate));
|
|
76
74
|
}
|
|
77
75
|
getBySlugPayload(payload) {
|
|
78
76
|
return {
|
|
@@ -83,14 +81,21 @@ class MedusaCategoryCapability extends CategoryCapability {
|
|
|
83
81
|
}
|
|
84
82
|
async getBySlug(payload) {
|
|
85
83
|
const sdk = await this.medusaApi.getClient();
|
|
86
|
-
const categoryResult = await sdk.store.category.list(
|
|
84
|
+
const categoryResult = await sdk.store.category.list(
|
|
85
|
+
this.getBySlugPayload(payload)
|
|
86
|
+
);
|
|
87
87
|
if (categoryResult.count === 0) {
|
|
88
88
|
return error({
|
|
89
89
|
type: "NotFound",
|
|
90
90
|
identifier: payload
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
|
-
return success(
|
|
93
|
+
return success(
|
|
94
|
+
this.factory.parseCategory(
|
|
95
|
+
this.context,
|
|
96
|
+
categoryResult.product_categories[0]
|
|
97
|
+
)
|
|
98
|
+
);
|
|
94
99
|
}
|
|
95
100
|
async getBreadcrumbPathToCategory(payload) {
|
|
96
101
|
const actualCategoryId = await this.resolveCategoryIdByExternalId(
|
|
@@ -107,7 +112,7 @@ class MedusaCategoryCapability extends CategoryCapability {
|
|
|
107
112
|
let results = new Array();
|
|
108
113
|
let current = path.product_category;
|
|
109
114
|
while (current) {
|
|
110
|
-
results.push(this.
|
|
115
|
+
results.push(this.factory.parseCategory(this.context, current));
|
|
111
116
|
current = current.parent_category;
|
|
112
117
|
}
|
|
113
118
|
results = results.reverse();
|
|
@@ -129,8 +134,13 @@ class MedusaCategoryCapability extends CategoryCapability {
|
|
|
129
134
|
if (!actualParent) {
|
|
130
135
|
throw new Error("Parent category not found " + payload.parentId.key);
|
|
131
136
|
}
|
|
132
|
-
const response = await sdk.store.category.list(
|
|
133
|
-
|
|
137
|
+
const response = await sdk.store.category.list(
|
|
138
|
+
this.findChildCategoriesPayload(payload, actualParent)
|
|
139
|
+
);
|
|
140
|
+
const result = this.factory.parseCategoryPaginatedResult(
|
|
141
|
+
this.context,
|
|
142
|
+
response
|
|
143
|
+
);
|
|
134
144
|
return success(result);
|
|
135
145
|
}
|
|
136
146
|
findTopCategoriesPayload(payload) {
|
|
@@ -143,42 +153,14 @@ class MedusaCategoryCapability extends CategoryCapability {
|
|
|
143
153
|
}
|
|
144
154
|
async findTopCategories(payload) {
|
|
145
155
|
const sdk = await this.medusaApi.getClient();
|
|
146
|
-
const response = await sdk.store.category.list(
|
|
147
|
-
|
|
148
|
-
return success(result);
|
|
149
|
-
}
|
|
150
|
-
parseSingle(_body) {
|
|
151
|
-
const identifier = CategoryIdentifierSchema.parse({
|
|
152
|
-
key: _body.metadata?.["external_id"] || ""
|
|
153
|
-
});
|
|
154
|
-
const name = _body.name;
|
|
155
|
-
const slug = _body.handle;
|
|
156
|
-
const text = _body.description || _body.name || "";
|
|
157
|
-
const parentCategory = _body.parent_category_id ? { key: _body.parent_category?.metadata?.["external_id"] + "" || "" } : void 0;
|
|
158
|
-
const result = {
|
|
159
|
-
identifier,
|
|
160
|
-
name,
|
|
161
|
-
slug,
|
|
162
|
-
text,
|
|
163
|
-
parentCategory,
|
|
164
|
-
images: []
|
|
165
|
-
};
|
|
166
|
-
return this.factory.parseCategory(this.context, result);
|
|
167
|
-
}
|
|
168
|
-
parsePaginatedResult(body) {
|
|
169
|
-
const items = body.product_categories.map((x) => this.parseSingle(x));
|
|
170
|
-
const totalPages = Math.ceil(
|
|
171
|
-
(body.count ?? 0) / Math.max(body.product_categories.length, 1)
|
|
156
|
+
const response = await sdk.store.category.list(
|
|
157
|
+
this.findTopCategoriesPayload(payload)
|
|
172
158
|
);
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
totalPages,
|
|
179
|
-
items
|
|
180
|
-
};
|
|
181
|
-
return this.factory.parseCategoryPaginatedResult(this.context, result);
|
|
159
|
+
const result = this.factory.parseCategoryPaginatedResult(
|
|
160
|
+
this.context,
|
|
161
|
+
response
|
|
162
|
+
);
|
|
163
|
+
return success(result);
|
|
182
164
|
}
|
|
183
165
|
}
|
|
184
166
|
__decorateClass([
|
|
@@ -10,14 +10,13 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
10
10
|
return result;
|
|
11
11
|
};
|
|
12
12
|
import {
|
|
13
|
-
|
|
13
|
+
CheckoutCapability,
|
|
14
14
|
CheckoutMutationAddPaymentInstructionSchema,
|
|
15
15
|
CheckoutMutationFinalizeCheckoutSchema,
|
|
16
16
|
CheckoutMutationInitiateCheckoutSchema,
|
|
17
17
|
CheckoutMutationRemovePaymentInstructionSchema,
|
|
18
18
|
CheckoutMutationSetShippingAddressSchema,
|
|
19
19
|
CheckoutMutationSetShippingInstructionSchema,
|
|
20
|
-
CheckoutCapability,
|
|
21
20
|
CheckoutQueryByIdSchema,
|
|
22
21
|
CheckoutQueryForAvailablePaymentMethodsSchema,
|
|
23
22
|
CheckoutQueryForAvailableShippingMethodsSchema,
|
|
@@ -27,14 +26,13 @@ import {
|
|
|
27
26
|
Reactionary,
|
|
28
27
|
ShippingMethodIdentifierSchema,
|
|
29
28
|
ShippingMethodSchema,
|
|
30
|
-
success
|
|
31
|
-
error
|
|
29
|
+
success
|
|
32
30
|
} from "@reactionary/core";
|
|
33
31
|
import createDebug from "debug";
|
|
34
32
|
import * as z from "zod";
|
|
35
|
-
import { handleProviderError, parseMedusaCostBreakdown, parseMedusaItemPrice } from "../utils/medusa-helpers.js";
|
|
36
33
|
import {
|
|
37
34
|
} from "../schema/medusa.schema.js";
|
|
35
|
+
import { handleProviderError } from "../utils/medusa-helpers.js";
|
|
38
36
|
const debug = createDebug("reactionary:medusa:checkout");
|
|
39
37
|
class CheckoutNotReadyForFinalizationError extends Error {
|
|
40
38
|
constructor(checkoutIdentifier) {
|
|
@@ -85,14 +83,14 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
85
83
|
fields: this.includedFields
|
|
86
84
|
}
|
|
87
85
|
);
|
|
88
|
-
return success(this.
|
|
86
|
+
return success(this.factory.parseCheckout(this.context, response.cart));
|
|
89
87
|
}
|
|
90
88
|
async getById(payload) {
|
|
91
89
|
const client = await this.medusaApi.getClient();
|
|
92
90
|
const response = await client.store.cart.retrieve(payload.identifier.key, {
|
|
93
91
|
fields: this.includedFields
|
|
94
92
|
});
|
|
95
|
-
return success(this.
|
|
93
|
+
return success(this.factory.parseCheckout(this.context, response.cart));
|
|
96
94
|
}
|
|
97
95
|
setShippingAddressPayload(payload) {
|
|
98
96
|
return {
|
|
@@ -108,7 +106,7 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
108
106
|
fields: this.includedFields
|
|
109
107
|
}
|
|
110
108
|
);
|
|
111
|
-
return success(this.
|
|
109
|
+
return success(this.factory.parseCheckout(this.context, response.cart));
|
|
112
110
|
}
|
|
113
111
|
getAvailableShippingMethodsPayload(payload) {
|
|
114
112
|
return {
|
|
@@ -127,18 +125,7 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
127
125
|
);
|
|
128
126
|
const shippingMethods = [];
|
|
129
127
|
for (const sm of shippingMethodResponse.shipping_options) {
|
|
130
|
-
shippingMethods.push(
|
|
131
|
-
ShippingMethodSchema.parse({
|
|
132
|
-
identifier: ShippingMethodIdentifierSchema.parse({ key: sm.id }),
|
|
133
|
-
name: sm.name,
|
|
134
|
-
description: sm.type.description || "",
|
|
135
|
-
price: MonetaryAmountSchema.parse({
|
|
136
|
-
value: sm.calculated_price.calculated_amount || 0,
|
|
137
|
-
currency: sm.calculated_price.currency_code?.toUpperCase()
|
|
138
|
-
}),
|
|
139
|
-
deliveryTime: ""
|
|
140
|
-
})
|
|
141
|
-
);
|
|
128
|
+
shippingMethods.push(this.factory.parseShippingMethod(this.context, sm));
|
|
142
129
|
}
|
|
143
130
|
if (debug.enabled) {
|
|
144
131
|
debug(
|
|
@@ -147,7 +134,7 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
147
134
|
);
|
|
148
135
|
}
|
|
149
136
|
return success(
|
|
150
|
-
shippingMethods
|
|
137
|
+
shippingMethods
|
|
151
138
|
);
|
|
152
139
|
}
|
|
153
140
|
getAvailablePaymentMethodsPayload(payload, regionId) {
|
|
@@ -171,18 +158,8 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
171
158
|
);
|
|
172
159
|
const paymentMethods = [];
|
|
173
160
|
for (const pm of paymentMethodResponse.payment_providers) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
identifier: {
|
|
177
|
-
method: pm.id,
|
|
178
|
-
name: pm.id,
|
|
179
|
-
paymentProcessor: pm.id
|
|
180
|
-
},
|
|
181
|
-
logo: void 0,
|
|
182
|
-
description: pm.id,
|
|
183
|
-
isPunchOut: true
|
|
184
|
-
}
|
|
185
|
-
);
|
|
161
|
+
const payMethod = this.factory.parsePaymentMethod(this.context, pm);
|
|
162
|
+
paymentMethods.push(payMethod);
|
|
186
163
|
}
|
|
187
164
|
if (debug.enabled) {
|
|
188
165
|
debug(
|
|
@@ -190,9 +167,7 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
190
167
|
paymentMethods
|
|
191
168
|
);
|
|
192
169
|
}
|
|
193
|
-
return success(
|
|
194
|
-
paymentMethods.map((x) => this.factory.parsePaymentMethod(this.context, x))
|
|
195
|
-
);
|
|
170
|
+
return success(paymentMethods);
|
|
196
171
|
}
|
|
197
172
|
addPaymentInstructionPayload(payload) {
|
|
198
173
|
return {
|
|
@@ -221,11 +196,11 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
221
196
|
fields: this.includedFields
|
|
222
197
|
}
|
|
223
198
|
);
|
|
224
|
-
return success(this.
|
|
225
|
-
} catch (
|
|
226
|
-
debug("Failed to add payment instruction: {0}", [
|
|
199
|
+
return success(this.factory.parseCheckout(this.context, updatedCartResponse.cart));
|
|
200
|
+
} catch (error) {
|
|
201
|
+
debug("Failed to add payment instruction: {0}", [error]);
|
|
227
202
|
throw new Error(
|
|
228
|
-
`Failed to add payment instruction: ${
|
|
203
|
+
`Failed to add payment instruction: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
229
204
|
);
|
|
230
205
|
}
|
|
231
206
|
}
|
|
@@ -259,11 +234,11 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
259
234
|
fields: this.includedFields
|
|
260
235
|
});
|
|
261
236
|
if (response.cart) {
|
|
262
|
-
return success(this.
|
|
237
|
+
return success(this.factory.parseCheckout(this.context, response.cart));
|
|
263
238
|
}
|
|
264
239
|
throw new Error("Failed to set shipping method");
|
|
265
|
-
} catch (
|
|
266
|
-
handleProviderError("set shipping method",
|
|
240
|
+
} catch (error) {
|
|
241
|
+
handleProviderError("set shipping method", error);
|
|
267
242
|
}
|
|
268
243
|
}
|
|
269
244
|
async finalizeCheckout(payload) {
|
|
@@ -278,7 +253,7 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
278
253
|
await client.store.cart.update(medusaId.key, {
|
|
279
254
|
metadata: {
|
|
280
255
|
order_id: response.order.id,
|
|
281
|
-
order_display_id: response.order?.display_id ?
|
|
256
|
+
order_display_id: response.order?.display_id ? response.order.display_id + "" : ""
|
|
282
257
|
}
|
|
283
258
|
});
|
|
284
259
|
const refreshedCheckout = await this.getById({
|
|
@@ -307,171 +282,6 @@ class MedusaCheckoutCapability extends CheckoutCapability {
|
|
|
307
282
|
country_code: address.countryCode?.toLowerCase()
|
|
308
283
|
};
|
|
309
284
|
}
|
|
310
|
-
/**
|
|
311
|
-
* Extension point to control the parsing of an address from a store cart address
|
|
312
|
-
* @param storeAddress
|
|
313
|
-
* @returns
|
|
314
|
-
*/
|
|
315
|
-
composeAddressFromStoreAddress(storeAddress) {
|
|
316
|
-
return {
|
|
317
|
-
identifier: AddressIdentifierSchema.parse({
|
|
318
|
-
nickName: storeAddress.id
|
|
319
|
-
}),
|
|
320
|
-
firstName: storeAddress.first_name || "",
|
|
321
|
-
lastName: storeAddress.last_name || "",
|
|
322
|
-
streetAddress: storeAddress.address_1 || "",
|
|
323
|
-
streetNumber: storeAddress.address_2 || "",
|
|
324
|
-
city: storeAddress.city || "",
|
|
325
|
-
postalCode: storeAddress.postal_code || "",
|
|
326
|
-
countryCode: storeAddress.country_code || "",
|
|
327
|
-
region: ""
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Extension point to control the parsing of a single cart item price
|
|
332
|
-
* @param remoteItem
|
|
333
|
-
* @param currency
|
|
334
|
-
* @returns
|
|
335
|
-
*/
|
|
336
|
-
parseItemPrice(remoteItem, currency) {
|
|
337
|
-
return parseMedusaItemPrice(remoteItem, currency);
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Extension point to control the parsing of the cost breakdown of a cart
|
|
341
|
-
* @param remote
|
|
342
|
-
* @returns
|
|
343
|
-
*/
|
|
344
|
-
parseCostBreakdown(remote) {
|
|
345
|
-
return parseMedusaCostBreakdown(remote);
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* Extension point to control the parsing of a single checkout item
|
|
349
|
-
* @param remoteItem
|
|
350
|
-
* @param currency
|
|
351
|
-
* @returns
|
|
352
|
-
*/
|
|
353
|
-
parseCheckoutItem(remoteItem, currency) {
|
|
354
|
-
const unitPrice = remoteItem.unit_price || 0;
|
|
355
|
-
const totalPrice = unitPrice * remoteItem.quantity || 0;
|
|
356
|
-
const discountTotal = remoteItem.discount_total || 0;
|
|
357
|
-
const item = {
|
|
358
|
-
identifier: {
|
|
359
|
-
key: remoteItem.id
|
|
360
|
-
},
|
|
361
|
-
variant: {
|
|
362
|
-
sku: remoteItem.variant_sku || ""
|
|
363
|
-
},
|
|
364
|
-
quantity: remoteItem.quantity || 1,
|
|
365
|
-
price: this.parseItemPrice(remoteItem, currency)
|
|
366
|
-
};
|
|
367
|
-
return item;
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Extension point to control the parsing of the Checkout object
|
|
371
|
-
* @param remote
|
|
372
|
-
* @returns
|
|
373
|
-
*/
|
|
374
|
-
parseSingle(remote) {
|
|
375
|
-
const identifier = {
|
|
376
|
-
key: remote.id
|
|
377
|
-
// region_id: remote.region_id,
|
|
378
|
-
};
|
|
379
|
-
const name = "" + (remote.metadata?.["name"] || "");
|
|
380
|
-
const description = "" + (remote.metadata?.["description"] || "");
|
|
381
|
-
const price = this.parseCostBreakdown(remote);
|
|
382
|
-
const items = new Array();
|
|
383
|
-
for (const remoteItem of remote.items || []) {
|
|
384
|
-
items.push(this.parseCheckoutItem(remoteItem, price.grandTotal.currency));
|
|
385
|
-
}
|
|
386
|
-
const billingAddress = remote.billing_address ? this.composeAddressFromStoreAddress(remote.billing_address) : void 0;
|
|
387
|
-
const shippingAddress = remote.shipping_address ? this.composeAddressFromStoreAddress(remote.shipping_address) : void 0;
|
|
388
|
-
const backupUnattendedDelivery = remote.metadata?.["consent_for_unattended_delivery"] !== void 0 ? remote.metadata?.["consent_for_unattended_delivery"] === "true" : void 0;
|
|
389
|
-
const backupInstructions = remote.metadata?.["instructions"] !== void 0 ? remote.metadata?.["instructions"] + "" : void 0;
|
|
390
|
-
const backupPickupPoint = remote.metadata?.["pickup_point"] !== void 0 ? remote.metadata?.["pickup_point"] + "" : void 0;
|
|
391
|
-
let shippingInstruction;
|
|
392
|
-
remote.shipping_methods?.forEach((sm) => {
|
|
393
|
-
let pickupPoint = "";
|
|
394
|
-
let instructions = "";
|
|
395
|
-
let consentForUnattendedDelivery = false;
|
|
396
|
-
if (sm.data) {
|
|
397
|
-
pickupPoint = sm.data["pickup_point"] + "" || "";
|
|
398
|
-
instructions = sm.data["instructions"] + "" || "";
|
|
399
|
-
consentForUnattendedDelivery = sm.data["consent_for_unattended_delivery"] === "true";
|
|
400
|
-
}
|
|
401
|
-
if (!pickupPoint) {
|
|
402
|
-
pickupPoint = backupPickupPoint || "";
|
|
403
|
-
}
|
|
404
|
-
if (!instructions) {
|
|
405
|
-
instructions = backupInstructions || "";
|
|
406
|
-
}
|
|
407
|
-
if (!consentForUnattendedDelivery) {
|
|
408
|
-
consentForUnattendedDelivery = backupUnattendedDelivery || false;
|
|
409
|
-
}
|
|
410
|
-
shippingInstruction = {
|
|
411
|
-
shippingMethod: { key: sm.shipping_option_id },
|
|
412
|
-
consentForUnattendedDelivery,
|
|
413
|
-
instructions,
|
|
414
|
-
pickupPoint
|
|
415
|
-
};
|
|
416
|
-
});
|
|
417
|
-
const paymentInstructions = new Array();
|
|
418
|
-
for (const remotePayment of remote.payment_collection?.payment_sessions || []) {
|
|
419
|
-
if (remotePayment.status === "canceled" || remotePayment.status === "error") {
|
|
420
|
-
console.warn(
|
|
421
|
-
`Skipping payment session ${remotePayment.id} with status ${remotePayment.status}`
|
|
422
|
-
);
|
|
423
|
-
continue;
|
|
424
|
-
}
|
|
425
|
-
const paymentMethodIdentifier = {
|
|
426
|
-
method: remotePayment.provider_id,
|
|
427
|
-
name: remotePayment.provider_id,
|
|
428
|
-
paymentProcessor: remotePayment.provider_id
|
|
429
|
-
};
|
|
430
|
-
paymentInstructions.push(
|
|
431
|
-
{
|
|
432
|
-
identifier: {
|
|
433
|
-
key: remotePayment.id
|
|
434
|
-
},
|
|
435
|
-
amount: {
|
|
436
|
-
value: remotePayment.amount,
|
|
437
|
-
currency: remotePayment.currency_code?.toUpperCase()
|
|
438
|
-
},
|
|
439
|
-
paymentMethod: paymentMethodIdentifier,
|
|
440
|
-
protocolData: remotePayment.data ? Object.entries(remotePayment.data).map(([key, value]) => ({
|
|
441
|
-
key,
|
|
442
|
-
value: String(value)
|
|
443
|
-
})) : [],
|
|
444
|
-
status: "pending"
|
|
445
|
-
}
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
const originalCartReference = {
|
|
449
|
-
key: remote.id,
|
|
450
|
-
region: remote.region_id
|
|
451
|
-
};
|
|
452
|
-
let resultingOrder = void 0;
|
|
453
|
-
if (remote.metadata?.["order_id"]) {
|
|
454
|
-
resultingOrder = {
|
|
455
|
-
key: remote.metadata?.["order_id"] + "" || "",
|
|
456
|
-
display_id: Number(remote.metadata?.["order_display_id"] + "" || "0")
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
const result = {
|
|
460
|
-
identifier,
|
|
461
|
-
name,
|
|
462
|
-
description,
|
|
463
|
-
price,
|
|
464
|
-
items,
|
|
465
|
-
originalCartReference,
|
|
466
|
-
paymentInstructions,
|
|
467
|
-
readyForFinalization: false,
|
|
468
|
-
billingAddress,
|
|
469
|
-
resultingOrder,
|
|
470
|
-
shippingAddress,
|
|
471
|
-
shippingInstruction
|
|
472
|
-
};
|
|
473
|
-
return this.factory.parseCheckout(this.context, result);
|
|
474
|
-
}
|
|
475
285
|
}
|
|
476
286
|
__decorateClass([
|
|
477
287
|
Reactionary({
|