-
Notifications
You must be signed in to change notification settings - Fork 107
/
RazorpayCheckout.js
37 lines (32 loc) · 1.21 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
'use strict';
import { NativeModules, NativeEventEmitter } from 'react-native';
const razorpayEvents = 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) {
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.RNRazorpayCheckout.open(options);
});
}
static onExternalWalletSelection(externalWalletCallback) {
razorpayEvents.addListener('Razorpay::EXTERNAL_WALLET_SELECTED', (data) => {
externalWalletCallback(data);
removeSubscriptions();
});
}
}
export default RazorpayCheckout;