项目集成支付宝支付有挺长一段时间了,闲来没事总结下,给心存疑惑的人以指导,大神无视即可。
android集成支付宝其实是一件很简单的事情。后台的事情咱们不考虑。对于客户端而言,总结四步:
第一步:拷贝支付宝sdk到你的项目中,并做相关配置。
第二步:根据后台提供的订单接口创建订单。
第三步:如果创建订单成功直接调sdk进行支付
第四步:根据支付的结果进行相关操作处理。一般支付成功后要再进行一次后台请求,因为支付结果应该以异步为准。
所用到的主要的类如下:
AliPayAPI.java 是封装的用于调用支付宝支付的工具类。创建订单成功后直接调用startPay即可。当然要看后台怎么给你返回了。。。
/**
* 进行支付宝支付先获得AliPayAPI的实例,然后调用startPay方法
*
* @author let
*/
public class AliPayAPI {
private static AliPayAPI aliPayAPI;
private OnAliPayListener mOnAliPayListener;
private static final int SDK_PAY_FLAG = 1;
private AliPayAPI() {
}
public static AliPayAPI getInstance(OnAliPayListener mOnAliPayListener) {
if (aliPayAPI == null) {
aliPayAPI = new AliPayAPI();
}
aliPayAPI.mOnAliPayListener = mOnAliPayListener;
return aliPayAPI;
}
@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")) {
if (mOnAliPayListener != null)
mOnAliPayListener.onAliPaySuccess();
} else {
// 判断resultStatus 为非“9000”则代表可能支付失败
// “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)
// “6004”支付结果未知(有可能已经支付成功)
if (TextUtils.equals(resultStatus, "8000") || TextUtils.equals(resultStatus, "6004")) {
if (mOnAliPayListener != null)
mOnAliPayListener.onAliPayConfirming();
} else {
// 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误
if (mOnAliPayListener != null)
mOnAliPayListener.onAliPayFailure();
}
}
break;
default:
break;
}
};
};
/**
* 进行支付宝支付
*
* @param activity
* @param orderModel
*/
public void startPay(final Activity activity, OrderModel orderModel) {
AlipayPayOrderModel alipayPayOrderModel = orderModel.getAlipayPayOrderModel();
Map<String, String> orderParamMap = OrderInfoUtil2_0.buildOrderParamMap(alipayPayOrderModel);
final String orderInfo = OrderInfoUtil2_0.buildOrderParam(orderParamMap) + "&" + alipayPayOrderModel.getSign();
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask(activity);
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();
}
public interface OnAliPayListener {
/**
* 支付成功
*/
public void onAliPaySuccess();
/**
* 支付失败
*/
public void onAliPayFailure();
/**
* 等待确认
*/
public void onAliPayConfirming();
}
}
然后OrderInfoUtil2_0 .java如下。 该类其实是从Demo拷贝来并修改的
public class OrderInfoUtil2_0 {
/**
* 构造支付订单参数列表
*
* @param aliOrder
* @return
*/
public static Map<String, String> buildOrderParamMap(AlipayPayOrderModel aliOrder) {
Map<String, String> keyValues = new HashMap<String, String>();
keyValues.put("app_id", aliOrder.getApp_id());
keyValues.put("biz_content", aliOrder.getBiz_content());
keyValues.put("charset", aliOrder.getCharset());
keyValues.put("method", aliOrder.getMethod());
keyValues.put("sign_type", aliOrder.getSign_type());
keyValues.put("timestamp", aliOrder.getTimestamp());
keyValues.put("version", aliOrder.getVersion());
return keyValues;
}
/**
* 构造支付订单参数信息
*
* @param map
* 支付订单参数
* @return
*/
public static String buildOrderParam(Map<String, String> map) {
List<String> keys = new ArrayList<String>(map.keySet());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.size() - 1; i++) {
String key = keys.get(i);
String value = map.get(key);
sb.append(buildKeyValue(key, value, true));
sb.append("&");
}
String tailKey = keys.get(keys.size() - 1);
String tailValue = map.get(tailKey);
sb.append(buildKeyValue(tailKey, tailValue, true));
return sb.toString();
}
/**
* 拼接键值对
*
* @param key
* @param value
* @param isEncode
* @return
*/
private static String buildKeyValue(String key, String value, boolean isEncode) {
StringBuilder sb = new StringBuilder();
sb.append(key);
sb.append("=");
if (isEncode) {
try {
sb.append(URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
sb.append(value);
}
} else {
sb.append(value);
}
return sb.toString();
}
}
PayResult 也是从Demo里拷贝来的
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;
}
}