频道栏目
首页 > 资讯 > 微信公众平台开发 > 正文

Android微信支付完整步骤

16-07-08        来源:[db:作者]  
收藏   我要投稿

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

     * 请求服务器
     */
    private void postServer() {
        HashMap 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() {
            @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

    
        
            
                
            
        

        

5、导入SDK包

这里写图片描述

相关TAG标签
上一篇:自动化部署入门Git+Nexus+SonarQube+Hudson
下一篇:微信分享-js代码
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站