package com.fuban.user.wxapi;
|
|
import android.annotation.SuppressLint;
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.util.Log;
|
import android.widget.Toast;
|
|
import com.fuban.user.network.entity.OrderPayBean;
|
import com.fuban.user.utils.Const;
|
import com.fuban.user.utils.pay.PayListener;
|
import com.fuban.user.utils.pay.PayUtil;
|
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.modelbiz.WXLaunchMiniProgram;
|
import com.tencent.mm.opensdk.modelpay.PayReq;
|
import com.tencent.mm.opensdk.openapi.IWXAPI;
|
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
|
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
|
import org.jetbrains.anko.ToastsKt;
|
|
import cn.sinata.xldutils.UtilKt;
|
|
|
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
|
|
private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
|
|
private IWXAPI api;
|
|
public static String wxXCXId = "gh_bc8f199f11ae";
|
|
private int type;
|
|
@Override
|
public void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
api = WXAPIFactory.createWXAPI(this, Const.WX_APP_ID);
|
api.handleIntent(getIntent(), this);
|
initApi();
|
type = getIntent().getIntExtra("type", -1);
|
if (type == 1) {
|
OrderPayBean bean = (OrderPayBean) getIntent().getSerializableExtra("data");
|
if (bean != null) {
|
WXPay(bean);
|
}
|
}
|
}
|
|
public void WXPay(OrderPayBean orderInfo) {
|
PayReq request = new PayReq();
|
request.appId = orderInfo.getAppid();
|
request.partnerId = orderInfo.getPartnerid();
|
request.prepayId = orderInfo.getPrepayid();
|
request.packageValue = orderInfo.getPackageX();
|
request.nonceStr = orderInfo.getNoncestr();
|
request.timeStamp = orderInfo.getTimestamp();
|
request.sign = orderInfo.getSign();
|
Boolean Pay = api.sendReq(request);
|
// Toast.makeText(this, "发起支付", Toast.LENGTH_SHORT).show();
|
finish();
|
}
|
|
|
|
private void initApi() {
|
api = WXAPIFactory.createWXAPI(this, Const.WX_APP_ID, false);
|
api.handleIntent(getIntent(), this);
|
api.registerApp(Const.WX_APP_ID);
|
}
|
|
|
static IWXAPIEventHandler handler = new IWXAPIEventHandler() {
|
@Override
|
public void onReq(BaseReq baseReq) {
|
|
}
|
|
@Override
|
public void onResp(BaseResp baseResp) {
|
if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
|
WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
|
String extraData =launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
|
|
}
|
}
|
};
|
|
|
@Override
|
protected void onNewIntent(Intent intent) {
|
super.onNewIntent(intent);
|
setIntent(intent);
|
// api.handleIntent(intent, handler);
|
}
|
|
@Override
|
public void onReq(BaseReq req) {
|
|
}
|
|
Long time = 0L;
|
@SuppressLint("LongLogTag")
|
@Override
|
public void onResp(BaseResp resp) {
|
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
|
|
if (resp.errCode == 0) {
|
ToastsKt.toast(this,"支付成功");
|
//使用广播模式通知支付页面
|
// val intent = Intent(Const.PAY_ACTION)
|
// sendBroadcast(intent)
|
for (int i = 0; i <PayUtil.INSTANCE.getPayListeners().size(); i++) {
|
if (i == PayUtil.INSTANCE.getPayListeners().size()-1){
|
PayListener listener = PayUtil.INSTANCE.getPayListeners().get(i);
|
if (System.currentTimeMillis() - time < 1000){
|
return;
|
}
|
listener.onPaySuccess();
|
time = System.currentTimeMillis();
|
}
|
|
}
|
} else {
|
if (resp.errCode == -2) {
|
ToastsKt.toast(this,"取消支付");
|
for (int i = 0; i <PayUtil.INSTANCE.getPayListeners().size(); i++) {
|
if (i == PayUtil.INSTANCE.getPayListeners().size()-1){
|
PayListener listener = PayUtil.INSTANCE.getPayListeners().get(i);
|
listener.onPayCancel();
|
}
|
}
|
} else if (resp.errCode == -1) {
|
ToastsKt.toast(this,"支付出现错误!");
|
}
|
}
|
}
|
finish();
|
}
|
|
@Override
|
public void onBackPressed() {
|
super.onBackPressed();
|
}
|
}
|