Skip to content

Commit 59d8321

Browse files
committed
initial
0 parents  commit 59d8321

File tree

6 files changed

+324
-0
lines changed

6 files changed

+324
-0
lines changed

.gitignore

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Built application files
2+
*.apk
3+
*.aar
4+
*.ap_
5+
*.aab
6+
7+
# Files for the ART/Dalvik VM
8+
*.dex
9+
10+
# Java class files
11+
*.class
12+
13+
# Generated files
14+
bin/
15+
gen/
16+
out/
17+
# Uncomment the following line in case you need and you don't have the release build type files in your app
18+
# release/
19+
20+
# Gradle files
21+
.gradle/
22+
build/
23+
24+
# Local configuration file (sdk path, etc)
25+
local.properties
26+
27+
# Proguard folder generated by Eclipse
28+
proguard/
29+
30+
# Log Files
31+
*.log
32+
33+
# Android Studio Navigation editor temp files
34+
.navigation/
35+
36+
# Android Studio captures folder
37+
captures/
38+
39+
# IntelliJ
40+
*.iml
41+
.idea/workspace.xml
42+
.idea/tasks.xml
43+
.idea/gradle.xml
44+
.idea/assetWizardSettings.xml
45+
.idea/dictionaries
46+
.idea/libraries
47+
# Android Studio 3 in .gitignore file.
48+
.idea/caches
49+
.idea/modules.xml
50+
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
51+
.idea/navEditor.xml
52+
53+
# Keystore files
54+
# Uncomment the following lines if you do not want to check your keystore files in.
55+
#*.jks
56+
#*.keystore
57+
58+
# External native build folder generated in Android Studio 2.2 and later
59+
.externalNativeBuild
60+
.cxx/
61+
62+
# Google Services (e.g. APIs or Firebase)
63+
# google-services.json
64+
65+
# Freeline
66+
freeline.py
67+
freeline/
68+
freeline_project_description.json
69+
70+
# fastlane
71+
fastlane/report.xml
72+
fastlane/Preview.html
73+
fastlane/screenshots
74+
fastlane/test_output
75+
fastlane/readme.md
76+
77+
# Version control
78+
vcs.xml
79+
80+
# lint
81+
lint/intermediates/
82+
lint/generated/
83+
lint/outputs/
84+
lint/tmp/
85+
# lint/reports/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# codeeshop-cespay-cordova
2+
codeeshop-cespay-cordova

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "codeeshop-cespay-cordova",
3+
"version": "1.0.0",
4+
"description": "Cordova Payment Method Integration with In-App Browser",
5+
"cordova": {
6+
"id": "codeeshop-cespay-cordova",
7+
"platforms": [
8+
"android"
9+
]
10+
},
11+
"keywords": [
12+
"ecosystem:cordova",
13+
"cordova-android"
14+
],
15+
"author": "Code-E-Shop"
16+
}

plugin.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version='1.0' encoding='utf-8'?>
2+
<plugin id="codeeshop-cespay-cordova" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<name>cespay</name>
4+
<js-module name="CustomPayment" src="www/CustomPayment.js">
5+
<clobbers target="CustomPayment" />
6+
</js-module>
7+
<platform name="android">
8+
<config-file parent="/*" target="res/xml/config.xml">
9+
<feature name="CESPay">
10+
<param name="android-package" value="com.codeeshop.CESPay" />
11+
</feature>
12+
</config-file>
13+
<config-file parent="/*" target="AndroidManifest.xml">
14+
15+
</config-file>
16+
<source-file src="src/android/CESPay.java" target-dir="com/codeeshop/CESPay" />
17+
</platform>
18+
</plugin>

src/android/CESPay.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.codeeshop;
2+
3+
import org.apache.cordova.CordovaPlugin;
4+
import org.apache.cordova.CallbackContext;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
10+
/**
11+
* This class echoes a string called from JavaScript.
12+
*/
13+
public class CESPay extends CordovaPlugin {
14+
private static final String TAG = "SodexoPay";
15+
16+
@Override
17+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
18+
Log.i(TAG, "Actions: " + action);
19+
if (action.equals("startPayment")) {
20+
String url = args.getString(0);
21+
this.startPayment(message, callbackContext);
22+
return true;
23+
}
24+
25+
return false;
26+
}
27+
28+
private void startPayment(String url, CallbackContext callbackContext) {
29+
if (url != null && url.length() > 0) {
30+
callbackContext.success();
31+
} else {
32+
callbackContext.error("Expected one non-empty string argument.");
33+
}
34+
}
35+
}

www/CustomPayment.js

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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 = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-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

Comments
 (0)