Commit d8e8eb97 by patpat

增加离线支付action

parent 9239cb6e
package com.bgycc.smartcanteen.action;
import com.bgycc.smartcanteen.App;
import com.bgycc.smartcanteen.event.PayStateEvent;
import com.bgycc.smartcanteen.server.websocket.MainWebSocket;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class PayOfflineAction extends Action {
public PayOfflineAction() {
super(ActionEnum.PAY_OFFLINE.name());
}
@Override
protected void setState(State state) {
if (state != getState()) {
switch (state) {
case RESQUEST_SUCCESS:
success("");
break;
case RESQUEST_FAIL:
case RESQUEST_TIMEOUT:
fail("");
break;
}
}
setState(state);
}
public void exec(String payCode, String payCodeType, ActionResult result) {
if (getState() != State.INITED) return;
setState(State.STARTED);
setActionResult(result);
final MainWebSocket.Response response = new MainWebSocket.Response() {
@Override
protected void onSuccess(JSONObject data, String message) {
cancel();
setState(State.RESQUEST_SUCCESS);
}
@Override
protected void onFail(JSONObject data, String message, String code) {
cancel();
setState(State.RESQUEST_FAIL);
}
};
timeout(new Runnable() {
@Override
public void run() {
if (response.isCancelled()) return;
response.cancel();
setState(State.RESQUEST_TIMEOUT);
}
}, 10000);
try {
JSONObject params = new JSONObject();
params.put("equipmentNo", App.Companion.getDeviceSN());
params.put("payCode", payCode);
params.put("terminalType", payCodeType);
params.put("time", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date()));
setState(State.RESQUEST);
MainWebSocket.action(getAction(), params, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
......@@ -66,18 +66,22 @@ public class PayOnlineAction extends Action {
final MainWebSocket.Response response = new MainWebSocket.Response() {
@Override
protected void onSuccess(JSONObject data, String message) {
cancel();
setState(State.RESQUEST_SUCCESS, message);
}
@Override
protected void onFail(JSONObject data, String message, String code) {
cancel();
setState(State.RESQUEST_FAIL, message);
}
};
timeout(new Runnable() {
@Override
public void run() {
if (response.isCancelled()) return;
response.cancel();
setState(State.RESPONSE_FAIL, "交易失败");
setState(State.RESQUEST_TIMEOUT, "交易失败");
}
}, 10000);
......
......@@ -250,6 +250,9 @@ public class MainWebSocket extends WebSocketClient {
}
}
public boolean isCancelled() {
return this.cancel;
}
public void cancel() {
this.cancel = true;
}
......
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