react-native-cashfree-pg-sdk 2.1.18 → 2.1.19-dev.2

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,6 +1,11 @@
1
1
  import { TextInput, TextInputProps } from 'react-native';
2
2
  import React, { forwardRef } from 'react';
3
- import { CFCardPayment, CFEnvironment, type CFSession, ElementCard } from 'cashfree-pg-api-contract';
3
+ import {
4
+ CFCardPayment,
5
+ CFEnvironment,
6
+ type CFSession,
7
+ ElementCard,
8
+ } from 'cashfree-pg-api-contract';
4
9
  import { CFPaymentGatewayService } from '../index';
5
10
 
6
11
  function luhnCheck(cardNumber: string): boolean {
@@ -37,7 +42,7 @@ function luhnCheck(cardNumber: string): boolean {
37
42
  */
38
43
  async function getTDR(session: CFSession, bin: string) {
39
44
  const route: string = `/pg/sdk/js/${session.payment_session_id}/v2/tdr`;
40
- const body: string = JSON.stringify({ 'code': bin, 'code_type': 'bin' });
45
+ const body: string = JSON.stringify({ code: bin, code_type: 'bin' });
41
46
  return await getInfo(session.environment, route, body);
42
47
  }
43
48
 
@@ -48,7 +53,7 @@ async function getTDR(session: CFSession, bin: string) {
48
53
  */
49
54
  async function getCardBin(session: CFSession, bin: string) {
50
55
  const route: string = `/pg/sdk/js/${session.payment_session_id}/cardBin`;
51
- const body: string = JSON.stringify({ 'card_number': bin });
56
+ const body: string = JSON.stringify({ card_number: bin });
52
57
  return await getInfo(session.environment, route, body);
53
58
  }
54
59
 
@@ -78,6 +83,10 @@ async function getInfo(env: string, route: string, bodyData: string) {
78
83
 
79
84
  export type CardPaymentHandle = {
80
85
  doPayment: (cardInfo: ElementCard) => void;
86
+ doPaymentWithPaymentSessionId: (
87
+ cardInfo: ElementCard,
88
+ cfSession: CFSession
89
+ ) => void;
81
90
  };
82
91
 
83
92
  export type CardInputProps = {
@@ -85,178 +94,195 @@ export type CardInputProps = {
85
94
  cardListener: (response: string) => void;
86
95
  } & TextInputProps;
87
96
 
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;
97
+ const CardInput: any = forwardRef<CardPaymentHandle, CardInputProps>(
98
+ ({ cfSession, cardListener, style, ...props }: CardInputProps, ref) => {
99
+ const [inputNumber, setInputNumber] = React.useState('');
100
+ React.useImperativeHandle(ref, () => ({
101
+ doPayment,
102
+ doPaymentWithPaymentSessionId,
103
+ }));
104
+ let tdrJson: any = null;
105
+ let cardBinJson: any = null;
106
+ let firstEightDigits: string = '';
107
+
108
+ const handleChange = React.useCallback(
109
+ async (cardNumber: string) => {
110
+ let completeResponse: any = {};
111
+ const textWithoutSpaces: string = cardNumber.replaceAll(' ', '');
112
+ if (textWithoutSpaces.length === 0) setInputNumber('');
113
+
114
+ let formattedText: string = '';
115
+ /**
116
+ * Code to format card input number & set to input box
117
+ */
118
+ for (let i: number = 0; i < textWithoutSpaces.length; i += 4) {
119
+ let end: number = i + 4;
120
+ if (end > textWithoutSpaces.length) {
121
+ end = textWithoutSpaces.length;
122
+ }
123
+ formattedText += textWithoutSpaces.substring(i, end);
124
+ if (end !== textWithoutSpaces.length) {
125
+ formattedText += ' ';
126
+ }
127
+ setInputNumber(formattedText);
115
128
  }
116
- formattedText += textWithoutSpaces.substring(i, end);
117
- if (end !== textWithoutSpaces.length) {
118
- formattedText += ' ';
119
- }
120
- setInputNumber(formattedText);
121
- }
122
129
 
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;
130
+ let tdrResponse: any = null;
131
+ let cardBinResponse: any = null;
132
+
133
+ /**
134
+ * Fetch Tdr & CardBin data & set to local variable
135
+ */
136
+ async function fetchDataAndSet() {
137
+ await getTDR(cfSession, textWithoutSpaces)
138
+ .then((response: string) => {
139
+ tdrResponse = response;
140
+ })
141
+ .catch(() => {
142
+ tdrResponse = null;
143
+ });
144
+
145
+ await getCardBin(cfSession, textWithoutSpaces)
146
+ .then((response: string) => {
147
+ cardBinResponse = response;
148
+ })
149
+ .catch(() => {
150
+ cardBinResponse = null;
151
+ });
152
+
153
+ if (tdrResponse) {
154
+ tdrJson = tdrResponse;
155
+ completeResponse['tdr_info'] = tdrJson;
156
+ }
157
+
158
+ if (cardBinResponse) {
159
+ cardBinJson = cardBinResponse;
160
+ completeResponse['card_bin_info'] = cardBinJson;
161
+ }
149
162
  }
150
163
 
151
- if (cardBinResponse) {
152
- cardBinJson = cardBinResponse;
153
- completeResponse['card_bin_info'] = cardBinJson;
164
+ if (textWithoutSpaces.length === 8) {
165
+ firstEightDigits = textWithoutSpaces;
166
+ await fetchDataAndSet();
167
+ } else if (textWithoutSpaces.length > 8) {
168
+ if (firstEightDigits === textWithoutSpaces.substring(0, 8)) {
169
+ completeResponse['tdr_info'] = tdrJson;
170
+ completeResponse['card_bin_info'] = cardBinJson;
171
+ } else {
172
+ firstEightDigits = textWithoutSpaces.substring(0, 8);
173
+ tdrJson = null;
174
+ cardBinJson = null;
175
+ await fetchDataAndSet();
176
+ }
154
177
  }
155
- }
156
178
 
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;
179
+ if (textWithoutSpaces.length < 8) {
167
180
  cardBinJson = null;
168
- await fetchDataAndSet();
181
+ tdrJson = null;
182
+ firstEightDigits = '';
169
183
  }
170
- }
171
184
 
172
- if (textWithoutSpaces.length < 8) {
173
- cardBinJson = null;
174
- tdrJson = null;
175
- firstEightDigits = '';
176
- }
185
+ if (cardBinJson !== null) {
186
+ completeResponse['card_network'] = cardBinJson['scheme'];
187
+ }
188
+ let luhnStatus = luhnCheck(textWithoutSpaces);
189
+ if (luhnStatus) {
190
+ completeResponse['luhn_check_info'] = 'SUCCESS';
191
+ if (textWithoutSpaces && textWithoutSpaces.length > 4) {
192
+ completeResponse['last_four_digit'] = textWithoutSpaces.substring(
193
+ textWithoutSpaces.length - 4
194
+ );
195
+ }
196
+ } else {
197
+ completeResponse['luhn_check_info'] = 'FAIL';
198
+ }
199
+ completeResponse['card_length'] = textWithoutSpaces.length;
177
200
 
178
- if (cardBinJson !== null) {
179
- completeResponse['card_network'] = cardBinJson['scheme'];
201
+ return cardListener(JSON.stringify(completeResponse));
202
+ },
203
+ [cardListener]
204
+ );
205
+
206
+ const doPayment = (cardInfo: ElementCard) => {
207
+ try {
208
+ cardInfo.cardNumber = inputNumber.replaceAll(' ', '');
209
+ const cardPayment = new CFCardPayment(cfSession, cardInfo);
210
+ CFPaymentGatewayService.makePayment(cardPayment);
211
+ } catch (e: any) {
212
+ console.log(e.message);
180
213
  }
181
- completeResponse['luhn_check_info'] = 'SUCCESS';
182
- if (!luhnCheck(textWithoutSpaces)) {
183
- completeResponse['luhn_check_info'] = 'FAIL';
214
+ };
215
+
216
+ const doPaymentWithPaymentSessionId = (
217
+ cardInfo: ElementCard,
218
+ session: CFSession
219
+ ) => {
220
+ try {
221
+ cfSession = session;
222
+ doPayment(cardInfo);
223
+ } catch (e: any) {
224
+ console.log(e.message);
184
225
  }
185
- completeResponse['card_length'] = textWithoutSpaces.length;
186
-
187
- return cardListener(JSON.stringify(completeResponse));
188
- },
189
- [cardListener],
190
- );
191
-
192
- const doPayment = (cardInfo: ElementCard) => {
193
- try {
194
- cardInfo.cardNumber = inputNumber.replaceAll(' ', '');
195
- const cardPayment = new CFCardPayment(cfSession, cardInfo);
196
- CFPaymentGatewayService.makePayment(cardPayment);
197
- } catch (e: any) {
198
- console.log(e.message);
199
- }
200
- };
226
+ };
201
227
 
202
- const handleSubmitEditingEvent = (event: any) => {
203
- const newEvent = { ...event };
204
- delete newEvent.nativeEvent.text;
205
- if (onSubmitEditing) {
206
- onSubmitEditing(newEvent);
207
- }
208
- };
228
+ const handleSubmitEditingEvent = (event: any) => {
229
+ const newEvent = { ...event };
230
+ delete newEvent.nativeEvent.text;
231
+ if (onSubmitEditing) {
232
+ onSubmitEditing(newEvent);
233
+ }
234
+ };
209
235
 
210
- const handleEndEditingEvent = (event: any) => {
211
- const newEvent = { ...event };
212
- delete newEvent.nativeEvent.text;
213
- if (onEndEditing) {
214
- onEndEditing(newEvent);
215
- }
216
- };
236
+ const handleEndEditingEvent = (event: any) => {
237
+ const newEvent = { ...event };
238
+ delete newEvent.nativeEvent.text;
239
+ if (onEndEditing) {
240
+ onEndEditing(newEvent);
241
+ }
242
+ };
217
243
 
218
- const handleFocusEvent = (event: any) => {
219
- const newEvent = { ...event };
220
- delete newEvent.nativeEvent.text;
221
- if (onFocus) {
222
- onFocus(newEvent);
223
- }
224
- };
244
+ const handleFocusEvent = (event: any) => {
245
+ const newEvent = { ...event };
246
+ delete newEvent.nativeEvent.text;
247
+ if (onFocus) {
248
+ onFocus(newEvent);
249
+ }
250
+ };
225
251
 
226
- const handleBlurEvent = (event: any) => {
227
- const newEvent = { ...event };
228
- delete newEvent.nativeEvent.text;
229
- if (onBlur) {
230
- onBlur(newEvent);
231
- }
232
- };
233
-
234
- const InputComponent: any = TextInput;
235
- const {
236
- onChangeText,
237
- onChange,
238
- onSubmitEditing,
239
- onEndEditing,
240
- onFocus,
241
- onBlur,
242
- ...otherProps
243
- } = props;
244
-
245
-
246
- return (
247
- <InputComponent
248
- keyboardType='numeric'
249
- inputMode={'numeric'}
250
- value={inputNumber}
251
- onChangeText={handleChange}
252
- onSubmitEditing={handleSubmitEditingEvent}
253
- onEndEditing={handleEndEditingEvent}
254
- onFocus={handleFocusEvent}
255
- onBlur={handleBlurEvent}
256
- style={style}
257
- {...otherProps}
258
- />
259
- );
260
- });
252
+ const handleBlurEvent = (event: any) => {
253
+ const newEvent = { ...event };
254
+ delete newEvent.nativeEvent.text;
255
+ if (onBlur) {
256
+ onBlur(newEvent);
257
+ }
258
+ };
259
+
260
+ const InputComponent: any = TextInput;
261
+ const {
262
+ onChangeText,
263
+ onChange,
264
+ onSubmitEditing,
265
+ onEndEditing,
266
+ onFocus,
267
+ onBlur,
268
+ ...otherProps
269
+ } = props;
270
+
271
+ return (
272
+ <InputComponent
273
+ keyboardType="numeric"
274
+ inputMode={'numeric'}
275
+ value={inputNumber}
276
+ onChangeText={handleChange}
277
+ onSubmitEditing={handleSubmitEditingEvent}
278
+ onEndEditing={handleEndEditingEvent}
279
+ onFocus={handleFocusEvent}
280
+ onBlur={handleBlurEvent}
281
+ style={style}
282
+ {...otherProps}
283
+ />
284
+ );
285
+ }
286
+ );
261
287
 
262
288
  export default CardInput;
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module version="4">
3
- <component name="SonarLintModuleSettings">
4
- <option name="uniqueId" value="867d332f-38aa-4470-a53c-439016e7f8c2" />
5
- </component>
6
- </module>