Android 微信支付随手笔记

首先第一步是去微信开发平台申请自己的开发者账号,这些都不多做介绍了 直接进入代码阶段

 

//这个是调用微信支付的界面必须有  直接粘贴复制即可

package com.langteng.courtesy.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

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, "自己的appid");  //微信申请的appid

        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) {
        //支付结果处理
        //startActivity(new Intent(this, WalletActivity.class));
        finish();


    }

}

 

//自己界面调用代码如下

private final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
private PayReq req;

//传入参数 用户id和钱数从后台获取相应的参数

private void wxPayStart() {
    OkGo.post(Urls.WE1XIN_PAY)
            .params("uid", (int) PrefUtils.get(this, "id", -1))
            .params("money", EmptyUtils.getEditTextEmtity(price))
            .execute(new DialogCallback(RechargeActivity.this, 0) {
                @Override
                public void onSuccess(Response response) {
                    super.onSuccess(response);
                    if (response.body().getCode() == 0) {
                        msgApi.registerApp(response.body().getContent().getAppid());
                        req.appId = response.body().getContent().getAppid();
                        req.partnerId = response.body().getContent().getPartnerid();
                        req.prepayId = response.body().getContent().getPrepayid();
                        req.nonceStr = response.body().getContent().getNoncestr();
                        req.timeStamp = response.body().getContent().getTimestamp();
                        req.packageValue = response.body().getContent().getPackageX();
                        req.sign = response.body().getContent().getSign();
                        msgApi.sendReq(req);

                    } else {

                        showToast(response.body().getMsg());
                    }
                }
            });


}

 

//大公告成。。

你可能感兴趣的:(Android 微信支付随手笔记)