react-native-cashfree-pg-sdk 2.3.0 → 2.3.1

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,203 @@
1
+ import { TextInput } from 'react-native';
2
+ import React, { forwardRef } from 'react';
3
+ import { CFSubsCardPayment, CFEnvironment, } from 'cashfree-pg-api-contract';
4
+ import { CFPaymentGatewayService } from '../index';
5
+ function luhnCheck(cardNumber) {
6
+ if (cardNumber.length === 0) {
7
+ return false;
8
+ }
9
+ cardNumber = cardNumber.replace(/\s/g, '');
10
+ let sum = 0;
11
+ let isAlternate = false;
12
+ for (let i = cardNumber.length - 1; i >= 0; i--) {
13
+ let digit = parseInt(cardNumber[i], 10);
14
+ if (isAlternate) {
15
+ digit *= 2;
16
+ if (digit > 9) {
17
+ digit -= 9;
18
+ }
19
+ }
20
+ sum += digit;
21
+ isAlternate = !isAlternate;
22
+ }
23
+ return sum % 10 === 0;
24
+ }
25
+ function getInputValidationDetails(cardBinResponse) {
26
+ if (!cardBinResponse || !cardBinResponse.scheme) {
27
+ return null;
28
+ }
29
+ const schemeType = cardBinResponse.scheme.toLowerCase();
30
+ let inputValidationDetails;
31
+ switch (schemeType) {
32
+ case 'amex':
33
+ inputValidationDetails = { max_input_length: 15, cvv_length: 4 };
34
+ break;
35
+ case 'diners':
36
+ inputValidationDetails = { max_input_length: 14, cvv_length: 3 };
37
+ break;
38
+ default: // Covers visa, mastercard, rupay, jcb, discover, and unknown schemes
39
+ inputValidationDetails = { max_input_length: 16, cvv_length: 3 };
40
+ }
41
+ return inputValidationDetails;
42
+ }
43
+ /**
44
+ * Fetching CardBin info with card bin data & CFSubscriptionSession object
45
+ * @param session : for subs sessionId & env
46
+ * @param bin : for card number
47
+ */
48
+ async function getCardBin(session, bin) {
49
+ const route = `/pg/sdk/js/subscription/card/bin`;
50
+ const body = JSON.stringify({ card_number: bin });
51
+ let baseUrl = 'https://api.cashfree.com';
52
+ if (session.environment === CFEnvironment.SANDBOX) {
53
+ baseUrl = 'https://sandbox.cashfree.com';
54
+ }
55
+ const url = baseUrl + route;
56
+ try {
57
+ const response = await fetch(url, {
58
+ method: 'POST',
59
+ headers: {
60
+ 'Content-Type': 'application/json',
61
+ 'x-sub-session-id': session.subscription_session_id,
62
+ },
63
+ body,
64
+ });
65
+ if (!response.ok) {
66
+ return null;
67
+ }
68
+ return await response.json();
69
+ }
70
+ catch (error) {
71
+ return null;
72
+ }
73
+ }
74
+ const SubsCardInput = forwardRef(({ cfSubscriptionSession, cardListener, style, ...props }, ref) => {
75
+ const [inputNumber, setInputNumber] = React.useState('');
76
+ const inputNumberRef = React.useRef('');
77
+ const sessionRef = React.useRef(cfSubscriptionSession);
78
+ React.useImperativeHandle(ref, () => ({
79
+ doSubscriptionPayment,
80
+ doSubscriptionPaymentWithNewSession,
81
+ }));
82
+ const cardBinJsonRef = React.useRef(null);
83
+ const firstEightDigitsRef = React.useRef('');
84
+ const handleChange = React.useCallback(async (cardNumber) => {
85
+ let completeResponse = {};
86
+ const textWithoutSpaces = cardNumber.replaceAll(' ', '');
87
+ if (textWithoutSpaces.length === 0)
88
+ setInputNumber('');
89
+ let formattedText = '';
90
+ for (let i = 0; i < textWithoutSpaces.length; i += 4) {
91
+ let end = i + 4;
92
+ if (end > textWithoutSpaces.length) {
93
+ end = textWithoutSpaces.length;
94
+ }
95
+ formattedText += textWithoutSpaces.substring(i, end);
96
+ if (end !== textWithoutSpaces.length) {
97
+ formattedText += ' ';
98
+ }
99
+ inputNumberRef.current = formattedText;
100
+ setInputNumber((prev) => prev === formattedText ? prev : formattedText);
101
+ }
102
+ let cardBinResponse = null;
103
+ async function fetchCardBinAndSet() {
104
+ await getCardBin(cfSubscriptionSession, textWithoutSpaces)
105
+ .then((response) => {
106
+ cardBinResponse = response;
107
+ firstEightDigitsRef.current = textWithoutSpaces.substring(0, 8);
108
+ })
109
+ .catch(() => {
110
+ cardBinResponse = null;
111
+ });
112
+ if (cardBinResponse) {
113
+ cardBinJsonRef.current = cardBinResponse;
114
+ completeResponse.card_bin_info = cardBinJsonRef.current;
115
+ completeResponse.input_validation = getInputValidationDetails(cardBinJsonRef.current);
116
+ }
117
+ }
118
+ if (textWithoutSpaces.length === 8) {
119
+ await fetchCardBinAndSet();
120
+ }
121
+ else if (textWithoutSpaces.length > 8) {
122
+ if (firstEightDigitsRef.current === textWithoutSpaces.substring(0, 8)) {
123
+ completeResponse.card_bin_info = cardBinJsonRef.current;
124
+ completeResponse.input_validation = getInputValidationDetails(cardBinJsonRef.current);
125
+ }
126
+ else {
127
+ cardBinJsonRef.current = null;
128
+ await fetchCardBinAndSet();
129
+ }
130
+ }
131
+ if (textWithoutSpaces.length < 8) {
132
+ cardBinJsonRef.current = null;
133
+ firstEightDigitsRef.current = '';
134
+ }
135
+ if (cardBinJsonRef.current !== null) {
136
+ completeResponse.card_network = cardBinJsonRef.current.scheme;
137
+ }
138
+ let luhnStatus = luhnCheck(textWithoutSpaces);
139
+ if (luhnStatus) {
140
+ completeResponse.luhn_check_info = 'SUCCESS';
141
+ if (textWithoutSpaces && textWithoutSpaces.length > 4) {
142
+ completeResponse.last_four_digit = textWithoutSpaces.substring(textWithoutSpaces.length - 4);
143
+ }
144
+ }
145
+ else {
146
+ completeResponse.luhn_check_info = 'FAIL';
147
+ }
148
+ completeResponse.card_length = textWithoutSpaces.length;
149
+ return cardListener(JSON.stringify(completeResponse));
150
+ }, [cfSubscriptionSession, cardListener]);
151
+ const doSubscriptionPayment = (cardInfo) => {
152
+ try {
153
+ let cfCardNumber = inputNumberRef.current;
154
+ cardInfo.cardNumber = cfCardNumber.replaceAll(' ', '');
155
+ const cardPayment = new CFSubsCardPayment(sessionRef.current, cardInfo);
156
+ CFPaymentGatewayService.makeSubsPayment(cardPayment);
157
+ }
158
+ catch (e) {
159
+ console.log(e.message);
160
+ }
161
+ };
162
+ const doSubscriptionPaymentWithNewSession = (cardInfo, session) => {
163
+ try {
164
+ sessionRef.current = session;
165
+ doSubscriptionPayment(cardInfo);
166
+ }
167
+ catch (e) {
168
+ console.log(e.message);
169
+ }
170
+ };
171
+ const handleSubmitEditingEvent = (event) => {
172
+ const newEvent = { ...event };
173
+ delete newEvent.nativeEvent.text;
174
+ if (onSubmitEditing) {
175
+ onSubmitEditing(newEvent);
176
+ }
177
+ };
178
+ const handleEndEditingEvent = (event) => {
179
+ const newEvent = { ...event };
180
+ delete newEvent.nativeEvent.text;
181
+ if (onEndEditing) {
182
+ onEndEditing(newEvent);
183
+ }
184
+ };
185
+ const handleFocusEvent = (event) => {
186
+ const newEvent = { ...event };
187
+ delete newEvent.nativeEvent.text;
188
+ if (onFocus) {
189
+ onFocus(newEvent);
190
+ }
191
+ };
192
+ const handleBlurEvent = (event) => {
193
+ const newEvent = { ...event };
194
+ delete newEvent.nativeEvent.text;
195
+ if (onBlur) {
196
+ onBlur(newEvent);
197
+ }
198
+ };
199
+ const InputComponent = TextInput;
200
+ const { onChangeText, onChange, onSubmitEditing, onEndEditing, onFocus, onBlur, ...otherProps } = props;
201
+ return (React.createElement(InputComponent, { keyboardType: "numeric", inputMode: 'numeric', value: inputNumber, onChangeText: handleChange, onSubmitEditing: handleSubmitEditingEvent, onEndEditing: handleEndEditingEvent, onFocus: handleFocusEvent, onBlur: handleBlurEvent, style: style, ...otherProps }));
202
+ });
203
+ export default SubsCardInput;
@@ -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
@@ -2,6 +2,7 @@ import { NativeAppEventEmitter, NativeEventEmitter, NativeModules, Platform, } f
2
2
  import { version } from '../package.json';
3
3
  import { CFUPIPayment, CFCardPayment, 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' +
@@ -183,4 +184,5 @@ export class CFErrorResponse {
183
184
  }
184
185
  }
185
186
  export const CFCard = CFCardComponent;
187
+ export const CFSubsCard = CFSubsCardComponent;
186
188
  export const CFPaymentGatewayService = new CFPaymentGateway();
package/src/index.ts CHANGED
@@ -17,6 +17,7 @@ import {
17
17
  CFSubsNBPayment,
18
18
  } from 'cashfree-pg-api-contract';
19
19
  import CFCardComponent from './Card/CFCardComponent';
20
+ import CFSubsCardComponent from './Card/CFSubsCardComponent';
20
21
 
21
22
  const LINKING_ERROR =
22
23
  `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` +
@@ -244,4 +245,5 @@ export class CFErrorResponse {
244
245
  }
245
246
 
246
247
  export const CFCard = CFCardComponent;
248
+ export const CFSubsCard = CFSubsCardComponent;
247
249
  export const CFPaymentGatewayService = new CFPaymentGateway();