import React, { useState, ReactElement, useEffect } from "react"; import { Button } from "react-bootstrap"; import { useForm } from "react-hook-form"; import { StagerComponent } from "../components/stager"; import { Summary } from "../components/summary"; import { FormSchema, getPaymentFrequency, getPaymentPlan } from "../schema"; import { usePostResource } from "../services/rest-resource.service"; import { upperFirst } from "lodash-es"; import { get as getEnv, getStr as getEnvStr } from '../env'; import { useCurrentRouter } from "../services/router.service"; export const ConfirmationPage: StagerComponent = ({ data, onCompleted }) => { const organisationName = getEnv('ORGANISATION_NAME'); const organisationBankName = getEnv('ORGANISATION_BANK_NAME'); const organisationEmailAddress = getEnv('ORGANISATION_EMAIL_ADDRESS'); const organisationMailToLink = `mailto:${organisationEmailAddress}`; const chargebeeSiteName = getEnv('CHARGEBEE_SITE_NAME'); const joiningVerb = getEnvStr('JOINING_VERB'); const form = useForm(); const router = useCurrentRouter(); const join = usePostResource>("/join"); const autoSubmit = Boolean(data.gcBillingRequestId || data.stripePaymentIntentId || data.cbHostedPageId); // Default "requestInFlight" to true if autoSubmit is set, as the request is sent // immediately through useEffect() below const [requestInFlight, setRequestInFlight] = useState(autoSubmit); const [joinError, setJoinError] = useState( false ); const joiningSpinner = (
Please wait
{joiningVerb} {organisationName}
); let directDebitDetailsMessage = null; // Only show Direct Debit payment details if there's actually an amount to pay if (data.paymentMethod === "directDebit" && data.membership) { const plan = getPaymentPlan(data.membership); const isAmountZero = plan && Number(plan.amount) === 0; const isCustomAmountAboveZero = (plan && plan.allowCustomAmount) ? Number(data.customMembershipAmount) > 0 : false; const hasDonation = data.donationAmount && Number(data.donationAmount) > 0; // Only show payment details if there's something to pay (membership, custom amount, or donation) if (!isAmountZero || isCustomAmountAboveZero || hasDonation) { const frequency = getPaymentFrequency(data.membership) directDebitDetailsMessage = (

You are paying for your membership of {organisationName} by Direct Debit.

This will be charged {frequency ? frequency + ' ' : ''}from your bank account.

On your bank statement the charge will appear as "{organisationBankName}".

You can contact the membership team of {organisationName} at{" "} {organisationEmailAddress}.

); } } const onSubmit = async () => { setRequestInFlight(true); join({ ...data, stage: 'confirm' }).then( () => { onCompleted(data); }, (error) => { setRequestInFlight(false); console.error(error.message); const errorInformaton = JSON.parse(error.message); let message =

Please try again in an hour.

; switch (errorInformaton?.data?.error_code) { case 1: message =

Please try re-entering your payment details.

; break; case 2: message = (

We couldn't charge the account as it didn't have sufficient funds. Maybe try another card?

); break; case 3: message = ( <>

Something is wrong with your Direct Debit mandate.

    {errorInformaton.data.fields.map((fieldInformation: any) => (
  • {upperFirst(fieldInformation.field).replace(/_/g, ' ')}{" "} {fieldInformation.message}
  • ))}
); break; case 4: message = (

We couldn't charge the account as the card you entered has expired. Maybe try another card?

); break; case 25: message = ( <>

We seem to already have an active membership for you.

If you'd like to update your details, we recommend using the{" "} {organisationName} membership management page .

Thanks for being a member already!

); break; case 101: message = (

We didn't find you in our members database. Make sure you're using the same email address that you've used for your membership before now. If you're trying to join up as a new member, use the{" "} { e.preventDefault(); router.setState({ ...router.state, stage: 'enter-details' }); }}>Join {" "}page instead.

); break; } setJoinError(message); } ); }; useEffect(() => { if (autoSubmit) { onSubmit() } }, []) return (

Confirm your details

{requestInFlight ? ( joiningSpinner ) : ( <> {directDebitDetailsMessage} {joinError && (

Sorry you cannot join {organisationName} at this time.

{joinError}

If you continue to have problems please contact{" "} {organisationEmailAddress}

)} )}
); };