react-native-cashfree-pg-sdk
Version:
Cashfree PG Plugin for React Native
158 lines (157 loc) • 6.11 kB
JavaScript
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { NativeAppEventEmitter, NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { version } from '../package.json';
import { CFUPIPayment, CFCardPayment } from 'cashfree-pg-api-contract';
const LINKING_ERROR = `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
ios: '- You have run \'pod install\'\n',
default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo managed workflow\n';
const CashfreePgApi = NativeModules.CashfreePgApi ? NativeModules.CashfreePgApi : new Proxy({}, {
get() {
throw new Error(LINKING_ERROR);
}
});
class CFPaymentGateway {
constructor() {
_defineProperty(this, "emitter", void 0);
_defineProperty(this, "successSubscription", null);
_defineProperty(this, "failureSubscription", null);
_defineProperty(this, "eventSubscription", null);
_defineProperty(this, "upiAppsSubscription", null);
this.emitter = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.CashfreeEventEmitter) : NativeAppEventEmitter;
}
doPayment(checkoutPayment) {
checkoutPayment.version = version;
CashfreePgApi.doPayment(JSON.stringify(checkoutPayment));
}
doUPIPayment(checkoutPayment) {
checkoutPayment.version = version;
CashfreePgApi.doUPIPayment(JSON.stringify(checkoutPayment));
}
doWebPayment(cfSession) {
CashfreePgApi.doWebPayment(cfSession);
}
/**
* @deprecated : Instead call makePayment
*/
doCardPayment(cardPayment) {
this.makePayment(cardPayment);
}
async getInstalledUpiApps() {
return new Promise((resolve, reject) => {
if (Platform.OS === 'ios') {
let fetchUpiList = apps => {
console.log(JSON.stringify(apps));
if (apps) {
resolve(apps);
} else {
reject('No UPI apps found');
}
};
this.upiAppsSubscription = this.emitter.addListener('cfUpiApps', fetchUpiList);
CashfreePgApi.getInstalledUpiApps();
} else {
CashfreePgApi.getInstalledUpiApps(apps => {
if (apps) {
resolve(apps);
} else {
reject('No UPI apps found');
}
});
}
});
}
makePayment(cfPayment) {
cfPayment.version = version;
const paymentData = JSON.stringify(cfPayment);
if (cfPayment instanceof CFUPIPayment) {
CashfreePgApi.doElementUPIPayment(paymentData);
} else if (cfPayment instanceof CFCardPayment) {
CashfreePgApi.doCardPayment(paymentData);
} else {
console.log('makePayment::==> Wrong payment object');
}
}
setEventSubscriber(cfEventCallback) {
let eventFunction = event => {
console.log(JSON.stringify(event));
let data = JSON.parse(event);
cfEventCallback.onReceivedEvent(data.eventName, data.meta);
};
this.eventSubscription = this.emitter.addListener('cfEvent', eventFunction);
CashfreePgApi.setEventSubscriber();
}
removeEventSubscriber() {
if (this.eventSubscription !== undefined && this.eventSubscription !== null) {
this.eventSubscription.remove();
this.eventSubscription = null;
}
CashfreePgApi.removeEventSubscriber();
}
setCallback(cfCallback) {
// this.cfCallback = cfCallback;
let successFunction = orderID => {
console.log('response is : ' + JSON.stringify(orderID));
cfCallback.onVerify(orderID);
};
let failureFunction = error => {
console.log('reason: ' + JSON.stringify(error));
const response = new CFErrorResponse();
// @ts-ignore
const message = JSON.parse(error);
response.fromJSON(message.error);
// @ts-ignore
cfCallback.onError(response, message.orderID);
};
this.successSubscription = this.emitter.addListener('cfSuccess', successFunction);
this.failureSubscription = this.emitter.addListener('cfFailure', failureFunction);
CashfreePgApi.setCallback();
}
removeCallback() {
if (this.successSubscription !== undefined && this.successSubscription !== null) {
this.successSubscription.remove();
this.successSubscription = null;
}
if (this.failureSubscription !== undefined && this.failureSubscription !== null) {
this.failureSubscription.remove();
this.failureSubscription = null;
}
if (this.upiAppsSubscription !== undefined && this.upiAppsSubscription !== null) {
this.upiAppsSubscription.remove();
this.upiAppsSubscription = null;
}
}
}
export class CFErrorResponse {
constructor() {
_defineProperty(this, "status", 'FAILED');
_defineProperty(this, "message", 'payment has failed');
_defineProperty(this, "code", 'payment_failed');
_defineProperty(this, "type", 'request_failed');
}
fromJSON(errorString) {
console.log('errorString :' + errorString);
const object = JSON.parse(errorString);
console.log('errorStringObject :' + object);
this.status = object.status;
this.message = object.message;
this.code = object.code;
this.type = object.type;
}
getStatus() {
return this.status;
}
getMessage() {
return this.message;
}
getCode() {
return this.code;
}
getType() {
return this.type;
}
}
export const CFPaymentGatewayService = new CFPaymentGateway();
//# sourceMappingURL=index.js.map