支付宝 Android 集成流程

参考

支付流程

  1. 客户端向服务器端发起请求
  2. 服务器端向客户端发送订单信息orderInfo
  3. 客户端根据orderInfo调起支付
  4. 客户端处理支付结果

Android客户端代码示例

唤起支付类示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
// import com.cocos.lib.JsbBridge;

import java.util.Map;

public class AliPay {

/**
* 上下文-创建支付对象时候使用
*/
private final Activity activity;

/**
* 支付标志 自定义常量
*/
private final static int SDK_PAY_FLAG = 1;

/**
* 授权标志 自定义常量
*/
private static final int SDK_AUTH_FLAG = 2;

/**
* 创建Handle
*/
private final Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SDK_PAY_FLAG: {

@SuppressWarnings("unchecked")
PayResult payResult = new PayResult((Map<String, String>) msg.obj);

processPayResult(payResult);
break;
}
case SDK_AUTH_FLAG: {
break;
}
default:
Log.w("AliPay", "收到未定义的消息标志");
Log.w("AliPay", msg.toString());
}
}
};

public AliPay(Activity activity) {
this.activity = activity;
}

/**
* 调用支付功能
*
* @param orderInfo 订单信息-由后端提供
*/
public void Pay(String orderInfo) {

final Runnable payRunnable = new Runnable() {

@Override
public void run() {
//创建支付宝SDK的支付任务类
PayTask alipay = new PayTask(activity);
//调用支付方法并且返回信息
Map<String, String> result = alipay.payV2(orderInfo, true);
Log.i("AliPay", result.toString());

//创建消息
Message msg = new Message();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};
//必须异步调用
Thread payThread = new Thread(payRunnable);
payThread.start();
}


/**
* 处理支付结果
*
* @param payResult 支付结果
*/
private void processPayResult(PayResult payResult) {
sendToScript("pay",payResult.toString());
}

/**
* 向游戏端发送支付结果消息
* @param arg0 事件名称
* @param arg1 事件参数
*/
private void sendToScript(String arg0, String arg1) {
Log.d("AliPay",arg0);
Log.d("AliPay",arg1);
// JsbBridge.sendToScript(arg0,arg1);
}
}

支付结果处理类示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import android.text.TextUtils;

import java.util.Map;

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 + ",\"result\":\"" + result + "\",\"memo\":\"" + memo + "\"}";
}

/**
* @return the resultStatus
*/
public String getResultStatus() {
return resultStatus;
}

/**
* @return the memo
*/
public String getMemo() {
return memo;
}

/**
* @return the result
*/
public String getResult() {
return result;
}
}