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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.secondworld.univeralsdk;
 
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
 
import com.mgbase.utils.BaseAnchorImpl;
import com.mgbase.utils.PreferenceUtils;
import com.mgbase.utils.StringUtils;
import com.mgpay.utils.MGPayAnchor;
import com.reyun.tracking.sdk.Tracking;
//import com.ss.android.common.lib.EventUtils;
import com.xy.xypay.bean.LoginResult;
import com.xy.xypay.bean.LogoutResult;
import com.xy.xypay.bean.PayArgsCheckResult;
import com.xy.xypay.bean.XPayArg;
import com.xy.xypay.inters.XYLoginCallback;
import com.xy.xypay.inters.XYLogoutCallback;
import com.xy.xypay.inters.XYPayCallback;
import com.xy.xypay.utils.XYPaySDK;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * Created by Administrator on 2018/9/3 0003.
 */
 
public class KYPlatformUtil
{
    private static final String TAG = "KYPlatformUtil";
 
    private boolean m_Init = false;
 
    private static KYPlatformUtil s_Instance;
 
    public static KYPlatformUtil getInstance()
    {
        if (s_Instance == null)
        {
            s_Instance = new KYPlatformUtil();
        }
        return s_Instance;
    }
 
    public boolean payProcessing = false;
 
    private Map<String, Object> m_Message = new HashMap<>();
 
    private static final int CALL_QUIT = 1;//退出游戏
    private static final int CALL_RESUME = 2;//返回按键处理
 
    public Handler handler = new Handler(new Handler.Callback()
    {
        @Override
        public boolean handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case CALL_QUIT:
                    m_Message.clear();
                    m_Message.put("code", CodeA2U.FreePlatformLogoutOk);
                    UniversalUtil.sendMessageToUnity(m_Message);
                    //悬浮球里切换帐号时退出游戏这里处理
                    XYPaySDK.hideFloatWindow(mainActivity);
                    login();
                    break;
                case CALL_RESUME:
                    //返回处理
                    break;
                default:
                    break;
 
            }
            return false;
        }
    });
 
    private Activity mainActivity;
 
    public void init(final Activity activity)
    {
        if (m_Init)
        {
            return;
        }
        mainActivity = activity;
 
        XYPaySDK.init(activity.getApplication(), new BaseAnchorImpl(), new MGPayAnchor());
        XYPaySDK.setHandler(handler);
    }
 
    private String m_AccountID;
 
    public void login()
    {
        XYPaySDK.login(mainActivity, new XYLoginCallback()
        {
            @Override
            public void onLogin(LoginResult loginResult)
            {
                //登录成功
                if (loginResult.getCode() == LoginResult.LOGIN_CODE_SUCCESS)
                {
                    //Toast.makeText(mainActivity,"登录成功:"+loginResult.isBindPhone(),Toast.LENGTH_LONG).show();
                    String _uid = PreferenceUtils.getUid(mainActivity);
                    String _token = PreferenceUtils.getToken(mainActivity);//
                    String _appId = PreferenceUtils.getAppid(mainActivity);
 
                    m_AccountID = _uid;
 
                    try
                    {
                        JSONObject _info = new JSONObject();
                        _info.put("account", m_AccountID);
                        _info.put("token", _token);
                        _info.put("account_id", m_AccountID);
                        _info.put("appId", _appId);
                        m_Message.put("code", CodeA2U.FreePlatformLoginOk);
                        m_Message.put("info", _info);
                        UniversalUtil.sendMessageToUnity(m_Message);
                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                }
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_APPID_NOT_FOUND)
                {
                    m_Message.clear();
                    m_Message.put("code", CodeA2U.FreePlatformLoginFail);
                    UniversalUtil.sendMessageToUnity(m_Message);
                }
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_FAILED)
                {
                    // Toast.makeText(mainActivity,"登录失败",Toast.LENGTH_LONG).show();
                    PreferenceUtils.setHasChangeAccount(mainActivity, "Y");
                    login();
                }
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_CANCEL)
                {
                    m_Message.clear();
                    m_Message.put("code", CodeA2U.FreePlatformLoginFail);
                    UniversalUtil.sendMessageToUnity(m_Message);
                }
            }
        });
    }
 
    // public void logout(final Activity activity) { }
 
    public void pay(String productId,
                    String productName,
                    String money,
                    String roleID,
                    String roleName,
                    String orderId,
                    String extraData,
                    String serverID,
                    String notifyurl)
    {
        XPayArg payArg = new XPayArg();
        payArg.APP_USER_ID = roleID;
        payArg.NOTIFY_URI = notifyurl;
        String amount = money;
        if (StringUtils.isEmpty(amount))
        {
            payArg.AMOUNT = "0.01";
        }
        else
        {
            payArg.AMOUNT = amount;
        }
        payArg.APP_EXT1 = extraData;
        payArg.APP_NAME = UniversalUtil.getAppName(mainActivity);
        payArg.APP_ORDER_ID = orderId;
        payArg.APP_USER_NAME = roleName;
        payArg.PRODUCT_ID = productId;
        payArg.OPEN_UID = PreferenceUtils.getUid(mainActivity);
        payArg.SID = serverID;
        payArg.PRODUCT_NAME = productName;
        XYPaySDK.pay(mainActivity, payArg, new XYPayCallback()
        {
            @Override
            public void onPayFinished(int status)
            {
                Log.i(TAG, "支付回调结果: " + status);
            }
 
            //检测参数是否正确
            public void onStart(PayArgsCheckResult result)
            {
                switch (result.getCode())
                {
                    case PayArgsCheckResult.CHECK_RESULT_OK:
                        break;
                    case PayArgsCheckResult.NOT_INIT:
                        Log.i(TAG, "未调用XYPaySDK.init()方法");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_NULL:
                        Log.i(TAG, "支付参数未配置");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_CALLBACK_NULL:
                        Log.i(TAG, "支付回调函数未配置");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_AMOUNT:
                        Log.i(TAG, "金额无效");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_ID:
                        Log.i(TAG, "app_id无效,请检查AndroidManifest.xml文件");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_NAME:
                        Log.i(TAG, "应用名称无效");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_USER_ID:
                        Log.i(TAG, "用户编号(角色)为空");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_APP_USER_NAME:
                        Log.i(TAG, "角色名为空");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_NOTIFY_URI:
                        Log.i(TAG, "通知服务器地址为空");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_OPEN_UID:
                        Log.i(TAG, "用户open_uid为空");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_ORDER_ID:
                        Log.i(TAG, "订单号无效");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_PRODUCT_ID:
                        Log.i(TAG, "产品号为空");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_PRODUCT_NAME:
                        Log.i(TAG, "产品名称为空");
                        break;
                    case PayArgsCheckResult.CHECK_RESULT_PAY_INVALID_RESOURCE_ID:
                        Log.i(TAG, "未登录或APP_KEY无效");
                        break;
                }
            }
        });
    }
 
    public void logout()
    {
        XYPaySDK.logout(mainActivity, new XYLogoutCallback()
        {
            @Override
 
            public void onLogout(LogoutResult logoutResult)
            {
                switch (logoutResult.getCode()){
                    case LogoutResult.LOGOUT_CODE_OUT:
                        //Toast.makeText(MyActivity.this,"退出游戏",Toast.LENGTH_LONG).show();
                        android.os.Process.killProcess(android.os.Process.myPid());
                        break;
                    case LogoutResult.LOGOUT_CODE_BBS:
                       // Toast.makeText(MyActivity.this,"进入论坛",Toast.LENGTH_LONG).show();
                        break;
                }
            }
        });
    }
 
    public void switchAccount()
    {
        XYPaySDK.changAccount(mainActivity, new XYLoginCallback()
        {
            @Override
            public void onLogin(LoginResult loginResult)
            {
                //登录成功
                if (loginResult.getCode() == LoginResult.LOGIN_CODE_SUCCESS)
                {
                    String _uid = PreferenceUtils.getUid(mainActivity);
                    String _token = PreferenceUtils.getToken(mainActivity);//
                    String _appId = PreferenceUtils.getAppid(mainActivity);
                    m_AccountID = _uid;
                    try
                    {
                        JSONObject _info = new JSONObject();
                        _info.put("account", m_AccountID);
                        _info.put("token", _token);
                        _info.put("account_id", m_AccountID);
                        _info.put("appId", _appId);
                        m_Message.put("code", CodeA2U.FreePlatformLoginOk);
                        m_Message.put("info", _info);
                        UniversalUtil.sendMessageToUnity(m_Message);
                    } catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                    //Toast.makeText(mainActivity, "登录成功:"+loginResult.isBindPhone(), Toast.LENGTH_LONG).show();
                }
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_APPID_NOT_FOUND)
                {
                    Log.i(TAG,"APP_ID未在AndroidManifest.xml中配置");
                }
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_FAILED)
                {
                    Log.i(TAG,"登录失败");
                    switchAccount();
                }
                else if (loginResult.getCode() == LoginResult.LOGIN_CODE_CANCEL)
                {
                    Log.i(TAG,"登录取消");
                }
                //  txt.setText(loginResult.getCode() + ";" + loginResult.getMessage() + ";" + loginResult.getOpenUid());
            }
        });
    }
 
    public void createRole(String roleId, String sid, String roleName)
    {
        XYPaySDK.createRole(roleId, sid, roleName);
    }
 
    public void enterWorld(String roleID,
                           String roleName,
                           String roleLv,
                           String serverID,
                           String job)
    {
        XYPaySDK.roleLogin(roleID, serverID, roleName, roleLv, job);
    }
 
    public void SendRegisterEvent()
    {
        Tracking.setRegisterWithAccountID(m_AccountID);
//        EventUtils.setRegister("mobile", true);
    }
 
    public void SendLoginEvent()
    {
        Tracking.setLoginSuccessBusiness(m_AccountID);
    }
 
    public void DownloadStart()
    {
        String type = "hotStart";
        XYPaySDK.hotActivation(mainActivity, type);
    }
 
    public void DownloadEnd()
    {
        String type = "hotEnd";
        XYPaySDK.hotActivation(mainActivity, type);
    }
 
}