package com.fanghua.driver.ui.mine.money_bag
|
|
import android.annotation.SuppressLint
|
import android.os.Handler
|
import android.os.Message
|
import android.text.Editable
|
import android.text.TextUtils
|
import android.text.TextWatcher
|
import android.widget.CheckedTextView
|
import cn.sinata.xldutils.utils.SpanBuilder
|
import cn.sinata.xldutils.utils.clickDelay
|
import cn.sinata.xldutils.utils.visible
|
import com.alipay.sdk.app.PayTask
|
import com.fanghua.driver.R
|
import com.fanghua.driver.base.MyBaseActivity
|
import com.fanghua.driver.bean.StringBean
|
import com.fanghua.driver.bean.WxBean
|
import com.fanghua.driver.netUtls.Api
|
import com.fanghua.driver.netUtls.callNet
|
import com.fanghua.driver.netUtls.getMapByAny
|
import com.fanghua.driver.ui.DialogUtil
|
import com.fanghua.driver.utils.payutil.PayResult
|
import com.fanghua.driver.wxapi.WXPayEntryActivity
|
import com.lljjcoder.style.citylist.Toast.ToastUtils
|
import kotlinx.android.synthetic.main.activity_recharge.*
|
import kotlinx.android.synthetic.main.dialog_change_pay_type.view.*
|
import org.jetbrains.anko.toast
|
|
class RechargeActivity : MyBaseActivity() {
|
override fun setContentView() {
|
setContentView(R.layout.activity_recharge)
|
}
|
|
private val items = arrayListOf<CheckedTextView>()
|
private var money = 10 //充值金额
|
|
override fun initView() {
|
setTitleText("充值")
|
tv_balance.text = SpanBuilder.content("¥%.2f".format(intent.getDoubleExtra("balance",0.0))).sizeSpan(0,1,20).build()
|
items.addAll(arrayOf(tv_10,tv_20,tv_50,tv_100,tv_200))
|
}
|
|
override fun setOnclick() {
|
items.forEachIndexed { index, checkedTextView ->
|
checkedTextView.setOnClickListener {
|
items.forEach {
|
it.isChecked = false
|
}
|
checkedTextView.isChecked = true
|
et_money.setText("")
|
money = checkedTextView.text.toString().substring(0,checkedTextView.text.length-1).toInt()
|
}
|
}
|
|
et_money.addTextChangedListener(object : TextWatcher {
|
override fun afterTextChanged(s: Editable?) {
|
if (s.isNullOrEmpty())
|
money = 0
|
else{
|
items.forEach {
|
it.isChecked = false
|
}
|
money = try {
|
s.toString().toInt()
|
}catch (e:Exception){
|
0
|
}
|
}
|
|
}
|
|
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
}
|
|
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
|
}
|
})
|
|
tv_action.clickDelay {
|
if (money == 0)
|
toast("请选择充值金额")
|
else if (money%10 != 0)
|
toast("请填写10的倍数")
|
else{
|
val dialog2 = DialogUtil.getDialog(this, R.layout.dialog_change_pay_type)
|
dialog2.window?.decorView?.let {
|
it.tv_title.visible()
|
it.bg.setOnClickListener {
|
dialog2.dismiss()
|
}
|
it.rv_wx.isChecked = true
|
it.tv_action.clickDelay {
|
dialog2.dismiss()
|
val map = getMapByAny()
|
map["amount"] = money
|
map["type"] = if (it.rv_wx.isChecked) 1 else 2
|
callNet(Api.balanceRecharge,map){_it->
|
if (it.rv_wx.isChecked) {
|
var data = gson.fromJson<WxBean>(_it,WxBean::class.java)
|
WXPayEntryActivity.to(this, 1, data.resultUtil.data)
|
}else{
|
var data = gson.fromJson<StringBean>(_it, StringBean::class.java)
|
payV2(data.resultUtil.data)
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
|
/**
|
* 支付宝支付业务示例
|
*/
|
open fun payV2(orderInfo: String?) { //if (TextUtils.isEmpty(APPID) || (TextUtils.isEmpty(RSA2_PRIVATE) && TextUtils.isEmpty(RSA_PRIVATE))) {
|
val payRunnable = Runnable {
|
val alipay = PayTask(mContext as MyBaseActivity)
|
val result =
|
alipay.payV2(orderInfo, true)
|
val msg = Message()
|
msg.what = 1
|
msg.obj = result
|
mHandler.sendMessage(msg)
|
}
|
// 必须异步调用
|
val payThread = Thread(payRunnable)
|
payThread.start()
|
|
}
|
|
@SuppressLint("HandlerLeak")
|
private val mHandler: Handler = object : Handler() {
|
override fun handleMessage(msg: Message) {
|
when (msg.what) {
|
1 -> {
|
val payResult =
|
PayResult(msg.obj as Map<String?, String?>)
|
/**
|
* 对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
|
*/
|
val resultInfo: String = payResult.getResult() // 同步返回需要验证的信息
|
val resultStatus: String = payResult.getResultStatus()
|
// 判断resultStatus 为9000则代表支付成功 //out_trade_no 为订单编号 截取 英文编号 + 后14位数字
|
if (TextUtils.equals(resultStatus, "9000")) { // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
|
toast("支付成功")
|
finish()
|
// showAlert(AlipayActivity.this, "支付成功" + payResult);
|
} else { // 该笔订单真实的支付结果,需要依赖服务端的异步通知。
|
ToastUtils.showShortToast(mContext, "支付取消")
|
}
|
}
|
}
|
}
|
}
|
|
}
|