Android 微信和支付宝支付

1. 微信支付

AppID的申请和环境搭建详见Android QQ和微信分享

创建WXPayEntryActivity类,目录必须是"[您的应用包名].wxapi",并实现IWXAPIEventHandler接口用来回调

注册AppID

IWXAPI mApi = WXAPIFactory.createWXAPI(this, APP_ID, false);
// 将该app注册到微信
mApi.registerApp(APP_ID);

调用支付

partnerId是商户号,prepayId是预支付交易会话ID,package暂填写固定值Sign=WXPay,noncestr是随机字符串,timeStamp是时间戳,sign是签名,详见调起支付API

PayReq request = new PayReq();
request.appId = APP_ID;
request.partnerId = PARENT_ID;
request.prepayId = prepayId;
request.packageValue = PACKAGE;
request.nonceStr = NON_CE_STR;
request.timeStamp = timeStamp;

String sign = "appid=" + APP_ID
        + "&noncestr=" + NON_CE_STR
        + "&package=" + PACKAGE
        + "&partnerid=" + PARENT_ID
        + "&prepayid=" + prepayId
        + "×tamp=" + timeStamp
        + "&key=" + KEY;
request.sign = CryptTool.md5Encrypt(sign).toUpperCase();
mApi.sendReq(request); 

需要注意的是签名,详见签名生成算法

支付结果回调

@Override
public void onResp(BaseResp baseResp) {
    if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
        int errCord = baseResp.errCode; // 0 成功 -1 错误 -2 用户取消
    }
}

2. 支付宝支付

2.1 申请AppID

直接去蚂蚁金服开放平台申请即可。

2.2 搭建环境

首先到App支付客户端DEMO&SDK下载aar文件,并拷贝到libs目录下

修改Projectbuild.gradle文件

allprojects {
    repositories {
        google()
        jcenter()
        // 添加下面的内容
        flatDir {
            dirs 'libs'
        }        
    }
}

appbuild.gradle文件下添加依赖

dependencies {
    // 添加下面的内容
    implementation (name: 'alipaySdk-*', ext: 'aar')
    // 其他依赖
}

配置AndroidManifest.xml文件

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

混淆配置

-keep class com.alipay.android.app.IAlixPay{*;}
-keep class com.alipay.android.app.IAlixPay$Stub{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
-keep class com.alipay.sdk.app.PayTask{ public *;}
-keep class com.alipay.sdk.app.AuthTask{ public *;}
-keep class com.alipay.sdk.app.H5PayCallback {
    ;
    ;
}
-keep class com.alipay.android.phone.mrpc.core.** { *; }
-keep class com.alipay.apmobilesecuritysdk.** { *; }
-keep class com.alipay.mobile.framework.service.annotation.** { *; }
-keep class com.alipay.mobilesecuritysdk.face.** { *; }
-keep class com.alipay.tscenter.biz.rpc.** { *; }
-keep class org.json.alipay.** { *; }
-keep class com.alipay.tscenter.** { *; }
-keep class com.ta.utdid2.** { *;}
-keep class com.ut.device.** { *;}

-dontwarn android.net.**
-keep class android.net.SSLCertificateSocketFactory{*;}

2.3 支付和回调

调用支付

final String orderInfo = xxx;
Runnable payRunnable = new Runnable() {

    @Override
    public void run() {
        PayTask alipay = new PayTask(AliPayActivity.this);
        final Map<String, String> result = alipay.payV2(orderInfo, true);
        App.post(new Runnable() {
            @Override
            public void run() {
                callBack(result);
            }
        });
    }
};
// 必须异步调用
Thread payThread = new Thread(payRunnable);
payThread.start();

orderInfo从服务器获取数据并拼接而成,详见请求参数说明,参数都应该使用URLEncoder转码过。

String orderInfo = "app_id="+ app_id
        + "&biz_content=" + biz_content
        + "&charset=" + charset
        + "&method=" + method
        + "¬ify_url=" + notify_url
        + "&sign_type=" + sign_type
        + "×tamp=" + timestamp
        + "&version=" + version
        + "&sign=" + sign;

结果回调

private void callBack(Map<String, String> result) {
    PayResult payResult = new PayResult(result);
    /**
     * 对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
     */
    String resultInfo = payResult.getResult();// 同步返回需要验证的信息

    String resultStatus = payResult.getResultStatus();
    // 判断resultStatus 为9000则代表支付成功
    if (TextUtils.equals(resultStatus, "9000")) {
        // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
        setResult(RESULT_OK);
    } else {
        // 该笔订单真实的支付结果,需要依赖服务端的异步通知。
        setResult(RESULT_CANCELED);
    }
    finish();
}

你可能感兴趣的:(Android,第三方应用)