UNPKG

gapp-checkout

Version:

Mobile Gapp flow for Checkout

193 lines (191 loc) 9.36 kB
import React from 'react'; import { httpRequest } from '../shared/httpRequest'; import useCheckout from './useCheckout'; function useViewModel(_ref) { var _details$payment_meth3, _details$payment_meth4, _checkoutDataOut$chec, _checkoutDataOut$chec2, _details$processor, _details$processor$re, _details$processor2, _details$processor2$r, _details$processor3, _details$processor3$r, _details$processor4, _details$payment_meth5, _details$payment_meth6; let { dataLoad, dataIn, dataOut } = _ref; const { handleCheckoutPayload } = useCheckout({ dataLoad, dataIn, dataOut }); const [checkoutDataOut, setCheckoutDataOut] = React.useState({ loading: false, cartItems: [] }); const dataLoadType = dataIn.dataLoadType || {}; const { endpoints, axiosOrder, axiosCheckout, payload: axiosPayload, baseUrl, ...resDataLoad } = dataLoad || {}; const postOrder = endpoints === null || endpoints === void 0 ? void 0 : endpoints.postOrder; const postCheckout = endpoints === null || endpoints === void 0 ? void 0 : endpoints.postCheckout; const details = axiosPayload || {}; // handle checkout api integration to create an order const handleCheckout = async () => { try { var _details$payment_meth, _details$payment_meth2; const payloadOrder = dataIn !== null && dataIn !== void 0 && dataIn.constructOrderPayload ? dataIn === null || dataIn === void 0 ? void 0 : dataIn.constructOrderPayload(Object.assign(details)) : details; const notCash = (details === null || details === void 0 ? void 0 : (_details$payment_meth = details.payment_method) === null || _details$payment_meth === void 0 ? void 0 : _details$payment_meth.type) !== 'cash '; console.log('notCash', notCash, details === null || details === void 0 ? void 0 : (_details$payment_meth2 = details.payment_method) === null || _details$payment_meth2 === void 0 ? void 0 : _details$payment_meth2.type); if (dataLoadType === 'network-service') { var _resOrder$data; setCheckoutDataOut({ key: 'submit-order', loading: true }); const resOrder = postOrder && axiosOrder && (await axiosOrder.post(postOrder, payloadOrder)); const checkoutPayload = await handleCheckoutPayload({ order: (resOrder === null || resOrder === void 0 ? void 0 : (_resOrder$data = resOrder.data) === null || _resOrder$data === void 0 ? void 0 : _resOrder$data.data) || (resOrder === null || resOrder === void 0 ? void 0 : resOrder.data), details }); const resCheckout = postCheckout && axiosCheckout && notCash && (await axiosCheckout.post(postCheckout, checkoutPayload)); console.log('resCheckout network-service', resCheckout); setCheckoutDataOut({ key: 'submit-order', loading: false, successful: true, orderResponse: resOrder, checkoutResponse: notCash ? resCheckout : {} }); } else if (dataLoadType === 'network-service-config') { var _resOrder$data2; setCheckoutDataOut({ key: 'submit-order', loading: true }); const resOrder = (baseUrl === null || baseUrl === void 0 ? void 0 : baseUrl.order) && postOrder && (await httpRequest({ ...resDataLoad, url: `${baseUrl.order}${postOrder}`, method: 'POST', requestPostData: payloadOrder })); const checkoutPayload = await handleCheckoutPayload({ order: (resOrder === null || resOrder === void 0 ? void 0 : (_resOrder$data2 = resOrder.data) === null || _resOrder$data2 === void 0 ? void 0 : _resOrder$data2.data) || (resOrder === null || resOrder === void 0 ? void 0 : resOrder.data), details }); const resCheckout = (baseUrl === null || baseUrl === void 0 ? void 0 : baseUrl.checkout) && postCheckout && notCash && (await httpRequest({ ...resDataLoad, url: `${baseUrl.checkout}${postCheckout}`, method: 'POST', requestPostData: checkoutPayload })); console.log('resCheckout network-service-config', resCheckout); setCheckoutDataOut({ key: 'submit-order', loading: false, successful: true, orderResponse: resOrder, checkoutResponse: notCash ? resCheckout : {} }); } } catch (err) { setCheckoutDataOut({ key: 'submit-checkout', loading: false, successful: false, errorResponse: (err === null || err === void 0 ? void 0 : err.response) || err }); } }; // life-cycle method to dataOut (your GApp) React.useEffect(() => { dataOut(checkoutDataOut); }, [JSON.stringify(checkoutDataOut)]); return { 'customer-details': { dataIn: { fullName: details.payee, firstName: details.first_name, lastName: details.last_name, contactNumber: details.contact_number, email: details.email, region: details.region, city: details.city, barangay: details.barangay, street: details.street, buildingName: details.building_name, landmark: details.landmark } }, 'order-items': { dataIn: { items: details.items } }, 'payment-method': { dataIn: { label: (_details$payment_meth3 = details.payment_method) === null || _details$payment_meth3 === void 0 ? void 0 : _details$payment_meth3.name } }, 'order-summary': { dataIn: { subTotal: details.subtotal || 0, voucherDiscount: details.discount || 0, shopperFee: details.shopee_fee || 0, deliveryFee: details.delivery_fee || 0, grandTotal: details.total_amount || 0 } }, 'submit-order': { dataIn: { disabled: ((details === null || details === void 0 ? void 0 : details.items) || []).length > 0 && details !== null && details !== void 0 && (_details$payment_meth4 = details.payment_method) !== null && _details$payment_meth4 !== void 0 && _details$payment_meth4.name ? false : true, loading: checkoutDataOut === null || checkoutDataOut === void 0 ? void 0 : checkoutDataOut.loading }, dataOut: handleCheckout }, 'web-view': { dataLoad: { url: checkoutDataOut === null || checkoutDataOut === void 0 ? void 0 : (_checkoutDataOut$chec = checkoutDataOut.checkoutResponse) === null || _checkoutDataOut$chec === void 0 ? void 0 : (_checkoutDataOut$chec2 = _checkoutDataOut$chec.data) === null || _checkoutDataOut$chec2 === void 0 ? void 0 : _checkoutDataOut$chec2.payment_link, successURL: details === null || details === void 0 ? void 0 : (_details$processor = details.processor) === null || _details$processor === void 0 ? void 0 : (_details$processor$re = _details$processor.redirect_url) === null || _details$processor$re === void 0 ? void 0 : _details$processor$re.success, failedURL: details === null || details === void 0 ? void 0 : (_details$processor2 = details.processor) === null || _details$processor2 === void 0 ? void 0 : (_details$processor2$r = _details$processor2.redirect_url) === null || _details$processor2$r === void 0 ? void 0 : _details$processor2$r.failure, canceledURL: details === null || details === void 0 ? void 0 : (_details$processor3 = details.processor) === null || _details$processor3 === void 0 ? void 0 : (_details$processor3$r = _details$processor3.redirect_url) === null || _details$processor3$r === void 0 ? void 0 : _details$processor3$r.cancel, paymentId: details === null || details === void 0 ? void 0 : (_details$processor4 = details.processor) === null || _details$processor4 === void 0 ? void 0 : _details$processor4.token } }, 'checkout-details': { dataLoad: { data: { ...details, customer_details: { fullName: details.payee, firstName: details.first_name, lastName: details.last_name, contactNumber: details.contact_number, email: details.email, region: details.region, city: details.city, barangay: details.barangay, street: details.street, buildingName: details.building_name, landmark: details.landmark }, items: details.items, payment_method: (_details$payment_meth5 = details.payment_method) === null || _details$payment_meth5 === void 0 ? void 0 : _details$payment_meth5.name, subTotal: details.subtotal || 0, voucherDiscount: details.discount || 0, shopperFee: details.shopee_fee || 0, deliveryFee: details.delivery_fee || 0, grandTotal: details.total_amount || 0 } }, dataIn: { disabled: ((details === null || details === void 0 ? void 0 : details.items) || []).length > 0 && details !== null && details !== void 0 && (_details$payment_meth6 = details.payment_method) !== null && _details$payment_meth6 !== void 0 && _details$payment_meth6.name ? false : true, loading: checkoutDataOut === null || checkoutDataOut === void 0 ? void 0 : checkoutDataOut.loading, onPressPayNow: handleCheckout } } }; } export default useViewModel; //# sourceMappingURL=useViewModel.js.map