react-native-cashfree-pg-sdk 2.1.10 → 2.1.12-dev.0

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.
Files changed (38) hide show
  1. package/android/.gradle/6.8/fileChanges/last-build.bin +0 -0
  2. package/android/.gradle/6.8/fileHashes/fileHashes.lock +0 -0
  3. package/android/.gradle/6.8/gc.properties +0 -0
  4. package/android/.gradle/7.2/executionHistory/executionHistory.bin +0 -0
  5. package/android/.gradle/7.2/executionHistory/executionHistory.lock +0 -0
  6. package/android/.gradle/7.2/fileHashes/fileHashes.bin +0 -0
  7. package/android/.gradle/7.2/fileHashes/fileHashes.lock +0 -0
  8. package/android/.gradle/7.2/fileHashes/resourceHashesCache.bin +0 -0
  9. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  10. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  11. package/android/.gradle/checksums/checksums.lock +0 -0
  12. package/android/.gradle/checksums/md5-checksums.bin +0 -0
  13. package/android/.gradle/checksums/sha1-checksums.bin +0 -0
  14. package/android/.idea/jarRepositories.xml +5 -0
  15. package/android/.idea/sonarlint/issuestore/4/4/44aaf172f11159c609e569cf1b49bcd49376a770 +1 -15
  16. package/android/build.gradle +1 -1
  17. package/lib/commonjs/Card/CFCardComponent.js +251 -0
  18. package/lib/commonjs/Card/CFCardComponent.js.map +1 -0
  19. package/lib/commonjs/Card/index.js +14 -0
  20. package/lib/commonjs/Card/index.js.map +1 -0
  21. package/lib/commonjs/index.js +5 -1
  22. package/lib/commonjs/index.js.map +1 -1
  23. package/lib/module/Card/CFCardComponent.js +234 -0
  24. package/lib/module/Card/CFCardComponent.js.map +1 -0
  25. package/lib/module/Card/index.js +2 -0
  26. package/lib/module/Card/index.js.map +1 -0
  27. package/lib/module/index.js +2 -1
  28. package/lib/module/index.js.map +1 -1
  29. package/lib/typescript/src/Card/CFCardComponent.d.ts +8 -0
  30. package/lib/typescript/src/Card/index.d.ts +1 -0
  31. package/lib/typescript/src/index.d.ts +1 -0
  32. package/package.json +1 -1
  33. package/src/Card/CFCardComponent.js +199 -0
  34. package/src/Card/CFCardComponent.tsx +268 -0
  35. package/src/Card/index.js +1 -0
  36. package/src/Card/index.ts +1 -0
  37. package/src/index.js +2 -0
  38. package/src/index.ts +2 -0
@@ -0,0 +1,268 @@
1
+ import { TextInput, TextInputProps } from 'react-native';
2
+ import React, { forwardRef } from 'react';
3
+ import { Card, CFCardPayment, CFEnvironment, type CFSession } 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 CardInputProps = {
80
+ cfSession: CFSession;
81
+ cardListener: (response: string) => void;
82
+ } & TextInputProps;
83
+
84
+ const CardInput: any = forwardRef(({
85
+ cfSession,
86
+ cardListener,
87
+ style,
88
+ ...props
89
+ }: CardInputProps, ref) => {
90
+
91
+ const [inputNumber, setInputNumber] = React.useState('');
92
+ React.useImperativeHandle(ref, () => ({ completePayment }));
93
+ let tdrJson: any = null;
94
+ let cardBinJson: any = null;
95
+ let firstEightDigits: string = '';
96
+
97
+ const handleChange = React.useCallback(
98
+ async (cardNumber: string) => {
99
+ let completeResponse: any = {};
100
+ const textWithoutSpaces: string = cardNumber.replaceAll(' ', '');
101
+ if (textWithoutSpaces.length === 0) setInputNumber('');
102
+
103
+ let formattedText: string = '';
104
+ /**
105
+ * Code to format card input number & set to input box
106
+ */
107
+ for (let i: number = 0; i < textWithoutSpaces.length; i += 4) {
108
+ let end: number = i + 4;
109
+ if (end > textWithoutSpaces.length) {
110
+ end = textWithoutSpaces.length;
111
+ }
112
+ formattedText += textWithoutSpaces.substring(i, end);
113
+ if (end !== textWithoutSpaces.length) {
114
+ formattedText += ' ';
115
+ }
116
+ setInputNumber(formattedText);
117
+ }
118
+
119
+ let tdrResponse: any = null;
120
+ let cardBinResponse: any = null;
121
+
122
+ /**
123
+ * Fetch Tdr & CardBin data & set to local variable
124
+ */
125
+ async function fetchDataAndSet() {
126
+ await getTDR(cfSession, textWithoutSpaces)
127
+ .then((response: string) => {
128
+ tdrResponse = response;
129
+ })
130
+ .catch(() => {
131
+ tdrResponse = null;
132
+ });
133
+
134
+ await getCardBin(cfSession, textWithoutSpaces)
135
+ .then((response: string) => {
136
+ cardBinResponse = response;
137
+ })
138
+ .catch(() => {
139
+ cardBinResponse = null;
140
+ });
141
+
142
+ if (tdrResponse) {
143
+ tdrJson = tdrResponse;
144
+ completeResponse['tdr_info'] = tdrJson;
145
+ }
146
+
147
+ if (cardBinResponse) {
148
+ cardBinJson = cardBinResponse;
149
+ completeResponse['card_bin_info'] = cardBinJson;
150
+ }
151
+ }
152
+
153
+ if (textWithoutSpaces.length === 8) {
154
+ firstEightDigits = textWithoutSpaces;
155
+ await fetchDataAndSet();
156
+ } else if (textWithoutSpaces.length > 8) {
157
+ if (firstEightDigits === textWithoutSpaces.substring(0, 8)) {
158
+ completeResponse['tdr_info'] = tdrJson;
159
+ completeResponse['card_bin_info'] = cardBinJson;
160
+ } else {
161
+ firstEightDigits = textWithoutSpaces.substring(0, 8);
162
+ tdrJson = null;
163
+ cardBinJson = null;
164
+ await fetchDataAndSet();
165
+ }
166
+ }
167
+
168
+ if (textWithoutSpaces.length < 8) {
169
+ cardBinJson = null;
170
+ tdrJson = null;
171
+ firstEightDigits = '';
172
+ }
173
+
174
+ if (cardBinJson !== null) {
175
+ completeResponse['card_network'] = cardBinJson['scheme'];
176
+ }
177
+ completeResponse['luhn_check_info'] = 'SUCCESS';
178
+ if (!luhnCheck(textWithoutSpaces)) {
179
+ completeResponse['luhn_check_info'] = 'FAIL';
180
+ }
181
+ completeResponse['card_length'] = textWithoutSpaces.length;
182
+
183
+ return cardListener(JSON.stringify(completeResponse));
184
+ },
185
+ [cardListener],
186
+ );
187
+
188
+ const completePayment = (otherCardInfo: string) => {
189
+ try {
190
+ let cardInfo = JSON.parse(otherCardInfo);
191
+ const cardNumber: string = inputNumber.replaceAll(' ', '');
192
+ const card = new Card(cardNumber,
193
+ cardInfo['cardHolderName'],
194
+ cardInfo['cardExpiryMM'],
195
+ cardInfo['cardExpiryYY'],
196
+ cardInfo['cardCVV'],
197
+ cardInfo['toggleCheckBox'] ? cardInfo['toggleCheckBox'] : false,
198
+ );
199
+
200
+ console.log('Card', JSON.stringify(card));
201
+ const cardPayment = new CFCardPayment(cfSession, card);
202
+ CFPaymentGatewayService.makePayment(cardPayment);
203
+ } catch (e: any) {
204
+ console.log(e.message);
205
+ }
206
+ };
207
+
208
+ const handleSubmitEditingEvent = (event: any) => {
209
+ const newEvent = { ...event };
210
+ delete newEvent.nativeEvent.text;
211
+ if (onSubmitEditing) {
212
+ onSubmitEditing(newEvent);
213
+ }
214
+ };
215
+
216
+ const handleEndEditingEvent = (event: any) => {
217
+ const newEvent = { ...event };
218
+ delete newEvent.nativeEvent.text;
219
+ if (onEndEditing) {
220
+ onEndEditing(newEvent);
221
+ }
222
+ };
223
+
224
+ const handleFocusEvent = (event: any) => {
225
+ const newEvent = { ...event };
226
+ delete newEvent.nativeEvent.text;
227
+ if (onFocus) {
228
+ onFocus(newEvent);
229
+ }
230
+ };
231
+
232
+ const handleBlurEvent = (event: any) => {
233
+ const newEvent = { ...event };
234
+ delete newEvent.nativeEvent.text;
235
+ if (onBlur) {
236
+ onBlur(newEvent);
237
+ }
238
+ };
239
+
240
+ const InputComponent: any = TextInput;
241
+ const {
242
+ onChangeText,
243
+ onChange,
244
+ onSubmitEditing,
245
+ onEndEditing,
246
+ onFocus,
247
+ onBlur,
248
+ ...otherProps
249
+ } = props;
250
+
251
+
252
+ return (
253
+ <InputComponent
254
+ keyboardType='numeric'
255
+ inputMode={'numeric'}
256
+ value={inputNumber}
257
+ onChangeText={handleChange}
258
+ onSubmitEditing={handleSubmitEditingEvent}
259
+ onEndEditing={handleEndEditingEvent}
260
+ onFocus={handleFocusEvent}
261
+ onBlur={handleBlurEvent}
262
+ style={style}
263
+ {...otherProps}
264
+ />
265
+ );
266
+ });
267
+
268
+ export default CardInput;
@@ -0,0 +1 @@
1
+ export { default } from './CFCardComponent';
@@ -0,0 +1 @@
1
+ export { default } from './CFCardComponent';
package/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { NativeAppEventEmitter, NativeEventEmitter, NativeModules, Platform, } from 'react-native';
2
2
  import { version } from '../package.json';
3
3
  import { CFUPIPayment, CFCardPayment } from 'cashfree-pg-api-contract';
4
+ import CFCardComponent from './Card/CFCardComponent';
4
5
  const LINKING_ERROR = `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` +
5
6
  Platform.select({ ios: '- You have run \'pod install\'\n', default: '' }) +
6
7
  '- You rebuilt the app after installing the package\n' +
@@ -162,4 +163,5 @@ export class CFErrorResponse {
162
163
  return this.type;
163
164
  }
164
165
  }
166
+ export const CFCard = CFCardComponent;
165
167
  export const CFPaymentGatewayService = new CFPaymentGateway();
package/src/index.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  } from 'react-native';
9
9
  import { version } from '../package.json';
10
10
  import { type CheckoutPayment, type CFSession, CFUPIPayment, CFCardPayment } from 'cashfree-pg-api-contract';
11
+ import CFCardComponent from './Card/CFCardComponent';
11
12
 
12
13
  const LINKING_ERROR =
13
14
  `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` +
@@ -214,4 +215,5 @@ export class CFErrorResponse {
214
215
  }
215
216
  }
216
217
 
218
+ export const CFCard = CFCardComponent
217
219
  export const CFPaymentGatewayService = new CFPaymentGateway();