react-native-cashfree-pg-sdk 2.3.0 → 2.3.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.
@@ -0,0 +1,294 @@
1
+ import { TextInput, TextInputProps } from 'react-native';
2
+ import React, { forwardRef } from 'react';
3
+ import {
4
+ CFSubsCardPayment,
5
+ CFEnvironment,
6
+ type CFSubscriptionSession,
7
+ ElementCard,
8
+ } from 'cashfree-pg-api-contract';
9
+ import { CFPaymentGatewayService } from '../index';
10
+
11
+ function luhnCheck(cardNumber: string): boolean {
12
+ if (cardNumber.length === 0) {
13
+ return false;
14
+ }
15
+
16
+ cardNumber = cardNumber.replace(/\s/g, '');
17
+
18
+ let sum = 0;
19
+ let isAlternate = false;
20
+
21
+ for (let i = cardNumber.length - 1; i >= 0; i--) {
22
+ let digit = parseInt(cardNumber[i], 10);
23
+
24
+ if (isAlternate) {
25
+ digit *= 2;
26
+ if (digit > 9) {
27
+ digit -= 9;
28
+ }
29
+ }
30
+
31
+ sum += digit;
32
+ isAlternate = !isAlternate;
33
+ }
34
+
35
+ return sum % 10 === 0;
36
+ }
37
+
38
+ function getInputValidationDetails(cardBinResponse: any) {
39
+ if (!cardBinResponse || !cardBinResponse.scheme) {
40
+ return null;
41
+ }
42
+
43
+ const schemeType = cardBinResponse.scheme.toLowerCase();
44
+ let inputValidationDetails: { max_input_length: number; cvv_length: number };
45
+ switch (schemeType) {
46
+ case 'amex':
47
+ inputValidationDetails = { max_input_length: 15, cvv_length: 4 };
48
+ break;
49
+ case 'diners':
50
+ inputValidationDetails = { max_input_length: 14, cvv_length: 3 };
51
+ break;
52
+ default: // Covers visa, mastercard, rupay, jcb, discover, and unknown schemes
53
+ inputValidationDetails = { max_input_length: 16, cvv_length: 3 };
54
+ }
55
+ return inputValidationDetails;
56
+ }
57
+
58
+ /**
59
+ * Fetching CardBin info with card bin data & CFSubscriptionSession object
60
+ * @param session : for subs sessionId & env
61
+ * @param bin : for card number
62
+ */
63
+ async function getCardBin(session: CFSubscriptionSession, bin: string) {
64
+ const route: string = `/pg/sdk/js/subscription/card/bin`;
65
+ const body: string = JSON.stringify({ card_number: bin });
66
+ let baseUrl = 'https://api.cashfree.com';
67
+ if (session.environment === CFEnvironment.SANDBOX) {
68
+ baseUrl = 'https://sandbox.cashfree.com';
69
+ }
70
+ const url = baseUrl + route;
71
+ try {
72
+ const response = await fetch(url, {
73
+ method: 'POST',
74
+ headers: {
75
+ 'Content-Type': 'application/json',
76
+ 'x-sub-session-id': session.subscription_session_id,
77
+ },
78
+ body,
79
+ });
80
+ if (!response.ok) {
81
+ return null;
82
+ }
83
+ return await response.json();
84
+ } catch (error) {
85
+ return null;
86
+ }
87
+ }
88
+
89
+ export type SubsCardPaymentHandle = {
90
+ doSubscriptionPayment: (cardInfo: ElementCard) => void;
91
+ doSubscriptionPaymentWithNewSession: (
92
+ cardInfo: ElementCard,
93
+ cfSubscriptionSession: CFSubscriptionSession
94
+ ) => void;
95
+ };
96
+
97
+ export type SubsCardInputProps = {
98
+ cfSubscriptionSession: CFSubscriptionSession;
99
+ cardListener: (response: string) => void;
100
+ } & TextInputProps;
101
+
102
+ const SubsCardInput: any = forwardRef<
103
+ SubsCardPaymentHandle,
104
+ SubsCardInputProps
105
+ >(
106
+ (
107
+ {
108
+ cfSubscriptionSession,
109
+ cardListener,
110
+ style,
111
+ ...props
112
+ }: SubsCardInputProps,
113
+ ref
114
+ ) => {
115
+ const [inputNumber, setInputNumber] = React.useState('');
116
+ const inputNumberRef = React.useRef('');
117
+ const sessionRef = React.useRef(cfSubscriptionSession);
118
+ React.useImperativeHandle(ref, () => ({
119
+ doSubscriptionPayment,
120
+ doSubscriptionPaymentWithNewSession,
121
+ }));
122
+
123
+ const cardBinJsonRef = React.useRef<any>(null);
124
+ const firstEightDigitsRef = React.useRef<string>('');
125
+
126
+ const handleChange = React.useCallback(
127
+ async (cardNumber: string) => {
128
+ let completeResponse: any = {};
129
+ const textWithoutSpaces: string = cardNumber.replaceAll(' ', '');
130
+ if (textWithoutSpaces.length === 0) setInputNumber('');
131
+
132
+ let formattedText: string = '';
133
+ for (let i: number = 0; i < textWithoutSpaces.length; i += 4) {
134
+ let end: number = i + 4;
135
+ if (end > textWithoutSpaces.length) {
136
+ end = textWithoutSpaces.length;
137
+ }
138
+ formattedText += textWithoutSpaces.substring(i, end);
139
+ if (end !== textWithoutSpaces.length) {
140
+ formattedText += ' ';
141
+ }
142
+ inputNumberRef.current = formattedText;
143
+ setInputNumber((prev) =>
144
+ prev === formattedText ? prev : formattedText
145
+ );
146
+ }
147
+
148
+ let cardBinResponse: any = null;
149
+
150
+ async function fetchCardBinAndSet() {
151
+ await getCardBin(cfSubscriptionSession, textWithoutSpaces)
152
+ .then((response: string) => {
153
+ cardBinResponse = response;
154
+ firstEightDigitsRef.current = textWithoutSpaces.substring(0, 8);
155
+ })
156
+ .catch(() => {
157
+ cardBinResponse = null;
158
+ });
159
+
160
+ if (cardBinResponse) {
161
+ cardBinJsonRef.current = cardBinResponse;
162
+ completeResponse.card_bin_info = cardBinJsonRef.current;
163
+ completeResponse.input_validation = getInputValidationDetails(
164
+ cardBinJsonRef.current
165
+ );
166
+ }
167
+ }
168
+
169
+ if (textWithoutSpaces.length === 8) {
170
+ await fetchCardBinAndSet();
171
+ } else if (textWithoutSpaces.length > 8) {
172
+ if (
173
+ firstEightDigitsRef.current === textWithoutSpaces.substring(0, 8)
174
+ ) {
175
+ completeResponse.card_bin_info = cardBinJsonRef.current;
176
+ completeResponse.input_validation = getInputValidationDetails(
177
+ cardBinJsonRef.current
178
+ );
179
+ } else {
180
+ cardBinJsonRef.current = null;
181
+ await fetchCardBinAndSet();
182
+ }
183
+ }
184
+
185
+ if (textWithoutSpaces.length < 8) {
186
+ cardBinJsonRef.current = null;
187
+ firstEightDigitsRef.current = '';
188
+ }
189
+
190
+ if (cardBinJsonRef.current !== null) {
191
+ completeResponse.card_network = cardBinJsonRef.current.scheme;
192
+ }
193
+ let luhnStatus = luhnCheck(textWithoutSpaces);
194
+ if (luhnStatus) {
195
+ completeResponse.luhn_check_info = 'SUCCESS';
196
+ if (textWithoutSpaces && textWithoutSpaces.length > 4) {
197
+ completeResponse.last_four_digit = textWithoutSpaces.substring(
198
+ textWithoutSpaces.length - 4
199
+ );
200
+ }
201
+ } else {
202
+ completeResponse.luhn_check_info = 'FAIL';
203
+ }
204
+ completeResponse.card_length = textWithoutSpaces.length;
205
+
206
+ return cardListener(JSON.stringify(completeResponse));
207
+ },
208
+ [cfSubscriptionSession, cardListener]
209
+ );
210
+
211
+ const doSubscriptionPayment = (cardInfo: ElementCard) => {
212
+ try {
213
+ let cfCardNumber = inputNumberRef.current;
214
+ cardInfo.cardNumber = cfCardNumber.replaceAll(' ', '');
215
+ const cardPayment = new CFSubsCardPayment(sessionRef.current, cardInfo);
216
+ CFPaymentGatewayService.makeSubsPayment(cardPayment);
217
+ } catch (e: any) {
218
+ console.log(e.message);
219
+ }
220
+ };
221
+
222
+ const doSubscriptionPaymentWithNewSession = (
223
+ cardInfo: ElementCard,
224
+ session: CFSubscriptionSession
225
+ ) => {
226
+ try {
227
+ sessionRef.current = session;
228
+ doSubscriptionPayment(cardInfo);
229
+ } catch (e: any) {
230
+ console.log(e.message);
231
+ }
232
+ };
233
+
234
+ const handleSubmitEditingEvent = (event: any) => {
235
+ const newEvent = { ...event };
236
+ delete newEvent.nativeEvent.text;
237
+ if (onSubmitEditing) {
238
+ onSubmitEditing(newEvent);
239
+ }
240
+ };
241
+
242
+ const handleEndEditingEvent = (event: any) => {
243
+ const newEvent = { ...event };
244
+ delete newEvent.nativeEvent.text;
245
+ if (onEndEditing) {
246
+ onEndEditing(newEvent);
247
+ }
248
+ };
249
+
250
+ const handleFocusEvent = (event: any) => {
251
+ const newEvent = { ...event };
252
+ delete newEvent.nativeEvent.text;
253
+ if (onFocus) {
254
+ onFocus(newEvent);
255
+ }
256
+ };
257
+
258
+ const handleBlurEvent = (event: any) => {
259
+ const newEvent = { ...event };
260
+ delete newEvent.nativeEvent.text;
261
+ if (onBlur) {
262
+ onBlur(newEvent);
263
+ }
264
+ };
265
+
266
+ const InputComponent: any = TextInput;
267
+ const {
268
+ onChangeText,
269
+ onChange,
270
+ onSubmitEditing,
271
+ onEndEditing,
272
+ onFocus,
273
+ onBlur,
274
+ ...otherProps
275
+ } = props;
276
+
277
+ return (
278
+ <InputComponent
279
+ keyboardType="numeric"
280
+ inputMode={'numeric'}
281
+ value={inputNumber}
282
+ onChangeText={handleChange}
283
+ onSubmitEditing={handleSubmitEditingEvent}
284
+ onEndEditing={handleEndEditingEvent}
285
+ onFocus={handleFocusEvent}
286
+ onBlur={handleBlurEvent}
287
+ style={style}
288
+ {...otherProps}
289
+ />
290
+ );
291
+ }
292
+ );
293
+
294
+ export default SubsCardInput;
package/src/Card/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export { default } from './CFCardComponent';
2
+ export { default as SubsCardInput } from './CFSubsCardComponent';
package/src/Card/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export { default } from './CFCardComponent';
2
+ export { default as SubsCardInput } from './CFSubsCardComponent';
package/src/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { NativeAppEventEmitter, NativeEventEmitter, NativeModules, Platform, } from 'react-native';
2
2
  import { version } from '../package.json';
3
- import { CFUPIPayment, CFCardPayment, CFSubsUPIPayment, CFSubsCardPayment, CFSubsNBPayment, } from 'cashfree-pg-api-contract';
3
+ import { CFUPIPayment, CFCardPayment, CFNBPayment, CFSubsUPIPayment, CFSubsCardPayment, CFSubsNBPayment, } from 'cashfree-pg-api-contract';
4
4
  import CFCardComponent from './Card/CFCardComponent';
5
+ import CFSubsCardComponent from './Card/CFSubsCardComponent';
5
6
  const LINKING_ERROR = `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` +
6
7
  Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
7
8
  '- You rebuilt the app after installing the package\n' +
@@ -81,6 +82,9 @@ class CFPaymentGateway {
81
82
  else if (cfPayment instanceof CFCardPayment) {
82
83
  CashfreePgApi.doCardPayment(paymentData);
83
84
  }
85
+ else if (cfPayment instanceof CFNBPayment) {
86
+ CashfreePgApi.doElementNBPayment(paymentData);
87
+ }
84
88
  else {
85
89
  console.log('makePayment::==> Wrong payment object');
86
90
  }
@@ -183,4 +187,5 @@ export class CFErrorResponse {
183
187
  }
184
188
  }
185
189
  export const CFCard = CFCardComponent;
190
+ export const CFSubsCard = CFSubsCardComponent;
186
191
  export const CFPaymentGatewayService = new CFPaymentGateway();
package/src/index.ts CHANGED
@@ -12,11 +12,13 @@ import {
12
12
  type CFSubscriptionSession,
13
13
  CFUPIPayment,
14
14
  CFCardPayment,
15
+ CFNBPayment,
15
16
  CFSubsUPIPayment,
16
17
  CFSubsCardPayment,
17
18
  CFSubsNBPayment,
18
19
  } from 'cashfree-pg-api-contract';
19
20
  import CFCardComponent from './Card/CFCardComponent';
21
+ import CFSubsCardComponent from './Card/CFSubsCardComponent';
20
22
 
21
23
  const LINKING_ERROR =
22
24
  `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` +
@@ -109,6 +111,8 @@ class CFPaymentGateway {
109
111
  CashfreePgApi.doElementUPIPayment(paymentData);
110
112
  } else if (cfPayment instanceof CFCardPayment) {
111
113
  CashfreePgApi.doCardPayment(paymentData);
114
+ } else if (cfPayment instanceof CFNBPayment) {
115
+ CashfreePgApi.doElementNBPayment(paymentData);
112
116
  } else {
113
117
  console.log('makePayment::==> Wrong payment object');
114
118
  }
@@ -244,4 +248,5 @@ export class CFErrorResponse {
244
248
  }
245
249
 
246
250
  export const CFCard = CFCardComponent;
251
+ export const CFSubsCard = CFSubsCardComponent;
247
252
  export const CFPaymentGatewayService = new CFPaymentGateway();