-
Notifications
You must be signed in to change notification settings - Fork 1
/
MOLPay.cs
383 lines (336 loc) · 12.6 KB
/
MOLPay.cs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using MiniJSON;
using VoxelBusters.NativePlugins;
namespace MOLPayXDK
{
public class MOLPay : MonoBehaviour
{
public const string mp_amount = "mp_amount";
public const string mp_username = "mp_username";
public const string mp_password = "mp_password";
public const string mp_merchant_ID = "mp_merchant_ID";
public const string mp_app_name = "mp_app_name";
public const string mp_order_ID = "mp_order_ID";
public const string mp_currency = "mp_currency";
public const string mp_country = "mp_country";
public const string mp_verification_key = "mp_verification_key";
public const string mp_channel = "mp_channel";
public const string mp_bill_description = "mp_bill_description";
public const string mp_bill_name = "mp_bill_name";
public const string mp_bill_email = "mp_bill_email";
public const string mp_bill_mobile = "mp_bill_mobile";
public const string mp_channel_editing = "mp_channel_editing";
public const string mp_editing_enabled = "mp_editing_enabled";
public const string mp_transaction_id = "mp_transaction_id";
public const string mp_request_type = "mp_request_type";
public const string mp_is_escrow = "mp_is_escrow";
public const string mp_bin_lock = "mp_bin_lock";
public const string mp_bin_lock_err_msg = "mp_bin_lock_err_msg";
public const string mp_custom_css_url = "mp_custom_css_url";
public const string mp_preferred_token = "mp_preferred_token";
public const string mp_tcctype = "mp_tcctype";
public const string mp_is_recurring = "mp_is_recurring";
public const string mp_sandbox_mode = "mp_sandbox_mode";
public const string mp_allowed_channels = "mp_allowed_channels";
public const string mp_express_mode = "mp_express_mode";
public const string mp_advanced_email_validation_enabled = "mp_advanced_email_validation_enabled";
public const string mp_advanced_phone_validation_enabled = "mp_advanced_phone_validation_enabled";
public const string mp_bill_name_edit_disabled = "mp_bill_name_edit_disabled";
public const string mp_bill_email_edit_disabled = "mp_bill_email_edit_disabled";
public const string mp_bill_mobile_edit_disabled = "mp_bill_mobile_edit_disabled";
public const string mp_bill_description_edit_disabled = "mp_bill_description_edit_disabled";
public const string mp_language = "mp_language";
public const string mp_dev_mode = "mp_dev_mode";
public const string mp_cash_waittime = "mp_cash_waittime";
public const string mp_non_3DS = "mp_non_3DS";
public const string mp_card_list_disabled = "mp_card_list_disabled";
public const string mp_disabled_channels = "mp_disabled_channels";
#if UNITY_IOS
private const string mpopenmolpaywindow = "mpopenmolpaywindow//";
private const string mpcloseallwindows = "mpcloseallwindows//";
private const string mptransactionresults = "mptransactionresults//";
private const string mprunscriptonpopup = "mprunscriptonpopup//";
private const string mppinstructioncapture = "mppinstructioncapture//";
#elif UNITY_ANDROID
private const string mpopenmolpaywindow = "mpopenmolpaywindow://";
private const string mpcloseallwindows = "mpcloseallwindows://";
private const string mptransactionresults = "mptransactionresults://";
private const string mprunscriptonpopup = "mprunscriptonpopup://";
private const string mppinstructioncapture = "mppinstructioncapture://";
#endif
private const string molpayresulturl = "MOLPay/result.php";
private const string molpaynbepayurl = "MOLPay/nbepay.php";
private const string uniwebview = "uniwebview://";
private const string module_id = "module_id";
private const string wrapper_version = "wrapper_version";
private const string webview_url_prefix = "webview_url_prefix";
private Dictionary<string, object> paymentDetails;
private string transactionResult;
private string finishLoadUrl;
private bool isClosingReceipt = false;
private bool hijackWindowOpen = false;
private UniWebView mpMainUI, mpMOLPayUI, mpBankUI;
private Action<string> callback;
private string webViewUrl
{
get
{
#if UNITY_IOS
return Application.streamingAssetsPath + "/molpay-mobile-xdk-www/index.html";
#elif UNITY_ANDROID
return "file:///android_asset/molpay-mobile-xdk-www/index.html";
#endif
}
}
#if UNITY_IOS
[DllImport("__Internal")]
private static extern void _SavePhoto(string readAddr);
#endif
public void StartMolpay(Dictionary<string, object> paymentDetails, Action<string> callback)
{
this.paymentDetails = paymentDetails;
this.callback = callback;
transactionResult = string.Empty;
mpMainUI = CreateWebView();
mpMainUI.url = webViewUrl;
mpMainUI.insets = new UniWebViewEdgeInsets(UniWebViewHelper.screenHeight / 12, 0, 0, 0);
mpMainUI.OnReceivedMessage += MPMainUIOnReceivedMessage;
mpMainUI.OnLoadComplete += MPMainUIOnLoadComplete;
mpMainUI.Load();
mpMainUI.Show();
}
public void CloseMolpay()
{
mpMainUI.EvaluatingJavaScript("closemolpay()");
if (isClosingReceipt)
{
isClosingReceipt = false;
Finish();
}
}
private void MPMainUIOnReceivedMessage(UniWebView webView, UniWebViewMessage message)
{
try
{
string loadingUrl = string.Empty;
if (message.rawMessage != null && message.rawMessage != string.Empty)
{
loadingUrl = message.rawMessage.Replace(uniwebview, "");
}
if (loadingUrl != string.Empty && loadingUrl.StartsWith(mpopenmolpaywindow))
{
mpMainUI.Stop();
string base64String = loadingUrl.Replace(mpopenmolpaywindow, "");
#if UNITY_IOS
base64String = base64String.Replace ("-", "+");
base64String = base64String.Replace ("_", "=");
#endif
byte[] data = Convert.FromBase64String(base64String);
string decodedString = Encoding.UTF8.GetString(data);
if (decodedString.Length > 0)
{
mpMOLPayUI = CreateWebView();
LoadFromText(mpMOLPayUI, decodedString);
mpMOLPayUI.OnLoadBegin += MPMOLPayUIOnLoadBegin;
mpMOLPayUI.OnLoadComplete += MPMOLPayUIOnLoadComplete;
}
}
else if (loadingUrl != string.Empty && loadingUrl.StartsWith(mpcloseallwindows))
{
if (mpBankUI != null)
{
mpBankUI.url = "about:blank";
mpBankUI.Load();
mpBankUI.Hide();
mpBankUI.CleanCache();
mpBankUI.CleanCookie();
mpBankUI = null;
}
mpMOLPayUI.url = "about:blank";
mpMOLPayUI.Load();
mpMOLPayUI.Hide();
mpMOLPayUI.CleanCache();
mpMOLPayUI.CleanCookie();
mpMOLPayUI = null;
}
else if (loadingUrl != string.Empty && loadingUrl.StartsWith(mptransactionresults))
{
string base64String = loadingUrl.Replace(mptransactionresults, "");
#if UNITY_IOS
base64String = base64String.Replace ("-", "+");
base64String = base64String.Replace ("_", "=");
#endif
byte[] data = Convert.FromBase64String(base64String);
string decodedString = Encoding.UTF8.GetString(data);
if (decodedString.Length > 0)
{
transactionResult = decodedString;
try
{
Dictionary<string, object> jsonResult = Json.Deserialize(transactionResult) as Dictionary<string, object>;
object requestType;
jsonResult.TryGetValue("mp_request_type", out requestType);
if (!jsonResult.ContainsKey("mp_request_type") || (string)requestType != "Receipt" || jsonResult.ContainsKey("error_code"))
{
Finish();
}
else
{
isClosingReceipt = true;
}
}
catch (Exception)
{
Finish();
}
}
}
else if (loadingUrl != string.Empty && loadingUrl.StartsWith(mprunscriptonpopup))
{
string base64String = loadingUrl.Replace(mprunscriptonpopup, "");
#if UNITY_IOS
base64String = base64String.Replace ("-", "+");
base64String = base64String.Replace ("_", "=");
#endif
byte[] data = Convert.FromBase64String(base64String);
string decodedString = Encoding.UTF8.GetString(data);
if (decodedString.Length > 0)
{
mpMOLPayUI.EvaluatingJavaScript(decodedString);
}
}
else if (loadingUrl != string.Empty && loadingUrl.StartsWith(mppinstructioncapture))
{
string base64String = loadingUrl.Replace(mppinstructioncapture, "");
#if UNITY_IOS
base64String = base64String.Replace ("-", "+");
base64String = base64String.Replace ("_", "=");
#endif
byte[] data = Convert.FromBase64String(base64String);
string decodedString = Encoding.UTF8.GetString(data);
Dictionary<string, object> jsonResult = Json.Deserialize(decodedString) as Dictionary<string, object>;
object base64ImageUrlData;
jsonResult.TryGetValue("base64ImageUrlData", out base64ImageUrlData);
object filename;
jsonResult.TryGetValue("filename", out filename);
byte[] imageData = Convert.FromBase64String(base64ImageUrlData.ToString());
string[] temp = (Application.persistentDataPath.Replace("Android","")).Split(new string[] { "//" }, System.StringSplitOptions.None);
string imageLocation = temp[0] + "/Download/" + filename.ToString();
File.WriteAllBytes(imageLocation, imageData);
#if UNITY_IOS
_SavePhoto(imageLocation);
#elif UNITY_ANDROID
using (AndroidJavaClass jcUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject joActivity = jcUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (AndroidJavaObject joContext = joActivity.Call<AndroidJavaObject>("getApplicationContext"))
using (AndroidJavaClass jcMediaScannerConnection = new AndroidJavaClass("android.media.MediaScannerConnection"))
using (AndroidJavaClass jcEnvironment = new AndroidJavaClass("android.os.Environment"))
using (AndroidJavaObject joExDir = jcEnvironment.CallStatic<AndroidJavaObject>("getExternalStorageDirectory"))
{
jcMediaScannerConnection.CallStatic("scanFile", joContext, new string[] { imageLocation }, null, null);
}
#endif
Debug.Log("imageLocation: " + imageLocation);
if (System.IO.File.Exists(imageLocation))
{
NPBinding.UI.ShowAlertDialogWithSingleButton("Info", "Image saved", "OK", OnButtonPressed);
}
else
{
NPBinding.UI.ShowAlertDialogWithSingleButton("Info", "Image not saved", "OK", OnButtonPressed);
}
}
}
catch (Exception)
{
}
}
private void OnButtonPressed(string _buttonPressed)
{
return;
}
private void MPMainUIOnLoadComplete(UniWebView webView, bool success, string errorMessage)
{
if (success)
{
paymentDetails.Add(module_id, "molpay-mobile-xdk-unity3d");
paymentDetails.Add(wrapper_version, "1");
paymentDetails.Add(webview_url_prefix, uniwebview);
webView.EvaluatingJavaScript("updateSdkData(" + Json.Serialize(paymentDetails) + ")");
webView.OnLoadComplete -= MPMainUIOnLoadComplete;
}
}
private void MPMOLPayUIOnLoadBegin(UniWebView webView, string loadingUrl)
{
if (loadingUrl != null && loadingUrl.Contains(molpayresulturl))
{
NativeWebRequestUrlUpdates(mpMainUI, loadingUrl);
}
else if (loadingUrl != null && loadingUrl.Contains(molpaynbepayurl))
{
hijackWindowOpen = true;
}
finishLoadUrl = loadingUrl;
}
private void MPMOLPayUIOnLoadComplete(UniWebView webView, bool success, string errorMessage)
{
if (hijackWindowOpen)
{
webView.EvaluatingJavaScript("window.open = function (open) {" +
"return function(url, name, features) {" +
"window.location = url;" +
"return window;" +
"};" +
"} (window.open); ");
}
NativeWebRequestUrlUpdates(mpMainUI, finishLoadUrl);
}
private UniWebView CreateWebView()
{
var webViewGameObject = GameObject.Find("WebView");
if (webViewGameObject == null)
{
webViewGameObject = new GameObject("WebView");
}
var webView = webViewGameObject.AddComponent<UniWebView>();
webView.toolBarShow = false;
webView.backButtonEnable = false;
return webView;
}
private void LoadFromText(UniWebView webView, string htmlText)
{
webView.LoadHTMLString(htmlText, null);
webView.insets = new UniWebViewEdgeInsets(UniWebViewHelper.screenHeight / 12, 0, 0, 0);
webView.Show();
}
private void NativeWebRequestUrlUpdates(UniWebView webView, string url)
{
try
{
Dictionary<string, object> data = new Dictionary<string, object>();
data.Add("requestPath", url);
webView.EvaluatingJavaScript("nativeWebRequestUrlUpdates(" + Json.Serialize(data) + ")");
}
catch (Exception)
{
}
}
private void Finish ()
{
mpMainUI.url = "about:blank";
mpMainUI.Load();
mpMainUI.Hide();
mpMainUI.CleanCache();
mpMainUI.CleanCookie();
mpMainUI = null;
callback(transactionResult);
}
}
}