PayToALi.java
4.11 KB
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
package com.cnlive.goldenline.pay;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import com.alipay.sdk.app.PayTask;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.pay.model.AliPayResult;
import com.cnlive.goldenline.pay.model.EventPayment;
import com.cnlive.goldenline.pay.model.PayCloudResponse;
import org.greenrobot.eventbus.EventBus;
import cn.cmvideo.sdk.common.util.ToastUtil;
import static com.cnlive.goldenline.util.LogUtils.LOGD;
import static com.cnlive.goldenline.util.LogUtils.LOGE;
/**
* Created by gujun on 2017-04-10.
*/
public class PayToALi {
private static final int SDK_PAY_FLAG = 1;
private static Activity mActivity;
public static void startPay(Activity activity,final PayCloudResponse unifyPayMessage) {
mActivity = activity;
if (unifyPayMessage == null || unifyPayMessage.getData().getAlipay_app() == null) return;
try {
//签名拼接支付信息操作交由后台
LOGD("ExternalPartner start pay");
// start the pay.
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask(mActivity);
String result = alipay.pay(unifyPayMessage.getData().getAlipay_app(), true);
Message msg = new Message();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
alipayHandler.sendMessage(msg);
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
} catch (Exception ex) {
LOGE("alipay error".concat(ex.getMessage()), ex);
ToastUtil.show(mActivity, mActivity.getString(R.string.data_error));
}
}
// ============================================================================================
// 支付宝不用动的
// ============================================================================================
@SuppressLint("HandlerLeak")
private static Handler alipayHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SDK_PAY_FLAG:
if (mActivity != null) {
String result = (String) msg.obj;
AliPayResult resultObj = new AliPayResult(result);
String resultStatus = resultObj.resultStatus;
// 判断resultStatus 为“9000”则代表支付成功,具体状态码代表含义可参考接口文档
if (resultStatus.equals("9000")) {
ToastUtil.show(mActivity, "支付成功");
EventBus.getDefault().post(new EventPayment(SignUtil.PAY_TYPE_ALIPAY, SignUtil.PAY_RESULT_STATUS_SUCCESS));
} else {
// 判断resultStatus 为非“9000”则代表可能支付失败
// “8000” 代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)
if (resultStatus.equals("8000")) {
ToastUtil.show(mActivity, "支付结果确认中");
} else if (resultStatus.equals("6001")) {
ToastUtil.show(mActivity, "取消支付");
} else if (resultStatus.equals("6002")) {
ToastUtil.show(mActivity, "网络连接出错");
} else {
ToastUtil.show(mActivity, "支付失败");
EventBus.getDefault().post(new EventPayment(SignUtil.PAY_TYPE_ALIPAY, SignUtil.PAY_RESULT_STATUS_FAIL));
}
}
}
break;
default:
break;
}
}
};
}