UNPKG

react-native-cashfree-pg-sdk

Version:
187 lines (186 loc) 7.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CFSubsCard = exports.CFPaymentGatewayService = exports.CFErrorResponse = exports.CFCard = void 0; var _reactNative = require("react-native"); var _package = require("../package.json"); var _cashfreePgApiContract = require("cashfree-pg-api-contract"); var _CFCardComponent = _interopRequireDefault(require("./Card/CFCardComponent")); var _CFSubsCardComponent = _interopRequireDefault(require("./Card/CFSubsCardComponent")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } const LINKING_ERROR = `The package 'react-native-cashfree-pg-api' doesn't seem to be linked. Make sure: \n\n` + _reactNative.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 = _reactNative.NativeModules.CashfreePgApi ? _reactNative.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 = _reactNative.Platform.OS === 'ios' ? new _reactNative.NativeEventEmitter(_reactNative.NativeModules.CashfreeEventEmitter) : _reactNative.NativeAppEventEmitter; } doPayment(checkoutPayment) { checkoutPayment.version = _package.version; CashfreePgApi.doPayment(JSON.stringify(checkoutPayment)); } doUPIPayment(checkoutPayment) { checkoutPayment.version = _package.version; CashfreePgApi.doUPIPayment(JSON.stringify(checkoutPayment)); } doWebPayment(cfSession) { CashfreePgApi.doWebPayment(JSON.stringify(cfSession)); } doSubscriptionPayment(cfSession) { CashfreePgApi.doSubscriptionPayment(JSON.stringify(cfSession)); } /** * @deprecated : Instead call makePayment */ doCardPayment(cardPayment) { this.makePayment(cardPayment); } async getInstalledUpiApps() { return new Promise((resolve, reject) => { if (_reactNative.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 = _package.version; const paymentData = JSON.stringify(cfPayment); if (cfPayment instanceof _cashfreePgApiContract.CFUPIPayment) { CashfreePgApi.doElementUPIPayment(paymentData); } else if (cfPayment instanceof _cashfreePgApiContract.CFCardPayment) { CashfreePgApi.doCardPayment(paymentData); } else if (cfPayment instanceof _cashfreePgApiContract.CFNBPayment) { CashfreePgApi.doElementNBPayment(paymentData); } else { console.log('makePayment::==> Wrong payment object'); } } makeSubsPayment(cfPayment) { cfPayment.version = _package.version; const paymentData = JSON.stringify(cfPayment); if (cfPayment instanceof _cashfreePgApiContract.CFSubsUPIPayment) { CashfreePgApi.doSubsUPIPayment(paymentData); } else if (cfPayment instanceof _cashfreePgApiContract.CFSubsCardPayment) { CashfreePgApi.doSubsCardPayment(paymentData); } else if (cfPayment instanceof _cashfreePgApiContract.CFSubsNBPayment) { CashfreePgApi.doSubsNBPayment(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; } } } 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; } } exports.CFErrorResponse = CFErrorResponse; const CFCard = exports.CFCard = _CFCardComponent.default; const CFSubsCard = exports.CFSubsCard = _CFSubsCardComponent.default; const CFPaymentGatewayService = exports.CFPaymentGatewayService = new CFPaymentGateway(); //# sourceMappingURL=index.js.mapp