PayInfoDetailView.kt 9.75 KB
Newer Older
严久程 committed
1
package com.yidianling.ydl_pay.widget
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.yidianling.ydl_pay.R
import com.yidianling.ydl_pay.bean.CommonCouponBean
import com.yidianling.ydl_pay.bean.OrderInfoBean
import kotlinx.android.synthetic.main.view_pay_info_detail.view.*
import java.math.BigDecimal


/**
 * @author jiucheng
 * @描述:支付详细信息view
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/4/4
 */
@SuppressLint("ViewConstructor")
严久程 committed
23 24
class PayInfoDetailView(context: Context, var listener: OnCouponDetailClickListener) :
    LinearLayout(context) {
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
    private var orderInfoBean: OrderInfoBean? = null
    /**
     * 是否使用余额
     */
    private var isUseChange = true

    /**
     * 使用余额,余额是否足够
     */
    private var isBalanceSufficient = false
    //需要sdk(支付宝、微信)支付的金额
    private var sdkPayMoney = 0.00f

    /**
     * 支付方式
     * 1024.微信支付 1025.支付宝支付
     */
    private var payWay = PAY_WECHAT

    /**
     * 支付类型
     * 1余额,2三方支付,3混合
     */
    private var useMoneyType = 1

    /**
     * 可用优惠券的数量
     */
    private var availableCount: Int = 0

    companion object {
        /**
         * 支付方式
         * 1024.微信支付 1025.支付宝支付
         */
        private const val PAY_WECHAT = 1024
        private const val PAY_ALI = 1025


        /**
         * 支付类型
         * 1余额,2三方支付,3混合
         */
        private const val MONEY_TYPE_CHANGE = 1
        private const val MONEY_TYPE_THIRD_PAY = 2
        private const val MONEY_TYPE_MIXED = 3
    }


    init {
        initView()
    }

    private fun initView() {
        orientation = VERTICAL
严久程 committed
80 81 82 83
        val params = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
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
        layoutParams = params
        View.inflate(context, R.layout.view_pay_info_detail, this)


        rl_discount.setOnClickListener {
            listener.selectCoupon("")
        }

        tv_change_money.setOnClickListener {
            if (isBalanceSufficient) {
                isUseChange = true
            } else {
                isUseChange = !isUseChange
            }

            setChooseChangeMoney(isUseChange)
            updateNeedPayMoney()
        }

        rl_weixin_pay.setOnClickListener {
            if (isBalanceSufficient) {
                isUseChange = false
                setChooseChangeMoney(isUseChange)
                updateNeedPayMoney()
            }
            setSelectPayWay(PAY_WECHAT)
        }

        rl_ali_pay.setOnClickListener {
            if (isBalanceSufficient) {
                isUseChange = false
                setChooseChangeMoney(isUseChange)
                updateNeedPayMoney()
            }
            setSelectPayWay(PAY_ALI)
        }

        tv_ensure_pay.setOnClickListener {
            useMoneyType = if (!isUseChange) {
                MONEY_TYPE_THIRD_PAY
            } else if (isUseChange && sdkPayMoney > 0) {
                MONEY_TYPE_MIXED
            } else {
                MONEY_TYPE_CHANGE
            }
            if (orderInfoBean!!.maxCoupon != null) {
严久程 committed
130 131 132 133 134 135 136
                listener.ensurePay(
                    payWay,
                    sdkPayMoney,
                    useMoneyType,
                    orderInfoBean!!.maxCoupon.code,
                    orderInfoBean!!.maxCoupon.couponType
                )
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
            } else {
                listener.ensurePay(payWay, sdkPayMoney, useMoneyType, "", "")
            }
        }
    }

    fun setAvailableCount(availableCount: Int) {
        this.availableCount = availableCount
    }

    /**
     * 订单的信息
     * @param payBusinessType 业务类型
     */
    @SuppressLint("SetTextI18n")
    fun setData(orderInfoBean: OrderInfoBean, payBusinessType: Int) {
        this.orderInfoBean = orderInfoBean
        if (payBusinessType == 1) {
            tv_pay_title.text = "课程服务"
        }
        if (payBusinessType == 2) {
            tv_pay_title.text = "倾诉服务"
        }
        if (payBusinessType == 3) {
            tv_pay_title.text = "测评服务"
        }
        if (payBusinessType == 4) {
            tv_pay_title.text = "咨询服务"
        }

严久程 committed
167 168 169 170 171 172 173 174 175 176
        if (payBusinessType == 5) {
            tv_pay_title.text = "送感谢"

            tv_change_money.isEnabled = false
            if (orderInfoBean.availableMoney >= orderInfoBean.applyFee) {
                rl_third_pay.visibility = View.INVISIBLE
                rl_third_pay.isEnabled = false
            }
        }

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

        //价格
        tv_price.text = orderInfoBean.applyFee.toString()
        //可用余额
        tv_change_useful.text = "[可用¥${orderInfoBean.availableMoney}]"
        rl_discount.visibility = if (orderInfoBean.isShowCoupon) View.VISIBLE else View.GONE

        setCouponData(orderInfoBean.maxCoupon)
    }

    /**
     * 设置优惠券的数据
     *
     */
    @SuppressLint("SetTextI18n")
    fun setCouponData(commonCouponBean: CommonCouponBean?) {
        if (commonCouponBean == null) {
            tv_discount_money.text = "0"
            tv_flag.visibility = View.GONE
            tv_discount_money.visibility = View.GONE
            tv_no_choose.visibility = View.VISIBLE
            if (availableCount == 0) {
                tv_no_choose.text = "暂无优惠"
            } else {
                tv_no_choose.text = "有${availableCount}张优惠券可用"
            }
            orderInfoBean!!.maxCoupon = null

        } else {
            tv_flag.visibility = View.VISIBLE
            tv_discount_money.visibility = View.VISIBLE
            tv_no_choose.visibility = View.GONE

            orderInfoBean!!.maxCoupon = commonCouponBean
            //优惠
            tv_discount_money.text = commonCouponBean.combinedAmount.toString()
        }

        updateNeedPayMoney()
    }

    /**
     * 更新还需支付的金额
     */
    private fun updateNeedPayMoney() {
        var needPay = if (orderInfoBean!!.maxCoupon == null) {
            orderInfoBean!!.applyFee
        } else {
            val b1 = BigDecimal(orderInfoBean!!.applyFee.toString())
            val b2 = BigDecimal(orderInfoBean!!.maxCoupon.combinedAmount.toString())
            b1.subtract(b2).toFloat()
        }

        if (needPay == 0f) {//优惠券已经完全抵扣
            ll_third_pay.visibility = View.GONE
            view_empty.visibility = View.VISIBLE
            ll_third_pay.isEnabled = false
        } else {//不能完全抵扣
            ll_third_pay.visibility = View.VISIBLE
            view_empty.visibility = View.GONE
            ll_third_pay.isEnabled = true
        }

严久程 committed
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
        if (isUseChange) {//使用余额
            if (orderInfoBean!!.availableMoney >= needPay) {
                tv_change_money.text = needPay.toString()
                sdkPayMoney = 0f
                setSelectPayWay(0)

                isBalanceSufficient = true
            } else {
                tv_change_money.text = orderInfoBean!!.availableMoney.toString()

                val bd1 = BigDecimal(needPay.toString())
                val bd2 = BigDecimal(orderInfoBean!!.availableMoney.toString())
                sdkPayMoney = bd1.subtract(bd2).toFloat()
//                sdkPayMoney = needPay - orderInfoBean!!.availableMoney
                if (payWay == 0) {
                    setSelectPayWay(PAY_WECHAT)
                }
                isBalanceSufficient = false
            }
        } else {//不使用余额
            sdkPayMoney = needPay
        }

//        val bg = BigDecimal(sdkPayMoney.toDouble()).setScale(2, RoundingMode.UP)

        tv_pay_money.text = sdkPayMoney.toString()
    }

    /**
     * 选择支付方式
     */
    private fun setSelectPayWay(way: Int) {
        if (payWay == way) {
            return
        }
        payWay = way
        when (payWay) {
            PAY_WECHAT -> {//微信
严久程 committed
279 280
                img_weixin_way.setImageResource(R.drawable.pay_img_select)
                img_ali_way.setImageResource(R.drawable.pay_img_no_select)
281 282
            }
            PAY_ALI -> {//支付宝
严久程 committed
283 284
                img_weixin_way.setImageResource(R.drawable.pay_img_no_select)
                img_ali_way.setImageResource(R.drawable.pay_img_select)
285 286
            }
            else -> {
严久程 committed
287 288
                img_ali_way.setImageResource(R.drawable.pay_img_no_select)
                img_weixin_way.setImageResource(R.drawable.pay_img_no_select)
289 290 291 292 293 294 295 296 297 298
            }
        }
    }


    /**
     * 是否选使用余额
     */
    private fun setChooseChangeMoney(isUse: Boolean) {
        if (isUse) {
严久程 committed
299 300 301 302 303 304
            tv_change_money.setCompoundDrawablesWithIntrinsicBounds(
                0,
                0,
                R.drawable.pay_img_select,
                0
            )
305
        } else {
严久程 committed
306 307 308 309 310 311
            tv_change_money.setCompoundDrawablesWithIntrinsicBounds(
                0,
                0,
                R.drawable.pay_img_no_select,
                0
            )
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        }
    }


    interface OnCouponDetailClickListener {
        /**
         * 选择优惠券
         */
        fun selectCoupon(couponId: String)

        /**
         * 确认支付
         * @param payWay  支付方式  1024.微信支付 1025.支付宝支付
         * @param payMoney  还需支付金额
         * @param useMoneyType   支付类型  1余额,2三方支付,3混合
         * @param code    优惠券code
         * @param couponType    券类型 1兑换券 2新优惠券
         *
         */
严久程 committed
331 332 333 334 335 336 337
        fun ensurePay(
            payWay: Int,
            payMoney: Float,
            useMoneyType: Int,
            code: String,
            couponType: String
        )
338 339
    }
}