Skip to content

Commit

Permalink
Update change handler to support paste
Browse files Browse the repository at this point in the history
  • Loading branch information
ioan-ghisoi-cko committed Apr 10, 2024
1 parent caa8a1f commit 3d06229
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 56 deletions.
13 changes: 8 additions & 5 deletions src/Frames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { tokenize, formatDataForTokenization } from "./utils/http";
export const FramesContext = React.createContext({} as FramesContextType);

const Frames = (props: FramesProps) => {
// @ts-ignore
const [state, dispatch]: [FramesState, FramesDispatch] = React.useReducer(
framesReducer,
// @ts-ignore
{
cardNumber: null,
cardBin: {
Expand All @@ -28,10 +28,10 @@ const Frames = (props: FramesProps) => {
cvv: null,
cvvLength: 3,
validation: {
cardNumber: false,
expiryDate: false,
cvv: false,
card: false,
cardNumber: true,
expiryDate: true,
cvv: true,
card: true,
},
}
);
Expand All @@ -57,11 +57,13 @@ const Frames = (props: FramesProps) => {
} catch (error) {
if (props.config.debug)
console.info(`Emitting "cardTokenizationFailed"`, error);
// @ts-ignore
if (props.cardTokenizationFailed) props.cardTokenizationFailed(error);
log(
"error",
"com.checkout.frames-mobile-sdk.exception",
props.config,
// @ts-ignore
error
);
}
Expand All @@ -77,6 +79,7 @@ const Frames = (props: FramesProps) => {

useEffect(() => {
if (state.cardNumber !== null) {
console.log("TRIGGERS use effect");
let payload = {
element: "card-number",
isValid: state.validation.cardNumber,
Expand Down
12 changes: 6 additions & 6 deletions src/components/CardNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ const CardNumber: React.FC<FramesCardFieldProps> = (props) => {
return (
<View style={styles.wrapper}>
<TextInput
autoCompleteType="cc-number"
autoComplete="cc-number"
keyboardType="number-pad"
returnKeyType="done"
placeholder={DEFAULT_CARD_NUMBER_PLACEHOLDER}
{...props}
value={state.cardNumber}
style={[styles.cardNumber, props.style]}
onChangeText={(val: string) => {
dispatch({ type: CARD_CHANGE, payload: val });
onChange={({ nativeEvent: { eventCount, target, text } }) => {
dispatch({ type: CARD_CHANGE, payload: text });
if (
val.replace(/[^0-9]/g, "").length >= 8 &&
val.replace(/[^0-9]/g, "").substring(0, 8) !==
text.replace(/[^0-9]/g, "").length >= 8 &&
text.replace(/[^0-9]/g, "").substring(0, 8) !==
state.cardBin.bin
) {
dispatch({
type: BIN_CHANGE,
payload: val.replace(/[^0-9]/g, "").substring(0, 8),
payload: text.replace(/[^0-9]/g, "").substring(0, 8),
});
}
}}
Expand Down
9 changes: 5 additions & 4 deletions src/components/Cvv.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DEFAULT_CVV_PLACEHOLDER } from "../utils/constants";
import { CVV_CHANGE } from "../utils/actions";
import { FramesFieldProps } from "../types/types";

//@ts-ignore
const Cvv: React.SFC<FramesFieldProps> = (props) => {
return (
<FramesConsumer>
Expand All @@ -15,7 +16,7 @@ const Cvv: React.SFC<FramesFieldProps> = (props) => {
}
return (
<TextInput
autoCompleteType="cc-csc"
autoComplete="cc-csc"
keyboardType="number-pad"
returnKeyType="done"
secureTextEntry={true}
Expand All @@ -24,9 +25,9 @@ const Cvv: React.SFC<FramesFieldProps> = (props) => {
style={[styles.cvv, props.style]}
value={state.cvv}
maxLength={state.cvvLength}
onChangeText={(val: string) =>
dispatch({ type: CVV_CHANGE, payload: val })
}
onChange={({ nativeEvent: { eventCount, target, text } }) => {
dispatch({ type: CVV_CHANGE, payload: text });
}}
/>
);
}}
Expand Down
10 changes: 5 additions & 5 deletions src/components/ExpiryDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FramesConsumer } from "../Frames";
import { DEFAULT_CARD_EXPIRY_DATE_PLACEHOLDER } from "../utils/constants";
import { DATE_CHANGE } from "../utils/actions";
import { FramesFieldProps } from "../types/types";

//@ts-ignore
const ExpiryDate: React.SFC<FramesFieldProps> = (props) => {
return (
<FramesConsumer>
Expand All @@ -15,17 +15,17 @@ const ExpiryDate: React.SFC<FramesFieldProps> = (props) => {
}
return (
<TextInput
autoCompleteType="cc-exp"
autoComplete="cc-exp"
keyboardType="number-pad"
maxLength={5}
returnKeyType="done"
placeholder={DEFAULT_CARD_EXPIRY_DATE_PLACEHOLDER}
{...props}
style={[styles.expiryDate, props.style]}
value={state.expiryDate}
onChangeText={(val: string) =>
dispatch({ type: DATE_CHANGE, payload: val })
}
onChange={({ nativeEvent: { eventCount, target, text } }) => {
dispatch({ type: DATE_CHANGE, payload: text });
}}
/>
);
}}
Expand Down
10 changes: 8 additions & 2 deletions src/utils/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ const types = [mada, ...creditcardsTypes];
const cards = new Card(types);
const cvc = new Cvc(types);

const extendedCardTypeMapWhitelist = { 6011111111111117: "Discover" };
interface CardTypeMap {
[key: string]: string;
}

const extendedCardTypeMapWhitelist: CardTypeMap = {
"6011111111111117": "Discover",
};

export const Icons = {
Visa: require("../icons/visa.png"),
Expand All @@ -31,7 +37,7 @@ export const formatCard = (text: string): string => {
export const getCardType = (text: string) => {
const sanitizedValue = cards.parse(text);
const cardType: IconKey =
extendedCardTypeMapWhitelist[sanitizedValue] ||
extendedCardTypeMapWhitelist[String(sanitizedValue)] ||
cards.type(sanitizedValue, true);
return cardType;
};
Expand Down
61 changes: 27 additions & 34 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"esnext"
],
"jsx": "react-native",
"types": ["react", "jest"],
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"outDir": "./dist",
"sourceMap": true,
"noImplicitReturns": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"declaration": true,
"noFallthroughCasesInSwitch": true
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"**/__tests__/*"
]
}

"compilerOptions": {
"target": "es5",
"lib": ["esnext"],
"jsx": "react-native",
"types": ["react", "jest"],
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"outDir": "./dist",
"sourceMap": true,
"noImplicitReturns": true,
"ignoreDeprecations": "5.0",
"noUnusedLocals": true,
"declaration": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true
},
"include": ["src/*"],
"exclude": ["node_modules", "**/__tests__/*"]
}

0 comments on commit 3d06229

Please sign in to comment.