支付宝后台申请appid 秘钥…等数据
1.将支付宝的libs下的jar拷贝到自己项目中
添加为依赖库
2.在Androidmanifest.xml中添加
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation|screenSize"
android:exported="false"
android:screenOrientation="behind"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
<activity
android:name="com.alipay.sdk.app.H5AuthActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
<!-- 支付宝 sdk end -->
3.在需要调用的地方调用支付宝接口
创建一个PayResult类:
public class PayResult {
private String resultStatus;
private String result;
private String memo;
public PayResult(Map<String, String> rawResult) {
if (rawResult == null) {
return;
}
for (String key : rawResult.keySet()) {
if (TextUtils.equals(key, "resultStatus")) {
resultStatus = rawResult.get(key);
} else if (TextUtils.equals(key, "result")) {
result = rawResult.get(key);
} else if (TextUtils.equals(key, "memo")) {
memo = rawResult.get(key);
}
}
}
@Override
public String toString() {
return "resultStatus={" + resultStatus + "};memo={" + memo
+ "};result={" + result + "}";
}
/**
* @return the resultStatus
*/
public String getResultStatus() {
return resultStatus;
}
/**
* @return the memo
*/
public String getMemo() {
return memo;
}
/**
* @return the result
*/
public String getResult() {
return result;
}
}
4.调用支付宝,签名,参数都通过服务端做,通过服务端拿到数据
//支付宝支付
public void callAliPay() {
connectAlipayWeb();
}
private List params = new ArrayList();
public String post(String url) {
String rev = “”;
org.apache.http.HttpResponse httpResponse = null;
org.apache.http.client.methods.HttpPost httpPost = new org.apache.http.client.methods.HttpPost(url);
try {
httpPost.setEntity(new org.apache.http.client.entity.UrlEncodedFormEntity(params, “UTF-8”));
httpResponse = (new org.apache.http.impl.client.DefaultHttpClient()).execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
rev = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception var6) {
var6.printStackTrace();
}
params.clear();
return rev;
}
private void connectAlipayWeb() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
params.add(new BasicNameValuePair(“method”, “xxxx”));
params.add(new BasicNameValuePair(“key”, “xxxxxx”));
params.add(new BasicNameValuePair(“payType”, payType));//1支付宝 0微信
params.add(new BasicNameValuePair(“orderId”,Manager.getInstance().js_order));//Manager.getInstance().js_order
String result = post(“自己服务地址”);
Log.d(“gmf result:”,result);
String orderInfo=result;
toALiPay(activity,orderInfo);
return;
}
}, 0);
}
/**
* 签名在服务端来做
*
* @param context
* @param orderInfo
*/
public void toALiPay(final Activity context, final String orderInfo) {
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask(context);
Map<String, String> result = alipay.payV2(orderInfo, true);
Message msg = new Message();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
}
//支付宝 监听回调函数
@SuppressLint("HandlerLeak")
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(activity, “支付宝支付成功”, Toast.LENGTH_SHORT).show();
Manager.getInstance().notifyPay(1,pid,"");
dialog.dismiss();
} else {
// 该笔订单真实的支付结果,需要依赖服务端的异步通知。
// Toast.makeText(activity, “支付宝支付失败”, Toast.LENGTH_SHORT).show();
dialog.dismiss();
Manager.getInstance().notifyPay(-1,pid,"");
}
break;
}
default:
break;
}
};
};
/**
* get the sdk version. 获取支付宝SDK版本号
*
*/
public void getSDKVersion() {
PayTask payTask = new PayTask(activity);
String version = payTask.getVersion();
Toast.makeText(activity, version, Toast.LENGTH_SHORT).show();
}