关于微信支付的那些事

来源:互联网 发布:手机照片整理软件 编辑:程序博客网 时间:2024/05/24 05:41

自己总结一下微信支付吧。

微信app开发文档区:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1

集成微信支付:

1-在微信开放平台申请(省略)

2-在app的gradle文件中的dependencies下加入

compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'

3-在调用微信API之前先要初始化

//初始化微信支付    public void initWxPay() {        IWXAPI msgApi = WXAPIFactory.createWXAPI(context, null);        msgApi.registerApp(app_id);    }

4-定义支付回调类WXPayEntryActivity

清单注册:     <activity        android:name=".wxapi.WXPayEntryActivity"        android:exported="true"        android:launchMode="singleTop"        android:screenOrientation="portrait"/>

5-请求支付接口

向app端服务器请求支付参数,然后带上参数请求支付接口:PayReq request = new PayReq();        if (null != param) {           //微信支付参数 由服务端返回           request.appId = param.appid;           request.partnerId = param.partnerid;           request.prepayId = param.prepayid;           request.packageValue = "Sign=WXPay";           request.nonceStr = param.noncestr;//生成一个随机字符串           request.timeStamp = param.timestamp;           request.sign = param.sign;//签名(服务端生成)        }        if (null != mIWXAPI) {           mIWXAPI.sendReq(request);        }

6-支付回调

    @Override   public void onResp(BaseResp baseResp) {      if (0 == baseResp.errCode) {         //支付成功 0         CanBus.getDefault().post(new Intent(Constants.WX_PAY_SUCCESS_CODE));      } else if (-1 == baseResp.errCode) {         //支付失败 -1 可能参数错误,检查签名         CanBus.getDefault().post(new Intent(Constants.PAY_FAILURE_CODE));      } else {         //支付取消 -2         CanBus.getDefault().post(new Intent(Constants.PAY_FAILURE_CODE));      }      Utils.finish(this);   }
1 0
原创粉丝点击