Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 57 additions & 49 deletions lib/UpiPayment.js
Original file line number Diff line number Diff line change
@@ -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 = [];
Expand Down Expand Up @@ -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
) {
Expand All @@ -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;
module.exports = RNUpiPayment;