WvPayYyrhDialog.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package com.yyrh.pay;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.webkit.DownloadListener;
  9. import android.webkit.WebChromeClient;
  10. import android.webkit.WebSettings;
  11. import android.webkit.WebView;
  12. import android.webkit.WebViewClient;
  13. import android.widget.ImageView;
  14. import android.widget.LinearLayout;
  15. import com.yyrh.constant.SDKSettings;
  16. import com.yyrh.ui.PageJsInteraction;
  17. import com.yyrh.sdk.YyrhSdkManager;
  18. import com.yyrh.ui.BaseDialog;
  19. import com.yyrh.utils.ResourceUtil;
  20. import com.yyrh.utils.TosUtil;
  21. import com.yyrh.utils.Utils;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24. import java.util.HashMap;
  25. import static com.yyrh.constant.SDKSettings.isLandscape;
  26. public class WvPayYyrhDialog extends BaseDialog implements View.OnClickListener{
  27. private Context context;
  28. private WebView webView;
  29. private WebSettings webSettings;
  30. private String url,orderId;
  31. private ImageView iv_reback_close;
  32. private HashMap<String, Object> payMap;
  33. public WvPayYyrhDialog(Context context) {
  34. super(context);
  35. this.context = context;
  36. }
  37. public WvPayYyrhDialog(Context context, int theme, String url, HashMap<String, Object> map) {
  38. super(context, theme);
  39. this.context = context;
  40. this.payMap = map;
  41. this.url = url;
  42. }
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(ResourceUtil.getLayoutId(context,
  47. "qj_p_webview"));
  48. if (!isLandscape){
  49. LinearLayout ll_p_wb = findViewById(ResourceUtil.getId(context, "ll_p_wb"));
  50. Float x = Float.valueOf("0.9").floatValue();
  51. Float y = Float.valueOf("0.9").floatValue();
  52. ll_p_wb.setScaleX(x);
  53. ll_p_wb.setScaleY(y);
  54. }
  55. iv_reback_close = findViewById(ResourceUtil.getId(context,
  56. "iv_reback_close"));
  57. iv_reback_close.setOnClickListener(this);
  58. webView = findViewById(ResourceUtil.getId(context,
  59. "wb_with_xd"));
  60. webSettings = webView.getSettings();
  61. webSettings.setAllowFileAccess(true);
  62. webSettings.setJavaScriptEnabled(true);
  63. webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
  64. webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
  65. webSettings.setBuiltInZoomControls(false);// 设置支持缩放
  66. webSettings.setDomStorageEnabled(true);
  67. webView.addJavascriptInterface(new PageJsInteraction(this), "yyrhsdk");
  68. webView.setHorizontalScrollBarEnabled(false);//水平不显示
  69. webView.setVerticalScrollBarEnabled(false); //垂直不显示
  70. webView.setWebChromeClient(new WebChromeClient());
  71. webView.setWebViewClient(new WebViewClient(){
  72. @Override
  73. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  74. String httpHead = url.substring(0, 5);
  75. if (httpHead.equals("http:") || httpHead.equals("https")) {
  76. return super.shouldOverrideUrlLoading(view, url);
  77. } else if (url.equals("yyrhsdk://pay_close")) {
  78. dismiss();
  79. return true;
  80. } else if (url.equals("yyrhsdk://pay_success")) {
  81. dismiss();
  82. return true;
  83. } else {
  84. if (httpHead.equals("yyrhsdk")) {
  85. toDoHttp_https(url);
  86. return true;
  87. } else {
  88. openWXPay(url);
  89. return true;
  90. }
  91. }
  92. }
  93. @Override
  94. public void onPageFinished(WebView view, String url) {
  95. super.onPageFinished(view, url);
  96. }
  97. private void toDoHttp_https(String url) {
  98. String[] strArray = url.split("=");
  99. String strurl = Utils.decoBase64(strArray[1]);
  100. String code_url = null;
  101. try {
  102. JSONObject json = new JSONObject(strurl);
  103. code_url = json.getString("code_url");
  104. } catch (JSONException e1) {
  105. // TODO Auto-generated catch block
  106. e1.printStackTrace();
  107. }
  108. openWXPay(code_url);
  109. }
  110. private void openWXPay(String code_url) {
  111. try {
  112. Intent it = new Intent(Intent.ACTION_VIEW);
  113. it.setData(Uri.parse(code_url));
  114. context.startActivity(it);
  115. } catch (Exception e) {
  116. }
  117. }
  118. });
  119. webView.setDownloadListener(new DownloadListener() {
  120. @Override
  121. public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
  122. Uri uri = Uri.parse(url);
  123. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  124. context.startActivity(intent);
  125. }
  126. });
  127. webView.loadUrl(url);
  128. }
  129. @Override
  130. public void onClick(View v) {
  131. int id = v.getId();
  132. if (id == ResourceUtil.getId(context, "iv_reback_close")) {
  133. dismiss();
  134. }
  135. }
  136. @Override
  137. public void dismiss() {
  138. super.dismiss();
  139. if (!Utils.judgeStrNull(orderId)){
  140. YyrhSdkManager.defaultManager(context).queryOrder(orderId,this.payMap);
  141. }
  142. }
  143. @Override
  144. public void zfOrder(String message) {
  145. super.zfOrder(message);
  146. orderId = message;
  147. }
  148. @Override
  149. public void zfColse() {
  150. super.zfColse();
  151. dismiss();
  152. }
  153. @Override
  154. public void showJsMsg(String message) {
  155. super.showJsMsg(message);
  156. new TosUtil(context,TosUtil.TOAST_ERROR, message).show();
  157. }
  158. }