UNPKG

indusbit-math

Version:

React Native package for Snapmint payment integration with EMI widget components and enhanced callback-based API

139 lines (118 loc) 4.58 kB
// Shared Snapmint utility functions for popup selection and amount calculations // Round up like Android/iOS implementations (ceil but only when there is a decimal part) export const roundUp = (num) => { const n = Number(num) || 0; const floored = Math.floor(n); return floored + (n - floored > 0 ? 1 : 0); }; export const getNumberSuffix = (number) => { const num = parseInt(number, 10); if (num >= 11 && num <= 13) return 'th'; const last = num % 10; if (last === 1) return 'st'; if (last === 2) return 'nd'; if (last === 3) return 'rd'; return 'th'; }; export const findPopUpItem = (orderValue, popUpList) => { if (!Array.isArray(popUpList) || popUpList.length === 0) return null; const value = Number(orderValue) || 0; return ( popUpList.find((item) => { const min = Number(item.min); const max = Number(item.max); if (max === -1) return value > min; return value >= min && value <= max; }) || null ); }; export const determineEmiPopup = (orderValue, jsonData) => { try { if (!jsonData) return ''; const value = Number(orderValue) || 0; const fromList = findPopUpItem(value, jsonData.pop_up_list); if (fromList && fromList.popup) return fromList.popup; // Fallback to legacy fields if (value < 2000) return jsonData.emi_pop_up_1; if (value < 4000) return jsonData.emi_pop_up_2; if (value < 6000) return jsonData.emi_pop_up_3; if (value < 10000) return jsonData.emi_pop_up_4; return jsonData.emi_pop_up_5; } catch (e) { return jsonData?.emi_pop_up_1 || ''; } }; export const calculateAmounts = (totalOrder, model) => { const orderValue = Number(totalOrder) || 0; // Defaults when no model available if (!model) { const amountPay = roundUp(orderValue * 0.33); const firstEmi = roundUp(orderValue * 0.33); const secondEmi = roundUp(orderValue * 0.34); const thirdEmi = 0; return { downPaymentPrice: amountPay, firstEmiPrice: firstEmi, secondEmiPrice: secondEmi, thirdEmiPrice: thirdEmi, }; } // Prefer new pop_up_list configuration const item = findPopUpItem(orderValue, model.pop_up_list); if (item) { const payNowPct = Number(item.pay_now_percentage) || 0; const emiPct = Number(item.emi_percentage) || 0; let amountPay = (orderValue * payNowPct) / 100; let firstEmi = (orderValue * emiPct) / 100; let secondEmi = (orderValue * emiPct) / 100; let thirdEmi = (orderValue * emiPct) / 100; amountPay = roundUp(amountPay); firstEmi = roundUp(firstEmi); secondEmi = roundUp(secondEmi); thirdEmi = roundUp(thirdEmi); return { downPaymentPrice: amountPay, firstEmiPrice: firstEmi, secondEmiPrice: secondEmi, thirdEmiPrice: thirdEmi, }; } // Legacy logic fallback const payNowPercentage = orderValue > 2000 ? (model.pay_now_percentage_3_tenure || '25') : (model.pay_now_percentage || '33.34'); const emiOnePercentage = orderValue > 2000 ? (model.emi_one_percentage_3_tenure || '25') : (model.emi_one_percentage || '33.33'); const emiSecondPercentage = orderValue > 2000 ? (model.emi_second_percentage_3_tenure || '25') : (model.emi_second_percentage || '33.33'); const emiThirdPercentage = model.emi_third_percentage_3_tenure || '25'; let amountPay = (orderValue * parseFloat(payNowPercentage)) / 100; let firstEmi = (orderValue * parseFloat(emiOnePercentage)) / 100; let secondEmi = (orderValue * parseFloat(emiSecondPercentage)) / 100; let thirdEmi = orderValue > 2000 ? (orderValue * parseFloat(emiThirdPercentage)) / 100 : 0; amountPay = roundUp(amountPay); firstEmi = roundUp(firstEmi); secondEmi = roundUp(secondEmi); thirdEmi = roundUp(thirdEmi); return { downPaymentPrice: amountPay, firstEmiPrice: firstEmi, secondEmiPrice: secondEmi, thirdEmiPrice: thirdEmi, }; }; export const calculateTenureAmounts = (totalOrder, model) => { if (!model || !Array.isArray(model.tenure_list)) return {}; const orderValue = Number(totalOrder) || 0; const { downPaymentPrice } = calculateAmounts(orderValue, model); const tenureAmounts = {}; model.tenure_list.forEach((tenure) => { const t = Number(tenure.tenure) || 0; const roi = Number(tenure.roi) || 0; let tenureValue; if (roi > 0) { tenureValue = (orderValue * roi) / 100; } else { tenureValue = t > 0 ? (orderValue - downPaymentPrice) / t : 0; } const rounded = roundUp(tenureValue); tenureAmounts[`tenure_${t}`] = Math.round(rounded); }); return tenureAmounts; };