1+ const exec = require ( 'cordova/exec' ) ;
2+
3+ const CONFIG = {
4+ url : "https://pay.gw.zetapay.in/" ,
5+ target : '_blank' ,
6+ options : "location=no,hidenavigationbuttons=yes,hideurlbar=yes,hardwareback=no,fullscreen=yes" ,
7+ callbackSuccessURL : 'success' ,
8+ callbackErrorURL : 'failed' ,
9+ }
10+
11+ var inAppBrowserRef ;
12+
13+ let CONFIGPARAMS = { } ;
14+
15+ const startPayment = function ( config , success , error ) {
16+ Object . keys ( CONFIG ) . forEach ( function ( k ) {
17+ CONFIGPARAMS [ k ] = ( config [ k ] || CONFIG [ k ] )
18+ } )
19+
20+ if ( isValidURL ( CONFIGPARAMS . url ) ) {
21+ onDeviceReady ( CONFIGPARAMS )
22+ exec ( success , error , 'CESPay' , 'startPayment' , [ CONFIGPARAMS . url ] ) ;
23+ } else {
24+ console . error ( 'Invalid URL' )
25+ }
26+ } ;
27+
28+ function RegisterEvents ( ) {
29+ if ( typeof inAppBrowserRef != 'undefined' ) {
30+ inAppBrowserRef . addEventListener ( 'loadstart' , loadStartCallBack ) ;
31+ inAppBrowserRef . addEventListener ( 'loadstop' , loadStopCallBack ) ;
32+ inAppBrowserRef . addEventListener ( 'loaderror' , loadErrorCallBack ) ;
33+ inAppBrowserRef . addEventListener ( 'exit' , exitCallback ) ;
34+ inAppBrowserRef . addEventListener ( 'message' , messageCallBack ) ;
35+ }
36+ }
37+
38+ function UnregisterEvents ( ) {
39+ if ( typeof inAppBrowserRef != 'undefined' ) {
40+ inAppBrowserRef . removeEventListener ( 'loadstart' , loadStartCallBack ) ;
41+ inAppBrowserRef . removeEventListener ( 'loadstop' , loadStopCallBack ) ;
42+ inAppBrowserRef . removeEventListener ( 'loaderror' , loadErrorCallBack ) ;
43+ inAppBrowserRef . removeEventListener ( 'exit' , exitCallback ) ;
44+ // inAppBrowserRef.removeEventListener('message', messageCallBack);
45+ }
46+ }
47+
48+ function onDeviceReady ( CONFIGPARAMS ) {
49+ let iab = cordova . InAppBrowser ;
50+
51+ const {
52+ url,
53+ target,
54+ options
55+ } = CONFIGPARAMS
56+
57+ inAppBrowserRef = iab . open ( url , target , options ) ;
58+
59+ RegisterEvents ( )
60+
61+ }
62+
63+ function loadStartCallBack ( event ) {
64+ console . debug ( 'Loading started: ' + event . url )
65+ }
66+
67+ async function loadStopCallBack ( event ) {
68+ console . debug ( 'Loading finished: ' + event . url )
69+ if ( await paymentChecks ( event ) ) {
70+ closeIt ( )
71+ }
72+ }
73+
74+ function messageCallBack ( params ) {
75+ console . debug ( params , 'messageCallBack params' )
76+ // if (event.origin !== "http://example.com")
77+ // return;
78+ }
79+
80+ async function paymentChecks ( event ) {
81+ let data ;
82+
83+ if ( typeof event != 'undefined' && typeof event . url != 'undefined' && event . url ) {
84+ if ( event . url . match ( "https://payu.herokuapp.com/success" ) || event . url . includes ( CONFIGPARAMS . callbackSuccessURL ) ) {
85+ data = {
86+ code : 'success'
87+ }
88+ } else if ( event . url . includes ( CONFIGPARAMS . callbackErrorURL ) ) {
89+ data = {
90+ code : 'error'
91+ }
92+ }
93+
94+ if ( typeof data != 'undefined' ) {
95+ sendMessage ( JSON . stringify ( data ) )
96+ await new Promise ( function ( resolve ) {
97+ let timeout = setTimeout ( function ( ) {
98+ clearTimeout ( timeout )
99+ resolve ( "Payment !!" ) ;
100+ } , 1000 ) ;
101+ } ) ;
102+
103+ return true
104+ }
105+
106+ }
107+
108+ return false
109+ }
110+
111+ function sendMessage ( data ) {
112+ inAppBrowserRef . executeScript ( {
113+ code : "\
114+ webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(" + data + "));"
115+ } ) ;
116+ }
117+
118+ async function errorMessage ( data ) {
119+ sendMessage ( JSON . stringify ( data ) )
120+
121+ await new Promise ( function ( resolve ) {
122+ let timeout = setTimeout ( function ( ) {
123+ clearTimeout ( timeout )
124+ resolve ( "Payment Error !!" ) ;
125+ } , 1000 ) ;
126+ } ) ;
127+
128+ return true
129+ }
130+
131+ function closeIt ( ) {
132+ if ( typeof inAppBrowserRef != 'undefined' ) {
133+ UnregisterEvents ( )
134+ inAppBrowserRef . close ( ) ;
135+ inAppBrowserRef = undefined ;
136+ }
137+ }
138+
139+ async function loadErrorCallBack ( error ) {
140+ console . debug ( 'Loading error: ' + error . message )
141+ if ( await errorMessage ( {
142+ code : 'error' ,
143+ message : error . message
144+ } ) ) {
145+ closeIt ( )
146+ }
147+ }
148+
149+ async function exitCallback ( event ) {
150+ console . debug ( 'Browser is closed...' , event )
151+ if ( await paymentChecks ( event ) ) {
152+ closeIt ( )
153+ }
154+ }
155+
156+ function isValidURL ( str ) {
157+ var regexp = / ^ (?: (?: h t t p s ? | f t p ) : \/ \/ ) ? (?: (? ! (?: 1 0 | 1 2 7 ) (?: \. \d { 1 , 3 } ) { 3 } ) (? ! (?: 1 6 9 \. 2 5 4 | 1 9 2 \. 1 6 8 ) (?: \. \d { 1 , 3 } ) { 2 } ) (? ! 1 7 2 \. (?: 1 [ 6 - 9 ] | 2 \d | 3 [ 0 - 1 ] ) (?: \. \d { 1 , 3 } ) { 2 } ) (?: [ 1 - 9 ] \d ? | 1 \d \d | 2 [ 0 1 ] \d | 2 2 [ 0 - 3 ] ) (?: \. (?: 1 ? \d { 1 , 2 } | 2 [ 0 - 4 ] \d | 2 5 [ 0 - 5 ] ) ) { 2 } (?: \. (?: [ 1 - 9 ] \d ? | 1 \d \d | 2 [ 0 - 4 ] \d | 2 5 [ 0 - 4 ] ) ) | (?: (?: [ a - z \u00a1 - \uffff 0 - 9 ] - * ) * [ a - z \u00a1 - \uffff 0 - 9 ] + ) (?: \. (?: [ a - z \u00a1 - \uffff 0 - 9 ] - * ) * [ a - z \u00a1 - \uffff 0 - 9 ] + ) * (?: \. (?: [ a - z \u00a1 - \uffff ] { 2 , } ) ) ) (?: : \d { 2 , 5 } ) ? (?: \/ \S * ) ? $ / ;
158+ if ( regexp . test ( str ) ) {
159+ return true ;
160+ } else {
161+ return false ;
162+ }
163+ }
164+
165+
166+ module . exports = {
167+ startPayment
168+ } ;
0 commit comments