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 用户取消
}
}
直接去蚂蚁金服开放平台申请即可。
首先到App支付客户端DEMO&SDK下载aar
文件,并拷贝到libs
目录下
修改Project
下build.gradle
文件
allprojects {
repositories {
google()
jcenter()
// 添加下面的内容
flatDir {
dirs 'libs'
}
}
}
在app
下build.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{*;}
调用支付
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();
}