From 3e3374af31ce7ab6d4a2e044eb3d3ee918d5326c Mon Sep 17 00:00:00 2001 From: Wisley Alves Date: Wed, 27 Sep 2023 18:12:55 -0300 Subject: [PATCH] chore(galaxpay): define new shipping method --- .../galaxpay/update-subscription.ts | 55 +++++++--- .../src/functions-lib/galaxpay/webhook.ts | 103 +++++++++++++----- 2 files changed, 117 insertions(+), 41 deletions(-) diff --git a/packages/apps/galaxpay/src/functions-lib/galaxpay/update-subscription.ts b/packages/apps/galaxpay/src/functions-lib/galaxpay/update-subscription.ts index d9e852e3a..bbf9f1e25 100644 --- a/packages/apps/galaxpay/src/functions-lib/galaxpay/update-subscription.ts +++ b/packages/apps/galaxpay/src/functions-lib/galaxpay/update-subscription.ts @@ -86,7 +86,10 @@ const getNewFreight = async ( (serviceFind) => serviceFind.service_code === shippingLineOriginal.app.service_code, ); - return service || sameApp.response?.shipping_services[0]; + return { + app: sameApp, + service: service || sameApp.response?.shipping_services[0], + }; } let minPrice = result[0]?.response?.shipping_services[0]?.shipping_line?.total_price; @@ -99,7 +102,7 @@ const getNewFreight = async ( const service = app.response?.shipping_services[j]; if (service.service_code === shippingLineOriginal.app.service_code) { - return service; + return { app, service }; } if (minPrice > service?.shipping_line?.total_price) { @@ -110,7 +113,10 @@ const getNewFreight = async ( } } - return result[indexPosition.app]?.response?.shipping_services[indexPosition.service]; + return { + app: result[indexPosition.app], + service: result[indexPosition.app]?.response?.shipping_services[indexPosition.service], + }; } catch (err) { logger.error(err); return null; @@ -151,15 +157,35 @@ const checkItemsAndRecalculeteOrder = async ( if (subtotal > 0) { if (shippingLine) { - const service = await getNewFreight(items, shippingLine?.to, subtotal, shippingLine); + const newFreight = await getNewFreight(items, shippingLine?.to, subtotal, shippingLine); + let service; + let app; - if (service && service?.shipping_line?.total_price) { - shippingLine = { ...shippingLine, ...service.shipping_line }; - if (shippingLine) { - delete shippingLine._id; - } + if (newFreight?.service) { + service = newFreight.service; + } + + if (newFreight?.app) { + app = newFreight.app; + } + + if (service && service?.service_code !== shippingLine?.app?.service_code) { + shippingLine = { + ...service.shipping_line, + app: { + _id: app?._id, + service_code: service?.service_code, + label: service?.label, + }, + }; amount.freight = service.shipping_line.total_price; } + + if (shippingLine) { + delete shippingLine._id; + delete shippingLine.tracking_codes; + delete shippingLine.invoices; + } } amount.subtotal = subtotal; @@ -179,9 +205,12 @@ const checkItemsAndRecalculeteOrder = async ( amount.discount = planDiscount || amount.discount || 0; amount.total -= amount.discount || 0; - return amount.total > 0 ? Math.floor(parseFloat(amount.total.toFixed(2)) * 1000) / 10 : 0; + return { + value: amount.total > 0 ? Math.floor(parseFloat((amount.total).toFixed(2)) * 1000) / 10 : 0, + shippingLine, + }; } - return 0; + return { value: 0, shippingLine }; }; const getSubscriptionsByListMyIds = async ( @@ -229,7 +258,7 @@ const updateValueSubscriptionGalaxpay = async ( galaxpayAxios, subscriptionId: string, value: number, - oldValue: number, + oldValue?: number, ) => { if (!oldValue) { const { data } = await galaxpayAxios.axios.get(`subscriptions?myIds=${subscriptionId}&startAt=0&limit=1`); @@ -259,7 +288,7 @@ const checkAndUpdateSubscriptionGalaxpay = async ( if (shippingLine) { copyShippingLine = { ...shippingLine }; } - const value = await checkItemsAndRecalculeteOrder( + const { value } = await checkItemsAndRecalculeteOrder( { ...amount }, [...items], { ...plan }, diff --git a/packages/apps/galaxpay/src/functions-lib/galaxpay/webhook.ts b/packages/apps/galaxpay/src/functions-lib/galaxpay/webhook.ts index 8645fa15d..ef2109f79 100644 --- a/packages/apps/galaxpay/src/functions-lib/galaxpay/webhook.ts +++ b/packages/apps/galaxpay/src/functions-lib/galaxpay/webhook.ts @@ -13,12 +13,16 @@ import { findOrderById, getOrderIntermediatorTId, } from '../ecom/request-api'; -import { createItemsAndAmount } from '../firestore/utils'; +import { + createItemsAndAmount, + updateDocSubscription, +} from '../firestore/utils'; import GalaxpayAxios from './auth/create-access'; import { checkAndUpdateSubscriptionGalaxpay, checkItemsAndRecalculeteOrder, compareDocItemsWithOrder, + updateValueSubscriptionGalaxpay, } from './update-subscription'; type FinancialStatusCurrent = Exclude['current'] @@ -121,10 +125,16 @@ const createTransaction = async ( } // recalculate order const shippingLine = shippingLines && shippingLines[0]; - await checkItemsAndRecalculeteOrder(amount, items, plan, null, shippingLine); + const { shippingLine: newShippingLine } = await checkItemsAndRecalculeteOrder( + amount, + items, + plan, + null, + shippingLine, + ); - if (shippingLines?.length && shippingLine) { - shippingLines[0] = shippingLine; + if (shippingLines?.length && newShippingLine) { + shippingLines[0] = newShippingLine; } if (amount.balance) { @@ -243,6 +253,31 @@ const handleWehook = async (req: Request, res: Response) => { ); try { + const app = await getApp(); + + if (!process.env.GALAXPAY_ID) { + const galaxpayId = app.hidden_data?.galaxpay_id; + if (galaxpayId && typeof galaxpayId === 'string') { + process.env.GALAXPAY_ID = galaxpayId; + } else { + logger.warn('Missing GalaxPay ID'); + } + } + + if (!process.env.GALAXPAY_HASH) { + const galaxpayHash = app.hidden_data?.galaxpay_hash; + if (galaxpayHash && typeof galaxpayHash === 'string') { + process.env.GALAXPAY_HASH = galaxpayHash; + } else { + logger.warn('Missing GalaxPay Hash'); + } + } + + const galaxpayAxios = new GalaxpayAxios({ + galaxpayId: process.env.GALAXPAY_ID, + galaxpayHash: process.env.GALAXPAY_HASH, + }); + if (type === 'transaction.updateStatus') { const documentSnapshot = await collectionSubscription.doc(originalOrderId).get(); if (documentSnapshot && documentSnapshot.exists) { @@ -258,32 +293,9 @@ const handleWehook = async (req: Request, res: Response) => { let galaxPayTransactionStatus: string | undefined; let galaxpaySubscriptionStatus: string | undefined; let transactionCreatedAt: string | undefined; - const app = await getApp(); try { // check subscription and transaction status before in galaxpay - if (!process.env.GALAXPAY_ID) { - const galaxpayId = app.hidden_data?.galaxpay_id; - if (galaxpayId && typeof galaxpayId === 'string') { - process.env.GALAXPAY_ID = galaxpayId; - } else { - logger.warn('Missing GalaxPay ID'); - } - } - - if (!process.env.GALAXPAY_HASH) { - const galaxpayHash = app.hidden_data?.galaxpay_hash; - if (galaxpayHash && typeof galaxpayHash === 'string') { - process.env.GALAXPAY_HASH = galaxpayHash; - } else { - logger.warn('Missing GalaxPay Hash'); - } - } - - const galaxpayAxios = new GalaxpayAxios({ - galaxpayId: process.env.GALAXPAY_ID, - galaxpayHash: process.env.GALAXPAY_HASH, - }); await galaxpayAxios.preparing; if (galaxpayAxios.axios) { @@ -321,7 +333,7 @@ const handleWehook = async (req: Request, res: Response) => { [shippingLine] = order.shipping_lines; } - const newValue = await checkItemsAndRecalculeteOrder( + const { value: newValue } = await checkItemsAndRecalculeteOrder( order.amount, order.items, plan, @@ -542,6 +554,41 @@ const handleWehook = async (req: Request, res: Response) => { }, { merge: true }, ); + + try { + await galaxpayAxios.preparing; + + if (galaxpayAxios.axios) { + const { data } = await galaxpayAxios.axios + .get(`/transactions?galaxPayIds=${whGalaxPayTransaction.galaxPayId}&startAt=0&limit=1`); + + const value = itemsAndAmount?.amount?.total + && Math.floor(parseFloat((itemsAndAmount.amount.total).toFixed(2)) * 100); + const hasUpdateValue = value && data?.Transactions[0]?.value + && value !== data?.Transactions[0]?.value; + + if (hasUpdateValue) { + const resp = await updateValueSubscriptionGalaxpay( + galaxpayAxios, + originalOrderId, + value, + ); + + if (resp) { + logger.log('> Successful signature edit on Galax Pay'); + const body = { itemsAndAmount }; + if (value) { + Object.assign(body, { value }); + } + + await updateDocSubscription(collectionSubscription, body, originalOrderId); + } + } + } + } catch (error) { + logger.error(error); + } + return res.sendStatus(200); } // console.log('>> Transaction Original')