Skip to content

Commit e78dd6a

Browse files
committed
fix(modules): Saving transaction app.intermediator and discount from list payments on checkout
1 parent a18900e commit e78dd6a

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

packages/modules/src/firebase/checkout.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default async (req: Request, res: Response) => {
201201
);
202202
}
203203

204-
let listPaymentGateways: any;
204+
let listPaymentsResults: any[] | null | undefined;
205205
const listPayments = async () => {
206206
const { transaction, ...bodyPayment } = body;
207207
let paymentsBody: Payment;
@@ -216,25 +216,29 @@ export default async (req: Request, res: Response) => {
216216
transaction,
217217
};
218218
}
219-
listPaymentGateways = await requestModule(paymentsBody, modulesBaseURL, 'payment');
220-
msgErr = listPaymentGateways.msgErr;
221-
if (listPaymentGateways && !msgErr) {
222-
listPaymentGateways = getValidResults(listPaymentGateways, 'payment_gateways');
223-
handleListPayments(body, listPaymentGateways, paymentsBody, amount, orderBody);
224-
return true;
219+
const listPaymentsRes = await requestModule(paymentsBody, modulesBaseURL, 'payment');
220+
msgErr = listPaymentsRes.msgErr;
221+
if (listPaymentsResults && !msgErr) {
222+
listPaymentsResults = getValidResults(listPaymentsResults, 'payment_gateways');
223+
return handleListPayments(body, listPaymentsResults, paymentsBody, amount, orderBody);
225224
}
226-
listPaymentGateways = null;
227-
return false;
225+
listPaymentsResults = null;
226+
return {};
228227
};
229228

229+
// payment payment method discounts
230230
await listPayments();
231231
let discounts = await requestModule(body, modulesBaseURL, 'discount');
232232
if (discounts) {
233233
discounts = getValidResults(discounts);
234234
handleApplyDiscount(body, discounts, amount, orderBody);
235235
}
236+
// reset payment preview discount if any
237+
amount.discount = 0;
238+
fixAmount(amount, body, orderBody);
236239

237-
if (!(await listPayments()) && !listPaymentGateways) {
240+
const { paymentGateway, paymentDiscountValue } = await listPayments();
241+
if (!paymentGateway && !listPaymentsResults) {
238242
return sendError(
239243
res,
240244
msgErr?.status || 409,
@@ -256,5 +260,7 @@ export default async (req: Request, res: Response) => {
256260
orderBody,
257261
transactions,
258262
dateTime,
263+
paymentGateway,
264+
paymentDiscountValue,
259265
);
260266
};

packages/modules/src/firebase/functions-checkout/checkout-utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const getValidResults = (
9999

100100
const handleListPayments = (
101101
body: CheckoutBodyWithItems,
102-
listPayment: { [key: string]: any },
102+
listPayment: any[],
103103
paymentsBody: Payment,
104104
amount: Amount,
105105
orderBody: OrderSet,
@@ -143,27 +143,32 @@ const handleListPayments = (
143143
const { discount } = paymentGateway;
144144
// handle discount by payment method
145145
const applyDiscountIn = discount && discount.apply_at;
146+
let paymentDiscountValue = 0;
146147
if (applyDiscountIn && discount.value && amount[applyDiscountIn]) {
147148
const maxDiscount: number = amount[applyDiscountIn] || 0;
148149
// update amount discount and total
149-
let discountValue: number;
150150
if (discount.type === 'percentage') {
151-
discountValue = (maxDiscount * discount.value) / 100;
151+
paymentDiscountValue = (maxDiscount * discount.value) / 100;
152152
} else {
153-
discountValue = discount.value;
154-
if (discountValue > maxDiscount) {
155-
discountValue = maxDiscount;
153+
paymentDiscountValue = discount.value;
154+
if (paymentDiscountValue > maxDiscount) {
155+
paymentDiscountValue = maxDiscount;
156156
}
157157
}
158158
amount.discount = amount.discount || 0;
159-
amount.discount += discountValue;
159+
amount.discount += paymentDiscountValue;
160160
fixAmount(amount, body, orderBody);
161161
}
162162
// add to order body
163163
orderBody.payment_method_label = paymentGateway.label || '';
164+
return {
165+
paymentGateway,
166+
paymentDiscountValue,
167+
};
164168
}
165169
}
166170
}
171+
return {};
167172
};
168173

169174
const handleShippingServices = (
@@ -247,9 +252,6 @@ const handleApplyDiscount = (
247252
amount: Amount,
248253
orderBody: OrderSet,
249254
) => {
250-
// reset payment preview discount if any
251-
amount.discount = 0;
252-
fixAmount(amount, body, orderBody);
253255
for (let i = 0; i < listDiscount.length; i++) {
254256
const result = listDiscount[i];
255257
// treat apply discount response

packages/modules/src/firebase/functions-checkout/new-order.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
CheckoutTransaction,
77
TransactionOrder,
88
PaymentHistory,
9+
PaymentGateways,
910
} from '../../types/index';
1011
import { logger } from '@cloudcommerce/firebase/lib/config';
1112
import api from '@cloudcommerce/api';
@@ -32,6 +33,8 @@ const createOrder = async (
3233
orderBody: OrderSet,
3334
transactions: CheckoutTransaction[],
3435
dateTime: string,
36+
paymentGateway?: PaymentGateways[number],
37+
paymentDiscountValue?: number,
3538
) => {
3639
const { order, err } = await newOrder(orderBody);
3740
if (order) {
@@ -123,6 +126,16 @@ const createOrder = async (
123126
}
124127
// logger.log(transaction.app)
125128
}
129+
if (isFirstTransaction && paymentGateway) {
130+
if (!transaction.app.intermediator) {
131+
transaction.app.intermediator = paymentGateway.intermediator;
132+
} else if (paymentGateway.intermediator?.code) {
133+
transaction.app.intermediator.code = paymentGateway.intermediator.code;
134+
}
135+
if (paymentDiscountValue) {
136+
transaction.discount = paymentDiscountValue;
137+
}
138+
}
126139

127140
// check for transaction status
128141
if (!transaction.status) {

0 commit comments

Comments
 (0)