react-native-cashfree-pg-sdk 2.1.12-dev.1 → 2.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,263 +0,0 @@
1
- import { TextInput, TextInputProps } from 'react-native';
2
- import React, { forwardRef } from 'react';
3
- import { CFCardPayment, CFEnvironment, type CFSession, NonPciCard } from 'cashfree-pg-api-contract';
4
- import { CFPaymentGatewayService } from '../index';
5
-
6
- function luhnCheck(cardNumber: string): boolean {
7
- if (cardNumber.length === 0) {
8
- return false;
9
- }
10
-
11
- cardNumber = cardNumber.replace(/\s/g, ''); // Remove spaces
12
-
13
- let sum = 0;
14
- let isAlternate = false;
15
-
16
- for (let i = cardNumber.length - 1; i >= 0; i--) {
17
- let digit = parseInt(cardNumber[i], 10);
18
-
19
- if (isAlternate) {
20
- digit *= 2;
21
- if (digit > 9) {
22
- digit -= 9;
23
- }
24
- }
25
-
26
- sum += digit;
27
- isAlternate = !isAlternate;
28
- }
29
-
30
- return sum % 10 === 0;
31
- }
32
-
33
- /**
34
- * Fetching Tdr info with card bin data & cfSession object
35
- * @param session : for payment sessionId & env
36
- * @param bin : for card number
37
- */
38
- async function getTDR(session: CFSession, bin: string) {
39
- const route: string = `/pg/sdk/js/${session.payment_session_id}/v2/tdr`;
40
- const body: string = JSON.stringify({ 'code': bin, 'code_type': 'bin' });
41
- return await getInfo(session.environment, route, body);
42
- }
43
-
44
- /**
45
- * Fetching CardBin info with card bin data & cfSession object
46
- * @param session : for payment sessionId & env
47
- * @param bin : for card number
48
- */
49
- async function getCardBin(session: CFSession, bin: string) {
50
- const route: string = `/pg/sdk/js/${session.payment_session_id}/cardBin`;
51
- const body: string = JSON.stringify({ 'card_number': bin });
52
- return await getInfo(session.environment, route, body);
53
- }
54
-
55
- async function getInfo(env: string, route: string, bodyData: string) {
56
- let baseUrl = 'https://api.cashfree.com';
57
- if (env === CFEnvironment.SANDBOX) {
58
- baseUrl = 'https://sandbox.cashfree.com';
59
- }
60
- const url = baseUrl + route;
61
- try {
62
- const response = await fetch(url, {
63
- method: 'POST',
64
- headers: {
65
- 'Content-Type': 'application/json',
66
- },
67
- body: bodyData,
68
- });
69
-
70
- if (!response.ok) {
71
- return null;
72
- }
73
- return await response.json();
74
- } catch (error) {
75
- return null;
76
- }
77
- }
78
-
79
- export type CardPaymentHandle = {
80
- doPayment: (cardInfo: NonPciCard) => void;
81
- };
82
-
83
- export type CardInputProps = {
84
- cfSession: CFSession;
85
- cardListener: (response: string) => void;
86
- } & TextInputProps;
87
-
88
- const CardInput: any = forwardRef<CardPaymentHandle, CardInputProps>(({
89
- cfSession,
90
- cardListener,
91
- style,
92
- ...props
93
- }: CardInputProps, ref) => {
94
-
95
- const [inputNumber, setInputNumber] = React.useState('');
96
- React.useImperativeHandle(ref, () => ({ doPayment }));
97
- let tdrJson: any = null;
98
- let cardBinJson: any = null;
99
- let firstEightDigits: string = '';
100
-
101
- const handleChange = React.useCallback(
102
- async (cardNumber: string) => {
103
- let completeResponse: any = {};
104
- const textWithoutSpaces: string = cardNumber.replaceAll(' ', '');
105
- if (textWithoutSpaces.length === 0) setInputNumber('');
106
-
107
- let formattedText: string = '';
108
- /**
109
- * Code to format card input number & set to input box
110
- */
111
- for (let i: number = 0; i < textWithoutSpaces.length; i += 4) {
112
- let end: number = i + 4;
113
- if (end > textWithoutSpaces.length) {
114
- end = textWithoutSpaces.length;
115
- }
116
- formattedText += textWithoutSpaces.substring(i, end);
117
- if (end !== textWithoutSpaces.length) {
118
- formattedText += ' ';
119
- }
120
- setInputNumber(formattedText);
121
- }
122
-
123
- let tdrResponse: any = null;
124
- let cardBinResponse: any = null;
125
-
126
- /**
127
- * Fetch Tdr & CardBin data & set to local variable
128
- */
129
- async function fetchDataAndSet() {
130
- await getTDR(cfSession, textWithoutSpaces)
131
- .then((response: string) => {
132
- tdrResponse = response;
133
- })
134
- .catch(() => {
135
- tdrResponse = null;
136
- });
137
-
138
- await getCardBin(cfSession, textWithoutSpaces)
139
- .then((response: string) => {
140
- cardBinResponse = response;
141
- })
142
- .catch(() => {
143
- cardBinResponse = null;
144
- });
145
-
146
- if (tdrResponse) {
147
- tdrJson = tdrResponse;
148
- completeResponse['tdr_info'] = tdrJson;
149
- }
150
-
151
- if (cardBinResponse) {
152
- cardBinJson = cardBinResponse;
153
- completeResponse['card_bin_info'] = cardBinJson;
154
- }
155
- }
156
-
157
- if (textWithoutSpaces.length === 8) {
158
- firstEightDigits = textWithoutSpaces;
159
- await fetchDataAndSet();
160
- } else if (textWithoutSpaces.length > 8) {
161
- if (firstEightDigits === textWithoutSpaces.substring(0, 8)) {
162
- completeResponse['tdr_info'] = tdrJson;
163
- completeResponse['card_bin_info'] = cardBinJson;
164
- } else {
165
- firstEightDigits = textWithoutSpaces.substring(0, 8);
166
- tdrJson = null;
167
- cardBinJson = null;
168
- await fetchDataAndSet();
169
- }
170
- }
171
-
172
- if (textWithoutSpaces.length < 8) {
173
- cardBinJson = null;
174
- tdrJson = null;
175
- firstEightDigits = '';
176
- }
177
-
178
- if (cardBinJson !== null) {
179
- completeResponse['card_network'] = cardBinJson['scheme'];
180
- }
181
- completeResponse['luhn_check_info'] = 'SUCCESS';
182
- if (!luhnCheck(textWithoutSpaces)) {
183
- completeResponse['luhn_check_info'] = 'FAIL';
184
- }
185
- completeResponse['card_length'] = textWithoutSpaces.length;
186
-
187
- return cardListener(JSON.stringify(completeResponse));
188
- },
189
- [cardListener],
190
- );
191
-
192
- const doPayment = (cardInfo: NonPciCard) => {
193
- try {
194
- cardInfo.cardNumber = inputNumber.replaceAll(' ', '');
195
- console.log('NonPciCard', JSON.stringify(cardInfo));
196
- const cardPayment = new CFCardPayment(cfSession, cardInfo);
197
- CFPaymentGatewayService.makePayment(cardPayment);
198
- } catch (e: any) {
199
- console.log(e.message);
200
- }
201
- };
202
-
203
- const handleSubmitEditingEvent = (event: any) => {
204
- const newEvent = { ...event };
205
- delete newEvent.nativeEvent.text;
206
- if (onSubmitEditing) {
207
- onSubmitEditing(newEvent);
208
- }
209
- };
210
-
211
- const handleEndEditingEvent = (event: any) => {
212
- const newEvent = { ...event };
213
- delete newEvent.nativeEvent.text;
214
- if (onEndEditing) {
215
- onEndEditing(newEvent);
216
- }
217
- };
218
-
219
- const handleFocusEvent = (event: any) => {
220
- const newEvent = { ...event };
221
- delete newEvent.nativeEvent.text;
222
- if (onFocus) {
223
- onFocus(newEvent);
224
- }
225
- };
226
-
227
- const handleBlurEvent = (event: any) => {
228
- const newEvent = { ...event };
229
- delete newEvent.nativeEvent.text;
230
- if (onBlur) {
231
- onBlur(newEvent);
232
- }
233
- };
234
-
235
- const InputComponent: any = TextInput;
236
- const {
237
- onChangeText,
238
- onChange,
239
- onSubmitEditing,
240
- onEndEditing,
241
- onFocus,
242
- onBlur,
243
- ...otherProps
244
- } = props;
245
-
246
-
247
- return (
248
- <InputComponent
249
- keyboardType='numeric'
250
- inputMode={'numeric'}
251
- value={inputNumber}
252
- onChangeText={handleChange}
253
- onSubmitEditing={handleSubmitEditingEvent}
254
- onEndEditing={handleEndEditingEvent}
255
- onFocus={handleFocusEvent}
256
- onBlur={handleBlurEvent}
257
- style={style}
258
- {...otherProps}
259
- />
260
- );
261
- });
262
-
263
- export default CardInput;
package/src/Card/index.js DELETED
@@ -1 +0,0 @@
1
- export { default } from './CFCardComponent';
package/src/Card/index.ts DELETED
@@ -1 +0,0 @@
1
- export { default } from './CFCardComponent';