fix
lmw
2023-03-14 128cbeac95dbc995fe1760bbd0f0a985fa5d23ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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();
    }
}