Skip to content

Commit

Permalink
fix(modules): Saving transaction app.intermediator and discount f…
Browse files Browse the repository at this point in the history
…rom list payments on checkout
  • Loading branch information
leomp12 committed Jan 14, 2025
1 parent a18900e commit e78dd6a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
26 changes: 16 additions & 10 deletions packages/modules/src/firebase/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default async (req: Request, res: Response) => {
);
}

let listPaymentGateways: any;
let listPaymentsResults: any[] | null | undefined;
const listPayments = async () => {
const { transaction, ...bodyPayment } = body;
let paymentsBody: Payment;
Expand All @@ -216,25 +216,29 @@ export default async (req: Request, res: Response) => {
transaction,
};
}
listPaymentGateways = await requestModule(paymentsBody, modulesBaseURL, 'payment');
msgErr = listPaymentGateways.msgErr;
if (listPaymentGateways && !msgErr) {
listPaymentGateways = getValidResults(listPaymentGateways, 'payment_gateways');
handleListPayments(body, listPaymentGateways, paymentsBody, amount, orderBody);
return true;
const listPaymentsRes = await requestModule(paymentsBody, modulesBaseURL, 'payment');
msgErr = listPaymentsRes.msgErr;
if (listPaymentsResults && !msgErr) {
listPaymentsResults = getValidResults(listPaymentsResults, 'payment_gateways');
return handleListPayments(body, listPaymentsResults, paymentsBody, amount, orderBody);
}
listPaymentGateways = null;
return false;
listPaymentsResults = null;
return {};
};

// payment payment method discounts
await listPayments();
let discounts = await requestModule(body, modulesBaseURL, 'discount');
if (discounts) {
discounts = getValidResults(discounts);
handleApplyDiscount(body, discounts, amount, orderBody);
}
// reset payment preview discount if any
amount.discount = 0;
fixAmount(amount, body, orderBody);

if (!(await listPayments()) && !listPaymentGateways) {
const { paymentGateway, paymentDiscountValue } = await listPayments();
if (!paymentGateway && !listPaymentsResults) {
return sendError(
res,
msgErr?.status || 409,
Expand All @@ -256,5 +260,7 @@ export default async (req: Request, res: Response) => {
orderBody,
transactions,
dateTime,
paymentGateway,
paymentDiscountValue,
);
};
22 changes: 12 additions & 10 deletions packages/modules/src/firebase/functions-checkout/checkout-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const getValidResults = (

const handleListPayments = (
body: CheckoutBodyWithItems,
listPayment: { [key: string]: any },
listPayment: any[],
paymentsBody: Payment,
amount: Amount,
orderBody: OrderSet,
Expand Down Expand Up @@ -143,27 +143,32 @@ const handleListPayments = (
const { discount } = paymentGateway;
// handle discount by payment method
const applyDiscountIn = discount && discount.apply_at;
let paymentDiscountValue = 0;
if (applyDiscountIn && discount.value && amount[applyDiscountIn]) {
const maxDiscount: number = amount[applyDiscountIn] || 0;
// update amount discount and total
let discountValue: number;
if (discount.type === 'percentage') {
discountValue = (maxDiscount * discount.value) / 100;
paymentDiscountValue = (maxDiscount * discount.value) / 100;
} else {
discountValue = discount.value;
if (discountValue > maxDiscount) {
discountValue = maxDiscount;
paymentDiscountValue = discount.value;
if (paymentDiscountValue > maxDiscount) {
paymentDiscountValue = maxDiscount;
}
}
amount.discount = amount.discount || 0;
amount.discount += discountValue;
amount.discount += paymentDiscountValue;
fixAmount(amount, body, orderBody);
}
// add to order body
orderBody.payment_method_label = paymentGateway.label || '';
return {
paymentGateway,
paymentDiscountValue,
};
}
}
}
return {};
};

const handleShippingServices = (
Expand Down Expand Up @@ -247,9 +252,6 @@ const handleApplyDiscount = (
amount: Amount,
orderBody: OrderSet,
) => {
// reset payment preview discount if any
amount.discount = 0;
fixAmount(amount, body, orderBody);
for (let i = 0; i < listDiscount.length; i++) {
const result = listDiscount[i];
// treat apply discount response
Expand Down
13 changes: 13 additions & 0 deletions packages/modules/src/firebase/functions-checkout/new-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
CheckoutTransaction,
TransactionOrder,
PaymentHistory,
PaymentGateways,
} from '../../types/index';
import { logger } from '@cloudcommerce/firebase/lib/config';
import api from '@cloudcommerce/api';
Expand All @@ -32,6 +33,8 @@ const createOrder = async (
orderBody: OrderSet,
transactions: CheckoutTransaction[],
dateTime: string,
paymentGateway?: PaymentGateways[number],
paymentDiscountValue?: number,
) => {
const { order, err } = await newOrder(orderBody);
if (order) {
Expand Down Expand Up @@ -123,6 +126,16 @@ const createOrder = async (
}
// logger.log(transaction.app)
}
if (isFirstTransaction && paymentGateway) {
if (!transaction.app.intermediator) {
transaction.app.intermediator = paymentGateway.intermediator;
} else if (paymentGateway.intermediator?.code) {
transaction.app.intermediator.code = paymentGateway.intermediator.code;
}
if (paymentDiscountValue) {
transaction.discount = paymentDiscountValue;
}
}

// check for transaction status
if (!transaction.status) {
Expand Down

0 comments on commit e78dd6a

Please sign in to comment.