Commit d2bceb1b by patpat

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

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