package com.electric.chargingpile.activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.alipay.sdk.app.PayTask; import com.blankj.utilcode.util.EmptyUtils; import com.electric.chargingpile.R; import com.electric.chargingpile.application.MainApplication; import com.electric.chargingpile.data.RechargeData; import com.electric.chargingpile.util.BarColorUtil; import com.electric.chargingpile.util.JsonUtils; import com.electric.chargingpile.util.LoadingDialog; import com.electric.chargingpile.util.Md5Util; import com.electric.chargingpile.util.PayResult; import com.electric.chargingpile.util.ToastUtil; import com.electric.chargingpile.wxapi.WXPayEntryActivity; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.tencent.mm.sdk.modelpay.PayReq; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.WXAPIFactory; import com.zhy.autolayout.AutoLayoutActivity; import com.zhy.http.okhttp.OkHttpUtils; import com.zhy.http.okhttp.callback.StringCallback; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import okhttp3.Call; public class AccountRechargeActivity extends AutoLayoutActivity implements View.OnClickListener { private static final String TAG = "AccountRechargeActivity"; private static final int SDK_PAY_FLAG = 1; private ImageView iv_back, iv_weixin, iv_zhifubao; private RelativeLayout rl_weixin, rl_zhifubao; private String cost_way = "zhifubao"; private String recharge_id = "1"; private TextView tv_cz1, tv_cz2, tv_cz3, tv_cz4, tv_cz5, tv_cz6; private TextView tv_pay, tv_xieyi; ArrayList datas = new ArrayList(); private LoadingDialog dialog; List list_textView = new ArrayList<>(); List list_rd = new ArrayList<>(); private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SDK_PAY_FLAG: dialog.cancel(); PayResult payResult = new PayResult((String) msg.obj); String resultInfo = payResult.getResult();// 同步返回需要验证的信息 Log.e(TAG, "handleMessage: resultInfo=" + resultInfo); String resultStatus = payResult.getResultStatus(); // 判断resultStatus 为“9000”则代表支付成功,具体状态码代表含义可参考接口文档 if (TextUtils.equals(resultStatus, "9000")) { ToastUtil.showToast(AccountRechargeActivity.this, "支付成功", Toast.LENGTH_SHORT); finish(); } else { // 判断resultStatus 为非"9000"则代表可能支付失败 // "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态) if (TextUtils.equals(resultStatus, "8000")) { ToastUtil.showToast(AccountRechargeActivity.this, "支付结果确认中", Toast.LENGTH_SHORT); } else { // 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误 ToastUtil.showToast(AccountRechargeActivity.this, "支付失败", Toast.LENGTH_SHORT); } } break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_account_recharge); WXPayEntryActivity.recharge = "0"; BarColorUtil.initStatusBarColor(AccountRechargeActivity.this); initViews(); setCostWaySelect(); getRechargeAmount(); } private void initViews() { dialog = new LoadingDialog(this); dialog.setCanceledOnTouchOutside(false); rl_weixin = (RelativeLayout) findViewById(R.id.rl_weixin); rl_weixin.setOnClickListener(this); rl_zhifubao = (RelativeLayout) findViewById(R.id.rl_zhifubao); rl_zhifubao.setOnClickListener(this); iv_back = (ImageView) findViewById(R.id.iv_back); iv_back.setOnClickListener(this); iv_weixin = (ImageView) findViewById(R.id.iv_weixin_select); iv_zhifubao = (ImageView) findViewById(R.id.iv_zhifubao_select); tv_pay = (TextView) findViewById(R.id.tv_pay); tv_pay.setOnClickListener(this); tv_xieyi = (TextView) findViewById(R.id.tv_xieyi); tv_xieyi.setOnClickListener(this); tv_cz1 = (TextView) findViewById(R.id.tv_cz1); tv_cz2 = (TextView) findViewById(R.id.tv_cz2); tv_cz3 = (TextView) findViewById(R.id.tv_cz3); tv_cz4 = (TextView) findViewById(R.id.tv_cz4); tv_cz5 = (TextView) findViewById(R.id.tv_cz5); tv_cz6 = (TextView) findViewById(R.id.tv_cz6); list_textView.add(tv_cz1); list_textView.add(tv_cz2); list_textView.add(tv_cz3); list_textView.add(tv_cz4); list_textView.add(tv_cz5); list_textView.add(tv_cz6); for (int i = 0; i < list_textView.size(); i++) { list_textView.get(i).setOnClickListener(this); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_xieyi: Intent intent = new Intent(getApplication(), MyWebViewActivity.class); intent.putExtra("url", "http://evcharge.cc/activity/charge-proto.html"); startActivity(intent); break; case R.id.iv_back: finish(); break; case R.id.tv_cz1: if (list_rd.size() > 0) { resetView(); if (list_rd.get(0).getTag().equals("0")) { list_textView.get(0).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(0).setBackgroundResource(R.drawable.bg_bt_account_selected); } else { list_textView.get(0).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(0).setBackgroundResource(R.drawable.bg_bt_account_selected2); } recharge_id = list_rd.get(0).getId(); } break; case R.id.tv_cz2: if (list_rd.size() > 0) { resetView(); if (list_rd.get(1).getTag().equals("0")) { list_textView.get(1).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(1).setBackgroundResource(R.drawable.bg_bt_account_selected); } else { list_textView.get(1).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(1).setBackgroundResource(R.drawable.bg_bt_account_selected2); } recharge_id = list_rd.get(1).getId(); } break; case R.id.tv_cz3: if (list_rd.size() > 0) { resetView(); if (list_rd.get(2).getTag().equals("0")) { list_textView.get(2).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(2).setBackgroundResource(R.drawable.bg_bt_account_selected); } else { list_textView.get(2).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(2).setBackgroundResource(R.drawable.bg_bt_account_selected2); } recharge_id = list_rd.get(2).getId(); } break; case R.id.tv_cz4: if (list_rd.size() > 0) { resetView(); if (list_rd.get(3).getTag().equals("0")) { list_textView.get(3).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(3).setBackgroundResource(R.drawable.bg_bt_account_selected); } else { list_textView.get(3).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(3).setBackgroundResource(R.drawable.bg_bt_account_selected2); } recharge_id = list_rd.get(3).getId(); } break; case R.id.tv_cz5: if (list_rd.size() > 0) { resetView(); if (list_rd.get(4).getTag().equals("0")) { list_textView.get(4).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(4).setBackgroundResource(R.drawable.bg_bt_account_selected); } else { list_textView.get(4).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(4).setBackgroundResource(R.drawable.bg_bt_account_selected2); } recharge_id = list_rd.get(4).getId(); } break; case R.id.tv_cz6: if (list_rd.size() > 0) { resetView(); if (list_rd.get(5).getTag().equals("0")) { list_textView.get(5).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(5).setBackgroundResource(R.drawable.bg_bt_account_selected); } else { list_textView.get(5).setTextColor(getResources().getColor(R.color.lvse)); list_textView.get(5).setBackgroundResource(R.drawable.bg_bt_account_selected2); } recharge_id = list_rd.get(5).getId(); } break; case R.id.tv_pay: if (cost_way.equals("weixin")) { if (recharge_id.equals("")) { ToastUtil.showToast(getApplicationContext(), "请选择充值金额", Toast.LENGTH_SHORT); } else { dialog.show(); reqWeChatInfo(); } } else if (cost_way.equals("zhifubao")) { if (recharge_id.equals("")) { ToastUtil.showToast(getApplicationContext(), "请选择充值金额", Toast.LENGTH_SHORT); } else { dialog.show(); reqAliInfo(); } } else if (cost_way.equals("")) { ToastUtil.showToast(getApplicationContext(), "请选择充值方式", Toast.LENGTH_SHORT); } break; case R.id.rl_weixin: cost_way = "weixin"; setCostWaySelect(); break; case R.id.rl_zhifubao: cost_way = "zhifubao"; setCostWaySelect(); break; } } private void setCostWaySelect() { if (cost_way.equals("")) { iv_weixin.setImageResource(R.drawable.icon_wugou); iv_zhifubao.setImageResource(R.drawable.icon_wugou); } else if (cost_way.equals("weixin")) { iv_weixin.setImageResource(R.drawable.icon_xuangou); iv_zhifubao.setImageResource(R.drawable.icon_wugou); } else if (cost_way.equals("zhifubao")) { iv_weixin.setImageResource(R.drawable.icon_wugou); iv_zhifubao.setImageResource(R.drawable.icon_xuangou); } } private void reqWeChatInfo() { long appTime1 = System.currentTimeMillis() / 1000; long updatetime = appTime1 - MainMapActivity.cha; String token = String.valueOf(updatetime); String url = MainApplication.url + "/zhannew/basic/web/index.php/payment/wxpay?" + "recharge_id=" + recharge_id + "&supplier=星星充电&timer=" + token + "&user_id=" + MainApplication.userId + "&ver=1.0&sign=" + Md5Util.md5("recharge_id=" + recharge_id + "&supplier=星星充电&timer=" + token + "&user_id=" + MainApplication.userId + "&key=661f9efdcb4f46fe7ab5f2e78a705a2a"); ToastUtil.showToast(getApplicationContext(), "获取订单中...", Toast.LENGTH_SHORT); OkHttpUtils.get().url(url).build().connTimeOut(5000).readTimeOut(5000).execute(new StringCallback() { @Override public void onError(Call call, Exception e) { Toast.makeText(getApplicationContext(), "网络不给力,请检查网络状态", Toast.LENGTH_SHORT).show(); dialog.cancel(); } @Override public void onResponse(String response) { Log.e("response====", response); if (null != response) { String code = JsonUtils.getKeyResult(response, "code"); if (EmptyUtils.isNotEmpty(code)) { dialog.cancel(); String error_message = JsonUtils.getKeyResult(response, "error_message"); ToastUtil.showToast(getApplicationContext(), error_message, Toast.LENGTH_SHORT); } else { payWechat(response); } } } }); } private void payWechat(String response) { IWXAPI msgApi = WXAPIFactory.createWXAPI(this.getApplication(), null); msgApi.registerApp(getResources().getString(R.string.wx_app_key)); PayReq req = new PayReq(); String appid = JsonUtils.getKeyResult(response, "appid"); String partnerid = JsonUtils.getKeyResult(response, "partnerid"); String prepayid = JsonUtils.getKeyResult(response, "prepayid"); String packagee = JsonUtils.getKeyResult(response, "package"); String noncestr = JsonUtils.getKeyResult(response, "noncestr"); String timestamp = JsonUtils.getKeyResult(response, "timestamp"); String sign = JsonUtils.getKeyResult(response, "sign"); req.appId = appid; req.partnerId = partnerid; req.prepayId = prepayid; req.packageValue = packagee; req.nonceStr = noncestr; req.timeStamp = timestamp; req.sign = sign; msgApi.sendReq(req); dialog.cancel(); } private void reqAliInfo() { long appTime1 = System.currentTimeMillis() / 1000; long updatetime = appTime1 - MainMapActivity.cha; String token = String.valueOf(updatetime); String url = MainApplication.url + "/zhannew/basic/web/index.php/payment/alipay?" + "recharge_id=" + recharge_id + "&supplier=星星充电&timer=" + token + "&user_id=" + MainApplication.userId + "&ver=1.0&sign=" + Md5Util.md5("recharge_id=" + recharge_id + "&supplier=星星充电&timer=" + token + "&user_id=" + MainApplication.userId + "&key=661f9efdcb4f46fe7ab5f2e78a705a2a"); Toast.makeText(getApplicationContext(), "获取订单中...", Toast.LENGTH_SHORT).show(); OkHttpUtils.get().url(url).build().connTimeOut(5000).readTimeOut(5000).execute(new StringCallback() { @Override public void onError(Call call, Exception e) { Toast.makeText(getApplicationContext(), "网络不给力,请检查网络状态", Toast.LENGTH_SHORT).show(); dialog.cancel(); } @Override public void onResponse(String response) { Log.e("response====", response); if (null != response) { String code = JsonUtils.getKeyResult(response, "code"); if (EmptyUtils.isNotEmpty(code)) { dialog.cancel(); String error_message = JsonUtils.getKeyResult(response, "error_message"); ToastUtil.showToast(getApplicationContext(), error_message, Toast.LENGTH_SHORT); } else { String data = JsonUtils.getKeyResult(response, "data"); payAli(data); } } } }); } private void payAli(final String response) { Runnable payRunnable = new Runnable() { @Override public void run() { // 构造PayTask 对象 PayTask alipay = new PayTask(AccountRechargeActivity.this); // 调用支付接口,获取支付结果 String result = alipay.pay(response, true); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; // 必须异步调用 Thread payThread = new Thread(payRunnable); payThread.start(); dialog.cancel(); } private void getRechargeAmount() { long appTime1 = System.currentTimeMillis() / 1000; long updatetime = appTime1 - MainMapActivity.cha - 3; String token = String.valueOf(updatetime); String url = MainApplication.url + "/zhannew/basic/web/index.php/payment/recharge" + "?supplier=星星充电&timer=" + token + "&user_id=" + MainApplication.userId + "&ver=1.0&sign=" + Md5Util.md5("supplier=星星充电&timer=" + token + "&user_id=" + MainApplication.userId + "&key=661f9efdcb4f46fe7ab5f2e78a705a2a"); OkHttpUtils.get().url(url).build().execute(new StringCallback() { @Override public void onError(Call call, Exception e) { } @Override public void onResponse(String response) { Log.e("getRechargeAmount==", response); String data = JsonUtils.getKeyResult(response, "data"); Gson g = new Gson(); list_rd = g.fromJson(data, new TypeToken>() { }.getType()); for (int i = 0; i < list_rd.size(); i++) { RechargeData rd = list_rd.get(i); if (i == 0) { recharge_id = rd.getId(); } if (rd.getGive_money().equals("0")) { rd.setTag("0"); } else { rd.setTag("1"); } } Log.e(TAG, "onResponse: list_rd=" + list_rd.toString()); bindViewData(list_rd); } }); } private void bindViewData(List rechargeData) { for (int i = 0; i < rechargeData.size(); i++) { if (rechargeData.get(i).getTag().equals("0")) { list_textView.get(i).setText(rechargeData.get(i).getMoney() + "元"); list_textView.get(i).setBackgroundResource(R.drawable.bg_bt_account_select); } else { list_textView.get(i).setText(rechargeData.get(i).getMoney() + "返" + rechargeData.get(i).getGive_money()); list_textView.get(i).setBackgroundResource(R.drawable.bg_bt_account_select2); } } if (rechargeData.get(0).getTag().equals("0")) { list_textView.get(0).setBackgroundResource(R.drawable.bg_bt_account_selected); list_textView.get(0).setTextColor(getResources().getColor(R.color.lvse)); } else { list_textView.get(0).setBackgroundResource(R.drawable.bg_bt_account_selected2); list_textView.get(0).setTextColor(getResources().getColor(R.color.lvse)); } } private void resetView() { if (list_rd.size() > 0) { for (int i = 0; i < list_textView.size(); i++) { if (list_rd.get(i).getTag().equals("0")) { list_textView.get(i).setBackgroundResource(R.drawable.bg_bt_account_select); list_textView.get(i).setTextColor(getResources().getColor(R.color.ui_62)); } else { list_textView.get(i).setBackgroundResource(R.drawable.bg_bt_account_select2); list_textView.get(i).setTextColor(getResources().getColor(R.color.ui_62)); } } } } @Override protected void onResume() { super.onResume(); if (WXPayEntryActivity.recharge.equals("1")) { finish(); } } }