react-native-cashfree-pg-sdk 2.4.0 → 2.5.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.
- package/android/.project +6 -0
- package/android/build.gradle +1 -1
- package/ios/CashfreePgApi.swift +1 -1
- package/lib/commonjs/CFErrorResponse.js +43 -0
- package/lib/commonjs/CFErrorResponse.js.map +1 -0
- package/lib/commonjs/Card/CFCardComponent.js +8 -4
- package/lib/commonjs/Card/CFCardComponent.js.map +1 -1
- package/lib/commonjs/Card/CFSubsCardComponent.js +8 -0
- package/lib/commonjs/Card/index.js.map +1 -1
- package/lib/commonjs/PPIWallet/PPIWalletPayment.js +104 -0
- package/lib/commonjs/PPIWallet/PPIWalletPayment.js.map +1 -0
- package/lib/commonjs/index.js +21 -33
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/CFErrorResponse.js +36 -0
- package/lib/module/CFErrorResponse.js.map +1 -0
- package/lib/module/Card/CFCardComponent.js +9 -0
- package/lib/module/Card/CFSubsCardComponent.js +11 -3
- package/lib/module/Card/CFSubsCardComponent.js.map +1 -1
- package/lib/module/PPIWallet/PPIWalletPayment.js +98 -0
- package/lib/module/PPIWallet/PPIWalletPayment.js.map +1 -0
- package/lib/module/index.js +13 -30
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/CFErrorResponse.d.ts +16 -0
- package/lib/typescript/src/PPIWallet/PPIWalletPayment.d.ts +6 -0
- package/lib/typescript/src/index.d.ts +8 -13
- package/package.json +2 -2
- package/react-native-cashfree-pg-sdk.podspec +1 -1
- package/src/CFErrorResponse.js +35 -0
- package/src/CFErrorResponse.ts +38 -0
- package/src/PPIWallet/PPIWalletPayment.js +94 -0
- package/src/PPIWallet/PPIWalletPayment.ts +120 -0
- package/src/index.js +10 -27
- package/src/index.ts +12 -33
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CFEnvironment,
|
|
3
|
+
CFPPIWallet,
|
|
4
|
+
type CFPPIWalletPayment,
|
|
5
|
+
type CFSession,
|
|
6
|
+
} from 'cashfree-pg-api-contract';
|
|
7
|
+
import { CFErrorResponse } from '../CFErrorResponse';
|
|
8
|
+
|
|
9
|
+
const ROUTE = '/pg/orders/sessions';
|
|
10
|
+
const BASE_URLS: Record<string, string> = {
|
|
11
|
+
[CFEnvironment.SANDBOX]: 'https://sandbox.cashfree.com',
|
|
12
|
+
[CFEnvironment.PRODUCTION]: 'https://api.cashfree.com',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const failure = (message: string, code: string, type: string = code) =>
|
|
16
|
+
new CFErrorResponse({ message, code, type });
|
|
17
|
+
|
|
18
|
+
const validationError = (message: string) =>
|
|
19
|
+
failure(message, 'validation_error');
|
|
20
|
+
|
|
21
|
+
const stringField = (body: unknown, key: string): string | undefined => {
|
|
22
|
+
if (body !== null && typeof body === 'object') {
|
|
23
|
+
const value = (body as Record<string, unknown>)[key];
|
|
24
|
+
if (typeof value === 'string' && value !== '') {
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const parseJson = (text: string): unknown => {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(text);
|
|
34
|
+
} catch {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Validation is repeated here (on top of the contract constructors) because
|
|
40
|
+
// JS callers can mutate or hand-build these objects.
|
|
41
|
+
function buildRequest(payment: CFPPIWalletPayment) {
|
|
42
|
+
if (
|
|
43
|
+
payment == null ||
|
|
44
|
+
typeof payment.getSession !== 'function' ||
|
|
45
|
+
typeof payment.getWallet !== 'function'
|
|
46
|
+
) {
|
|
47
|
+
throw validationError('payment cannot be empty.');
|
|
48
|
+
}
|
|
49
|
+
const session: CFSession = payment.getSession();
|
|
50
|
+
if (
|
|
51
|
+
session == null ||
|
|
52
|
+
typeof session.payment_session_id !== 'string' ||
|
|
53
|
+
session.payment_session_id.trim() === ''
|
|
54
|
+
) {
|
|
55
|
+
throw validationError('payment_session_id cannot be empty.');
|
|
56
|
+
}
|
|
57
|
+
const baseUrl = BASE_URLS[session.environment];
|
|
58
|
+
if (baseUrl === undefined) {
|
|
59
|
+
throw validationError('environment is invalid.');
|
|
60
|
+
}
|
|
61
|
+
let wallet: CFPPIWallet;
|
|
62
|
+
try {
|
|
63
|
+
wallet = new CFPPIWallet(payment.getWallet());
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw validationError((error as Error).message);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
url: baseUrl + ROUTE,
|
|
69
|
+
body: JSON.stringify({
|
|
70
|
+
payment_session_id: session.payment_session_id,
|
|
71
|
+
payment_method: {
|
|
72
|
+
app: {
|
|
73
|
+
provider: wallet.provider,
|
|
74
|
+
channel: wallet.channel,
|
|
75
|
+
phone: wallet.phone,
|
|
76
|
+
wallet_id: wallet.walletId,
|
|
77
|
+
user_id: wallet.userId,
|
|
78
|
+
cf_sub_wallet_id: wallet.cfSubWalletId,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Creates a PPI wallet order session. Resolves with the API JSON as-is,
|
|
87
|
+
* rejects with CFErrorResponse. Request/response carry PII: never log them.
|
|
88
|
+
*/
|
|
89
|
+
export async function doPPIWalletPayment(
|
|
90
|
+
payment: CFPPIWalletPayment
|
|
91
|
+
): Promise<unknown> {
|
|
92
|
+
const { url, body: requestBody } = buildRequest(payment);
|
|
93
|
+
|
|
94
|
+
let response: Response;
|
|
95
|
+
let text: string;
|
|
96
|
+
try {
|
|
97
|
+
response = await fetch(url, {
|
|
98
|
+
method: 'POST',
|
|
99
|
+
headers: { 'Content-Type': 'application/json' },
|
|
100
|
+
body: requestBody,
|
|
101
|
+
});
|
|
102
|
+
text = await response.text();
|
|
103
|
+
} catch (error) {
|
|
104
|
+
throw failure(
|
|
105
|
+
(error as Error)?.message || 'Network request failed',
|
|
106
|
+
'network_error'
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const responseBody = parseJson(text);
|
|
111
|
+
if (response.ok && responseBody !== undefined) {
|
|
112
|
+
return responseBody;
|
|
113
|
+
}
|
|
114
|
+
throw failure(
|
|
115
|
+
stringField(responseBody, 'message') ??
|
|
116
|
+
'Something went wrong. Please try again.',
|
|
117
|
+
stringField(responseBody, 'code') ?? 'request_failed',
|
|
118
|
+
stringField(responseBody, 'type') ?? 'api_error'
|
|
119
|
+
);
|
|
120
|
+
}
|
package/src/index.js
CHANGED
|
@@ -3,6 +3,8 @@ import { version } from '../package.json';
|
|
|
3
3
|
import { CFUPIPayment, CFCardPayment, CFNBPayment, CFSubsUPIPayment, CFSubsCardPayment, CFSubsNBPayment, } from 'cashfree-pg-api-contract';
|
|
4
4
|
import CFCardComponent from './Card/CFCardComponent';
|
|
5
5
|
import CFSubsCardComponent from './Card/CFSubsCardComponent';
|
|
6
|
+
import { CFErrorResponse } from './CFErrorResponse';
|
|
7
|
+
import { doPPIWalletPayment } from './PPIWallet/PPIWalletPayment';
|
|
6
8
|
const LINKING_ERROR = `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` +
|
|
7
9
|
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
|
|
8
10
|
'- You rebuilt the app after installing the package\n' +
|
|
@@ -105,6 +107,13 @@ class CFPaymentGateway {
|
|
|
105
107
|
console.log('makePayment::==> Wrong payment object');
|
|
106
108
|
}
|
|
107
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Creates a PPI wallet order session via Cashfree's API.
|
|
112
|
+
* Resolves with the API response as-is; rejects with CFErrorResponse.
|
|
113
|
+
*/
|
|
114
|
+
doPPIWalletPayment(payment) {
|
|
115
|
+
return doPPIWalletPayment(payment);
|
|
116
|
+
}
|
|
108
117
|
setEventSubscriber(cfEventCallback) {
|
|
109
118
|
let eventFunction = (event) => {
|
|
110
119
|
console.log(JSON.stringify(event));
|
|
@@ -159,33 +168,7 @@ class CFPaymentGateway {
|
|
|
159
168
|
}
|
|
160
169
|
}
|
|
161
170
|
}
|
|
162
|
-
export
|
|
163
|
-
status = 'FAILED';
|
|
164
|
-
message = 'payment has failed';
|
|
165
|
-
code = 'payment_failed';
|
|
166
|
-
type = 'request_failed';
|
|
167
|
-
fromJSON(errorString) {
|
|
168
|
-
console.log('errorString :' + errorString);
|
|
169
|
-
const object = JSON.parse(errorString);
|
|
170
|
-
console.log('errorStringObject :' + object);
|
|
171
|
-
this.status = object.status;
|
|
172
|
-
this.message = object.message;
|
|
173
|
-
this.code = object.code;
|
|
174
|
-
this.type = object.type;
|
|
175
|
-
}
|
|
176
|
-
getStatus() {
|
|
177
|
-
return this.status;
|
|
178
|
-
}
|
|
179
|
-
getMessage() {
|
|
180
|
-
return this.message;
|
|
181
|
-
}
|
|
182
|
-
getCode() {
|
|
183
|
-
return this.code;
|
|
184
|
-
}
|
|
185
|
-
getType() {
|
|
186
|
-
return this.type;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
171
|
+
export { CFErrorResponse };
|
|
189
172
|
export const CFCard = CFCardComponent;
|
|
190
173
|
export const CFSubsCard = CFSubsCardComponent;
|
|
191
174
|
export const CFPaymentGatewayService = new CFPaymentGateway();
|
package/src/index.ts
CHANGED
|
@@ -16,9 +16,12 @@ import {
|
|
|
16
16
|
CFSubsUPIPayment,
|
|
17
17
|
CFSubsCardPayment,
|
|
18
18
|
CFSubsNBPayment,
|
|
19
|
+
type CFPPIWalletPayment,
|
|
19
20
|
} from 'cashfree-pg-api-contract';
|
|
20
21
|
import CFCardComponent from './Card/CFCardComponent';
|
|
21
22
|
import CFSubsCardComponent from './Card/CFSubsCardComponent';
|
|
23
|
+
import { CFErrorResponse } from './CFErrorResponse';
|
|
24
|
+
import { doPPIWalletPayment } from './PPIWallet/PPIWalletPayment';
|
|
22
25
|
|
|
23
26
|
const LINKING_ERROR =
|
|
24
27
|
`The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` +
|
|
@@ -132,6 +135,14 @@ class CFPaymentGateway {
|
|
|
132
135
|
}
|
|
133
136
|
}
|
|
134
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Creates a PPI wallet order session via Cashfree's API.
|
|
140
|
+
* Resolves with the API response as-is; rejects with CFErrorResponse.
|
|
141
|
+
*/
|
|
142
|
+
doPPIWalletPayment(payment: CFPPIWalletPayment): Promise<unknown> {
|
|
143
|
+
return doPPIWalletPayment(payment);
|
|
144
|
+
}
|
|
145
|
+
|
|
135
146
|
setEventSubscriber(cfEventCallback: CFEventCallback) {
|
|
136
147
|
let eventFunction = (event: string) => {
|
|
137
148
|
console.log(JSON.stringify(event));
|
|
@@ -214,39 +225,7 @@ export interface CFEventCallback {
|
|
|
214
225
|
onReceivedEvent(eventName: string, map: Map<string, string>): void;
|
|
215
226
|
}
|
|
216
227
|
|
|
217
|
-
export
|
|
218
|
-
private status: string = 'FAILED';
|
|
219
|
-
private message: string = 'payment has failed';
|
|
220
|
-
private code: string = 'payment_failed';
|
|
221
|
-
private type: string = 'request_failed';
|
|
222
|
-
|
|
223
|
-
fromJSON(errorString: string) {
|
|
224
|
-
console.log('errorString :' + errorString);
|
|
225
|
-
const object = JSON.parse(errorString);
|
|
226
|
-
console.log('errorStringObject :' + object);
|
|
227
|
-
this.status = object.status;
|
|
228
|
-
this.message = object.message;
|
|
229
|
-
this.code = object.code;
|
|
230
|
-
this.type = object.type;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
getStatus(): string {
|
|
234
|
-
return this.status;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
getMessage(): string {
|
|
238
|
-
return this.message;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
getCode(): string {
|
|
242
|
-
return this.code;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
getType(): string {
|
|
246
|
-
return this.type;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
228
|
+
export { CFErrorResponse };
|
|
250
229
|
export const CFCard = CFCardComponent;
|
|
251
230
|
export const CFSubsCard = CFSubsCardComponent;
|
|
252
231
|
export const CFPaymentGatewayService = new CFPaymentGateway();
|