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
package com.huawei.android.hms.agent.pay;
 
import android.os.Handler;
import android.os.Looper;
 
import com.huawei.android.hms.agent.HMSAgent;
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.GetProductDetailsHandler;
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.ProductDetailRequest;
import com.huawei.hms.support.api.pay.HuaweiPay;
import com.huawei.hms.support.api.pay.ProductDetailResult;
 
/**
 * 查询订单请求
 */
public class GetProductDetailsApi extends BaseApiAgent {
 
    /**
     * client 无效最大重试次数
     */
    private static final int MAX_RETRY_TIMES = 1;
 
    /**
     * 查询商品信息请求,请求体
     */
    private ProductDetailRequest productDetailReq;
 
    /**
     * 查询商品信息请求回调接口
     */
    private GetProductDetailsHandler handler;
 
    /**
     * 剩余重试次数
     */
    private int retryTimes = MAX_RETRY_TIMES;
 
    /**
     * 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");
            onProductDetailResult(rst, null);
            return;
        }
 
        // 调用HMS-SDK getOrderDetail 接口
        PendingResult<ProductDetailResult> checkPayResult = HuaweiPay.HuaweiPayApi.getProductDetails(client, productDetailReq);
        checkPayResult.setResultCallback(new ResultCallback<ProductDetailResult>() {
            @Override
            public void onResult(ProductDetailResult result) {
 
                if (result == null) {
                    HMSAgentLog.e("result is null");
                    onProductDetailResult(HMSAgent.AgentResultCode.RESULT_IS_NULL, null);
                    return;
                }
 
                Status status = result.getStatus();
                if (status == null) {
                    HMSAgentLog.e("status is null");
                    onProductDetailResult(HMSAgent.AgentResultCode.STATUS_IS_NULL, null);
                    return;
                }
 
                int rstCode = status.getStatusCode();
                // 需要重试的错误码,并且可以重试
                if ((rstCode == CommonCode.ErrorCode.SESSION_INVALID
                        || rstCode == CommonCode.ErrorCode.CLIENT_API_INVALID) && retryTimes > 0) {
                    retryTimes--;
                    connect();
                } else {
                    onProductDetailResult(rstCode, result);
                }
            }
        });
    }
 
    private void onProductDetailResult(int retCode, ProductDetailResult productDetailResult){
        HMSAgentLog.i("getOrderDetail:callback=" + StrUtils.objDesc(handler) +" retCode=" + retCode + "  productDetailResult=" + StrUtils.objDesc(productDetailResult));
        if (handler != null) {
            new Handler(Looper.getMainLooper()).post(new CallbackResultRunnable<ProductDetailResult>(handler, retCode, productDetailResult));
            handler = null;
        }
 
        productDetailReq = null;
        retryTimes = MAX_RETRY_TIMES;
    }
 
    /**
     * 查询商品信息接口
     * @param request 查询商品信息请求体
     * @param handler 查询商品信息结果回调
     */
    public void getProductDetails(ProductDetailRequest request, GetProductDetailsHandler handler) {
        HMSAgentLog.i("getOrderDetail:request=" + StrUtils.objDesc(request) + "  handler=" + StrUtils.objDesc(handler));
        this.productDetailReq = request;
        this.handler = handler;
        this.retryTimes = MAX_RETRY_TIMES;
        connect();
    }
}