72 lines
2.6 KiB
Java
72 lines
2.6 KiB
Java
package com.qxcm.qxlive.wxapi;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Intent;
|
||
import android.os.Bundle;
|
||
|
||
import com.blankj.utilcode.util.ToastUtils;
|
||
import com.tencent.mm.opensdk.constants.ConstantsAPI;
|
||
import com.tencent.mm.opensdk.modelbase.BaseReq;
|
||
import com.tencent.mm.opensdk.modelbase.BaseResp;
|
||
import com.tencent.mm.opensdk.openapi.IWXAPI;
|
||
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
|
||
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
||
import com.xscm.moduleutil.base.CommonAppContext;
|
||
import com.xscm.moduleutil.event.PayEvent;
|
||
|
||
import org.greenrobot.eventbus.EventBus;
|
||
|
||
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
|
||
private IWXAPI api;
|
||
public static final String APP_ID = CommonAppContext.getInstance().getCurrentEnvironment().getWxAppId();
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
api = WXAPIFactory.createWXAPI(this, APP_ID);
|
||
api.handleIntent(getIntent(), this); // 必须调用,否则无法接收回调
|
||
}
|
||
|
||
@Override
|
||
protected void onNewIntent(Intent intent) {
|
||
super.onNewIntent(intent);
|
||
setIntent(intent);
|
||
api.handleIntent(intent, this);
|
||
}
|
||
|
||
@Override
|
||
public void onReq(BaseReq baseReq) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onResp(BaseResp resp) {
|
||
// 支付结果回调(resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX)
|
||
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
|
||
switch (resp.errCode) {
|
||
case BaseResp.ErrCode.ERR_OK:
|
||
// 支付成功:这里需要调用后台接口确认支付状态(避免本地判断不可靠)
|
||
PayEvent messageEvent = new PayEvent(1, "支付成功");
|
||
EventBus.getDefault().post(messageEvent);
|
||
break;
|
||
case BaseResp.ErrCode.ERR_USER_CANCEL:
|
||
checkPayResultFromServer();
|
||
break;
|
||
default:
|
||
// 支付失败
|
||
checkPayResultFromServer();
|
||
break;
|
||
}
|
||
finish(); // 处理完后关闭页面
|
||
}
|
||
}
|
||
|
||
// 关键:必须从后台确认支付状态(不能仅依赖前端回调)
|
||
private void checkPayResultFromServer() {
|
||
// 调用后台接口,传入订单号查询实际支付状态
|
||
// 用户取消支付
|
||
ToastUtils.showShort("支付取消");
|
||
PayEvent messageEvent = new PayEvent(-2, "支付取消");
|
||
EventBus.getDefault().post(messageEvent);
|
||
}
|
||
} |