diff --git a/lib/UpiPayment.js b/lib/UpiPayment.js index eb8b350..9d1f425 100644 --- a/lib/UpiPayment.js +++ b/lib/UpiPayment.js @@ -1,28 +1,22 @@ -import { NativeModules } from 'react-native'; +import { NativeModules } from "react-native"; const UpiModule = NativeModules.UpiPayment; - const RNUpiPayment = { - requiredFields: [ - 'vpa', - 'amount', - 'payeeName', - 'transactionRef' - ], + requiredFields: ["vpa", "amount", "payeeName", "transactionRef"], upiConfig: { - vpa: 'pa', - payeeName: 'pn', - transactionRef: 'tr', - amount: 'am', - transactionNote: 'tn', - currency: 'cu' + vpa: "pa", + payeeName: "pn", + transactionRef: "tr", + amount: "am", + transactionNote: "tn", + currency: "cu", }, - UPI_APP_NOT_INSTALLED: 'UPI supporting app not installed', - REQUEST_CODE_MISMATCH: 'Request Code Mismatch', - NO_ACTION_TAKEN: 'No action taken', + UPI_APP_NOT_INSTALLED: "UPI supporting app not installed", + REQUEST_CODE_MISMATCH: "Request Code Mismatch", + NO_ACTION_TAKEN: "No action taken", validateObject(config: Object) { const errorArray = []; @@ -50,12 +44,13 @@ const RNUpiPayment = { data = JSON.parse(data); let failureObj = {}; - if (typeof (data.nameValuePairs.message) == "undefined") { + if (typeof data.nameValuePairs.message == "undefined") { failure(data.nameValuePairs); - } - else { - const failureString = data.nameValuePairs && data.nameValuePairs.message; - if (failureString === this.UPI_APP_NOT_INSTALLED || + } else { + const failureString = + data.nameValuePairs && data.nameValuePairs.message; + if ( + failureString === this.UPI_APP_NOT_INSTALLED || failureString === this.REQUEST_CODE_MISMATCH || failureString === this.NO_ACTION_TAKEN ) { @@ -68,53 +63,66 @@ const RNUpiPayment = { }; }, + //this function updated to fixed unwanted crash from some upi app convertStringToObject(responseString: string) { - let object = {}; - const stringArray = responseString.split('&'); - object = stringArray.reduce((accumulator, current) => { - const currentArray = current.split('='); - accumulator[currentArray[0]] = currentArray[1]; - return accumulator; - }, {}); - - return object; + if (responseString === undefined) { + return ""; + } else { + let object = {}; + const stringArray = responseString.split("&"); + object = stringArray.reduce((accumulator, current) => { + const currentArray = current.split("="); + accumulator[currentArray[0]] = currentArray[1]; + return accumulator; + }, {}); + + return object; + } }, initializePayment(config, success, failure) { - if (typeof success !== 'function') { - throw new Error('Success callback not a function'); + if (typeof success !== "function") { + throw new Error("Success callback not a function"); } - if (typeof failure !== 'function') { - throw new Error('Failure callback not a function'); + if (typeof failure !== "function") { + throw new Error("Failure callback not a function"); } - if (typeof config !== 'object') { - throw new Error('config not of type object'); + if (typeof config !== "object") { + throw new Error("config not of type object"); } const errorArray = this.validateObject(config); if (errorArray.length > 0) { - throw new Error(`Following keys are required ${JSON.stringify(errorArray)}`); + throw new Error( + `Following keys are required ${JSON.stringify(errorArray)}` + ); } - config.currency = 'INR'; - let upiString = 'upi://pay?'; + config.currency = "INR"; + let upiString = "upi://pay?"; let queryString = Object.keys(config).reduce((accumulator, current) => { - let prefix = ''; + let prefix = ""; if (accumulator) { - prefix = '&'; + prefix = "&"; } - accumulator = accumulator + prefix + + accumulator = + accumulator + + prefix + `${this.upiConfig[current]}=${encodeURIComponent(config[current])}`; return accumulator; - }, ''); - const upiConfig = {} + }, ""); + const upiConfig = {}; upiConfig.upiString = `upi://pay?${queryString}`; - UpiModule.intializePayment(upiConfig, this.successCallback(success), this.failureCallback(failure)); - } -} + UpiModule.intializePayment( + upiConfig, + this.successCallback(success), + this.failureCallback(failure) + ); + }, +}; -module.exports = RNUpiPayment; \ No newline at end of file +module.exports = RNUpiPayment;