Skip to content

Commit

Permalink
Customer mode payment
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharindu Kumarasiri committed Sep 15, 2024
1 parent 5335bf8 commit b0feb6a
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 93 deletions.
1 change: 1 addition & 0 deletions src/assets/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"CARD_NUMBER": "Card Number",
"SCAN_CARD": "Scan Card",
"PAY": "Pay",
"SAVE": "Save",
"NAME_SHOWN_ON_RECEIPT": "Name (shown on receipt)",
"FULL_NAME_ON_RECEIPT": "Full name on receipt",
"EMAIL": "Email",
Expand Down
1 change: 1 addition & 0 deletions src/assets/languages/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"CARD_NUMBER": "カード番号",
"SCAN_CARD": "カードをスキャン",
"PAY": "支払い",
"SAVE": "保存",
"NAME_SHOWN_ON_RECEIPT": "氏名",
"FULL_NAME_ON_RECEIPT": "氏名を入力(レシートで表示されます)",
"EMAIL": "メールアドレス",
Expand Down
16 changes: 6 additions & 10 deletions src/components/CardInputGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
memo,
useContext,
useEffect,
useState,
useCallback,
} from "react";
import { memo, useContext, useEffect, useState, useCallback } from "react";

import { StyleSheet, View, Image, Dimensions } from "react-native";

Expand All @@ -18,6 +12,7 @@ import {
import {
CardTypes,
PaymentType,
sessionDataType,
sessionShowPaymentMethodType,
ThemeSchemeType,
} from "../util/types";
Expand Down Expand Up @@ -50,8 +45,9 @@ const CardInputGroup = ({ inputErrors, resetError }: Props) => {
// const [toggleScanCard, setToggleScanCard] = useState<boolean>(false);
const theme = useCurrentTheme();
const styles = getStyles(theme);
const { cardCVV, cardNumber, cardExpiredDate, paymentMethods } =
const { cardCVV, cardNumber, cardExpiredDate, sessionData } =
useContext(StateContext);
const SessionData = sessionData as sessionDataType;

useEffect(() => {
// Determine card type and set it on first render if cardNumber is not empty
Expand Down Expand Up @@ -83,7 +79,7 @@ const CardInputGroup = ({ inputErrors, resetError }: Props) => {
// Create card image list
const cardImage = useCallback(() => {
// Select credit card payment method data from session response payment methods
const cardPaymentMethodData = paymentMethods?.find(
const cardPaymentMethodData = SessionData.paymentMethods?.find(
(method: sessionShowPaymentMethodType) =>
method?.type === PaymentType.CREDIT
);
Expand Down Expand Up @@ -116,7 +112,7 @@ const CardInputGroup = ({ inputErrors, resetError }: Props) => {
} else {
return;
}
}, [cardType, paymentMethods]);
}, [cardType, SessionData.paymentMethods]);

// const onCardScanned = useCallback(
// (cardDetails: { cardNumber?: string; expirationDate?: string }) => {
Expand Down
12 changes: 9 additions & 3 deletions src/components/PillContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { StyleSheet, View, Image, FlatList } from "react-native";

import { StateContext } from "../context/state";

import { PaymentType, sessionShowPaymentMethodType } from "../util/types";
import {
PaymentType,
sessionDataType,
sessionShowPaymentMethodType,
} from "../util/types";

import PaymentMethodImages from "../assets/images/paymentMethodImages";

Expand All @@ -24,7 +28,9 @@ const squareSizeImages = [
];

const PillContainer = ({ onSelect, selectedItem }: Props) => {
const { paymentMethods } = useContext(StateContext);
const { sessionData } = useContext(StateContext) as {
sessionData: sessionDataType;
};

const getIcon = (slug: PaymentType) => {
return (
Expand Down Expand Up @@ -53,7 +59,7 @@ const PillContainer = ({ onSelect, selectedItem }: Props) => {
return (
<View style={styles.container}>
<FlatList
data={paymentMethods}
data={sessionData.paymentMethods}
renderItem={renderItem}
horizontal
showsHorizontalScrollIndicator={false}
Expand Down
49 changes: 33 additions & 16 deletions src/components/sections/CardSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import CardInputGroup from "../CardInputGroup";
import Input from "../Input";
import SubmitButton from "../SubmitButton";
import useThreeDSecureHandler from "../../hooks/useThreeDSecureHandler";
import { PaymentMode, PaymentType, sessionDataType } from "../../util/types";
import useSessionPayHandler from "../../hooks/useSessionPayHandler";

const initialErrors = {
name: false,
Expand All @@ -30,15 +32,11 @@ const initialErrors = {
const CardSection = (): JSX.Element => {
const [inputErrors, setInputErrors] = useState(initialErrors);
const { threeDSecurePayment } = useThreeDSecureHandler();
const {
cardholderName,
cardCVV,
cardNumber,
cardExpiredDate,
amount,
currency,
} = useContext(StateContext);
const { sessionPay } = useSessionPayHandler();
const { cardholderName, cardCVV, cardNumber, cardExpiredDate, sessionData } =
useContext(StateContext);
const dispatch = useContext(DispatchContext);
const SessionData = sessionData as sessionDataType;

const resetError = (type: string) => {
// TODO: Fix this type error
Expand All @@ -58,12 +56,24 @@ const CardSection = (): JSX.Element => {
});

if (isValid) {
threeDSecurePayment({
cardholderName,
cardCVV,
cardNumber,
cardExpiredDate,
});
if (SessionData.mode === PaymentMode.Customer) {
sessionPay({
paymentType: PaymentType.CREDIT,
paymentDetails: {
cardholderName,
cardCVV,
cardNumber,
cardExpiredDate,
},
});
} else {
threeDSecurePayment({
cardholderName,
cardCVV,
cardNumber,
cardExpiredDate,
});
}
}
};

Expand All @@ -87,8 +97,15 @@ const CardSection = (): JSX.Element => {
<CardInputGroup inputErrors={inputErrors} resetError={resetError} />
<View style={styles.btn}>
<SubmitButton
label="PAY"
labelSuffix={formatCurrency({ amount, currency })}
label={SessionData?.mode === PaymentMode.Customer ? "SAVE" : "PAY"}
labelSuffix={
SessionData?.mode === PaymentMode.Customer
? ""
: formatCurrency({
amount: SessionData.amount,
currency: SessionData.currency,
})
}
onPress={onPay}
testID="PayCTA"
/>
Expand Down
12 changes: 8 additions & 4 deletions src/components/sections/KonbiniSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
brandType,
KonbiniType,
PaymentType,
sessionDataType,
sessionShowPaymentMethodType,
} from "../../util/types";
import { validateKonbiniFormFields } from "../../util/validator";
Expand All @@ -31,11 +32,11 @@ const KonbiniSection = (): JSX.Element => {
const [inputErrors, setInputErrors] = useState(initialErrors);

const { sessionPay } = useSessionPayHandler();
const { name, email, amount, currency, paymentMethods, selectedStore } =
useContext(StateContext);
const { name, email, sessionData, selectedStore } = useContext(StateContext);
const dispatch = useContext(DispatchContext);
const SessionData = sessionData as sessionDataType;

const konbiniPaymentMethodData = paymentMethods?.find(
const konbiniPaymentMethodData = SessionData.paymentMethods?.find(
(method: sessionShowPaymentMethodType) =>
method?.type === PaymentType.KONBINI
);
Expand Down Expand Up @@ -134,7 +135,10 @@ const KonbiniSection = (): JSX.Element => {
<View style={styles.btn}>
<SubmitButton
label="PAY"
labelSuffix={formatCurrency({ amount, currency })}
labelSuffix={formatCurrency({
amount: SessionData.amount,
currency: SessionData.currency,
})}
onPress={onPay}
/>
</View>
Expand Down
11 changes: 8 additions & 3 deletions src/components/sections/SingleInputFormSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Input from "../../components/Input";

import { LangKeys } from "../../util/constants";
import { formatCurrency } from "../../util/helpers";
import { PaymentType } from "../../util/types";
import { PaymentType, sessionDataType } from "../../util/types";

import { responsiveScale } from "../../theme/scalling";

Expand All @@ -22,7 +22,9 @@ type SingleInputFormSectionProps = {
const SingleInputFormSection = ({ type }: SingleInputFormSectionProps) => {
const [inputText, setInputText] = useState("");
const [inputError, setInputError] = useState(false);
const { amount, currency } = useContext(StateContext);
const { sessionData } = useContext(StateContext) as {
sessionData: sessionDataType;
};
const { sessionPay } = useSessionPayHandler();

const onPay = () => {
Expand Down Expand Up @@ -58,7 +60,10 @@ const SingleInputFormSection = ({ type }: SingleInputFormSectionProps) => {
<View style={styles.btn}>
<SubmitButton
label="PAY"
labelSuffix={formatCurrency({ amount, currency })}
labelSuffix={formatCurrency({
amount: sessionData.amount,
currency: sessionData.currency,
})}
onPress={onPay}
testID="PayCTA"
/>
Expand Down
10 changes: 7 additions & 3 deletions src/components/sections/TransferFormSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Actions, DispatchContext, StateContext } from "../../context/state";
import Input from "../../components/Input";

import { formatCurrency } from "../../util/helpers";
import { PaymentType } from "../../util/types";
import { PaymentType, sessionDataType } from "../../util/types";
import { validateTransferFormFields } from "../../util/validator";

import { responsiveScale } from "../../theme/scalling";
Expand All @@ -33,8 +33,9 @@ const TransferFormSection = ({ type }: TransferFormSectionProps) => {
const [inputErrors, setInputErrors] =
useState<typeof initialErrors>(initialErrors);

const { amount, currency, transferFormFields } = useContext(StateContext);
const { sessionData, transferFormFields } = useContext(StateContext);
const { sessionPay } = useSessionPayHandler();
const SessionData = sessionData as sessionDataType;

const onPay = () => {
const isValid = validateTransferFormFields({
Expand Down Expand Up @@ -148,7 +149,10 @@ const TransferFormSection = ({ type }: TransferFormSectionProps) => {
<View style={styles.btn}>
<SubmitButton
label="PAY"
labelSuffix={formatCurrency({ amount, currency })}
labelSuffix={formatCurrency({
amount: SessionData.amount,
currency: SessionData.currency,
})}
onPress={onPay}
testID="PayCTA"
/>
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useBackgroundHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Dispatch, SetStateAction, useContext, useEffect } from "react";
import { AppState, AppStateStatus } from "react-native";
import {
CreatePaymentFuncType,
PaymentStatuses,
PaymentType,
TokenResponseStatuses,
Expand All @@ -18,6 +19,8 @@ const useBackgroundHandler = (
const { paymentType, tokenId, providerPropsData, sessionData } =
useContext(StateContext);

const SessionData = sessionData as CreatePaymentFuncType;

const {
startLoading,
stopLoading,
Expand All @@ -38,13 +41,13 @@ const useBackgroundHandler = (
return () => {
windowChangeListener.remove();
};
}, [providerPropsData, paymentType, tokenId, sessionData, isDeepLinkOpened]);
}, [providerPropsData, paymentType, tokenId, SessionData, isDeepLinkOpened]);

const handleSessionPaymentResponse = async () => {
// if this is a session flow, check until session response changes from 'pending' to 'completed' or 'error'
const sessionShowPayload = {
publishableKey: providerPropsData.publishableKey,
sessionId: sessionData.sessionId,
sessionId: SessionData.sessionId,
};

// fetch session status to check if the payment is completed
Expand All @@ -57,7 +60,7 @@ const useBackgroundHandler = (
} else {
onPaymentAwaiting();
} // calling user passed onComplete method with session response data
sessionData.onComplete && sessionData.onComplete(sessionResponse);
SessionData.onComplete && SessionData.onComplete(sessionResponse);
} else if (sessionResponse?.payment?.status === PaymentStatuses.CANCELLED) {
onPaymentCancelled();
} else if (sessionResponse?.expired) {
Expand All @@ -84,7 +87,7 @@ const useBackgroundHandler = (
) {
const paymentResponse = await payForSession({
paymentType: PaymentType.CREDIT,
sessionId: sessionData.sessionId,
sessionId: SessionData.sessionId,
publishableKey: providerPropsData.publishableKey,
paymentDetails: { tokenId: tokenId },
});
Expand Down
12 changes: 8 additions & 4 deletions src/hooks/useDeepLinkHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Linking } from "react-native";
import { Dispatch, SetStateAction, useContext, useEffect } from "react";
import {
CreatePaymentFuncType,
PaymentStatuses,
PaymentType,
TokenResponseStatuses,
Expand All @@ -12,9 +13,12 @@ import { extractParameterFromUrl } from "../util/helpers";
import payForSession from "../services/payForSessionService";
import useMainStateUtils from "./useMainStateUtils";

const useDeepLinkHandler = (setIsDeepLinkOpened: Dispatch<SetStateAction<boolean>>) => {
const useDeepLinkHandler = (
setIsDeepLinkOpened: Dispatch<SetStateAction<boolean>>
) => {
const { paymentType, providerPropsData, sessionData } =
useContext(StateContext);
const SessionData = sessionData as CreatePaymentFuncType;

const {
startLoading,
Expand Down Expand Up @@ -43,7 +47,7 @@ const useDeepLinkHandler = (setIsDeepLinkOpened: Dispatch<SetStateAction<boolean
// if this is a session flow, check until session response changes from 'pending' to 'completed' or 'error'
const sessionShowPayload = {
publishableKey: providerPropsData.publishableKey,
sessionId: sessionData.sessionId,
sessionId: SessionData.sessionId,
};

// fetch session status to check if the payment is completed
Expand All @@ -67,7 +71,7 @@ const useDeepLinkHandler = (setIsDeepLinkOpened: Dispatch<SetStateAction<boolean
onPaymentAwaiting();
}
// calling user passed onComplete method with session response data
sessionData.onComplete && sessionData.onComplete(sessionResponse);
SessionData.onComplete && SessionData.onComplete(sessionResponse);
} else if (sessionResponse?.payment?.status === PaymentStatuses.CANCELLED) {
onPaymentCancelled();
} else {
Expand All @@ -92,7 +96,7 @@ const useDeepLinkHandler = (setIsDeepLinkOpened: Dispatch<SetStateAction<boolean
) {
const paymentResponse = await payForSession({
paymentType: PaymentType.CREDIT,
sessionId: sessionData.sessionId,
sessionId: SessionData.sessionId,
publishableKey: providerPropsData.publishableKey,
paymentDetails: { tokenId: token },
});
Expand Down
Loading

0 comments on commit b0feb6a

Please sign in to comment.