diff --git a/client/src/components/card-payment-modal/card-payment-modal.component..jsx b/client/src/components/card-payment-modal/card-payment-modal.component..jsx index f17ad4454..696ef9097 100644 --- a/client/src/components/card-payment-modal/card-payment-modal.component..jsx +++ b/client/src/components/card-payment-modal/card-payment-modal.component..jsx @@ -3,21 +3,44 @@ import axios from "axios"; import { useTranslation } from "react-i18next"; import { Button, Card, Form, Input, InputNumber, Row, Select } from "antd"; import moment from "moment"; -import { useMutation } from "@apollo/client"; +import { useMutation, useQuery } from "@apollo/client"; import LayoutFormRow from "../layout-form-row/layout-form-row.component"; import JobSearchSelectComponent from "../job-search-select/job-search-select.component"; import { INSERT_NEW_PAYMENT } from "../../graphql/payments.queries"; -import { INSERT_PAYMENT_RESPONSE } from "../_test/payment_response.queries"; +import { + INSERT_PAYMENT_RESPONSE, + QUERY_RO_AND_OWNER_BY_JOB_PK, +} from "../_test/payment_response.queries"; import DataLabel from "../data-label/data-label.component"; +import { insertAuditTrail } from "../../redux/application/application.actions"; +import AuditTrailMapping from "../../utils/AuditTrailMappings"; +import { connect } from "react-redux"; +import { toggleModalVisible } from "../../redux/modals/modals.actions"; -const CardPaymentModalComponent = ({ bodyshop, context }) => { +const mapDispatchToProps = (dispatch) => ({ + insertAuditTrail: ({ jobid, operation }) => + dispatch(insertAuditTrail({ jobid, operation })), + toggleModalVisible: () => dispatch(toggleModalVisible("cardPayment")), +}); + +const CardPaymentModalComponent = ({ + bodyshop, + context, + toggleModalVisible, + insertAuditTrail, +}) => { const [form] = Form.useForm(); const amount = Form.useWatch("amount", form); const payer = Form.useWatch("payer", form); + const jobid = Form.useWatch("jobid", form); const [insertPayment] = useMutation(INSERT_NEW_PAYMENT); const [insertPaymentResponse] = useMutation(INSERT_PAYMENT_RESPONSE); const { t } = useTranslation(); + const { data, refetch } = useQuery(QUERY_RO_AND_OWNER_BY_JOB_PK, { + variables: { jobid: context?.jobid ?? "" }, + }); + useEffect(() => { axios.get("/intellipay/lightbox_credentials").then((response) => { var rg = document.createRange(); @@ -26,6 +49,8 @@ const CardPaymentModalComponent = ({ bodyshop, context }) => { document.documentElement.appendChild(node); window.intellipay.initialize(); + // console.log("Declined Payments - Intellipay", window.intellipay); + window.intellipay.runOnClose(() => { window.intellipay.initialize(); }); @@ -33,32 +58,42 @@ const CardPaymentModalComponent = ({ bodyshop, context }) => { window.intellipay.runOnApproval(async function (response) { form.setFieldValue("paymentResponse", response); form.submit(); + + toggleModalVisible(); }); window.intellipay.runOnNonApproval(async function (response) { + console.log("Declined Payments", response); // Mutate unsuccessful payment - await insertPaymentResponse({ - variables: { - paymentResponse: { - amount: response.amount, - bodyshopid: bodyshop.id, - jobid: context.jobid, - declinereason: response.declinereason, - ext_paymentid: response.paymentid.toString(), - successful: false, - response, - }, - }, + + // await insertPaymentResponse({ + // variables: { + // paymentResponse: { + // amount: response.amount, + // bodyshopid: bodyshop.id, + // jobid: jobid || context.jobid, + // declinereason: response.declinereason, + // ext_paymentid: response.paymentid.toString(), + // successful: false, + // response, + // }, + // }, + // }); + + // Insert failed payment to audit trail + insertAuditTrail({ + jobid: jobid || context?.jobid, + operation: AuditTrailMapping.failedpayment(), }); }); }); - if (context.jobid) { + if (context?.jobid) { form.setFieldValue("jobid", context.jobid); } - }, []); - const disabled = false; + form.setFieldValue("payer", t("payments.labels.customer")); + }, []); const handleFinish = async (values) => { const paymentResult = await insertPayment({ @@ -72,6 +107,16 @@ const CardPaymentModalComponent = ({ bodyshop, context }) => { date: moment(Date.now()), }, }, + update(cache, { data }) { + cache.modify({ + id: cache.identify({ id: jobid, __typename: "jobs" }), + fields: { + payments(payments) { + return [...data.insert_payments.returning, ...payments]; + }, + }, + }); + }, }); await insertPaymentResponse({ @@ -105,9 +150,12 @@ const CardPaymentModalComponent = ({ bodyshop, context }) => { ]} > { + refetch({ jobid: e }); + }} /> @@ -120,6 +168,20 @@ const CardPaymentModalComponent = ({ bodyshop, context }) => { value={amount} hidden /> + + {/* Lightbox payment response when it is completed */}