Android微信支付完整步骤

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值