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.keqiang.highcloud.view.activity.setting; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.DialogInterface; 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.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.alipay.sdk.app.EnvUtils; import com.alipay.sdk.app.PayTask; import com.ta.utdid2.android.utils.PhoneInfoUtils; import com.tencent.mm.opensdk.modelpay.PayReq; import com.tencent.mm.opensdk.openapi.IWXAPI; import com.tencent.mm.opensdk.openapi.WXAPIFactory; import org.apache.http.conn.util.InetAddressUtils; import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Enumeration; import java.util.Map; import java.util.Timer; import java.util.TimerTask; import rx.Subscriber;
public class PayActivity extends BaseActivity { private TextView tvCount; private ZzTitleBar titleBar; private TextView tvPay; private static final int SDK_PAY_FLAG = 1; private static final int SDK_AUTH_FLAG = 2; private LinearLayout llAlipay; private LinearLayout llWxPay; private CheckBox cbAlipay; private CheckBox cbWxpay; private IWXAPI api; private TextView tvMinteShi; private TextView tvMinuteGe; private TextView tvSecondShi; private TextView tvSecondGe; private String time; private Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { countTime(time); } }); } }; private String orderId; private TextView tvProductName; private TextView tvNumber; private TextView tvType; private String orderNo; @Override public int getLayoutId() { return R.layout.activity_alipay; } @SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @SuppressWarnings("unused") public void handleMessage(Message msg) { switch (msg.what) { case SDK_PAY_FLAG: { @SuppressWarnings("unchecked") PayResult payResult = new PayResult((Map<String, String>) msg.obj);
String resultInfo = payResult.getResult(); String resultStatus = payResult.getResultStatus(); if (TextUtils.equals(resultStatus, "9000")) { ToastUtils.showCustomBgToast("支付成功!"); Intent intent = new Intent(PayActivity.this, ShopMyOrderActivity.class); startActWithIntent(intent); closeAct(); } else { switch (resultStatus) { case "6001": ToastUtils.showCustomBgToast("已取消支付操作"); break; default: ToastUtils.showCustomBgToast("支付失败,错误代码:"+resultStatus); break; } } break; } default: break; } } }; @Override public void initView() { titleBar = (ZzTitleBar) findViewById(R.id.title_bar); tvCount = (TextView) findViewById(R.id.tv_count); tvPay = (TextView) findViewById(R.id.tv_pay); tvType = (TextView) findViewById(R.id.tv_type); tvProductName = (TextView) findViewById(R.id.tv_product_name); tvNumber = (TextView) findViewById(R.id.tv_number); llAlipay = (LinearLayout) findViewById(R.id.ll_alipay); llWxPay = (LinearLayout) findViewById(R.id.ll_wxpay); cbAlipay = (CheckBox) findViewById(R.id.cb_alipay); cbWxpay = (CheckBox) findViewById(R.id.cb_weixin_pay); tvMinteShi = (TextView) findViewById(R.id.tv_minute_shi); tvMinuteGe = (TextView) findViewById(R.id.tv_minute_ge); tvSecondShi = (TextView) findViewById(R.id.tv_second_shi); tvSecondGe = (TextView) findViewById(R.id.tv_second_ge); } @Override public void initData() { api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false); orderId = getIntent().getStringExtra("orderId"); orderNo = getIntent().getStringExtra("orderNo"); String productName = getIntent().getStringExtra("productName"); String total = getIntent().getStringExtra("total"); String number = getIntent().getStringExtra("number"); String model = getIntent().getStringExtra("model"); tvType.setText(model); tvProductName.setText(productName); tvNumber.setText(number); tvCount.setText(total); String createTime = getIntent().getStringExtra("createTime"); time = DateUtil.getTimeAddMinute(createTime, 16); if (time == null) { ToastUtils.showCustomBgToast("订单超时或已取消"); return; } handler.postDelayed(runnable, 1000); } private void countTime(String time) { String curTime = DateUtil.getYearMonthDayHourMinuteSecond(); long seconds = DateUtil.getSecondDuration(curTime, time); if (seconds > 0) { long m = seconds / 60; if (m < 10) { tvMinteShi.setText("0"); tvMinuteGe.setText("" + m); } else { tvMinteShi.setText("" + (m / 10)); tvMinuteGe.setText("" + (m % 10)); } long s = seconds % 60; if (s < 10) { tvSecondShi.setText("0"); tvSecondGe.setText("" + s); } else { tvSecondShi.setText("" + (s / 10)); tvSecondGe.setText("" + (s % 10)); } handler.postDelayed(runnable, 1000); } else { handler.removeCallbacks(runnable); ToastUtils.showCustomBgToast("订单超时"); closeAct(); } } @Override public void initEvent() { titleBar.setOnTitleBarClickListener(new ZzTitleBar.OnTitleBarClickListener() { @Override public void onLeftClick() { closeAct(); } @Override public void onRightClick() { } }); tvPay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (cbAlipay.isChecked()) { payV2(); } else { payVX(); } } }); llAlipay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cbAlipay.setChecked(true); cbWxpay.setChecked(false); } }); llWxPay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cbWxpay.setChecked(true); cbAlipay.setChecked(false); } }); } private void payVX() { Api.getRegisterApi(HCApplication.getContext()) .vxPay(HCSharedUtil.getPhoneNumber(this), orderId) .compose(RxHelper.<VxPayResult>io_main()) .subscribe(new Subscriber<VxPayResult>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { ToastUtils.showCustomBgToast(getString(R.string.no_net_text) + e.toString()); } @Override public void onNext(VxPayResult vxPayResult) { if (vxPayResult.getCode().equals("1")) { PayReq req = new PayReq(); req.appId = vxPayResult.getData().getAppid(); req.partnerId = vxPayResult.getData().getMch_id(); req.prepayId = vxPayResult.getData().getPrepay_id(); req.nonceStr = vxPayResult.getData().getOld_nonce_str(); req.packageValue = "Sign=WXPay"; req.sign = vxPayResult.getData().getOld_sign(); req.timeStamp = vxPayResult.getData().getTimestamp(); api.sendReq(req); closeAct(); } else { ToastUtils.showCustomBgToast(vxPayResult.getMsg()); } } }); }
public void payV2() { Api.getRegisterApi(HCApplication.getContext()) .aliPay(HCSharedUtil.getPhoneNumber(this), orderId) .compose(RxHelper.<AliPayResult>io_main()) .subscribe(new Subscriber<AliPayResult>() { @Override public void onCompleted() { }
@Override public void onError(Throwable e) { ToastUtils.showCustomBgToast(getString(R.string.no_net_text) + e.toString()); } @Override public void onNext(AliPayResult aliPayResult) { if (aliPayResult.getCode().equals("1")) { final String orderInfo = aliPayResult.getOrderInfo(); Runnable payRunnable = new Runnable() { @Override public void run() { PayTask alipay = new PayTask(PayActivity.this); Map<String, String> result = alipay.payV2(orderInfo, true); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; Thread payThread = new Thread(payRunnable); payThread.start(); } } }); } @Override public void saveState(Bundle bundle) { } @Override public void restoreState(Bundle bundle) { } @Override public boolean isDefaultBackClose() { return false; } @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacks(runnable); handler.removeCallbacksAndMessages(null); handler = null; } }
|