PayActivity.kt 30.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package com.yidianling.ydl_pay.pay

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.Point
import android.support.v7.app.AlertDialog
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.ydl.ydlcommon.base.BaseActivity
konghaorui committed
16
import com.ydl.ydlcommon.bean.StatusBarOptions
17 18 19
import com.ydl.ydlcommon.data.PlatformDataManager
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
yjiucheng committed
20
import com.ydl.ydlcommon.modular.ModularServiceManager
21
import com.ydl.ydlcommon.utils.log.LogHelper
22 23
import com.ydl.ydlnet.utils.log.AliYunLogConfig
import com.ydl.ydlnet.utils.log.AliYunLogHelper
yjiucheng committed
24
import com.yidianling.common.tools.LogUtil
25
import com.yidianling.common.tools.ToastUtil
yjiucheng committed
26
import com.yidianling.im.api.service.IImService
27 28 29 30 31 32 33 34
import com.yidianling.ydl_pay.R
import com.yidianling.ydl_pay.common.bean.params.AliPayParam
import com.yidianling.ydl_pay.common.bean.params.BalanceParam
import com.yidianling.ydl_pay.common.bean.params.ChargePayParam
import com.yidianling.ydl_pay.common.bean.params.WxPayParam
import com.yidianling.ydl_pay.common.http.HttpUtils
import com.yidianling.ydl_pay.pay.PayParams.CREATOR.INLET_NORMAL
import com.yidianling.ydl_pay.pay.PayParams.CREATOR.INLET_TEST
yjiucheng committed
35 36 37 38
import com.yidianling.ydl_pay.pay.bean.PayStatus
import com.yidianling.ydl_pay.pay.bean.RechargeParam
import com.yidianling.ydl_pay.pay.bean.TestAddOrderBean
import com.yidianling.ydl_pay.pay.bean.WXRechargeIdParam
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import com.yidianling.ydl_pay.pay.redpacket.RedPacketBean
import com.yidianling.ydl_pay.pay.redpacket.RedPacketPayBean
import com.yidianling.ydl_pay.pay.redpacket.RedPacketPopWindow
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_pay.*
import kotlinx.android.synthetic.main.pay_fragment_loading_dialog.view.*
import java.text.DecimalFormat

/**
 * 付款
 * Created by softrice on 15/11/25.
 * Updated by Zhangwenchao on 18/4/19
 * Updated by yuanWai on 18/10/25 rxJava 简直太恶心了(只是界面逻辑重新梳理了一下,理想状态是拆分成两个fragment来做 然后把rxJava替换掉,但是时间不够,先这样吧)
 */
class PayActivity : BaseActivity() {

konghaorui committed
58 59 60 61
    override fun getStatusViewOptions(): StatusBarOptions {
        return StatusBarOptions(true, statusBarDarkMode = true)
    }

62 63 64 65 66 67 68 69
    companion object {
        /**
         * 入参
         * params 支付订单信息 正常入口传递
         * params_redPacket 红包列表数据+支付金额 测评入口传递
         */
        private const val PARAMS = "params"
        private const val PARAMS_RED = "params_redPacket"
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
        /**
         * 支付类型
         * 1.微信支付 2.支付宝支付
         */
        private const val PAY_WECHAT = 1
        private const val PAY_ALI = 2

        //传递订单号 目前除测评入口使用
        fun newIntent(activity: Activity, payParams: PayParams): Intent {
            LogHelper.getInstance().writeLogSync("跳转到支付界面")
            val intent = Intent(activity, PayActivity::class.java)
            intent.putExtra(PARAMS, payParams)
            return intent
        }

        //传递红包列表数据 目前只有测评入口使用 这个入口进来的调支付时 会先调订单生成接口--再调微信、支付宝支付(串联的行为)
        fun newIntent(activity: Activity, redPacketPayBean: RedPacketPayBean): Intent {
            LogHelper.getInstance().writeLogSync("跳转到支付界面")
            val intent = Intent(activity, PayActivity::class.java)
            intent.putExtra(PARAMS_RED, redPacketPayBean)
            return intent
        }
    }

    //订单信息数据
    private var payParams: PayParams? = null
97

98 99
    //红包列表数据+支付金额
    private var redPacketPayBean: RedPacketPayBean? = null
100

101 102
    //入口
    private var inlet = PayParams.INLET_NORMAL
103

104 105
    //红包列表弹窗
    private var redPacketPopWindow: RedPacketPopWindow? = null
106

107 108 109
    //加载中弹窗
    private var dialog: AlertDialog? = null
    private var dialogContentView: View? = null
110

111 112
    //红包id 默认为0
    private var redPacketId: String? = "0"
113

114 115
    //使用红包金额
    private var redPacketMoney: Float = 0f
116

117 118
    //使用红包文案
    private var redPacketMoneyContent: String = ""
119

120 121
    //账户余额
    private var balanceMoney: Float = 0f
122

123 124
    //支付金额
    private var payMoney: Float = 0F
125

126 127
    //可使用余额
    private var canBalanceMoney = 0F
128

129 130
    //支付类型
    private var payType = PAY_ALI
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
    //测评下单数据bean
    private var testAddOrderBean: TestAddOrderBean? = null

    override fun layoutResId(): Int {
        return R.layout.activity_pay
    }

    //获取参数
    private fun getParam() {
        if (null == intent) {
            return
        }
        if (intent.hasExtra(PARAMS)) {
            //订单信息数据
            payParams = intent.getParcelableExtra(PARAMS)
            if (null != payParams && null != payParams!!.coupon_money && 0f != payParams!!.coupon_money) {
                inlet = payParams!!.inlet
                redPacketMoney = payParams!!.coupon_money
                val sb = StringBuffer()
                sb.append("-").append("¥").append(numFormat(payParams!!.coupon_money))
                redPacketMoneyContent = sb.toString()
            }
        } else if (intent.hasExtra(PARAMS_RED)) {
            //红包列表数据
            redPacketPayBean = intent.getParcelableExtra(PARAMS_RED)
            if (null != redPacketPayBean) {
                if (null != redPacketPayBean!!.payParams) {
                    inlet = redPacketPayBean!!.payParams.inlet
                }
                if (null != redPacketPayBean!!.data && !redPacketPayBean!!.data.isEmpty()) {
                    redPacketMoney = redPacketPayBean!!.data[0].coupon_money!!
                    redPacketId = redPacketPayBean!!.data[0].id
                    val sb = StringBuffer()
                    sb.append("-").append("¥").append(numFormat(redPacketMoney))
                    redPacketMoneyContent = sb.toString()
                }
            }
        }
    }

    override fun initDataAndEvent() {
        //获取参数
        getParam()
        //界面初始化
        initView()
        //设置是否显示付款保障文案
        setPaymentVisible()
        //设置付款保障文案
        setPaymentText()
    }

    //初始化界面
    private fun initView() {
        //设置是否显示红包
        setRedPacketVisible()
        when (inlet) {
            INLET_NORMAL -> {
                //正常入口
                if (null == payParams) {
                    return
                }
                //设置类型标题
                setTypeTitle(payParams!!.title)
                //设置支付金额
                setTypeMoney(payParams!!.needPay)
                //底部支付按钮文案
                setPayBtnText(payParams!!.btnPayText)

                if (payParams!!.type == PayParams.RECHARGE) {
                    //界面类型 充值
                    ll_select_pay.visibility = View.VISIBLE
                    ll_Balance.visibility = View.GONE
                    tvRestPay.text = payParams!!.needPay.toString()
                    //是否显示余额和红包之间 的间隔View
                    isVisibleBalanceTop()
                } else {
                    //界面类型 付款
                    //请求余额接口
                    requestMyBalance()
                }
            }
            INLET_TEST -> {
                //测评入口 付款
                //请求余额接口
                requestMyBalance()
                //设置支付金额
                if (null == redPacketPayBean || null == redPacketPayBean!!.payParams) {
                    return
                }
                setTypeTitle(redPacketPayBean!!.payParams.title)
                setTypeMoney(redPacketPayBean!!.payParams.needPay)
                setPayBtnText(redPacketPayBean!!.payParams.btnPayText)
            }
        }

        //客服
        ll_kefu.setOnClickListener {
            //            ModularServiceManager.provide(IMServiceImpl::class.java).startP2PXiaoYi(this)
yjiucheng committed
230 231 232 233 234
            try {
                ModularServiceManager.provide(IImService::class.java).startP2PXiaoYi(this)
            } catch (e: Exception) {
                LogUtil.e(e.message)
            }
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        }
        //选择红包
        ll_redPacket.setOnClickListener {
            showRedPacketPopupWindow()
        }
        //默认选择微信支付
        selectPayType(0)
        //选择微信支付
        ll_wechat.setOnClickListener { selectPayType(0) }
        //选择支付宝支付
        ll_alipay.setOnClickListener { selectPayType(1) }
        //去支付
        rcb_pay.setOnClickListener { onClickPay() }
    }

    /**
     * 设置标题
     */
    private fun setTypeTitle(title: String) {
        tvType.text = title
    }

    /**
     * 设置支付金额
     */
    private fun setTypeMoney(needPay: Float) {
        val sb = StringBuffer()
        sb.append("¥").append(numFormat(needPay))
        tvTypeMoney.text = sb.toString()
    }

    /**
     * 设置是否显示红包
     */
    private fun setRedPacketVisible() {
        if (TextUtils.isEmpty(redPacketMoneyContent)) {
            ll_redPacket.visibility = View.GONE
        } else {
            //如果 为 正常入口 且 有红包数额 那么说明 该订单 已锁定 就不能更改红包 故隐藏箭头
            if (inlet == INLET_NORMAL) {
                tvRedPacketMore.visibility = View.GONE
            } else {
                tvRedPacketMore.visibility = View.VISIBLE
            }
            ll_redPacket.visibility = View.VISIBLE
        }
    }

    /**
     * 设置支付按钮文本
     */
    private fun setPayBtnText(text: String) {
        rcb_pay.text = text
    }

    /**
     * 请求账户余额接口
     */
    @SuppressLint("CheckResult")
    private fun requestMyBalance() {
        HttpUtils.getMyBalance(BalanceParam())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { showProgressDialog() }
            .doAfterTerminate { dismissProgressDialog() }
            .subscribe(Consumer {
                setData(it.data.balance)
                LogHelper.getInstance().writeLogSync(
//                            "付款界面信息:金额 = " + payParams!!.needPay +
//                                    ", id = "   + payParams!!.payId +
//                                    ", 类型 = " + payParams!!.title +
                    "余额 = " + it.data.balance
                )
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    ToastUtil.toastShort(msg)
                }
            })
    }

    /**
     * 设置 是否显示付款保障文案
     */
    private fun setPaymentVisible() {
        when (inlet) {
            INLET_NORMAL -> {
                //正常入口
                //设置是否显示付款保障
                if (null != payParams && payParams!!.visibleIndemnity) {
                    tv_hint_title.visibility = View.VISIBLE
                    tv_hint_content.visibility = View.VISIBLE
                }
            }
            INLET_TEST -> {
                //测评入口
                //设置是否显示付款保障
                if (null != redPacketPayBean && redPacketPayBean!!.payParams.visibleIndemnity) {
                    tv_hint_title.visibility = View.VISIBLE
                    tv_hint_content.visibility = View.VISIBLE
                }
            }
        }
    }

    /**
     * 设置付款保障文案
     */
    private fun setPaymentText() {
        val globalInfo = PlatformDataManager.getRam().getGlobalInfo()
        if (globalInfo != null) {
            tv_hint_title.text = globalInfo.info.pay_notice.title
            tv_hint_content.text = globalInfo.info.pay_notice.content.replace("<br>", "\n")
        }
    }

    private var select = 0

    private fun selectPayType(i: Int) {
        if (select == i)
            return
        select = i
        when (i) {
            0 -> {
                iv_wechat.setImageResource(R.drawable.pay_img_select)
                iv_alipay.setImageResource(R.drawable.pay_img_no_select)
                payType = PAY_WECHAT
            }
            1 -> {
                iv_alipay.setImageResource(R.drawable.pay_img_select)
                iv_wechat.setImageResource(R.drawable.pay_img_no_select)
                payType = PAY_ALI
            }
        }
    }

    /**
     * 设置余额
     * @param balance 账户余额
     */
    private fun setData(balance: Float) {
        this.balanceMoney = balance
        when (inlet) {
            INLET_NORMAL -> {
                //正常入口
                if (null == payParams) {
                    return
                }
                //如果没有红包 并且 没有余额 那么隐藏掉这条线
                if (0F == balance && TextUtils.isEmpty(redPacketMoneyContent)) {
                    v_redPacketAndBalance.visibility = View.GONE
                } else {
                    v_redPacketAndBalance.visibility = View.VISIBLE
                }
                resetPayMoney()
            }
            INLET_TEST -> {
                //测评入口
                if (null == redPacketPayBean) {
                    return
                }
                resetPayMoney()
            }
        }
    }

    /**
     * 重新计算 红包优惠 使用余额 还需支付
     */
    private fun resetPayMoney() {
        //设置红包文案
        tvRedPacketMoney.text = redPacketMoneyContent

        //如果 可使用的余额 大于 付款金额 - 红包金额
        when (inlet) {
            INLET_NORMAL -> {
                //正常入口
                //如果 有红包 说明 是待支付过来的
                if (redPacketMoney > 0) {
                    calculation(payParams!!.needPay)
                } else {
                    if (balanceMoney > payParams!!.needPay) {
                        //可使用余额 = 付款金额
                        canBalanceMoney = payParams!!.needPay
                        payMoney = 0F
                        ll_select_pay.visibility = View.GONE
                    } else {
                        canBalanceMoney = balanceMoney
                        payMoney = payParams!!.needPay - balanceMoney
                        ll_select_pay.visibility = View.VISIBLE
                    }
                }
            }
            INLET_TEST -> {
                //测评入口
                calculation(redPacketPayBean?.payParams!!.needPay)
            }
        }
        //如果没有可用余额 则隐藏
        if (balanceMoney == 0f) {
            ll_Balance.visibility = View.GONE
        } else {
            ll_Balance.visibility = View.VISIBLE
        }
        //设置 使用余额(可用¥X.XX)
        val balanceBuffer = StringBuffer()
        balanceBuffer.append("使用余额(可用¥").append(balanceMoney).append(")")
        val balanceBuilder = SpannableStringBuilder(balanceBuffer.toString())
        balanceBuilder.setSpan(
            ForegroundColorSpan(resources.getColor(R.color.platform_color_999999)),
            4,
            balanceBuffer.toString().length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        tvBalanceTitle.text = balanceBuilder
        //设置 可用余额
        val useBalanceBuffer = StringBuffer()
        useBalanceBuffer.append("-¥").append(numFormat(canBalanceMoney))
        tvUseBalance.text = useBalanceBuffer.toString()
        //设置 还需支付
        tvRestPay.text = numFormat(payMoney)
        //如果没有可用余额 并且 没有红包 则隐藏还需支付
        if (balanceMoney == 0F && (null == redPacketPayBean || null == redPacketPayBean!!.data || redPacketPayBean!!.data.isEmpty())) {
            rl_RestPay.visibility = View.GONE
        } else {
            rl_RestPay.visibility = View.VISIBLE
        }
        //是否显示余额和红包之间 的间隔View
        isVisibleBalanceTop()
        //设置 底部支付按钮文案
        val payBuffer = StringBuffer()
        payBuffer.append("(¥").append(numFormat(payMoney)).append(")确认付款")
        setPayBtnText(payBuffer.toString())
    }

    private fun isVisibleBalanceTop() {
        if (ll_redPacket.visibility == View.GONE && ll_Balance.visibility == View.GONE) {
            v_balanceTop.visibility = View.GONE
        } else {
            v_balanceTop.visibility = View.VISIBLE
        }
    }

    /**
     * 计算可使用余额
     */
    private fun calculation(needPay: Float) {
        when {
            redPacketMoney >= needPay -> {
                //如果红包金额 大于等于 付款金额
                canBalanceMoney = 0F
                payMoney = 0F
                ll_select_pay.visibility = View.GONE
            }
            balanceMoney > needPay - redPacketMoney -> {
                //可使用余额 = 付款金额 - 使用的红包金额
                canBalanceMoney = needPay - redPacketMoney
                payMoney = 0F
                ll_select_pay.visibility = View.GONE
            }
            else -> {
                canBalanceMoney = balanceMoney
                payMoney = needPay - redPacketMoney - balanceMoney
                ll_select_pay.visibility = View.VISIBLE
            }
        }
    }

    /**
     * 显示红包弹窗
     */
    private fun showRedPacketPopupWindow() {
        if (null == redPacketPayBean || null == redPacketPayBean!!.data || redPacketPayBean!!.data.isEmpty()) {
            return
        }
        if (null == redPacketPopWindow) {
            redPacketPopWindow = RedPacketPopWindow(
                PayActivity@ this,
                redPacketPayBean!!.data,
                object : RedPacketPopWindow.OnSelectListener {
                    override fun select(bean: RedPacketBean?) {
                        val sb = StringBuffer()
                        if (null == bean) {
                            sb.append(redPacketPayBean!!.data.size).append("个可用")
                            redPacketId = "0"
                            redPacketMoney = 0F
                        } else {
                            sb.append("-").append("¥").append(numFormat(bean.coupon_money!!))
                            redPacketId = bean.id
                            redPacketMoney = bean.coupon_money
                        }
                        redPacketMoneyContent = sb.toString()
                        resetPayMoney()
                    }
                })
        } else {
            redPacketPopWindow!!.bindData()
        }
        redPacketPopWindow!!.showBottom(rcb_pay)
    }

    /**
     * 格式化价格
     */
    private fun numFormat(money: Float): String {
        val decimalFormat = DecimalFormat("0.00")//构造方法的字符格式这里如果小数不足2位,会以0补足.
        return decimalFormat.format(money)
    }

    /**
     * 隐藏红包弹窗
     */
    private fun hideRedPacketPopupWindow() {
        if (null != redPacketPopWindow) {
            redPacketPopWindow!!.dismiss()
        }
    }

    /**
     * 点击支付
     */
    private fun onClickPay() {
556
        AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "点击支付")
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
        when (inlet) {
            INLET_NORMAL -> {
                //正常入口
                if (null == payParams) {
                    return
                }
                when (payParams!!.type) {
                    PayParams.RECHARGE -> {
                        //充值
                        recharge()
                    }
                    PayParams.PAY -> {
                        //付款
                        choicePayment()
                    }
                }
            }
            INLET_TEST -> {
                //测评入口
                addOrder()
            }
        }
    }

    //选择支付方式
    private fun choicePayment() {
        //支付金额为0 说明都抵扣了或者订单金额为0 使用余额支付
        if (payMoney == 0F) {
            balancePay()
        } else {
            //使用微信 支付宝 支付
            appPay()
        }
    }

    //测评下单
    private fun addOrder() {
//        if (null == redPacketPayBean || null == redPacketPayBean!!.payParams) {
//            return
//        }
//        YdlDataManager.getHttp().addOrder(AddOrderParam(redPacketPayBean!!.payParams.payId, redPacketId!!))
//                .compose(RxUtils.resultData())
//                .observeOn(AndroidSchedulers.mainThread())
//                .doOnSubscribe { showProgressDialog() }
//                .doAfterTerminate { dismissProgressDialog() }
//                .subscribe(Consumer {
//                    testAddOrderBean = it
//                    LogHelper.getInstance().writeLogSync("测评下单成功")
//                    choicePayment()
//                }, object : ThrowableConsumer() {
//                    override fun accept(msg: String) {
//                        ToastUtil.toastShort(msg)
//                        LogHelper.getInstance().writeLogSync("测评下单失败:$msg")
//                    }
//                })
    }

    var rechargeId: String? = null

    // 充值
    @SuppressLint("CheckResult")
    private fun recharge() {
        if (null == payParams) {
            return
        }
622
        AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "进入充值, 0微信充值,其他支付宝充值:$select")
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

        HttpUtils.recharge(RechargeParam(payParams!!.needPay.toInt().toString()))
            .subscribeOn(Schedulers.io())
            .flatMap {
                rechargeId = it.data.rechargeId
                when (select) {
                    0 ->
                        HttpUtils.wxRecharge(WXRechargeIdParam(it.data.rechargeId))
                            .compose(RxUtils.resultData())
                            .observeOn(AndroidSchedulers.mainThread())
                            .doOnNext { dismissProgressDialog() }
                            .flatMap { RxPay.wxPay(this, it.option) }
                    else -> RxPay.aliPay(this, it.data.aliSign)
                }
            }
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { showProgressDialog("") }
            .doAfterTerminate { dismissProgressDialog() }
            .subscribe(Consumer {
642 643
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "支付成功")

644 645 646 647 648 649 650 651 652 653 654
                LogHelper.getInstance().writeLogSync("支付成功")
                ToastUtil.toastShort("支付成功")
                var intent = Intent()
                intent.putExtra("rechargeId", rechargeId)
                setResult(Activity.RESULT_OK, intent)

                finish()
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    ToastUtil.toastShort(msg)
                    LogHelper.getInstance().writeLogSync("支付失败$msg")
655 656
                    AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "支付失败$msg")

657 658 659 660 661 662 663 664 665 666 667
                }
            })

    }


    // 余额支付
    private fun balancePay() {
        when (inlet) {
            INLET_NORMAL -> {
                //正常入口
668 669
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "余额支付 正常入口")

670 671 672 673
                balancePayById(payParams!!.payId)
            }
            INLET_TEST -> {
                //测评入口
674 675
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "余额支付 测评入口")

676 677 678
                balancePayById(testAddOrderBean!!.payId!!)
            }
            else -> {
679 680
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "余额支付")

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
                balancePayById(payParams!!.payId)
            }
        }

    }

    /**
     * 余额支付
     * @param payId 支付Id
     */
    @SuppressLint("CheckResult")
    private fun balancePayById(payId: String) {
        var bean = ChargePayParam()
        bean.payId = payId
        HttpUtils.payByCharge(bean)
            .compose(RxUtils.resultData())
697
            .subscribeOn(Schedulers.newThread())
698 699 700 701 702
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { showProgressDialog() }
            .doAfterTerminate { dismissProgressDialog() }
            .subscribe(Consumer {
                LogHelper.getInstance().writeLogSync("支付成功")
703
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "余额支付:支付成功")
704 705 706 707 708 709 710
                ToastUtil.toastShort("支付成功")
                setResult(Activity.RESULT_OK)
                finish()
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    ToastUtil.toastShort(msg)
                    LogHelper.getInstance().writeLogSync("支付失败:$msg")
711 712
                    AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "余额支付:支付失败:$msg")

713 714 715 716 717 718 719 720 721
                }
            })
    }

    /**
     * app 支付,微信或支付宝
     */
    @SuppressLint("CheckResult")
    private fun appPay() {
722
        AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "微信支付 0 其他支付宝支付 select:${select}")
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

        Observable.just(select)
            .subscribeOn(Schedulers.io())
            .flatMap {
                when (it) {
                    0 -> {
                        wxPay()
                    }
                    else -> {
                        aliPay()
                    }
                }
            }
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { showProgressDialog("") }
            .doAfterTerminate { dismissProgressDialog() }
            .subscribe(Consumer {
740
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "支付成功")
741 742 743 744 745 746 747
                LogHelper.getInstance().writeLogSync("支付成功")
                setResult(Activity.RESULT_OK)
                finish()
            }, object : ThrowableConsumer() {
                override fun accept(msg: String) {
                    ToastUtil.toastShort(msg)
                    LogHelper.getInstance().writeLogSync("支付失败:$msg")
748 749
                    AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "支付失败:$msg")

750 751 752 753 754 755 756 757 758 759 760
                }
            })
    }

    /**
     * 微信支付
     */
    private fun wxPay(): Observable<PayStatus>? {
        return when (inlet) {
            INLET_NORMAL -> {
                //正常入口
761 762
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "微信支付:正常入口")
                wxPayById(payParams!!.payId, payParams?.merchantType.toString())
763 764 765 766
            }
            INLET_TEST -> {
                //测评入口
                if (null != testAddOrderBean && !TextUtils.isEmpty(testAddOrderBean!!.payId!!)) {
767 768
                    AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "微信支付:测评入口")
                    wxPayById(testAddOrderBean!!.payId!!, payParams?.merchantType.toString())
769 770 771 772 773
                } else {
                    null
                }
            }
            else -> {
774 775
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "微信支付")
                wxPayById(payParams!!.payId, payParams?.merchantType.toString())
776 777 778 779 780 781 782 783
            }
        }
    }

    /**
     * 微信支付
     * @param payId 支付id
     */
784 785
    private fun wxPayById(payId: String, merchantType: String): Observable<PayStatus> {
        val bean = WxPayParam(payId, 1, merchantType)
786 787 788 789 790 791 792 793 794 795 796 797 798 799
        return HttpUtils.wxPay(bean)
            .compose(RxUtils.resultData())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext { dismissProgressDialog() }
            .flatMap { RxPay.wxPay(this, it.option) }
    }

    /**
     * 支付宝支付
     */
    private fun aliPay(): Observable<PayStatus>? {
        return when (inlet) {
            INLET_NORMAL -> {
                //正常入口
800 801
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "支付宝支付:正常入口")

802 803 804 805 806
                aliPayById(payParams!!.payId)
            }
            INLET_TEST -> {
                //测评入口
                if (null != testAddOrderBean && !TextUtils.isEmpty(testAddOrderBean!!.payId!!)) {
807 808
                    AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "支付宝支付:测评入口")

809 810 811 812 813 814
                    aliPayById(testAddOrderBean!!.payId!!)
                } else {
                    null
                }
            }
            else -> {
815 816
                AliYunLogHelper.getInstance().sendLog(AliYunLogConfig.PAY, "支付宝支付")

817 818 819 820 821 822 823 824 825 826 827
                aliPayById(payParams!!.payId)
            }
        }
    }

    /**
     * 支付宝支付
     * @param payId 支付Id
     */
    private fun aliPayById(payId: String): Observable<PayStatus> {
        var bean = AliPayParam(payId, 1)
yjiucheng committed
828
        return HttpUtils.getAliPayOrderId(bean)
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
            .compose(RxUtils.resultData())
            .flatMap {
                when (inlet) {
                    INLET_NORMAL -> {
                        RxPay.aliPay(this, it.aliSign)
                    }
                    INLET_TEST -> {
                        RxPay.aliPay(this, it.aliSign)
                    }
                    else -> {
                        RxPay.aliPay(this, it.aliSign)
                    }
                }
            }
    }

    /**
     * 显示加载中弹窗
     */
    override fun showProgressDialog(str: String?) {
        if (dialog != null && dialog!!.isShowing) {
            return
        }
        if (dialog == null) {
            dialog = AlertDialog.Builder(PayActivity@ this)
                .create()
        }
        if (dialogContentView == null) {
857 858
            dialogContentView =
                LayoutInflater.from(this).inflate(R.layout.pay_fragment_loading_dialog, null)
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
            dialogContentView?.tvMsg?.visibility = View.GONE
        }
        dialog?.show()
        dialog?.window?.setContentView(dialogContentView)
        dialog?.window?.setGravity(Gravity.CENTER)
        val p = dialog?.window!!.attributes  //获取对话框当前的参数值
        var point = Point()
        windowManager.defaultDisplay.getSize(point)// 获取屏幕宽、高用
        p.height = (point.y * 0.2).toInt()
        p.width = (point.x * 0.35).toInt()
        dialog?.window!!.attributes = p     //设置生效
    }

    /**
     * 隐藏加载中弹窗
     */
    override fun dismissProgressDialog() {
        if (dialog != null && dialog!!.isShowing) {
            dialog!!.dismiss()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        hideRedPacketPopupWindow()
    }
}