微信支付(Android篇)

官方接入指北:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5

SDK&DEMO下载:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1

1. 微信开发者账号中创建应用
获取app_id ,app_secret

2. Application中初始化操作

public static IWXAPI WXapi;
private void initWX() {
    WXapi = WXAPIFactory.createWXAPI(this, BuildConfig.WX_APP_ID, true);
    WXapi.registerApp(BuildConfig.WX_APP_ID);
}

3. 支付相关代码

 /**
     * 微信支付
     */
    private void channelWechat() {
            WxPayModel wxPayModel ;//后台接口获取订单信息
            PayReq request = new PayReq();
            request.appId = wxPayModel.getAppid();
            request.partnerId = wxPayModel.getPartnerid();
            request.prepayId = wxPayModel.getPrepayid();
            request.packageValue = wxPayModel.getPackageStr();
            request.nonceStr = wxPayModel.getNoncestr();
            request.timeStamp = wxPayModel.getTimestamp();
            request.sign = wxPayModel.getSign();
            AppApplication.WXapi.sendReq(request);
    }
 @Override
    public void onEventMainThread(EventBean bean) {
        super.onEventMainThread(bean);
        if (bean.getFrom() == Constant.Event_Msg.PAY_SUCCESS_WECHAT) {
            //0成功;-1错误;-2用户取消
            if (bean.getIndex() == -2)
                doBack();
            else
                payResult(bean.getIndex() == 0 ? true : false);
        }
    }

    private void payResult(boolean success) {
        DialogUtils.showMsg(this, "提示", "订单支付" + (success ? "成功" : "失败,重新支付"), (context, builder, dialog, dialogId, which, editText) -> {
            EventBus.getDefault().post(new EventBean(Constant.Event_Msg.PAY_SUCCESS));
            finish();
        }, (context, builder, dialog, dialogId, which, editText) -> {
            if (success) {
                EventBus.getDefault().post(new EventBean(Constant.Event_Msg.PAY_SUCCESS));
                finish();
            }
        }, !success, "取消", "确定", false);

    }

    private void doBack() {
        DialogUtils.showMsg(this, "提示", "您的订单还未支付完成,请尽快完成支付。", (context, builder, dialog, dialogId, which, editText) -> {
            EventBus.getDefault().post(new EventBean(Constant.Event_Msg.PAY_SUCCESS));
            finish();
        }, (context, builder, dialog, dialogId, which, editText) -> {
        }, true, "确认离开", "继续支付", false);
    }

4. 本地创建WXPayEntryActivity类
注意WXPayEntryActivity类地址必须与applicationId一致。

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       api= AppApplication.WXapi;

//        api = WXAPIFactory.createWXAPI(this, BuildConfig.WX_APP_ID, true);
//        api.registerApp(BuildConfig.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        //0成功;-1错误;-2用户取消
        EventBus.getDefault().post(new EventBean(Constant.Event_Msg.PAY_SUCCESS_WECHAT, resp.errCode));
        finish();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值