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();
|
}
|
}
|