lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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.zhaoyang.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.zhaoyang.driver.base.BaseEvent;
import com.zhaoyang.driver.bean.OrderPayBean;
import com.zhaoyang.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;
 
    public static String wxXCXId = "wx94a1a55229c933ac";
 
    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);
    }
 
    /***
     * 小程序支付
     * @param context
     * @param intent
     * @param path
     */
    public static void toWx(Context context,Intent intent,String path){
        IWXAPI api = WXAPIFactory.createWXAPI(context, WXEntryActivity.WX_APP_ID);
        api.handleIntent(intent,handler);
        api.registerApp(WXEntryActivity.WX_APP_ID);
        WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
        req.userName = wxXCXId;
        req.path = path;
        req.miniprogramType = Api.WXLAUID; // WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;
        api.sendReq(req);
    }
 
    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, 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();
    }
}