forked from razorpay/react-native-razorpay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRazorpayCheckout.js
52 lines (46 loc) · 1.97 KB
/
RazorpayCheckout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
import {NativeEventEmitter, NativeModules, Platform} from 'react-native';
const razorpayEvents = (Platform.OS === 'ios') ?
(NativeModules !== undefined &&
NativeModules !== null &&
NativeModules.RazorpayEventEmitter !== undefined &&
NativeModules.RazorpayEventEmitter !== null) ? new NativeEventEmitter(NativeModules.RazorpayEventEmitter) : undefined
: new NativeEventEmitter(NativeModules.RazorpayEventEmitter);
const removeSubscriptions = () => {
razorpayEvents.removeAllListeners('Razorpay::PAYMENT_SUCCESS');
razorpayEvents.removeAllListeners('Razorpay::PAYMENT_ERROR');
razorpayEvents.removeAllListeners('Razorpay::EXTERNAL_WALLET_SELECTED');
}
;
class RazorpayCheckout {
static open(options, successCallback, errorCallback) {
if (razorpayEvents) {
return new Promise(function (resolve, reject) {
razorpayEvents.addListener('Razorpay::PAYMENT_SUCCESS', (data) => {
let resolveFn = successCallback || resolve;
resolveFn(data);
removeSubscriptions();
});
razorpayEvents.addListener('Razorpay::PAYMENT_ERROR', (data) => {
let rejectFn = errorCallback || reject;
rejectFn(data);
removeSubscriptions();
});
NativeModules.RazorpayCheckout.open(options);
});
} else {
return Promise.reject("Native Modules is null hence cannot proceed");
}
}
static onExternalWalletSelection(externalWalletCallback) {
if (razorpayEvents) {
razorpayEvents.addListener('Razorpay::EXTERNAL_WALLET_SELECTED', (data) => {
externalWalletCallback(data);
removeSubscriptions();
});
} else {
return Promise.reject("Native Modules is null hence cannot proceed");
}
}
}
export default RazorpayCheckout;