支付宝支付的接入就比微信的简单了,开始表演!申请参数这些就不说了,简简单单
开始接入:1、将alipay-sdk-common/alipaySdk-xxxxxxxx.jar包放入项目应用工程的libs目录下2、在AndroidManifest.xml文件里面添加声明:
<pre>
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
</activity>
<activity
android:name="com.alipay.sdk.auth.AuthActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
</activity>
</pre>
需要的权限:
<pre>
<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" />
</pre>
添加混淆规则,在工程的proguard-project.txt文件下
<pre>
-libraryjars libs/alipaySDK-20150602.jar
-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 *;}
</pre>
基本配置好了以后就开始上主菜了
<pre>
//我这里使用OKhttp来做网络访问,将需要的参数传递给服务端小哥 // PAY_NET_URL 服务端地址
RequestCall requestCall= OkHttpUtils.get().url(Constants.PAY_NET_URL)
.addParams("userID", uid)
.addParams("appID", appid)
.addParams("productID", productID+"")
.addParams("productName",productName)
.addParams("productDesc",productDesc)
.addParams("money", "" + price)
.addParams("roleID", roleID)
.addParams("roleName", roleName)
.addParams("serverID",serverId)
.addParams("serverName",serverName)
.addParams("payID", "" + 1)
.addParams("orderId",orderId)
.addParams("notifyUrl", payNotifyURL)
.build();
requestCall.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int i) {
Log.e("requestCall is onError",e.getMessage());
}
@Override
public void onResponse(String json, int i) {
// 使用Gson解析
result1 = new Gson().fromJson(json, Result.class);
final String extension = result1.getData().getExtension();
Runnable payRunnable = new Runnable() {
@Override
public void run() {
// 这里调用支付宝api
PayTask alipay = new PayTask(mActivty);
Map<String, String> result = alipay.payV2(extension,true);
Log.i("msp", result.toString());
Message msg = new Message();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
}
});
</pre>
支付宝Handler(用来接收支付宝支付结果)
<pre>
private Handler mHandler = new Handler() {
@SuppressWarnings("unused")
public void handleMessage(Message msg) {
switch (msg.what) {
case SDK_PAY_FLAG: {
@SuppressWarnings("unchecked")
PayResult payResult = new PayResult((Map<String, String>) msg.obj);
/**
对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
*/
String resultInfo = payResult.getResult();// 同步返回需要验证的信息
String resultStatus = payResult.getResultStatus();
// 判断resultStatus 为9000则代表支付成功
if (TextUtils.equals(resultStatus, "9000")) {
Toast.makeText(mContext, "支付成功", Toast.LENGTH_SHORT).show();
} else {
// 判断resultStatus 为非"9000"则代表可能支付失败
// "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)
if (TextUtils.equals(resultStatus, "8000")) {
Toast.makeText(mContext, "支付结果确认中", Toast.LENGTH_SHORT).show();
} else {
// 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误
Toast.makeText(mContext, "支付失败", Toast.LENGTH_SHORT).show();
}
}
break;
}
default:
break;
}
}
};//handler结束
</pre>
Result.class
<pre>
public class Result {
private DataBean data;
private int state;
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public static class DataBean {
private String extension;
private long orderID;
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public long getOrderID() {
return orderID;
}
public void setOrderID(long orderID) {
this.orderID = orderID;
}
}
}
</pre>
到这里就结束了,一般参数没问题就可以开心的支付了。
ps: 要是写的有问题希望大家提出我将虚心接受批评及积极改正!