支付寶支付的接入就比微信的簡單了,開始表演逃顶!申請參數(shù)這些就不說了讨便,簡簡單單
開始接入: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>
添加混淆規(guī)則霸褒,在工程的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來做網(wǎng)絡訪問香伴,將需要的參數(shù)傳遞給服務端小哥 // 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() {
// 這里調(diào)用支付寶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"代表支付結果因為支付渠道原因或者系統(tǒng)原因還在等待支付結果確認翠语,最終交易是否成功以服務端異步通知為準(小概率狀態(tài))
if (TextUtils.equals(resultStatus, "8000")) {
Toast.makeText(mContext, "支付結果確認中", Toast.LENGTH_SHORT).show();
} else {
// 其他值就可以判斷為支付失敗嚎杨,包括用戶主動取消支付座云,或者系統(tǒng)返回的錯誤
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>
到這里就結束了袒炉,一般參數(shù)沒問題就可以開心的支付了旁理。
ps: 要是寫的有問題希望大家提出我將虛心接受批評及積極改正!