react-native-cashfree-pg-sdk 2.5.0 → 3.0.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 (42) hide show
  1. package/README.md +36 -16
  2. package/android/build.gradle +5 -3
  3. package/android/src/main/java/com/reactnativecashfreepgsdk/CashfreePgApiModule.java +75 -27
  4. package/android/src/main/java/com/reactnativecashfreepgsdk/CashfreePgApiPackage.java +32 -18
  5. package/ios/CashfreeEmitter.swift +11 -11
  6. package/ios/CashfreePgApi.swift +35 -19
  7. package/ios/CashfreePgApiAdapter.h +15 -0
  8. package/ios/CashfreePgApiAdapter.mm +94 -0
  9. package/lib/commonjs/Card/CFCardComponent.js +4 -2
  10. package/lib/commonjs/Card/CFCardComponent.js.map +1 -1
  11. package/lib/commonjs/Card/CFSubsCardComponent.js +4 -2
  12. package/lib/commonjs/Card/CFSubsCardComponent.js.map +1 -1
  13. package/lib/commonjs/NativeCashfreePgApi.js +9 -0
  14. package/lib/commonjs/NativeCashfreePgApi.js.map +1 -0
  15. package/lib/commonjs/index.js +18 -50
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/module/Card/CFCardComponent.js +12 -6
  18. package/lib/module/Card/CFCardComponent.js.map +1 -1
  19. package/lib/module/Card/CFSubsCardComponent.js +4 -2
  20. package/lib/module/Card/CFSubsCardComponent.js.map +1 -1
  21. package/lib/module/NativeCashfreePgApi.js +3 -0
  22. package/lib/module/NativeCashfreePgApi.js.map +1 -0
  23. package/lib/module/PPIWallet/PPIWalletPayment.js.map +1 -1
  24. package/lib/module/index.js +4 -38
  25. package/lib/module/index.js.map +1 -1
  26. package/lib/typescript/src/NativeCashfreePgApi.d.ts +21 -0
  27. package/lib/typescript/src/index.d.ts +1 -2
  28. package/package.json +31 -9
  29. package/react-native-cashfree-pg-sdk.podspec +5 -2
  30. package/src/Card/CFCardComponent.tsx +4 -2
  31. package/src/Card/CFSubsCardComponent.tsx +4 -2
  32. package/src/NativeCashfreePgApi.ts +23 -0
  33. package/src/index.ts +5 -59
  34. package/ios/CashfreeEventEmitter.m +0 -16
  35. package/ios/CashfreeEventEmitter.swift +0 -28
  36. package/ios/CashfreePgApi.m +0 -26
  37. package/src/CFErrorResponse.js +0 -35
  38. package/src/Card/CFCardComponent.js +0 -238
  39. package/src/Card/CFSubsCardComponent.js +0 -203
  40. package/src/Card/index.js +0 -2
  41. package/src/PPIWallet/PPIWalletPayment.js +0 -94
  42. package/src/index.js +0 -174
@@ -1,238 +0,0 @@
1
- import { TextInput } from 'react-native';
2
- import React, { forwardRef } from 'react';
3
- import { CFCardPayment, 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, ''); // Remove spaces
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
- let 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 Tdr info with card bin data & cfSession object
45
- * @param session : for payment sessionId & env
46
- * @param bin : for card number
47
- */
48
- async function getTDR(session, bin) {
49
- const route = `/pg/sdk/js/${session.payment_session_id}/v2/tdr`;
50
- const body = JSON.stringify({ code: bin, code_type: 'bin' });
51
- return await getInfo(session.environment, route, body);
52
- }
53
- /**
54
- * Fetching CardBin info with card bin data & cfSession object
55
- * @param session : for payment sessionId & env
56
- * @param bin : for card number
57
- */
58
- async function getCardBin(session, bin) {
59
- const route = `/pg/sdk/js/${session.payment_session_id}/cardBin`;
60
- const body = JSON.stringify({ card_number: bin });
61
- return await getInfo(session.environment, route, body);
62
- }
63
- async function getInfo(env, route, bodyData) {
64
- let baseUrl = 'https://api.cashfree.com';
65
- if (env === CFEnvironment.SANDBOX) {
66
- baseUrl = 'https://sandbox.cashfree.com';
67
- }
68
- const url = baseUrl + route;
69
- try {
70
- const response = await fetch(url, {
71
- method: 'POST',
72
- headers: {
73
- 'Content-Type': 'application/json',
74
- },
75
- body: bodyData,
76
- });
77
- if (!response.ok) {
78
- return null;
79
- }
80
- return await response.json();
81
- }
82
- catch (error) {
83
- return null;
84
- }
85
- }
86
- const CardInput = forwardRef(({ cfSession, cardListener, style, ...props }, ref) => {
87
- const [inputNumber, setInputNumber] = React.useState('');
88
- const inputNumberRef = React.useRef('');
89
- const sessionRef = React.useRef(cfSession);
90
- React.useImperativeHandle(ref, () => ({
91
- doPayment,
92
- doPaymentWithPaymentSessionId,
93
- }));
94
- const tdrJsonRef = React.useRef(null);
95
- const cardBinJsonRef = React.useRef(null);
96
- const firstEightDigitsRef = React.useRef('');
97
- const handleChange = React.useCallback(async (cardNumber) => {
98
- let completeResponse = {};
99
- const textWithoutSpaces = cardNumber.replaceAll(' ', '');
100
- if (textWithoutSpaces.length === 0)
101
- setInputNumber('');
102
- let formattedText = '';
103
- /**
104
- * Code to format card input number & set to input box
105
- */
106
- for (let i = 0; i < textWithoutSpaces.length; i += 4) {
107
- let end = i + 4;
108
- if (end > textWithoutSpaces.length) {
109
- end = textWithoutSpaces.length;
110
- }
111
- formattedText += textWithoutSpaces.substring(i, end);
112
- if (end !== textWithoutSpaces.length) {
113
- formattedText += ' ';
114
- }
115
- inputNumberRef.current = formattedText;
116
- setInputNumber((prev) => prev === formattedText ? prev : formattedText);
117
- }
118
- let tdrResponse = null;
119
- let cardBinResponse = null;
120
- /**
121
- * Fetch Tdr & CardBin data & set to local variable
122
- */
123
- async function fetchDataAndSet() {
124
- await getTDR(cfSession, textWithoutSpaces)
125
- .then((response) => {
126
- tdrResponse = response;
127
- firstEightDigitsRef.current = textWithoutSpaces.substring(0, 8);
128
- })
129
- .catch(() => {
130
- tdrResponse = null;
131
- });
132
- await getCardBin(cfSession, textWithoutSpaces)
133
- .then((response) => {
134
- cardBinResponse = response;
135
- firstEightDigitsRef.current = textWithoutSpaces.substring(0, 8);
136
- })
137
- .catch(() => {
138
- cardBinResponse = null;
139
- });
140
- if (tdrResponse) {
141
- tdrJsonRef.current = tdrResponse;
142
- completeResponse.tdr_info = tdrJsonRef.current;
143
- }
144
- if (cardBinResponse) {
145
- cardBinJsonRef.current = cardBinResponse;
146
- completeResponse.card_bin_info = cardBinJsonRef.current;
147
- completeResponse.input_validation = getInputValidationDetails(cardBinJsonRef.current);
148
- }
149
- }
150
- if (textWithoutSpaces.length === 8) {
151
- await fetchDataAndSet();
152
- }
153
- else if (textWithoutSpaces.length > 8) {
154
- if (firstEightDigitsRef.current === textWithoutSpaces.substring(0, 8)) {
155
- completeResponse.tdr_info = tdrJsonRef.current;
156
- completeResponse.card_bin_info = cardBinJsonRef.current;
157
- completeResponse.input_validation = getInputValidationDetails(cardBinJsonRef.current);
158
- }
159
- else {
160
- tdrJsonRef.current = null;
161
- cardBinJsonRef.current = null;
162
- await fetchDataAndSet();
163
- }
164
- }
165
- if (textWithoutSpaces.length < 8) {
166
- tdrJsonRef.current = null;
167
- cardBinJsonRef.current = null;
168
- firstEightDigitsRef.current = '';
169
- }
170
- if (cardBinJsonRef.current !== null) {
171
- completeResponse.card_network = cardBinJsonRef.current.scheme;
172
- }
173
- let luhnStatus = luhnCheck(textWithoutSpaces);
174
- if (luhnStatus) {
175
- completeResponse.luhn_check_info = 'SUCCESS';
176
- if (textWithoutSpaces && textWithoutSpaces.length > 4) {
177
- completeResponse.last_four_digit = textWithoutSpaces.substring(textWithoutSpaces.length - 4);
178
- }
179
- }
180
- else {
181
- completeResponse.luhn_check_info = 'FAIL';
182
- }
183
- completeResponse.card_length = textWithoutSpaces.length;
184
- return cardListener(JSON.stringify(completeResponse));
185
- }, [cfSession, cardListener]);
186
- const doPayment = (cardInfo) => {
187
- try {
188
- let cfCardNumber = inputNumberRef.current;
189
- cardInfo.cardNumber = cfCardNumber.replaceAll(' ', '');
190
- const cardPayment = new CFCardPayment(sessionRef.current, cardInfo);
191
- CFPaymentGatewayService.makePayment(cardPayment);
192
- }
193
- catch (e) {
194
- console.log(e.message);
195
- }
196
- };
197
- const doPaymentWithPaymentSessionId = (cardInfo, session) => {
198
- try {
199
- sessionRef.current = session;
200
- doPayment(cardInfo);
201
- }
202
- catch (e) {
203
- console.log(e.message);
204
- }
205
- };
206
- const handleSubmitEditingEvent = (event) => {
207
- const newEvent = { ...event };
208
- delete newEvent.nativeEvent.text;
209
- if (onSubmitEditing) {
210
- onSubmitEditing(newEvent);
211
- }
212
- };
213
- const handleEndEditingEvent = (event) => {
214
- const newEvent = { ...event };
215
- delete newEvent.nativeEvent.text;
216
- if (onEndEditing) {
217
- onEndEditing(newEvent);
218
- }
219
- };
220
- const handleFocusEvent = (event) => {
221
- const newEvent = { ...event };
222
- delete newEvent.nativeEvent.text;
223
- if (onFocus) {
224
- onFocus(newEvent);
225
- }
226
- };
227
- const handleBlurEvent = (event) => {
228
- const newEvent = { ...event };
229
- delete newEvent.nativeEvent.text;
230
- if (onBlur) {
231
- onBlur(newEvent);
232
- }
233
- };
234
- const InputComponent = TextInput;
235
- const { onChangeText, onChange, onSubmitEditing, onEndEditing, onFocus, onBlur, ...otherProps } = props;
236
- return (React.createElement(InputComponent, { keyboardType: "numeric", inputMode: 'numeric', value: inputNumber, onChangeText: handleChange, onSubmitEditing: handleSubmitEditingEvent, onEndEditing: handleEndEditingEvent, onFocus: handleFocusEvent, onBlur: handleBlurEvent, style: style, ...otherProps }));
237
- });
238
- export default CardInput;
@@ -1,203 +0,0 @@
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;
package/src/Card/index.js DELETED
@@ -1,2 +0,0 @@
1
- export { default } from './CFCardComponent';
2
- export { default as SubsCardInput } from './CFSubsCardComponent';
@@ -1,94 +0,0 @@
1
- import { CFEnvironment, CFPPIWallet, } from 'cashfree-pg-api-contract';
2
- import { CFErrorResponse } from '../CFErrorResponse';
3
- const ROUTE = '/pg/orders/sessions';
4
- const BASE_URLS = {
5
- [CFEnvironment.SANDBOX]: 'https://sandbox.cashfree.com',
6
- [CFEnvironment.PRODUCTION]: 'https://api.cashfree.com',
7
- };
8
- const failure = (message, code, type = code) => new CFErrorResponse({ message, code, type });
9
- const validationError = (message) => failure(message, 'validation_error');
10
- const stringField = (body, key) => {
11
- if (body !== null && typeof body === 'object') {
12
- const value = body[key];
13
- if (typeof value === 'string' && value !== '') {
14
- return value;
15
- }
16
- }
17
- return undefined;
18
- };
19
- const parseJson = (text) => {
20
- try {
21
- return JSON.parse(text);
22
- }
23
- catch {
24
- return undefined;
25
- }
26
- };
27
- // Validation is repeated here (on top of the contract constructors) because
28
- // JS callers can mutate or hand-build these objects.
29
- function buildRequest(payment) {
30
- if (payment == null ||
31
- typeof payment.getSession !== 'function' ||
32
- typeof payment.getWallet !== 'function') {
33
- throw validationError('payment cannot be empty.');
34
- }
35
- const session = payment.getSession();
36
- if (session == null ||
37
- typeof session.payment_session_id !== 'string' ||
38
- session.payment_session_id.trim() === '') {
39
- throw validationError('payment_session_id cannot be empty.');
40
- }
41
- const baseUrl = BASE_URLS[session.environment];
42
- if (baseUrl === undefined) {
43
- throw validationError('environment is invalid.');
44
- }
45
- let wallet;
46
- try {
47
- wallet = new CFPPIWallet(payment.getWallet());
48
- }
49
- catch (error) {
50
- throw validationError(error.message);
51
- }
52
- return {
53
- url: baseUrl + ROUTE,
54
- body: JSON.stringify({
55
- payment_session_id: session.payment_session_id,
56
- payment_method: {
57
- app: {
58
- provider: wallet.provider,
59
- channel: wallet.channel,
60
- phone: wallet.phone,
61
- wallet_id: wallet.walletId,
62
- user_id: wallet.userId,
63
- cf_sub_wallet_id: wallet.cfSubWalletId,
64
- },
65
- },
66
- }),
67
- };
68
- }
69
- /**
70
- * Creates a PPI wallet order session. Resolves with the API JSON as-is,
71
- * rejects with CFErrorResponse. Request/response carry PII: never log them.
72
- */
73
- export async function doPPIWalletPayment(payment) {
74
- const { url, body: requestBody } = buildRequest(payment);
75
- let response;
76
- let text;
77
- try {
78
- response = await fetch(url, {
79
- method: 'POST',
80
- headers: { 'Content-Type': 'application/json' },
81
- body: requestBody,
82
- });
83
- text = await response.text();
84
- }
85
- catch (error) {
86
- throw failure(error?.message || 'Network request failed', 'network_error');
87
- }
88
- const responseBody = parseJson(text);
89
- if (response.ok && responseBody !== undefined) {
90
- return responseBody;
91
- }
92
- throw failure(stringField(responseBody, 'message') ??
93
- 'Something went wrong. Please try again.', stringField(responseBody, 'code') ?? 'request_failed', stringField(responseBody, 'type') ?? 'api_error');
94
- }