浏览代码

处理支付扫码延迟问题

zhongzhao 1 年之前
父节点
当前提交
211fda838c

+ 2 - 2
app/src/main/java/com/hw/nativeapp/httpnet/okhttp/OKHttpManager.java

@@ -54,8 +54,8 @@ public class OKHttpManager {
     private void initOKHttp() {
 
         okHttpClient = new OkHttpClient().newBuilder()
-                .readTimeout(30000, TimeUnit.SECONDS)//读数据,以秒为单位
-                .connectTimeout(30000, TimeUnit.SECONDS)//连接超时时间,以秒为单位
+                .readTimeout(40000, TimeUnit.SECONDS)//读数据,以秒为单位
+                .connectTimeout(40000, TimeUnit.SECONDS)//连接超时时间,以秒为单位
 //                .cache()
                 .build();
     }

+ 40 - 22
app/src/main/java/com/hw/nativeapp/ui/activity/PaymentMethodActivity.java

@@ -1,6 +1,7 @@
 package com.hw.nativeapp.ui.activity;
 import android.Manifest;
 import android.annotation.SuppressLint;
+import android.app.ProgressDialog;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -9,7 +10,9 @@ import android.content.pm.PackageManager;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.os.CountDownTimer;
+import android.os.Handler;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.ImageView;
@@ -20,6 +23,7 @@ import androidx.core.app.ActivityCompat;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.blankj.utilcode.util.ToastUtils;
 import com.google.zxing.qrcode.encoder.QRCode;
 import com.hjq.bar.OnTitleBarListener;
 import com.hjq.bar.TitleBar;
@@ -29,6 +33,7 @@ import com.hw.nativeapp.httpnet.ErrorConsumer;
 import com.hw.nativeapp.httpnet.ResponseConsumer;
 import com.hw.nativeapp.utils.ActivityUtils;
 import com.hw.nativeapp.utils.ImageUtils;
+import com.hw.nativeapp.utils.MaskUtil;
 import com.hw.nativeapp.utils.RxUtil;
 import com.squareup.picasso.Picasso;
 
@@ -42,6 +47,7 @@ public class PaymentMethodActivity extends BaseActivity  {
     private CountDownTimer timer;
     private CountDownTimer orderTimer;
     private TitleBar titleBar;
+    private ProgressDialog progressDialog;
 
     @BindView(R.id.wechat_pay)
     ImageView wechat_pay;
@@ -88,33 +94,29 @@ public class PaymentMethodActivity extends BaseActivity  {
     @Override
     protected void onStart() {
         super.onStart();
-//        registerReceiver();
+        registerReceiver();
     }
     @Override
     protected void onStop() {
         super.onStop();
         stopOrderCountDownTimer();
-        if (receiver != null){
-            if (receiver != null){
-                try {
-                    unregisterReceiver(receiver);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        }
+//        if (receiver != null){
+//            try {
+//                unregisterReceiver(receiver);
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+//        }
     }
     @Override
     protected void onDestroy() {
         super.onDestroy();
         stopOrderCountDownTimer();
         if (receiver != null){
-            if (receiver != null){
-                try {
-                    unregisterReceiver(receiver);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
+            try {
+                unregisterReceiver(receiver);
+            } catch (Exception e) {
+                e.printStackTrace();
             }
         }
     }
@@ -207,7 +209,8 @@ public class PaymentMethodActivity extends BaseActivity  {
             String code = intent.getStringExtra("data");
             if (code != null && !code.isEmpty())
             {
-                gotoMicroPay(code);
+                String codeStr = code.replace("\n", "");
+                gotoMicroPay(codeStr);
             }
         }
     };
@@ -216,17 +219,30 @@ public class PaymentMethodActivity extends BaseActivity  {
         JSONObject options = new JSONObject();
         options.put("orderId", orderId);
         options.put("authCode", authCode);
-        MApplication.getApiService().gotoNativePay( options, System.currentTimeMillis() / 1000)
+        progressDialog = MaskUtil.showProgressDialog( "支付中...", PaymentMethodActivity.this);
+        new Handler().postDelayed(() -> {
+            if(progressDialog != null){
+                progressDialog.dismiss();
+            }
+        }, 30000);
+        MApplication.getApiService().gotoMicroPay( options, System.currentTimeMillis() / 1000)
                 .compose(RxUtil.applyObservableAsync())
                 .subscribe(new ResponseConsumer<JSONObject>() {
                     @Override
                     public void onSuccess(JSONObject data) {
+                        if(progressDialog != null){
+                            progressDialog.dismiss();
+                        }
                         orderCountDownTimer();
                     }
 
                     @Override
                     public void onFailed(int code, String msg) {
-
+                        if(progressDialog != null){
+                            progressDialog.dismiss();
+                        }
+                        // 支付失败后开启扫码
+                        onSendSerialCmd("#SCNTRG1");
                     }
                 }, new ErrorConsumer());
     }
@@ -246,15 +262,17 @@ public class PaymentMethodActivity extends BaseActivity  {
         IntentFilter fifilter = new IntentFilter();
         fifilter.addAction("com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED");
         registerReceiver(receiver, fifilter);
-        onSendSerialCmd();
+        onSendSerialCmd("@SCNMOD0");
+        onSendSerialCmd("@ORTSET$12000");
+        onSendSerialCmd("#SCNTRG1");
     }
 
     /*
      **发送串口命令
      */
-    public void onSendSerialCmd() {
+    public void onSendSerialCmd(String str) {
         try {
-            String s = "@SCNMOD2";//串口命令,例如:NLS0302010;
+            String s = str;//串口命令,例如:NLS0302010;
             byte[] bytes = s.getBytes();
             byte[] cmd = new byte[bytes.length + 2];
             System.arraycopy(bytes, 0, cmd, 0, bytes.length);

+ 3 - 1
app/src/main/java/com/hw/nativeapp/ui/activity/ScanCodePayActivity.java

@@ -50,7 +50,9 @@ public class ScanCodePayActivity extends BaseActivity  {
 
             @Override
             public void onLeftClick(View v) {
-                ActivityUtils.activityFinish(ScanCodePayActivity.this, true );
+//                ActivityUtils.activityFinish(ScanCodePayActivity.this, true );
+                Intent i = new Intent (ScanCodePayActivity.this  ,  MainActivity.class  ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
+                startActivity(i);
             }
 
             @Override