lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
package com.kuanzhai.driver.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.kuanzhai.driver.base.BaseEvent;
import com.kuanzhai.driver.bean.OrderPayBean;
import com.kuanzhai.driver.netUtls.Api;
import com.lljjcoder.style.citylist.Toast.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.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.greenrobot.eventbus.EventBus;
 
 
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
 
    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
 
    private IWXAPI api;
 
    private int type;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this,WXEntryActivity.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();
    }
 
    /***
     *
     * @param context
     * @param type 1 去支付
     */
    public static void to(Context context, int type, OrderPayBean payBean) {
        Intent intent = new Intent(context, WXPayEntryActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("data", payBean);
        context.startActivity(intent);
    }
 
    private void initApi() {
        api = WXAPIFactory.createWXAPI(this, WXEntryActivity.WX_APP_ID, false);
        api.handleIntent(getIntent(), this);
        api.registerApp(WXEntryActivity.WX_APP_ID);
    }
 
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }
 
    @Override
    public void onReq(BaseReq req) {
 
    }
 
    @SuppressLint("LongLogTag")
    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.toString());
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0){
                EventBus.getDefault().post(new BaseEvent(BaseEvent.PAY_SUCCESS));
            }else {
                ToastUtils.showShortToast(this,"支付失败");
//                EventBus.getDefault().post(new BaseEvent(BaseEvent.PAY_FAIL,resp.errStr));
            }
            EventBus.getDefault().post(new BaseEvent(BaseEvent.WEIXIN_BACK));
//            finish();
        }
    }
 
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}