wpc
2018-11-27 c52fb0e4d9168e75390b3cf3536d66c06c50d605
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.huawei.android.hms.agent.pay;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
 
import com.huawei.android.hms.agent.HMSAgent;
import com.huawei.android.hms.agent.common.ActivityMgr;
import com.huawei.android.hms.agent.common.ApiClientMgr;
import com.huawei.android.hms.agent.common.BaseApiAgent;
import com.huawei.android.hms.agent.common.CallbackResultRunnable;
import com.huawei.android.hms.agent.common.HMSAgentLog;
import com.huawei.android.hms.agent.common.StrUtils;
import com.huawei.android.hms.agent.pay.handler.ProductPayHandler;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.support.api.client.PendingResult;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.core.CommonCode;
import com.huawei.hms.support.api.entity.pay.PayStatusCodes;
import com.huawei.hms.support.api.entity.pay.ProductPayRequest;
import com.huawei.hms.support.api.pay.HuaweiPay;
import com.huawei.hms.support.api.pay.PayResult;
import com.huawei.hms.support.api.pay.ProductPayResultInfo;
 
/**
 * 支付请求
 */
public final class ProductPayApi extends BaseApiAgent {
 
    /**
     * 单实例
     */
    public static final ProductPayApi INST = new ProductPayApi();
 
    /**
     * client 无效最大尝试次数
     */
    private static final int MAX_RETRY_TIMES = 1;
 
    /**
     * 请求体
     */
    private ProductPayRequest payReq;
 
    /**
     * 支付结果回调
     */
    private ProductPayHandler handler;
 
    /**
     * 当前剩余重试次数
     */
    private int retryTimes = MAX_RETRY_TIMES;
 
    /**
     * 待处理的支付
     */
    private Status statusForPay;
 
    /**
     * 私有构造方法
     */
    private ProductPayApi(){}
 
    /**
     * Huawei Api Client 连接回调
     * @param rst 结果码
     * @param client HuaweiApiClient 实例
     */
    @Override
    public void onConnect(int rst, HuaweiApiClient client) {
 
        HMSAgentLog.d("onConnect:" + rst);
 
        if (client == null || !ApiClientMgr.INST.isConnect(client)) {
            HMSAgentLog.e("client not connted");
            onPMSPayEnd(rst, null);
            return;
        }
 
        // 调用HMS-SDK pay 接口
        PendingResult<PayResult> payResult = HuaweiPay.HuaweiPayApi.productPay(client, payReq);
        payResult.setResultCallback(new ResultCallback<PayResult>() {
            @Override
            public void onResult(PayResult result) {
 
                if (result == null) {
                    HMSAgentLog.e("result is null");
                    onPMSPayEnd(HMSAgent.AgentResultCode.RESULT_IS_NULL, null);
                    return;
                }
 
                Status status = result.getStatus();
                if (status == null) {
                    HMSAgentLog.e("status is null");
                    onPMSPayEnd(HMSAgent.AgentResultCode.STATUS_IS_NULL, null);
                    return;
                }
 
                int rstCode = status.getStatusCode();
                HMSAgentLog.d("pms pay rstCode=" + rstCode);
                // 需要重试的错误码,并且可以重试
                if ((rstCode == CommonCode.ErrorCode.SESSION_INVALID
                        || rstCode == CommonCode.ErrorCode.CLIENT_API_INVALID) && retryTimes > 0) {
                    retryTimes--;
                    connect();
                } else if (rstCode == PayStatusCodes.PAY_STATE_SUCCESS) {
                    // 支付校验完成,取当前activity进行后续支付操作
                    Activity curActivity = ActivityMgr.INST.getLastActivity();
                    if (curActivity == null) {
                        HMSAgentLog.e("activity is null");
                        onPMSPayEnd(HMSAgent.AgentResultCode.NO_ACTIVITY_FOR_USE, null);
                        return;
                    }
 
                    if (statusForPay != null) {
                        HMSAgentLog.e("has already a pay to dispose");
                        onPMSPayEnd(HMSAgent.AgentResultCode.REQUEST_REPEATED, null);
                        return;
                    }
 
                    //启动支付流程
                    try {
                        statusForPay = status;
                        Intent intent = new Intent(curActivity, HMSPMSPayAgentActivity.class);
                        curActivity.startActivity(intent);
                    } catch (Exception e) {
                        HMSAgentLog.e("start HMSPayAgentActivity error:" + e.getMessage());
                        onPMSPayEnd(HMSAgent.AgentResultCode.START_ACTIVITY_ERROR, null);
                    }
                }else {
                    onPMSPayEnd(rstCode, null);
                }
            }
        });
    }
 
    /**
     * 获取待处理的支付,供 HMSPayAgentActivity 调用
     * @return 待处理的支付
     */
    Status getWaitPayStatus(){
        HMSAgentLog.d("getWaitPayStatus=" + StrUtils.objDesc(statusForPay));
        return statusForPay;
    }
 
    /**
     * 支付结果方法
     * @param rstCode 支付结果码
     * @param payInfo 支付结果信息
     */
    void onPMSPayEnd(int rstCode, ProductPayResultInfo payInfo) {
        HMSAgentLog.i("productPay:callback=" + StrUtils.objDesc(handler) +" retCode=" + rstCode + "  payInfo=" + StrUtils.objDesc(payInfo));
        if (handler != null) {
            new Handler(Looper.getMainLooper()).post(new CallbackResultRunnable<ProductPayResultInfo>(handler, rstCode, payInfo));
            handler = null;
        }
        statusForPay = null;
        payReq = null;
        retryTimes = MAX_RETRY_TIMES;
    }
 
    /**
     * 商品编码支付接口
     * @param requ 支付请求类
     * @param handler 支付结果回调
     */
    public void productPay(ProductPayRequest requ, final ProductPayHandler handler) {
        HMSAgentLog.i("productPay:requ=" + StrUtils.objDesc(requ) + "  handler=" + StrUtils.objDesc(handler));
 
        if (payReq != null) {
            HMSAgentLog.e("productPay:has already a pay to dispose");
            if (handler != null) {
                new Handler(Looper.getMainLooper()).post(new CallbackResultRunnable<ProductPayResultInfo>(handler, HMSAgent.AgentResultCode.REQUEST_REPEATED, null));
            }
            return;
        }
 
        this.payReq = requ;
        this.handler = handler;
        retryTimes = MAX_RETRY_TIMES;
        connect();
    }
}