@unchainedshop/core-orders 2.0.0-beta8 → 2.0.0-beta9
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/jest.config.js +9 -0
- package/lib/director/OrderPricingDirector.js +5 -5
- package/lib/director/OrderPricingDirector.js.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.d.ts +2 -1
- package/lib/module/configureOrderDeliveriesModule.js +26 -24
- package/lib/module/configureOrderDeliveriesModule.js.map +1 -1
- package/lib/module/configureOrderDiscountsModule.d.ts +2 -1
- package/lib/module/configureOrderDiscountsModule.js +35 -35
- package/lib/module/configureOrderDiscountsModule.js.map +1 -1
- package/lib/module/configureOrderPaymentsModule.d.ts +3 -1
- package/lib/module/configureOrderPaymentsModule.js +63 -37
- package/lib/module/configureOrderPaymentsModule.js.map +1 -1
- package/lib/module/configureOrderPositionsModule.d.ts +2 -1
- package/lib/module/configureOrderPositionsModule.js +35 -29
- package/lib/module/configureOrderPositionsModule.js.map +1 -1
- package/lib/module/configureOrdersModule-mutations.js +21 -26
- package/lib/module/configureOrdersModule-mutations.js.map +1 -1
- package/lib/module/configureOrdersModule-processing.js +52 -58
- package/lib/module/configureOrdersModule-processing.js.map +1 -1
- package/lib/module/configureOrdersModule-queries.d.ts +3 -2
- package/lib/module/configureOrdersModule-queries.js +1 -1
- package/lib/module/configureOrdersModule-queries.js.map +1 -1
- package/lib/module/configureOrdersModule-transformations.js +10 -10
- package/lib/module/configureOrdersModule-transformations.js.map +1 -1
- package/lib/module/configureOrdersModule.js +24 -26
- package/lib/module/configureOrdersModule.js.map +1 -1
- package/lib/service/createUserCartService.js +8 -8
- package/lib/service/createUserCartService.js.map +1 -1
- package/lib/service/migrateOrderCartService.js +3 -9
- package/lib/service/migrateOrderCartService.js.map +1 -1
- package/package.json +10 -9
- package/src/director/OrderPricingDirector.ts +5 -5
- package/src/module/configureOrderDeliveriesModule.ts +32 -42
- package/src/module/configureOrderDiscountsModule.ts +42 -54
- package/src/module/configureOrderPaymentsModule.ts +93 -76
- package/src/module/configureOrderPositionsModule.ts +57 -46
- package/src/module/configureOrdersModule-mutations.ts +43 -58
- package/src/module/configureOrdersModule-processing.ts +75 -85
- package/src/module/configureOrdersModule-queries.ts +1 -1
- package/src/module/configureOrdersModule-transformations.ts +10 -10
- package/src/module/configureOrdersModule.ts +29 -42
- package/src/service/createUserCartService.ts +20 -24
- package/src/service/migrateOrderCartService.ts +3 -11
- package/tests/orders-index.test.ts +103 -48
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Context } from '@unchainedshop/types/api';
|
|
2
1
|
import { Collection, Filter } from '@unchainedshop/types/common';
|
|
3
|
-
import { ModuleMutations } from '@unchainedshop/types/core';
|
|
2
|
+
import { ModuleMutations, UnchainedCore } from '@unchainedshop/types/core';
|
|
4
3
|
import { OrdersModule } from '@unchainedshop/types/orders';
|
|
5
4
|
import { OrderDiscount, OrderDiscountsModule } from '@unchainedshop/types/orders.discounts';
|
|
6
5
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
@@ -22,7 +21,7 @@ const OrderDiscountErrorCode = {
|
|
|
22
21
|
CODE_NOT_VALID: 'CODE_NOT_VALID',
|
|
23
22
|
};
|
|
24
23
|
|
|
25
|
-
const buildFindByIdSelector = (orderDiscountId: string) =>
|
|
24
|
+
export const buildFindByIdSelector = (orderDiscountId: string) =>
|
|
26
25
|
generateDbFilterById(orderDiscountId) as Filter<OrderDiscount>;
|
|
27
26
|
|
|
28
27
|
export const configureOrderDiscountsModule = ({
|
|
@@ -39,31 +38,31 @@ export const configureOrderDiscountsModule = ({
|
|
|
39
38
|
hasCreateOnly: false,
|
|
40
39
|
}) as ModuleMutations<OrderDiscount>;
|
|
41
40
|
|
|
42
|
-
const getAdapter = async (orderDiscount: OrderDiscount,
|
|
43
|
-
const order = await
|
|
41
|
+
const getAdapter = async (orderDiscount: OrderDiscount, unchainedAPI: UnchainedCore) => {
|
|
42
|
+
const order = await unchainedAPI.modules.orders.findOrder({
|
|
44
43
|
orderId: orderDiscount.orderId,
|
|
45
44
|
});
|
|
46
45
|
const Adapter = OrderDiscountDirector.getAdapter(orderDiscount.discountKey);
|
|
47
46
|
if (!Adapter) return null;
|
|
48
47
|
const adapter = await Adapter.actions({
|
|
49
|
-
context: { order, orderDiscount, code: orderDiscount.code, ...
|
|
48
|
+
context: { order, orderDiscount, code: orderDiscount.code, ...unchainedAPI },
|
|
50
49
|
});
|
|
51
50
|
return adapter;
|
|
52
51
|
};
|
|
53
52
|
|
|
54
|
-
const createDiscount: OrderDiscountsModule['create'] = async (doc
|
|
53
|
+
const createDiscount: OrderDiscountsModule['create'] = async (doc) => {
|
|
55
54
|
const normalizedTrigger = doc.trigger || OrderDiscountTrigger.USER;
|
|
56
55
|
|
|
57
56
|
log(`Create Order Discount: ${doc.discountKey} with trigger ${normalizedTrigger}`, {
|
|
58
57
|
orderId: doc.orderId,
|
|
59
58
|
});
|
|
60
59
|
|
|
61
|
-
const discountId = await mutations.create({ ...doc, trigger: normalizedTrigger }
|
|
60
|
+
const discountId = await mutations.create({ ...doc, trigger: normalizedTrigger });
|
|
62
61
|
const discount = await OrderDiscounts.findOne(buildFindByIdSelector(discountId));
|
|
63
62
|
return discount;
|
|
64
63
|
};
|
|
65
64
|
|
|
66
|
-
const deleteDiscount: OrderDiscountsModule['delete'] = async (orderDiscountId,
|
|
65
|
+
const deleteDiscount: OrderDiscountsModule['delete'] = async (orderDiscountId, unchainedAPI) => {
|
|
67
66
|
const selector = buildFindByIdSelector(orderDiscountId);
|
|
68
67
|
const discount = await OrderDiscounts.findOne(selector, {});
|
|
69
68
|
|
|
@@ -73,12 +72,12 @@ export const configureOrderDiscountsModule = ({
|
|
|
73
72
|
|
|
74
73
|
if (discount.trigger === OrderDiscountTrigger.USER) {
|
|
75
74
|
// Release
|
|
76
|
-
const adapter = await getAdapter(discount,
|
|
75
|
+
const adapter = await getAdapter(discount, unchainedAPI);
|
|
77
76
|
if (!adapter) return null;
|
|
78
77
|
await adapter.release();
|
|
79
78
|
|
|
80
79
|
await OrderDiscounts.deleteOne(selector);
|
|
81
|
-
await updateCalculation(discount.orderId,
|
|
80
|
+
await updateCalculation(discount.orderId, unchainedAPI);
|
|
82
81
|
} else {
|
|
83
82
|
await OrderDiscounts.deleteOne(selector);
|
|
84
83
|
await emit('ORDER_REMOVE_DISCOUNT', { discount });
|
|
@@ -86,14 +85,10 @@ export const configureOrderDiscountsModule = ({
|
|
|
86
85
|
return discount;
|
|
87
86
|
};
|
|
88
87
|
|
|
89
|
-
const updateDiscount: OrderDiscountsModule['update'] = async (orderDiscountId, doc
|
|
90
|
-
await mutations.update(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
$set: doc,
|
|
94
|
-
},
|
|
95
|
-
userId,
|
|
96
|
-
);
|
|
88
|
+
const updateDiscount: OrderDiscountsModule['update'] = async (orderDiscountId, doc) => {
|
|
89
|
+
await mutations.update(orderDiscountId, {
|
|
90
|
+
$set: doc,
|
|
91
|
+
});
|
|
97
92
|
|
|
98
93
|
const selector = buildFindByIdSelector(orderDiscountId);
|
|
99
94
|
const discount = await OrderDiscounts.findOne(selector, {});
|
|
@@ -101,24 +96,20 @@ export const configureOrderDiscountsModule = ({
|
|
|
101
96
|
return discount;
|
|
102
97
|
};
|
|
103
98
|
|
|
104
|
-
const reserveDiscount = async (orderDiscount: OrderDiscount,
|
|
105
|
-
const adapter = await getAdapter(orderDiscount,
|
|
99
|
+
const reserveDiscount = async (orderDiscount: OrderDiscount, unchainedAPI: UnchainedCore) => {
|
|
100
|
+
const adapter = await getAdapter(orderDiscount, unchainedAPI);
|
|
106
101
|
if (!adapter) return null;
|
|
107
102
|
|
|
108
103
|
const reservation = await adapter.reserve({
|
|
109
104
|
code: orderDiscount.code,
|
|
110
105
|
});
|
|
111
106
|
|
|
112
|
-
return updateDiscount(
|
|
113
|
-
orderDiscount._id,
|
|
114
|
-
{ orderId: orderDiscount.orderId, reservation },
|
|
115
|
-
requestContext.userId,
|
|
116
|
-
);
|
|
107
|
+
return updateDiscount(orderDiscount._id, { orderId: orderDiscount.orderId, reservation });
|
|
117
108
|
};
|
|
118
109
|
|
|
119
110
|
const grabDiscount = async (
|
|
120
111
|
{ code, orderId }: { code: string; orderId: string },
|
|
121
|
-
|
|
112
|
+
unchainedAPI: UnchainedCore,
|
|
122
113
|
) => {
|
|
123
114
|
log(`OrderDiscounts -> Try to grab ${code}`, { orderId });
|
|
124
115
|
|
|
@@ -131,12 +122,12 @@ export const configureOrderDiscountsModule = ({
|
|
|
131
122
|
|
|
132
123
|
const discountId = discount._id;
|
|
133
124
|
try {
|
|
134
|
-
const updatedDiscount = await updateDiscount(discountId, { orderId }
|
|
135
|
-
const reservedDiscount = await reserveDiscount(updatedDiscount,
|
|
125
|
+
const updatedDiscount = await updateDiscount(discountId, { orderId });
|
|
126
|
+
const reservedDiscount = await reserveDiscount(updatedDiscount, unchainedAPI);
|
|
136
127
|
return reservedDiscount;
|
|
137
128
|
} catch (error) {
|
|
138
129
|
// Rollback
|
|
139
|
-
await updateDiscount(discountId, { orderId: discount.orderId }
|
|
130
|
+
await updateDiscount(discountId, { orderId: discount.orderId });
|
|
140
131
|
|
|
141
132
|
throw error;
|
|
142
133
|
}
|
|
@@ -153,13 +144,13 @@ export const configureOrderDiscountsModule = ({
|
|
|
153
144
|
},
|
|
154
145
|
|
|
155
146
|
// Transformations
|
|
156
|
-
interface: async (orderDiscount,
|
|
157
|
-
const adapter = await getAdapter(orderDiscount,
|
|
147
|
+
interface: async (orderDiscount, unchainedAPI) => {
|
|
148
|
+
const adapter = await getAdapter(orderDiscount, unchainedAPI);
|
|
158
149
|
return adapter;
|
|
159
150
|
},
|
|
160
151
|
|
|
161
|
-
isValid: async (orderDiscount,
|
|
162
|
-
const adapter = await getAdapter(orderDiscount,
|
|
152
|
+
isValid: async (orderDiscount, unchainedAPI) => {
|
|
153
|
+
const adapter = await getAdapter(orderDiscount, unchainedAPI);
|
|
163
154
|
if (!adapter) return null;
|
|
164
155
|
|
|
165
156
|
if (orderDiscount.trigger === OrderDiscountTrigger.SYSTEM) {
|
|
@@ -176,9 +167,9 @@ export const configureOrderDiscountsModule = ({
|
|
|
176
167
|
orderDiscount,
|
|
177
168
|
adapterKey,
|
|
178
169
|
calculationSheet,
|
|
179
|
-
|
|
170
|
+
unchainedAPI,
|
|
180
171
|
) => {
|
|
181
|
-
const adapter = await getAdapter(orderDiscount,
|
|
172
|
+
const adapter = await getAdapter(orderDiscount, unchainedAPI);
|
|
182
173
|
if (!adapter) return null;
|
|
183
174
|
|
|
184
175
|
return adapter.discountForPricingAdapterKey({
|
|
@@ -188,35 +179,32 @@ export const configureOrderDiscountsModule = ({
|
|
|
188
179
|
},
|
|
189
180
|
|
|
190
181
|
// Mutations
|
|
191
|
-
createManualOrderDiscount: async ({ order, code },
|
|
182
|
+
createManualOrderDiscount: async ({ order, code }, unchainedAPI) => {
|
|
192
183
|
// Try to grab single-usage-discount
|
|
193
184
|
if (!code) throw new Error(OrderDiscountErrorCode.CODE_NOT_VALID);
|
|
194
185
|
|
|
195
|
-
const fetchedDiscount = await grabDiscount({ code, orderId: order._id },
|
|
186
|
+
const fetchedDiscount = await grabDiscount({ code, orderId: order._id }, unchainedAPI);
|
|
196
187
|
if (fetchedDiscount) return fetchedDiscount;
|
|
197
188
|
|
|
198
|
-
const director = await OrderDiscountDirector.actions({ order, code },
|
|
189
|
+
const director = await OrderDiscountDirector.actions({ order, code }, unchainedAPI);
|
|
199
190
|
const discountKey = await director.resolveDiscountKeyFromStaticCode({
|
|
200
191
|
code,
|
|
201
192
|
});
|
|
202
193
|
|
|
203
194
|
if (discountKey) {
|
|
204
|
-
const newDiscount = await createDiscount(
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
},
|
|
210
|
-
requestContext.userId,
|
|
211
|
-
);
|
|
195
|
+
const newDiscount = await createDiscount({
|
|
196
|
+
orderId: order._id,
|
|
197
|
+
code,
|
|
198
|
+
discountKey,
|
|
199
|
+
});
|
|
212
200
|
|
|
213
201
|
try {
|
|
214
|
-
const reservedDiscount = await reserveDiscount(newDiscount,
|
|
215
|
-
await updateCalculation(order._id,
|
|
202
|
+
const reservedDiscount = await reserveDiscount(newDiscount, unchainedAPI);
|
|
203
|
+
await updateCalculation(order._id, unchainedAPI);
|
|
216
204
|
await emit('ORDER_ADD_DISCOUNT', { discount: reserveDiscount });
|
|
217
205
|
return reservedDiscount;
|
|
218
206
|
} catch (error) {
|
|
219
|
-
await deleteDiscount(newDiscount._id,
|
|
207
|
+
await deleteDiscount(newDiscount._id, unchainedAPI);
|
|
220
208
|
throw error;
|
|
221
209
|
}
|
|
222
210
|
}
|
|
@@ -224,8 +212,8 @@ export const configureOrderDiscountsModule = ({
|
|
|
224
212
|
throw new Error(OrderDiscountErrorCode.CODE_NOT_VALID);
|
|
225
213
|
},
|
|
226
214
|
|
|
227
|
-
create: async (doc
|
|
228
|
-
const discount = await createDiscount(doc
|
|
215
|
+
create: async (doc) => {
|
|
216
|
+
const discount = await createDiscount(doc);
|
|
229
217
|
|
|
230
218
|
if (discount.trigger === OrderDiscountTrigger.USER) {
|
|
231
219
|
await emit('ORDER_CREATE_DISCOUNT', { discount });
|
|
@@ -236,8 +224,8 @@ export const configureOrderDiscountsModule = ({
|
|
|
236
224
|
|
|
237
225
|
delete: deleteDiscount,
|
|
238
226
|
|
|
239
|
-
update: async (orderDiscountId, doc
|
|
240
|
-
await mutations.update(orderDiscountId, doc
|
|
227
|
+
update: async (orderDiscountId, doc) => {
|
|
228
|
+
await mutations.update(orderDiscountId, doc);
|
|
241
229
|
|
|
242
230
|
const selector = buildFindByIdSelector(orderDiscountId);
|
|
243
231
|
const discount = await OrderDiscounts.findOne(selector, {});
|
|
@@ -14,10 +14,10 @@ import { OrderPaymentsSchema } from '../db/OrderPaymentsSchema';
|
|
|
14
14
|
|
|
15
15
|
const ORDER_PAYMENT_EVENTS: string[] = ['ORDER_UPDATE_PAYMENT', 'ORDER_SIGN_PAYMENT', 'ORDER_PAY'];
|
|
16
16
|
|
|
17
|
-
const buildFindByIdSelector = (orderPaymentId: string) =>
|
|
17
|
+
export const buildFindByIdSelector = (orderPaymentId: string) =>
|
|
18
18
|
generateDbFilterById(orderPaymentId) as Filter<OrderPayment>;
|
|
19
19
|
|
|
20
|
-
const buildFindByContextDataSelector = (context: any): Query => {
|
|
20
|
+
export const buildFindByContextDataSelector = (context: any): Query => {
|
|
21
21
|
const contextKeys = Object.keys(context);
|
|
22
22
|
|
|
23
23
|
if (contextKeys.length === 0) return null;
|
|
@@ -55,27 +55,28 @@ export const configureOrderPaymentsModule = ({
|
|
|
55
55
|
: (orderPayment.status as OrderPaymentStatus);
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
const buildPaymentProviderActionsContext = (
|
|
58
|
+
const buildPaymentProviderActionsContext = (
|
|
59
|
+
orderPayment: OrderPayment,
|
|
60
|
+
{ transactionContext, ...rest }: { transactionContext: any; userId: string },
|
|
61
|
+
) => ({
|
|
62
|
+
...rest,
|
|
63
|
+
orderPayment,
|
|
59
64
|
paymentProviderId: orderPayment.paymentProviderId,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
...(transactionContext || {}),
|
|
64
|
-
...(orderPayment.context || {}),
|
|
65
|
-
},
|
|
65
|
+
transactionContext: {
|
|
66
|
+
...(transactionContext || {}),
|
|
67
|
+
...(orderPayment.context || {}),
|
|
66
68
|
},
|
|
67
69
|
});
|
|
68
70
|
|
|
69
71
|
const updateStatus: OrderPaymentsModule['updateStatus'] = async (
|
|
70
72
|
orderPaymentId,
|
|
71
73
|
{ status, transactionId, info },
|
|
72
|
-
userId,
|
|
73
74
|
) => {
|
|
74
75
|
log(`OrderPayment ${orderPaymentId} -> New Status: ${status}`);
|
|
75
76
|
|
|
76
77
|
const date = new Date();
|
|
77
78
|
const modifier: Update<OrderPayment> = {
|
|
78
|
-
$set: { status, updated: new Date()
|
|
79
|
+
$set: { status, updated: new Date() },
|
|
79
80
|
$push: {
|
|
80
81
|
log: {
|
|
81
82
|
date,
|
|
@@ -126,16 +127,16 @@ export const configureOrderPaymentsModule = ({
|
|
|
126
127
|
}));
|
|
127
128
|
},
|
|
128
129
|
|
|
129
|
-
isBlockingOrderConfirmation: async (orderPayment,
|
|
130
|
+
isBlockingOrderConfirmation: async (orderPayment, unchainedAPI) => {
|
|
130
131
|
if (orderPayment.status === OrderPaymentStatus.PAID) return false;
|
|
131
132
|
|
|
132
|
-
const provider = await
|
|
133
|
+
const provider = await unchainedAPI.modules.payment.paymentProviders.findProvider({
|
|
133
134
|
paymentProviderId: orderPayment.paymentProviderId,
|
|
134
135
|
});
|
|
135
136
|
|
|
136
|
-
const isPayLaterAllowed = await
|
|
137
|
+
const isPayLaterAllowed = await unchainedAPI.modules.payment.paymentProviders.isPayLaterAllowed(
|
|
137
138
|
provider,
|
|
138
|
-
|
|
139
|
+
unchainedAPI,
|
|
139
140
|
);
|
|
140
141
|
|
|
141
142
|
return !isPayLaterAllowed;
|
|
@@ -156,92 +157,114 @@ export const configureOrderPaymentsModule = ({
|
|
|
156
157
|
|
|
157
158
|
// Mutations
|
|
158
159
|
|
|
159
|
-
create: async (doc
|
|
160
|
-
const orderPaymentId = await mutations.create(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
create: async (doc) => {
|
|
161
|
+
const orderPaymentId = await mutations.create({
|
|
162
|
+
...doc,
|
|
163
|
+
status: null,
|
|
164
|
+
context: doc.context || {},
|
|
165
|
+
});
|
|
164
166
|
|
|
165
167
|
const orderPayment = await OrderPayments.findOne(buildFindByIdSelector(orderPaymentId));
|
|
166
168
|
|
|
167
169
|
return orderPayment;
|
|
168
170
|
},
|
|
169
171
|
|
|
170
|
-
confirm: async (orderPayment,
|
|
171
|
-
const {
|
|
172
|
+
confirm: async (orderPayment, paymentContext, unchainedAPI) => {
|
|
173
|
+
const { modules } = unchainedAPI;
|
|
172
174
|
|
|
173
175
|
if (normalizedStatus(orderPayment) !== OrderPaymentStatus.PAID) {
|
|
174
176
|
return orderPayment;
|
|
175
177
|
}
|
|
176
178
|
|
|
177
|
-
const arbitraryResponseData = await
|
|
178
|
-
|
|
179
|
-
|
|
179
|
+
const arbitraryResponseData = await modules.payment.paymentProviders.confirm(
|
|
180
|
+
orderPayment.paymentProviderId,
|
|
181
|
+
buildPaymentProviderActionsContext(orderPayment, paymentContext),
|
|
182
|
+
unchainedAPI,
|
|
180
183
|
);
|
|
181
184
|
|
|
182
185
|
if (arbitraryResponseData) {
|
|
183
|
-
return updateStatus(
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
info: JSON.stringify(arbitraryResponseData),
|
|
188
|
-
},
|
|
189
|
-
requestContext.userId,
|
|
190
|
-
);
|
|
186
|
+
return updateStatus(orderPayment._id, {
|
|
187
|
+
status: OrderPaymentStatus.PAID,
|
|
188
|
+
info: JSON.stringify(arbitraryResponseData),
|
|
189
|
+
});
|
|
191
190
|
}
|
|
192
191
|
|
|
193
192
|
return orderPayment;
|
|
194
193
|
},
|
|
195
194
|
|
|
196
|
-
cancel: async (orderPayment,
|
|
197
|
-
const {
|
|
195
|
+
cancel: async (orderPayment, paymentContext, unchainedAPI) => {
|
|
196
|
+
const { modules } = unchainedAPI;
|
|
198
197
|
|
|
199
198
|
if (normalizedStatus(orderPayment) !== OrderPaymentStatus.PAID) {
|
|
200
199
|
return orderPayment;
|
|
201
200
|
}
|
|
202
201
|
|
|
203
|
-
const arbitraryResponseData = await
|
|
204
|
-
|
|
205
|
-
|
|
202
|
+
const arbitraryResponseData = await modules.payment.paymentProviders.cancel(
|
|
203
|
+
orderPayment.paymentProviderId,
|
|
204
|
+
buildPaymentProviderActionsContext(orderPayment, paymentContext),
|
|
205
|
+
unchainedAPI,
|
|
206
206
|
);
|
|
207
207
|
|
|
208
208
|
if (arbitraryResponseData) {
|
|
209
|
-
return updateStatus(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
info: JSON.stringify(arbitraryResponseData),
|
|
214
|
-
},
|
|
215
|
-
requestContext.userId,
|
|
216
|
-
);
|
|
209
|
+
return updateStatus(orderPayment._id, {
|
|
210
|
+
status: OrderPaymentStatus.REFUNDED,
|
|
211
|
+
info: JSON.stringify(arbitraryResponseData),
|
|
212
|
+
});
|
|
217
213
|
}
|
|
218
214
|
|
|
219
215
|
return orderPayment;
|
|
220
216
|
},
|
|
221
217
|
|
|
222
|
-
charge: async (orderPayment,
|
|
223
|
-
const {
|
|
218
|
+
charge: async (orderPayment, context, unchainedAPI) => {
|
|
219
|
+
const { modules } = unchainedAPI;
|
|
224
220
|
|
|
225
221
|
if (normalizedStatus(orderPayment) !== OrderPaymentStatus.OPEN) {
|
|
226
222
|
return orderPayment;
|
|
227
223
|
}
|
|
228
224
|
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
225
|
+
const paymentCredentials =
|
|
226
|
+
context.transactionContext?.paymentCredentials ||
|
|
227
|
+
(await modules.payment.paymentCredentials.findPaymentCredential({
|
|
228
|
+
userId: context.userId,
|
|
229
|
+
paymentProviderId: orderPayment.paymentProviderId,
|
|
230
|
+
isPreferred: true,
|
|
231
|
+
}));
|
|
232
|
+
|
|
233
|
+
const paymentContext = buildPaymentProviderActionsContext(orderPayment, {
|
|
234
|
+
...context,
|
|
235
|
+
transactionContext: {
|
|
236
|
+
...context.transactionContext,
|
|
237
|
+
paymentCredentials,
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const result = await modules.payment.paymentProviders.charge(
|
|
242
|
+
orderPayment.paymentProviderId,
|
|
243
|
+
paymentContext,
|
|
244
|
+
unchainedAPI,
|
|
232
245
|
);
|
|
233
246
|
|
|
247
|
+
if (!result) return orderPayment;
|
|
248
|
+
|
|
249
|
+
const { credentials, ...arbitraryResponseData } = result;
|
|
250
|
+
|
|
251
|
+
if (credentials) {
|
|
252
|
+
const { token, ...meta } = credentials;
|
|
253
|
+
await modules.payment.paymentCredentials.upsertCredentials({
|
|
254
|
+
userId: paymentContext.userId,
|
|
255
|
+
paymentProviderId: orderPayment.paymentProviderId,
|
|
256
|
+
token,
|
|
257
|
+
...meta,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
234
261
|
if (arbitraryResponseData) {
|
|
235
262
|
const { transactionId, ...info } = arbitraryResponseData;
|
|
236
|
-
return updateStatus(
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
info: JSON.stringify(info),
|
|
242
|
-
},
|
|
243
|
-
requestContext.userId,
|
|
244
|
-
);
|
|
263
|
+
return updateStatus(orderPayment._id, {
|
|
264
|
+
transactionId,
|
|
265
|
+
status: OrderPaymentStatus.PAID,
|
|
266
|
+
info: JSON.stringify(info),
|
|
267
|
+
});
|
|
245
268
|
}
|
|
246
269
|
|
|
247
270
|
return orderPayment;
|
|
@@ -263,21 +286,17 @@ export const configureOrderPaymentsModule = ({
|
|
|
263
286
|
return true;
|
|
264
287
|
},
|
|
265
288
|
|
|
266
|
-
markAsPaid: async (orderPayment, meta
|
|
289
|
+
markAsPaid: async (orderPayment, meta) => {
|
|
267
290
|
if (normalizedStatus(orderPayment) !== OrderPaymentStatus.OPEN) return;
|
|
268
291
|
|
|
269
|
-
await updateStatus(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
info: meta ? JSON.stringify(meta) : 'mark paid manually',
|
|
274
|
-
},
|
|
275
|
-
userId,
|
|
276
|
-
);
|
|
292
|
+
await updateStatus(orderPayment._id, {
|
|
293
|
+
status: OrderPaymentStatus.PAID,
|
|
294
|
+
info: meta ? JSON.stringify(meta) : 'mark paid manually',
|
|
295
|
+
});
|
|
277
296
|
await emit('ORDER_PAY', { orderPayment });
|
|
278
297
|
},
|
|
279
298
|
|
|
280
|
-
updateContext: async (orderPaymentId, context,
|
|
299
|
+
updateContext: async (orderPaymentId, context, unchainedAPI) => {
|
|
281
300
|
if (!context) return false;
|
|
282
301
|
|
|
283
302
|
const selector = buildFindByIdSelector(orderPaymentId);
|
|
@@ -292,12 +311,11 @@ export const configureOrderPaymentsModule = ({
|
|
|
292
311
|
$set: {
|
|
293
312
|
context: { ...(orderPayment.context || {}), ...context },
|
|
294
313
|
updated: new Date(),
|
|
295
|
-
updatedBy: requestContext.userId,
|
|
296
314
|
},
|
|
297
315
|
});
|
|
298
316
|
|
|
299
317
|
if (result.modifiedCount) {
|
|
300
|
-
await updateCalculation(orderId,
|
|
318
|
+
await updateCalculation(orderId, unchainedAPI);
|
|
301
319
|
await emit('ORDER_UPDATE_PAYMENT', {
|
|
302
320
|
orderPayment: {
|
|
303
321
|
...orderPayment,
|
|
@@ -312,16 +330,16 @@ export const configureOrderPaymentsModule = ({
|
|
|
312
330
|
|
|
313
331
|
updateStatus,
|
|
314
332
|
|
|
315
|
-
updateCalculation: async (orderPayment,
|
|
333
|
+
updateCalculation: async (orderPayment, unchainedAPI) => {
|
|
316
334
|
log(`OrderPayment ${orderPayment._id} -> Update Calculation`, {
|
|
317
335
|
orderId: orderPayment.orderId,
|
|
318
336
|
});
|
|
319
337
|
|
|
320
|
-
const calculation = await
|
|
338
|
+
const calculation = await unchainedAPI.modules.payment.paymentProviders.calculate(
|
|
321
339
|
{
|
|
322
340
|
item: orderPayment,
|
|
323
341
|
},
|
|
324
|
-
|
|
342
|
+
unchainedAPI,
|
|
325
343
|
);
|
|
326
344
|
|
|
327
345
|
const selector = buildFindByIdSelector(orderPayment._id);
|
|
@@ -329,7 +347,6 @@ export const configureOrderPaymentsModule = ({
|
|
|
329
347
|
$set: {
|
|
330
348
|
calculation,
|
|
331
349
|
updated: new Date(),
|
|
332
|
-
updatedBy: requestContext.userId,
|
|
333
350
|
},
|
|
334
351
|
});
|
|
335
352
|
|