|
36 | 36 | import android.widget.RelativeLayout; |
37 | 37 | import android.widget.Toast; |
38 | 38 |
|
| 39 | +import org.json.JSONArray; |
| 40 | +import org.json.JSONException; |
39 | 41 | import org.json.JSONObject; |
40 | 42 |
|
41 | 43 | import java.io.File; |
@@ -94,15 +96,15 @@ public class MOLPayActivity extends AppCompatActivity { |
94 | 96 | public final static String mp_dpa_id = "mp_dpa_id"; |
95 | 97 | public final static String mp_company = "mp_company"; |
96 | 98 |
|
97 | | - public final static String MOLPAY = "MOLPAY"; |
| 99 | + public final static String MOLPAY = "logMOLPAY"; |
98 | 100 | private final static String mpopenmolpaywindow = "mpopenmolpaywindow://"; |
99 | 101 | private final static String mpcloseallwindows = "mpcloseallwindows://"; |
100 | 102 | private final static String mptransactionresults = "mptransactionresults://"; |
101 | 103 | private final static String mprunscriptonpopup = "mprunscriptonpopup://"; |
102 | 104 | private final static String mppinstructioncapture = "mppinstructioncapture://"; |
103 | 105 | private final static String module_id = "module_id"; |
104 | 106 | private final static String wrapper_version = "wrapper_version"; |
105 | | - private final static String wrapperVersion = "0"; |
| 107 | + private final static String wrapperVersion = "9"; |
106 | 108 |
|
107 | 109 | private String base64Img; |
108 | 110 | private String filename; |
@@ -418,8 +420,13 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) { |
418 | 420 |
|
419 | 421 | if (dataString.length() > 0) { |
420 | 422 | Log.d(MOLPAY, "MPMainUIWebClient mpopenmolpaywindow success"); |
421 | | - mpMOLPayUI.loadDataWithBaseURL("", dataString, "text/html", "UTF-8", ""); |
422 | | - mpMOLPayUI.setVisibility(View.VISIBLE); |
| 423 | + if (mpMOLPayUI != null) { |
| 424 | + Log.d(MOLPAY, "mpMOLPayUI not NULL update UI"); |
| 425 | + mpMOLPayUI.loadDataWithBaseURL("", dataString, "text/html", "UTF-8", ""); |
| 426 | + mpMOLPayUI.setVisibility(View.VISIBLE); |
| 427 | + } else { |
| 428 | + Log.d(MOLPAY, "mpMOLPayUI NULL avoid crash"); |
| 429 | + } |
423 | 430 | } else { |
424 | 431 | Log.d(MOLPAY, "MPMainUIWebClient mpopenmolpaywindow empty dataString"); |
425 | 432 | } |
@@ -454,23 +461,31 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) { |
454 | 461 |
|
455 | 462 | Intent result = new Intent(); |
456 | 463 | result.putExtra(MOLPayTransactionResult, dataString); |
457 | | - setResult(RESULT_OK, result); |
458 | 464 |
|
459 | | - // Check if mp_request_type is "Receipt", if it is, don't finish() |
460 | | - try { |
461 | | - JSONObject jsonResult = new JSONObject(dataString); |
| 465 | + if (isJSONValid(dataString)){ |
| 466 | + Log.d(MOLPAY, "isJSONValid setResult"); |
| 467 | + setResult(RESULT_OK, result); |
462 | 468 |
|
463 | | - Log.d(MOLPAY, "MPMainUIWebClient jsonResult = " + jsonResult); |
| 469 | + // Check if mp_request_type is "Receipt", if it is, don't finish() |
| 470 | + try { |
| 471 | + JSONObject jsonResult = new JSONObject(dataString); |
| 472 | + |
| 473 | + Log.d(MOLPAY, "MPMainUIWebClient jsonResult = " + jsonResult); |
464 | 474 |
|
465 | | - if (!jsonResult.has("mp_request_type") || !jsonResult.getString("mp_request_type").equals("Receipt") || jsonResult.has("error_code")) { |
| 475 | + if (!jsonResult.has("mp_request_type") || !jsonResult.getString("mp_request_type").equals("Receipt") || jsonResult.has("error_code")) { |
| 476 | + finish(); |
| 477 | + } else { |
| 478 | + // Next close button click will finish() the activity |
| 479 | + isClosingReceipt = true; |
| 480 | + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); |
| 481 | + } |
| 482 | + } catch (Throwable t) { |
466 | 483 | finish(); |
467 | | - } else { |
468 | | - // Next close button click will finish() the activity |
469 | | - isClosingReceipt = true; |
470 | | - getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); |
471 | 484 | } |
472 | | - } catch (Throwable t) { |
473 | | - finish(); |
| 485 | + } |
| 486 | + else { |
| 487 | + Log.d(MOLPAY, "json not valid dont setResult"); |
| 488 | +// setResult(RESULT_CANCELED, result); |
474 | 489 | } |
475 | 490 |
|
476 | 491 | } else if (url != null && url.startsWith(mprunscriptonpopup)) { |
@@ -535,6 +550,20 @@ public void onPageFinished(WebView view, String url) { |
535 | 550 |
|
536 | 551 | } |
537 | 552 |
|
| 553 | + public boolean isJSONValid(String test) { |
| 554 | + try { |
| 555 | + new JSONObject(test); |
| 556 | + } catch (JSONException ex) { |
| 557 | + // in case JSONArray is valid as well |
| 558 | + try { |
| 559 | + new JSONArray(test); |
| 560 | + } catch (JSONException ex1) { |
| 561 | + return false; |
| 562 | + } |
| 563 | + } |
| 564 | + return true; |
| 565 | + } |
| 566 | + |
538 | 567 | private boolean storeImage(Bitmap image) { |
539 | 568 | String fullPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(); |
540 | 569 |
|
@@ -568,7 +597,7 @@ private boolean storeImage(Bitmap image) { |
568 | 597 | @TargetApi(23) |
569 | 598 | public boolean isStoragePermissionGranted() { |
570 | 599 | if (Build.VERSION.SDK_INT >= 23) { |
571 | | - if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { |
| 600 | + if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { |
572 | 601 | Log.d(MOLPAY, "isStoragePermissionGranted Permission granted"); |
573 | 602 | storeImage(imgBitmap); |
574 | 603 | return true; |
|
0 commit comments