-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarionMarket.js
186 lines (169 loc) · 6.78 KB
/
BarionMarket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var BarionMarket = (function(){
function BarionMarket(){
this.callbacks = {
vehicle: null,
defaultAddress: null,
address: null,
customer: null,
lastUsedVehicle: null,
aztecCode: null
};
}
/**
* Testing data
*/
BarionMarket.prototype.testData = {
selectAddress: "{\"location\": {\"region\": \"Fejer\",\"city\": \"Moha\",\"street\": \"Dozsa Utca\",\"postalCode\": \"8042\",\"stairway\": \"\",\"floor\": \"\",\"countryCode\": \"HU\",\"houseNumber\": \"6\",\"door\": \"\",\"doorBell\": \"\"},\"noteForDelivery\": \"\",\"customerName\": {\"firstName\": \"Takacs\",\"organizationName\": \"\",\"lastName\": \"Laszlo\"},\"phoneNumber\": \"\",\"taxNumber\": \"\",\"addressType\": \"Individual\",\"name\": \"\"}",
getDefaultAddress: "{\"location\": {\"region\": \"Fejer\",\"city\": \"Moha\",\"street\": \"Dozsa Utca\",\"postalCode\": \"8042\",\"stairway\": \"\",\"floor\": \"\",\"countryCode\": \"HU\",\"houseNumber\": \"6\",\"door\": \"\",\"doorBell\": \"\"},\"noteForDelivery\": \"\",\"customerName\": {\"firstName\": \"Takacs\",\"organizationName\": \"\",\"lastName\": \"Laszlo\"},\"phoneNumber\": \"\",\"taxNumber\": \"\",\"addressType\": \"Individual\",\"name\": \"\"}",
selectVehicle: "{\"licensePlate\":\"TEST001\",\"countryCode\":\"HU\",\"category\":\"CAR\"}",
getCustomer: "{\"loginName\":\"[email protected]\",\"language\":\"hu_HU\",\"token\":\"123456\", \"accountOwnerType\":\"Individual\"}",
getLastUsedVehicle: "{\"licensePlate\":\"TEST001\",\"countryCode\":\"HU\",\"category\":\"CAR\"}",
aztecCode: "{\"code\":\"123456789\"}"
}
/**
* This method will close your plugin in the Barion app. After this function was called your session will be killed.
*/
BarionMarket.prototype.closePlugin = function() {
var action = {
"action": "close"
}
this.postToBarionHandler(action, null);
};
/**
* If you call this method you will get the default address of the user in the callback function.
*/
BarionMarket.prototype.getDefaultAddress = function(callback) {
if (!isValid(callback)){
return;
}
var action = {
"action": "getDefaultAddress"
}
this.callbacks.defaultAddress = callback;
this.postToBarionHandler(action, callback);
};
/**
* If you call this method users can select an address from their saved addresses or create a new one.
*/
BarionMarket.prototype.selectAddress = function(callback) {
if (!isValid(callback)){
return;
}
var action = {
"action": "selectAddress"
}
this.callbacks.address = callback;
this.postToBarionHandler(action, callback);
};
/**
* You can get the user email address and the token in the callback function.
*/
BarionMarket.prototype.getCustomer = function(callback){
if (!isValid(callback)){
return;
}
var action = {
"action" : "getCustomer"
}
this.callbacks.customer = callback;
this.postToBarionHandler(action, callback);
}
/**
* If you call this method users can select a vehicle from their saved vehicles or save a new one in the Barion app and you will get the selected vehicle object in the callback function.
*/
BarionMarket.prototype.selectVehicle = function(callback) {
if (!isValid(callback)){
return;
}
var action = {
"action": "selectVehicle"
}
this.callbacks.vehicle = callback;
this.postToBarionHandler(action, callback);
}
/**
* If you call this method you will get the last used vehicle in the callback function.
*/
BarionMarket.prototype.getLastUsedVehicle = function(callback) {
if (!isValid(callback)){
return;
}
var action = {
"action": "getLastUsedVehicle"
}
this.callbacks.lastUsedVehicle = callback;
this.postToBarionHandler(action, callback);
}
BarionMarket.prototype.readAztecCode = function(callback) {
if (!isValid(callback)){
return;
}
var action = {
"action": "readAztecCode"
}
this.callbacks.aztecCode = callback;
this.postToBarionHandler(action, callback);
}
/**
* This method send data to android and ios platforms.
*/
BarionMarket.prototype.postToBarionHandler = function (obj, callback) {
var handler = null;
var message = JSON.stringify(obj);
if (typeof barionPluginHandler != "undefined") {
handler = barionPluginHandler;
} else {
handler = (typeof window.webkit != "undefined"
&& typeof window.webkit.messageHandlers != "undefined"
&& typeof window.webkit.messageHandlers.barionPluginHandler != "undefined") ? window.webkit.messageHandlers.barionPluginHandler : null;
}
if (typeof handler != "undefined" && handler != null) {
handler.postMessage(message);
} else {
if (callback == null || callback == "undefined"){
alert("Plugin closed");
} else {
switch (callback) {
case this.callbacks.vehicle:
callback(this.testData.selectVehicle);
break;
case this.callbacks.address:
callback(this.testData.selectAddress);
break;
case this.callbacks.defaultAddress:
callback(this.testData.getDefaultAddress);
break;
case this.callbacks.customer:
callback(this.testData.getCustomer);
break;
case this.callbacks.lastUsedVehicle:
callback(this.testData.getLastUsedVehicle);
break;
case this.callbacks.aztecCode:
callback(this.testData.aztecCode);
break;
default:
alert("Plugin closed");
break;
}
}
}
};
function isValid(callback){
if (typeof callback == "undefined" || callback == null){
console.error("callback function is invalid");
return false;
} else {
return true;
}
};
var instance;
return {
getInstance: function(){
if (instance == null){
instance = new BarionMarket();
}
return instance;
}
};
})();