Commit d2bceb1b by patpat

在线支付增加3次重试机制,修改超时文案

parent 89869522
...@@ -19,15 +19,19 @@ ...@@ -19,15 +19,19 @@
<activity android:name=".activity.MainActivity"> <activity android:name=".activity.MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="canteen"/>
</intent-filter>
</activity> </activity>
<!--<receiver android:name=".receiver.BootReceiver" >--> <!--<receiver android:name=".receiver.BootReceiver" >-->
......
...@@ -7,6 +7,7 @@ public abstract class Action { ...@@ -7,6 +7,7 @@ public abstract class Action {
RESPONSE_FAIL, RESPONSE_FAIL,
RESQUEST_TIMEOUT, RESQUEST_TIMEOUT,
RESQUEST_FAIL, RESQUEST_FAIL,
TIMEOUT,
FAIL, FAIL,
UNINIT, UNINIT,
INITED, INITED,
......
package com.bgycc.smartcanteen.action; package com.bgycc.smartcanteen.action;
import android.util.Log;
import com.bgycc.smartcanteen.App; import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.event.PayStateEvent; import com.bgycc.smartcanteen.event.PayStateEvent;
import com.bgycc.smartcanteen.helper.TimerHelper; import com.bgycc.smartcanteen.helper.TimerHelper;
...@@ -9,10 +8,6 @@ import com.bgycc.smartcanteen.util.DateUtil; ...@@ -9,10 +8,6 @@ import com.bgycc.smartcanteen.util.DateUtil;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject; import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class PayOnlineAction extends Action { public class PayOnlineAction extends Action {
private static final String TAG = PayOnlineAction.class.getSimpleName(); private static final String TAG = PayOnlineAction.class.getSimpleName();
...@@ -26,16 +21,18 @@ public class PayOnlineAction extends Action { ...@@ -26,16 +21,18 @@ public class PayOnlineAction extends Action {
} }
private String mPayCode; private String mPayCode;
private long mResponseTimeoutId = -1; private long mTimeoutId = -1;
private Runnable mActionRunnable;
public PayOnlineAction() { public PayOnlineAction() {
super(ActionEnum.PAY_ONLINE.name()); super(ActionEnum.PAY_ONLINE.name());
MainWebSocket.subscribe(ActionEnum.PAY_RESULT.name(), mPayResultResponse); MainWebSocket.subscribe(ActionEnum.PAY_RESULT.name(), mPayResultResponse);
} }
private void cancelResponseTimeout() { private void cancelTimeout() {
TimerHelper.INSTANCE.cancel(mResponseTimeoutId); TimerHelper.INSTANCE.cancel(mTimeoutId);
mResponseTimeoutId = -1; mTimeoutId = -1;
mActionRunnable = null;
} }
private void setState(State state, String message) { private void setState(State state, String message) {
...@@ -46,19 +43,11 @@ public class PayOnlineAction extends Action { ...@@ -46,19 +43,11 @@ public class PayOnlineAction extends Action {
break; break;
case STARTED: case STARTED:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.WAIT, message)); EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.WAIT, message));
cancelResponseTimeout(); cancelTimeout();
break;
case RESQUEST_SUCCESS:
mResponseTimeoutId = TimerHelper.INSTANCE.timeout(new Runnable() {
@Override
public void run() {
setState(State.RESPONSE_TIMEOUT, "交易失败");
}
}, 10000);
break; break;
case RESPONSE_SUCCESS: case RESPONSE_SUCCESS:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.SUCCESS, message)); EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.SUCCESS, message));
cancelResponseTimeout(); cancelTimeout();
mPayCode = ""; mPayCode = "";
success(message); success(message);
TimerHelper.INSTANCE.timeout(new Runnable() { TimerHelper.INSTANCE.timeout(new Runnable() {
...@@ -69,11 +58,10 @@ public class PayOnlineAction extends Action { ...@@ -69,11 +58,10 @@ public class PayOnlineAction extends Action {
}, 3000); }, 3000);
break; break;
case RESQUEST_FAIL: case RESQUEST_FAIL:
case RESQUEST_TIMEOUT:
case RESPONSE_FAIL: case RESPONSE_FAIL:
case RESPONSE_TIMEOUT: case TIMEOUT:
EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message)); EventBus.getDefault().post(new PayStateEvent(PayStateEvent.StateEnum.FAIL, message));
cancelResponseTimeout(); cancelTimeout();
mPayCode = ""; mPayCode = "";
fail(message); fail(message);
TimerHelper.INSTANCE.timeout(new Runnable() { TimerHelper.INSTANCE.timeout(new Runnable() {
...@@ -100,34 +88,44 @@ public class PayOnlineAction extends Action { ...@@ -100,34 +88,44 @@ public class PayOnlineAction extends Action {
setActionResult(result); setActionResult(result);
mPayCode = payCode; mPayCode = payCode;
final long timeoutId = TimerHelper.INSTANCE.timeout(new Runnable() { mTimeoutId = TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
@Override @Override
public void run() { public void run(long id, boolean isLastTime) {
setState(State.RESQUEST_TIMEOUT, "交易失败"); if (isLastTime) {
setState(State.TIMEOUT, "交易超时");
} else {
if (mActionRunnable != null) {
mActionRunnable.run();
}
}
} }
}, 10000); }, 10000, 4, 10000);
final MainWebSocket.Response response = new MainWebSocket.Response() { final MainWebSocket.Response response = new MainWebSocket.Response() {
@Override @Override
protected void onSuccess(JSONObject data, String message) { protected void onSuccess(JSONObject data, String message) {
TimerHelper.INSTANCE.cancel(timeoutId);
setState(State.RESQUEST_SUCCESS, message); setState(State.RESQUEST_SUCCESS, message);
} }
@Override @Override
protected void onFail(JSONObject data, String message, String code) { protected void onFail(JSONObject data, String message, String code) {
TimerHelper.INSTANCE.cancel(timeoutId);
setState(State.RESQUEST_FAIL, message); setState(State.RESQUEST_FAIL, message);
} }
}; };
try { try {
JSONObject params = new JSONObject(); final JSONObject params = new JSONObject();
params.put("equipmentNo", App.Companion.getDeviceSN()); params.put("equipmentNo", App.Companion.getDeviceSN());
params.put("payCode", payCode); params.put("payCode", payCode);
params.put("terminalType", payCodeType); params.put("terminalType", payCodeType);
params.put("time", DateUtil.getCurrentTimestamp()); params.put("time", DateUtil.getCurrentTimestamp());
setState(State.RESQUEST, ""); mActionRunnable = new Runnable() {
MainWebSocket.action(getAction(), params, response); @Override
public void run() {
setState(State.RESQUEST, "");
MainWebSocket.action(getAction(), params, response);
}
};
mActionRunnable.run();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -140,6 +138,7 @@ public class PayOnlineAction extends Action { ...@@ -140,6 +138,7 @@ public class PayOnlineAction extends Action {
String payCode = data.optString("payCode", ""); String payCode = data.optString("payCode", "");
if (payCode.equals(mPayCode)) { if (payCode.equals(mPayCode)) {
setState(State.RESPONSE_SUCCESS, message); setState(State.RESPONSE_SUCCESS, message);
MainWebSocket.response(data.optInt("serialNumber", -1), "0", "已接收");
} }
} }
@Override @Override
...@@ -148,6 +147,7 @@ public class PayOnlineAction extends Action { ...@@ -148,6 +147,7 @@ public class PayOnlineAction extends Action {
String payCode = data.optString("payCode", ""); String payCode = data.optString("payCode", "");
if (payCode.equals(mPayCode)) { if (payCode.equals(mPayCode)) {
setState(State.RESPONSE_FAIL, message); setState(State.RESPONSE_FAIL, message);
MainWebSocket.response(data.optInt("serialNumber", -1), "0", "已接收");
} }
} }
}; };
......
...@@ -110,7 +110,7 @@ public class MainWebSocket extends WebSocketClient { ...@@ -110,7 +110,7 @@ public class MainWebSocket extends WebSocketClient {
return list.remove(response); return list.remove(response);
} }
private static void response(int sessionId, String code, String message) { public static void response(int sessionId, String code, String message) {
try { try {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put(FieldEnum.equipmentId.name(), sDeviceSN); json.put(FieldEnum.equipmentId.name(), sDeviceSN);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment