CommonPayDialog.kt 30.2 KB
Newer Older
1
package com.yidianling.ydl_pay.common
2

严久程 committed
3
import android.annotation.SuppressLint
4 5 6 7 8
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Point
import android.os.Bundle
YKai committed
9 10
import androidx.viewpager.widget.PagerAdapter
import androidx.appcompat.app.AlertDialog
11 12 13 14 15 16
import android.text.TextUtils
import android.view.*
import android.widget.LinearLayout
import com.alipay.sdk.app.PayTask
import com.tencent.mm.opensdk.modelpay.PayReq
import com.tencent.mm.opensdk.openapi.WXAPIFactory
17
import com.ydl.ydlcommon.utils.log.AliYunLogConfig
18
import com.ydl.ydlcommon.utils.log.AliYunRichLogsHelper
严久程 committed
19
import com.yidianling.common.tools.RxImageTool
20 21 22 23 24 25 26 27 28
import com.yidianling.ydl_pay.R
import com.yidianling.ydl_pay.common.bean.*
import com.yidianling.ydl_pay.common.bean.params.*
import com.yidianling.ydl_pay.common.http.HttpUtils
import com.yidianling.ydl_pay.common.http.utils.NetUtils
import com.yidianling.ydl_pay.common.http.utils.RxDeviceTool
import com.yidianling.ydl_pay.common.toast.ToastHelper
import com.yidianling.ydl_pay.common.widget.PayCouponView
import com.yidianling.ydl_pay.common.widget.PayInfoDetailView
29 30 31
import de.greenrobot.event.EventBus
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
严久程 committed
32
import kotlinx.android.synthetic.main.pay_dialog_common_pay.*
33 34 35 36 37 38 39 40 41 42 43 44 45 46
import java.util.*

/**
 * @author jiucheng
 * @描述:支付的弹窗
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/4/3
 */
class CommonPayDialog : Dialog {
    private var goodsId: String? = null
    private lateinit var activity: Activity
    private var viewList = ArrayList<LinearLayout>()
    private var couponListBean: AllCouponListBean? = null
47

48 49 50 51 52
    //选中的优惠券
    private var selectedCouponBean: CommonCouponBean? = null
    private var payCouponView: PayCouponView? = null
    private var detailView: PayInfoDetailView? = null
    private var listener: OnPayResultListener? = null
严久程 committed
53 54
    private var payMoney: Float? = null
    private var thankPayId: String? = null
55

56 57 58
    //加载中弹窗
    private var dialog: AlertDialog? = null
    private var dialogContentView: View? = null
59

60 61 62 63 64
    /**
     * 支付方式
     * 1024.微信支付 1025.支付宝支付
     */
    private var payWay = 0
65

66 67 68 69 70 71
    /**
     * 支付的业务类型
     * [TYPE_COURSE] 课程支付
     * [TYPE_CONFIDE] 倾诉支付
     * [TYPE_TEST] 测评支付
     * [TYPE_CONSULTANT] 咨询支付
严久程 committed
72
     * [TYPE_TRENDS_THANKS] 动态打赏
73 74 75 76 77 78 79 80
     */
    private var payBusinessType = 0

    companion object {
        /**
         * 课程业务
         */
        const val TYPE_COURSE = 1
81

82 83 84 85
        /**
         * 倾诉
         */
        const val TYPE_CONFIDE = 2
86

87 88 89 90
        /**
         * 测评
         */
        const val TYPE_TEST = 3
91

92 93 94 95
        /**
         * 咨询
         */
        const val TYPE_CONSULTANT = 4
96

严久程 committed
97 98 99 100
        /**
         * 动态打赏
         */
        const val TYPE_TRENDS_THANKS = 5
101 102 103 104 105 106 107 108 109

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

严久程 committed
110
    private constructor(context: Context) : super(context, R.style.pay_common_dialog_style)
111

严久程 committed
112
    private constructor(build: Build) : this(build.mActivity) {
113 114 115 116
        this.goodsId = build.goodsId
        this.activity = build.mActivity
        this.listener = build.listener
        this.payBusinessType = build.payType
严久程 committed
117 118
        this.payMoney = build.payMoney
        this.thankPayId = build.payId
119 120 121 122
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
严久程 committed
123
        setContentView(R.layout.pay_dialog_common_pay)
124 125 126 127 128 129 130 131

        window.attributes.width = WindowManager.LayoutParams.MATCH_PARENT
        window.attributes.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.setGravity(Gravity.BOTTOM)
        setCanceledOnTouchOutside(true)
        EventBus.getDefault().register(this)


严久程 committed
132 133 134 135 136 137 138 139 140 141 142
        if (payBusinessType == TYPE_TRENDS_THANKS) {
            var parmas = view_pager.layoutParams
            parmas.width = ViewGroup.LayoutParams.MATCH_PARENT
            parmas.height = RxImageTool.dp2px(371f)

            view_pager.layoutParams = parmas

            getMyBalance()
        } else {
            getOrderInfo()
        }
143 144 145
    }


严久程 committed
146 147 148
    private fun initThanksPayInfoDetailView(bean: OrderInfoBean) {
        view_pager.setScanScroll(false)
        detailView =
149 150 151 152 153
            PayInfoDetailView(
                activity,
                object : PayInfoDetailView.OnCouponDetailClickListener {
                    override fun selectCoupon(couponId: String) {
                    }
严久程 committed
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                    override fun ensurePay(
                        payWay: Int,
                        payMoney: Float,
                        useMoneyType: Int,
                        code: String,
                        couponType: String
                    ) {
                        //感谢的时候比较暴力:payid是外部传过来的、余额一定会被扣掉
                        if (payBusinessType == TYPE_TRENDS_THANKS) {
                            showProgressDialog()
                            if (payMoney > 0) {
                                when (payWay) {
                                    PAY_WECHAT -> {
                                        getWeiXinPayOrderId(
                                            thankPayId!!,
170
                                            if (useMoneyType == 2) 1 else 0, bean.merchantType
171 172 173
                                        )
                                    }
                                    else -> {
174 175 176 177
                                        getAliPayOrderId(
                                            thankPayId!!,
                                            if (useMoneyType == 2) 1 else 0
                                        )
178
                                    }
严久程 committed
179
                                }
180 181 182
                            } else {
                                //余额支付
                                payByCharge()
严久程 committed
183 184 185
                            }
                        }
                    }
186
                })
严久程 committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        if (bean.maxCoupon != null) {
            //最优的优惠券id为0时,把最优优惠券对象置为null,因为正常的可用的优惠券id一定不为0,这里接口为了前端页面展示,返回的maxCoupon对象不为null,
            //但maxCoupon的属性均为null或初始值
            if (TextUtils.equals(bean.maxCoupon.id, "0")) {
                bean.maxCoupon = null
            }
        }
        detailView!!.setData(bean, payBusinessType)

        viewList.add(detailView!!)

        view_pager.adapter = ViewPagerAdapter()
        view_pager.currentItem = 0
    }

202 203 204 205 206 207 208 209
    override fun show() {
        super.show()
        showProgressDialog()
    }

    /**
     * 获取下单信息
     */
严久程 committed
210
    @SuppressLint("CheckResult")
211 212 213
    private fun getOrderInfo() {
        if (!NetUtils.isConnected(activity)) {
            ToastHelper.show(activity, activity.getString(R.string.net_error))
214
            AliYunRichLogsHelper.getInstance().sendRichLog(AliYunLogConfig.PAY, "获取下单信息,无网络")
215 216 217 218 219 220 221 222 223
//            progress.visibility = View.GONE
            dismissProgressDialog()
            return
        }

        val bean = OrderRequestBean()
        bean.goodsId = goodsId
        bean.orderType = payBusinessType.toString()
        HttpUtils.getOrderInfo(bean)
严久程 committed
224 225 226 227 228 229 230
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                dismissProgressDialog()
                if (it.code == 200) {
                    if (it.data != null) {
                        progress_layout.visibility = View.GONE
231

严久程 committed
232
                        view_pager.visibility = View.VISIBLE
233
                        AliYunRichLogsHelper.getInstance().sendRichLog(AliYunLogConfig.PAY, "获取下单信息,成功")
严久程 committed
234
                        updateDataOnView(it.data)
235
                    } else {
236 237
                        AliYunRichLogsHelper.getInstance()
                            .sendRichLog(AliYunLogConfig.PAY, "获取下单信息data=null  msg:  ${it.msg}")
238
                        ToastHelper.show(activity, it.msg)
严久程 committed
239 240 241
//                            progress.visibility = View.GONE
                    }
                } else {
242

243 244
                    AliYunRichLogsHelper.getInstance()
                        .sendRichLog(AliYunLogConfig.PAY, "获取下单信息code!=200  msg:  ${it.msg}")
严久程 committed
245
                    ToastHelper.show(activity, it.msg)
246
//                        progress.visibility = View.GONE
严久程 committed
247 248 249 250 251
                }
            }, { e ->
                dismissProgressDialog()
                //                    progress.visibility = View.GONE
                ToastHelper.show(activity, e.message!!)
252 253
                AliYunRichLogsHelper.getInstance()
                    .sendRichLog(AliYunLogConfig.PAY, "获取下单信息error   e:${e.message}")
254

严久程 committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
            })
    }


    @SuppressLint("CheckResult")
    private fun getMyBalance() {
        HttpUtils.getMyBalance(BalanceParam())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                dismissProgressDialog()
                if (it.status == 200) {
                    if (it.data != null) {
                        progress_layout.visibility = View.GONE
                        view_pager.visibility = View.VISIBLE

                        val bean = OrderInfoBean()
                        bean.availableMoney = it.data.balance
                        bean.applyFee = payMoney!!
                        bean.isShowCoupon = false
                        initThanksPayInfoDetailView(bean)
                    } else {
                        ToastHelper.show(activity, it.msg)
278
                    }
严久程 committed
279 280 281 282 283 284 285
                } else {
                    ToastHelper.show(activity, it.msg)
                }
            }, {
                dismissProgressDialog()
                ToastHelper.show(activity, it.message!!)
            })
286 287 288 289
    }

    private fun updateDataOnView(bean: OrderInfoBean) {
        view_pager.setScanScroll(false)
严久程 committed
290
        detailView =
291 292 293 294 295 296 297 298 299
            PayInfoDetailView(
                activity,
                object : PayInfoDetailView.OnCouponDetailClickListener {
                    override fun selectCoupon(couponId: String) {
                        if (couponListBean != null) {
                            view_pager.currentItem = 1
                        } else {
                            getAllCoupon()
                        }
严久程 committed
300
                    }
301

302 303 304 305 306 307 308 309 310 311
                    override fun ensurePay(
                        payWay: Int,
                        payMoney: Float,
                        useMoneyType: Int,
                        code: String,
                        couponType: String
                    ) {
                        createOrder(payWay, payMoney, useMoneyType, code, couponType)
                    }
                })
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
        if (bean.maxCoupon != null) {
            //最优的优惠券id为0时,把最优优惠券对象置为null,因为正常的可用的优惠券id一定不为0,这里接口为了前端页面展示,返回的maxCoupon对象不为null,
            //但maxCoupon的属性均为null或初始值
            if (TextUtils.equals(bean.maxCoupon.id, "0")) {
                bean.maxCoupon = null
            }
        }
        detailView!!.setData(bean, payBusinessType)
        payCouponView = PayCouponView(activity)
        payCouponView!!.setListener(object : PayCouponView.OnCouponClickListener {
            override fun onBackClick(selectedCouponBean: CommonCouponBean?) {
                this@CommonPayDialog.selectedCouponBean = selectedCouponBean
                view_pager.currentItem = 0

                detailView!!.setCouponData(selectedCouponBean)
            }

            override fun onSelectCoupon(selectedCouponBean: CommonCouponBean?) {
                this@CommonPayDialog.selectedCouponBean = selectedCouponBean
                view_pager.currentItem = 0

                detailView!!.setCouponData(selectedCouponBean)
            }
        })

        viewList.add(detailView!!)
        viewList.add(payCouponView!!)

        view_pager.adapter = ViewPagerAdapter()
        view_pager.currentItem = 0
    }

    /**
    获取所有选择优惠券
     */
严久程 committed
347
    @SuppressLint("CheckResult")
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    private fun getAllCoupon() {
        if (!NetUtils.isConnected(activity)) {
            ToastHelper.show(activity, activity.getString(R.string.net_error))
            return
        }

        val bean = GetAllCouponRequestBean()
        val packageName = activity.packageName
        if (packageName.endsWith("zj.android")) {//专家版
            bean.applyEnds = "5"
        } else {
            bean.applyEnds = "3"
        }
        bean.goodsId = goodsId
        if (payBusinessType == TYPE_COURSE) {
            bean.services = "3"
        }

        showProgressDialog()
        HttpUtils.selectCoupon(bean)
严久程 committed
368 369 370 371 372 373 374 375
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                dismissProgressDialog()
                if (it.code == 200) {
                    if (it.data != null) {
                        couponListBean = it.data
                        updateSelectCouponView()
376 377 378
                    } else {
                        ToastHelper.show(activity, it.msg)
                    }
严久程 committed
379 380 381 382 383 384 385
                } else {
                    ToastHelper.show(activity, it.msg)
                }
            }, { e ->
                dismissProgressDialog()
                ToastHelper.show(activity, e.message!!)
            })
386 387 388 389 390 391 392 393

    }

    /**
     * 更新选择优惠券的页面
     */
    private fun updateSelectCouponView() {
        detailView!!.setAvailableCount(couponListBean!!.availableCount)
严久程 committed
394 395 396 397 398 399
        payCouponView!!.setData(
            couponListBean!!,
            payBusinessType == TYPE_COURSE,
            selectedCouponBean,
            goodsId!!
        )
400 401 402 403 404 405 406 407 408 409 410
        view_pager.currentItem = 1
    }


    /**
     * 创建订单
     * @param payWay  支付方式  1024.微信支付 1025.支付宝支付
     * @param payMoney  还需支付金额
     * @param useMoneyType   支付类型  1余额,2三方支付,3混合
     *
     */
严久程 committed
411
    @SuppressLint("CheckResult")
严久程 committed
412 413 414 415 416 417 418
    private fun createOrder(
        payWay: Int,
        payMoney: Float,
        useMoneyType: Int,
        code: String,
        couponType: String
    ) {
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        if (!NetUtils.isConnected(activity)) {
            ToastHelper.show(activity, activity.getString(R.string.net_error))
            return
        }

        this.payWay = payWay
        val bean = CreateOrderRequestBean()
        bean.goodsId = goodsId
        if (TextUtils.isEmpty(code)) {
            bean.code = null
        } else {
            bean.code = code
        }
        if (TextUtils.isEmpty(couponType)) {
            bean.couponType = null
        } else {
            bean.couponType = couponType
        }
        bean.orderType = payBusinessType.toString()
        bean.payType = useMoneyType.toString()
439
        AliYunRichLogsHelper.getInstance().sendRichLog(
440 441 442
            AliYunLogConfig.PAY,
            "创建订单,支付方式payWay:$payWay---payMoney:$payMoney----useMoneyType:$useMoneyType"
        )
443 444 445

        showProgressDialog()
        HttpUtils.createOrder(bean)
严久程 committed
446 447 448 449 450 451
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (it.code == 200) {
                    if (it.data != null) {
                        if (it.data.payStatus) {
452
                            AliYunRichLogsHelper.getInstance().sendRichLog(
453 454 455
                                AliYunLogConfig.PAY,
                                "创建订单,支付成功"
                            )
严久程 committed
456 457 458 459 460 461 462 463 464 465 466
                            dismissProgressDialog()
                            ToastHelper.show(activity, "支付成功")
                            if (listener != null) {
                                listener!!.onSuccesed()
                            }
                            dismiss()
                        } else {
                            when (payWay) {
                                PAY_WECHAT -> {
                                    getWeiXinPayOrderId(
                                        it.data.payId,
467 468
                                        if (useMoneyType == 2) 1 else 0,
                                        it.data.merchantType
严久程 committed
469
                                    )
470
                                }
严久程 committed
471 472
                                else -> {
                                    getAliPayOrderId(it.data.payId, if (useMoneyType == 2) 1 else 0)
473 474 475 476 477 478 479
                                }
                            }
                        }
                    } else {
                        dismissProgressDialog()
                        ToastHelper.show(activity, it.msg)
                    }
严久程 committed
480
                } else {
481
                    dismissProgressDialog()
严久程 committed
482 483 484 485 486 487
                    ToastHelper.show(activity, it.msg)
                }
            }, { e ->
                dismissProgressDialog()
                ToastHelper.show(activity, e.message!!)
            })
488 489 490 491 492 493
    }

    /**
     * 支付宝支付
     *
     */
严久程 committed
494
    @SuppressLint("CheckResult")
495 496
    private fun getAliPayOrderId(payId: String, isThreePay: Int) {
        if (!NetUtils.isConnected(activity)) {
497
            AliYunRichLogsHelper.getInstance().sendRichLog(
498 499 500
                AliYunLogConfig.PAY,
                "支付宝支付:无网络"
            )
501 502 503 504 505 506 507 508
            activity.runOnUiThread {
                ToastHelper.show(activity, activity.getString(R.string.net_error))
            }
            return
        }

        var bean = AliPayParam(payId, isThreePay)
        HttpUtils.getAliPayOrderId(bean)
严久程 committed
509 510 511
            .subscribeOn(Schedulers.io())
            .subscribe({
                if (it.data != null) {
512
                    AliYunRichLogsHelper.getInstance().sendRichLog(
513 514 515
                        AliYunLogConfig.PAY,
                        "支付宝支付:无网络"
                    )
严久程 committed
516 517
                    aliPay(it.data.aliSign)
                } else {
518
                    AliYunRichLogsHelper.getInstance().sendRichLog(
519 520 521
                        AliYunLogConfig.PAY,
                        "支付宝支付:data = null msg: ${it.msg} "
                    )
522
                    activity.runOnUiThread {
严久程 committed
523
                        ToastHelper.show(activity, it.msg)
524 525
                    }
                    dismissProgressDialog()
严久程 committed
526 527
                }
            }, {
528
                AliYunRichLogsHelper.getInstance().sendRichLog(
529 530 531
                    AliYunLogConfig.PAY,
                    "支付宝支付:error  message: ${it.message} localizedMessage: ${it.localizedMessage}"
                )
严久程 committed
532 533 534 535 536
                activity.runOnUiThread {
                    ToastHelper.show(activity, it.localizedMessage)
                }
                dismissProgressDialog()
            })
537 538 539 540 541 542 543
    }

    private fun aliPay(aliSign: String) {
        val alipay = PayTask(activity)
        val result = alipay.payV2(aliSign, true)
        val payResult = PayResult(result)
        if ("9000" == payResult.resultStatus) {
544
            AliYunRichLogsHelper.getInstance().sendRichLog(
545 546 547
                AliYunLogConfig.PAY,
                "支付宝支付:支付成功"
            )
548 549 550 551 552 553 554
            activity.runOnUiThread {
                ToastHelper.show(activity, "支付成功")
                listener!!.onSuccesed()
                dismiss()
            }
            dismissProgressDialog()
        } else {
555
            AliYunRichLogsHelper.getInstance().sendRichLog(
556 557 558
                AliYunLogConfig.PAY,
                "支付宝支付:resultStatus != 9000  memo${payResult.memo} "
            )
559 560 561 562 563 564 565 566 567 568
            dismissProgressDialog()
            activity.runOnUiThread {
                ToastHelper.show(activity, payResult.memo)
            }
        }
    }

    /**
     * 微信支付
     */
严久程 committed
569
    @SuppressLint("CheckResult")
570
    private fun getWeiXinPayOrderId(payId: String, isThreePay: Int, merchantType: String) {
571
        if (!NetUtils.isConnected(activity)) {
572
            AliYunRichLogsHelper.getInstance().sendRichLog(
573 574 575
                AliYunLogConfig.PAY,
                "微信支付,无网络"
            )
576 577 578 579 580 581
            activity.runOnUiThread {
                ToastHelper.show(activity, activity.getString(R.string.net_error))
            }
            return
        }

582
        var bean = WxPayParam(payId, isThreePay, merchantType)
583
        HttpUtils.wxPay(bean)
严久程 committed
584 585 586 587 588
            .subscribeOn(Schedulers.io())
            .subscribe({
                if (it.data != null) {
                    weixinPay(it.data.option)
                } else {
589
                    AliYunRichLogsHelper.getInstance().sendRichLog(
590 591 592
                        AliYunLogConfig.PAY,
                        "微信支付data=null   ${it.msg}"
                    )
593
                    activity.runOnUiThread {
严久程 committed
594
                        ToastHelper.show(activity, it.msg)
595 596
                    }
                    dismissProgressDialog()
严久程 committed
597 598
                }
            }, {
599
                AliYunRichLogsHelper.getInstance().sendRichLog(
600 601 602
                    AliYunLogConfig.PAY,
                    "微信支付,异常:message:${it.message}----localizedMessage:${it.localizedMessage}"
                )
严久程 committed
603 604 605 606 607
                activity.runOnUiThread {
                    ToastHelper.show(activity, it.localizedMessage)
                }
                dismissProgressDialog()
            })
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    }

    private fun weixinPay(option: CommonWXPayBean.WXOption) {
        val api = WXAPIFactory.createWXAPI(context, null)
        if (api.wxAppSupportAPI < com.tencent.mm.opensdk.constants.Build.PAY_SUPPORTED_SDK_INT) {
            return
        }
        api.registerApp(option.appid)
        val request = PayReq()
        request.appId = option.appid
        request.partnerId = option.partnerid
        request.prepayId = option.prepayid
        request.packageValue = "Sign=WXPay"
        request.nonceStr = option.noncestr
        request.timeStamp = option.timestamp
        request.sign = option.sign
        api.sendReq(request)
    }

严久程 committed
627 628 629 630 631 632 633 634 635
    @SuppressLint("CheckResult")
    private fun payByCharge() {
        var bean = ChargePayParam()
        bean.payId = thankPayId
        HttpUtils.payByCharge(bean)
            .subscribeOn(Schedulers.io())
            .subscribe({
                dismissProgressDialog()
                if (it.data != null && it.code == 0) {
636
                    AliYunRichLogsHelper.getInstance().sendRichLog(
637 638 639
                        AliYunLogConfig.PAY,
                        "余额支付:成功"
                    )
严久程 committed
640 641 642 643 644
                    activity.runOnUiThread {
                        listener!!.onSuccesed()
                        dismiss()
                    }
                } else {
645
                    AliYunRichLogsHelper.getInstance().sendRichLog(
646 647 648
                        AliYunLogConfig.PAY,
                        "余额支付:失败   msg:${it.msg}"
                    )
严久程 committed
649 650 651 652 653
                    activity.runOnUiThread {
                        ToastHelper.show(activity, it.msg)
                    }
                }
            }, {
654
                AliYunRichLogsHelper.getInstance().sendRichLog(
655 656 657
                    AliYunLogConfig.PAY,
                    "余额支付:ERROR   message:${it.message}   localizedMessage: ${it.localizedMessage}"
                )
严久程 committed
658 659 660 661 662 663 664 665
                activity.runOnUiThread {
                    ToastHelper.show(activity, it.localizedMessage)
                }
                dismissProgressDialog()
            })
    }


666 667 668
    fun onEvent(event: WeiXinPayStatusEvent) {
        dismissProgressDialog()
        if (event.success) {
669
            AliYunRichLogsHelper.getInstance().sendRichLog(
670 671 672
                AliYunLogConfig.PAY,
                "微信支付成功"
            )
673 674 675 676
            ToastHelper.show(activity, "支付成功")
            listener!!.onSuccesed()
            dismiss()
        } else {
677
            AliYunRichLogsHelper.getInstance().sendRichLog(
678 679 680
                AliYunLogConfig.PAY,
                "微信支付:失败 msg:${event.msg}"
            )
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
            if (!TextUtils.isEmpty(event.msg)) {
                ToastHelper.show(activity, event.msg!!)
            }
            listener!!.onFailed()
        }
    }


    /**
     * 显示加载中弹窗
     */
    private fun showProgressDialog() {
        if (dialog != null && dialog!!.isShowing) {
            return
        }
        if (dialog == null) {
            dialog = AlertDialog.Builder(activity)
严久程 committed
698
                .create()
699 700
        }
        if (dialogContentView == null) {
严久程 committed
701 702
            dialogContentView =
                LayoutInflater.from(activity).inflate(R.layout.pay_loading_dialog, null)
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        }
        dialog?.show()
        dialog?.window?.setContentView(dialogContentView)
        dialog?.window?.setGravity(Gravity.CENTER)
        val p = dialog?.window!!.attributes  //获取对话框当前的参数值
        var point = Point()
        activity.windowManager.defaultDisplay.getSize(point)// 获取屏幕宽、高用
        p.height = (point.y * 0.2).toInt()
        p.width = (point.x * 0.35).toInt()
        dialog?.window!!.attributes = p     //设置生效
    }

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

    internal inner class ViewPagerAdapter : PagerAdapter() {

        override fun getCount(): Int {
            return viewList.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = viewList[position]
            container.addView(view)
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View)
        }
    }

    /**
     * =============================
     * =============================
     * =====      [Build]     ======
     * =============================
     * =============================
     */
    class Build(internal var mActivity: Activity) {
        internal var goodsId: String? = null
严久程 committed
756 757
        internal var payId: String? = null
        internal var payMoney: Float? = null
758 759 760 761 762
        private lateinit var uid: String
        private lateinit var token: String
        private lateinit var ffrom: String
        internal lateinit var listener: OnPayResultListener
        private var isTestEnvironment = true
763

764 765 766 767 768 769 770 771 772 773
        /**
         * 支付的业务类型[CommonPayDialog.payBusinessType]
         */
        internal var payType = 0

        /**
         * 课程id(如果是其他业务线支付时,请将[payBusinessType]设置为对应的[CommonPayDialog.payBusinessType]类型)
         */
        fun setCourseId(courseId: String): Build {
            this.goodsId = courseId
严久程 committed
774
            payType = TYPE_COURSE
775 776 777
            return this@Build
        }

严久程 committed
778 779 780 781 782 783 784 785 786 787 788 789
        fun setPayId(payId: String): Build {
            this.payId = payId
            payType = TYPE_TRENDS_THANKS
            return this@Build
        }

        fun setPayMoney(money: Float): Build {
            this.payMoney = money
            return this@Build
        }


790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 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 857 858
        /**
         * 商品id(调用此方法请务必再调用[setBusinessType]方法)
         */
        fun setGoodsId(goodsId: String): Build {
            this.goodsId = goodsId
            return this@Build
        }

        /**
         * 业务类型
         */
        fun setBusinessType(payType: Int): Build {
            this.payType = payType
            return this@Build
        }

        /**
         * uid
         */
        fun setUid(uid: String): Build {
            this.uid = uid
            return this@Build
        }

        /**
         * token
         */
        fun setToken(token: String): Build {
            this.token = token
            return this@Build
        }

        /**
         * ffrom
         */
        fun setFfrom(ffrom: String): Build {
            this.ffrom = ffrom
            return this@Build
        }


        /**
         * listener 回调
         */
        fun setListener(listener: OnPayResultListener): Build {
            this.listener = listener
            return this@Build
        }

        /**
         * [setIsTestEnvironment] true 测试环境   false 线上环境
         *  默认测试环境
         * release包无效
         */
        fun setIsTestEnvironment(isTestEnvironment: Boolean): Build {
            this.isTestEnvironment = isTestEnvironment
            return this@Build
        }

        fun build(): CommonPayDialog {
            initHttpHeadConfig()
            return CommonPayDialog(this)
        }

        /**
         * 初始化网络的的参数
         */
        private fun initHttpHeadConfig() {
            RxDeviceTool.initContext(mActivity)
严久程 committed
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
//            HttpConfig.version = RxDeviceTool.getAppVersionName(mActivity)
//            HttpConfig.osBuild = RxDeviceTool.getBuildMANUFACTURER() + "," + RxDeviceTool.getBuildBrandModel() + "," + RxDeviceTool.getOsBuileVersion() + "," + RxDeviceTool.getAppVersionName(mActivity)
//            HttpConfig.uid = uid
//            HttpConfig.accessToken = token
//            HttpConfig.ffrom = ffrom
//            HttpConfig.isTestEnvironment = isTestEnvironment
//            val packageName = mActivity.packageName
//            if (packageName.endsWith("zj.android")) {//专家版
//                HttpConfig.isFromApp = "2"
//            } else {
//                HttpConfig.isFromApp = "1"
//            }
//            if (BuildConfig.DEBUG) {
//                if (TextUtils.isEmpty(HttpConfig.ffrom)) {
//                    ToastHelper.show(mActivity, "请先调用--setFfrom()--方法")
//                }
//                if (TextUtils.isEmpty(HttpConfig.uid)) {
//                    ToastHelper.show(mActivity, "请先调用--setUid()--方法")
//                }
//                if (TextUtils.isEmpty(HttpConfig.accessToken)) {
//                    ToastHelper.show(mActivity, "请先调用--setToken()--方法")
//                }
//            }
//            PayRetrofitUtils.setConfig(HttpConfig)
883 884 885 886 887 888 889 890 891 892
        }
    }


    interface OnPayResultListener {
        fun onSuccesed()

        fun onFailed()
    }
}