Android微信支付完整步骤

来源:互联网 发布:国研网由哪些数据库 编辑:程序博客网 时间:2024/06/12 05:19

1、请求服务器获取支付bean信息

     * 请求服务器     */    private void postServer() {        HashMap<String, String> params = new HashMap<>();        params.put("uid", (String) SPUtils.getData(SPUtils.USER_ID,    ""));        params.put("fee", mMoney); //金额        params.put("type", Constants.THREE);//保障金   固定为3        params.put("payType", payType);//支付宝2 微信3        String jsonString = UIUtils.getAesJsonString(params);       mRequest.post(Constants2.WX_ALI_PAY, jsonString, new      DataCallback<String>() {            @Override            public void success(String info, String data) {                try {                    String json = Aes.decryptAES(data);                    if (payType.equals(Constants.TWO)) {                        AliPayBean bean = JsonUtils.jsonToBean(json, AliPayBean.class);                        aliPay(bean);                    } else if (payType.equals(Constants.THREE)) {                        WXPayBean bean = JsonUtils.jsonToBean(json, WXPayBean.class);                        wxPay(bean);                    }                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }

2、调用微信支付

    /**     * 微信支付     */    private void wxPay(WXPayBean bean) {        if (msgApi.getWXAppSupportAPI() >= Build.PAY_SUPPORTED_SDK_INT) {            PayReq req = new PayReq();            req.appId = bean.getAppid();            req.partnerId = bean.getPartnerid();            req.prepayId = bean.getPrepayid();            req.packageValue = "Sign=WXPay"; //固定的            req.nonceStr = bean.getNoncestr();            req.timeStamp = bean.getTimestamp();            req.sign = bean.getSign();            msgApi.sendReq(req);//生成付款请求,提交付款申请        } else {            UIUtils.showToast(this, "你的手机不支持微信支付,请你更新版本或下载微信");        }    }

3、创建WXPayEntryActivity回调类

必须在:包名+wxapi下。如: package com.flyant.android.fh.wxapi;

/** * 微信支付回调 */public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {    private IWXAPI api;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.pay_result);        api = WXAPIFactory.createWXAPI(this, Constants.WX_APPID);        api.handleIntent(getIntent(), this);        LogUtils.d("微信支付。。。onCreate。。");    }    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        setIntent(intent);        api.handleIntent(intent, this);        LogUtils.d("微信支付。。。onNewIntent。。");    }    @Override    public void onReq(BaseReq req) {        LogUtils.d("onReq。。。微信onReq!!!");    }    @Override    /**     * ,微信APP会返回到商户APP并回调onResp函数,开发者需要在该函数中接收通知,判断返回错误码,     * 如果支付成功则去后台查询支付结果再展示用户实际支付结果。     * 注意一定不能以客户端返回作为用户支付的结果,应以服务器端的接收的支付通知或查询API返回的结果为准     *     *    resp.errCode== 0 :表示支付成功     resp.errCode== -1 :表示支付失败     resp.errCode== -2 :表示取消支付     */    public void onResp(BaseResp resp) {//        LogUtils.d("onResp, 微信回调成功 = " + resp.errCode);//        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) { //支付成功的回调码//            UIUtils.showToast(this, "支付成功");//            finish();//            LogUtils.d("resp.errCode===" + resp.errCode);//        }        LogUtils.d("onResp, 微信回调成功 = " + resp.errCode+"---"+(resp.errCode==-2));        if (resp.errCode == 0) {            UIUtils.showToast(this, "支付成功");            finish();        } else if (resp.errCode == -1) {            UIUtils.showToast(this, "支付失败");            finish();        } else if (resp.errCode == -2) {            UIUtils.showToast(this, "取消支付");            finish();        }    }}

4、在清单文件配置Activity

    <!-- 微信支付 -->        <receiver android:name=".wxpay.AppRegister">            <intent-filter>                <action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP"/>            </intent-filter>        </receiver>        <activity            android:name=".wxapi.WXPayEntryActivity"            android:configChanges="screenSize|keyboardHidden|orientation"            android:exported="true"            android:launchMode="singleTop"/>

5、导入SDK包

这里写图片描述

6、真没了,很多事情服务器帮我们做了。比如生成签名

0 0
原创粉丝点击