From b8d8f9b855fdd453d06d78d2565f091db208ec8e Mon Sep 17 00:00:00 2001
From: lmw <125975490@qq.com>
Date: 星期二, 26 十一月 2024 19:30:59 +0800
Subject: [PATCH] 支付

---
 app/src/main/java/com/sinata/xqmuse/ui/home/BuyVoiceActivity.kt |   95 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 82 insertions(+), 13 deletions(-)

diff --git a/app/src/main/java/com/sinata/xqmuse/ui/home/BuyVoiceActivity.kt b/app/src/main/java/com/sinata/xqmuse/ui/home/BuyVoiceActivity.kt
index 926006b..199d538 100644
--- a/app/src/main/java/com/sinata/xqmuse/ui/home/BuyVoiceActivity.kt
+++ b/app/src/main/java/com/sinata/xqmuse/ui/home/BuyVoiceActivity.kt
@@ -1,10 +1,12 @@
 package com.sinata.xqmuse.ui.home
 
+import android.view.View
 import androidx.core.os.bundleOf
 import cn.sinata.xldutils.utils.SpanBuilder
 import cn.sinata.xldutils.utils.showAllowingStateLoss
 import com.sinata.xqmuse.R
 import com.sinata.xqmuse.dialog.BuyRuleDialog
+import com.sinata.xqmuse.dialog.TipDialog
 import com.sinata.xqmuse.network.HttpManager
 import com.sinata.xqmuse.network.entity.ConfirmOrder
 import com.sinata.xqmuse.network.request
@@ -14,6 +16,7 @@
 import com.sinata.xqmuse.utils.interfaces.StringCallback
 import com.sinata.xqmuse.utils.pay.PayListener
 import kotlinx.android.synthetic.main.activity_buy_course.*
+import org.jetbrains.anko.browse
 import org.jetbrains.anko.startActivity
 import org.jetbrains.anko.toast
 
@@ -23,6 +26,7 @@
     private val fromDetail by lazy { intent.getBooleanExtra("fromDetail",true) }
     private val id by lazy { intent.getStringExtra("id") }
     private var data:ConfirmOrder? = null
+    private var orderId = ""
 
     override fun initClick() {
         tv_buy_rule.clickDelay {
@@ -40,15 +44,18 @@
 
         tv_action.setOnClickListener {
             if (cb_rule.isChecked){
-                if (rb_ali.isChecked){
-                    pay(2)
-                }else if (rb_wx.isChecked){
-                    pay(1)
-                } else if (rb_balance.isChecked&&((data?.balance?:"0.0").toDouble()>(data?.generalPrice?:"0.0").toDouble())){
-                    payBalance()
-                }else{
-
-                }
+                if (cb_rule.isChecked){
+                    if (rb_balance.isChecked&&((data?.balance?:"0.0").toDouble()>=(data?.generalPrice?:"0.0").toDouble())){//余额勾选,并且可用余额大于课程价格,直接扣余额
+                        payBalance()
+                    }else if (rb_ali.isChecked){
+                        pay(2)
+                    }else if (rb_wx.isChecked){
+                        pay(1)
+                    }else{
+                        toast("请选择支付方式")
+                    }
+                }else
+                    toast("请阅读并同意《购买协议》")
             }else
                 toast("请阅读并同意《购买协议》")
         }
@@ -57,9 +64,13 @@
     private fun pay(way:Int) {
         tv_action.isEnabled = false
         showDialog()
-        HttpManager.placeOrder(1,way,2,null,null,data?.id?:"",null).request(this,success = {_,data->
-            onPaySuccess()
-
+        HttpManager.placeOrder(1,way,if (rb_balance.isChecked) 1 else 2,null,null,data?.id?:"",null,data?.orderId).request(this,success = {_,data->
+            tv_action.isEnabled = true
+            orderId = data?.orderId?:""
+            if (way == 1)
+                data?.jumpToWx(this)
+            else
+                browse(data?.qrcodeUrl?:"")
         }){_,_->
             tv_action.isEnabled = true
         }
@@ -76,7 +87,39 @@
     }
 
     override fun initView() {
-        getData()
+        val parcelableExtra = intent.getParcelableExtra<ConfirmOrder>("data")
+        if (parcelableExtra == null)
+            getData()
+        else{
+            data = parcelableExtra
+            data?.apply {
+                generalPrice = amount
+                if (!orderId.isNullOrEmpty()){
+                    titleBar.addRightButton("取消订单",onClickListener = View.OnClickListener {
+                        TipDialog.show(supportFragmentManager,"是否确认取消该订单?",object : TipDialog.OnClickCallback{
+                            override fun onOk() {
+                                HttpManager.cancel(orderId).request(this@BuyVoiceActivity){_,_->
+                                    toast("取消成功")
+                                    setResult(RESULT_OK)
+                                    finish()
+                                }
+                            }
+
+                            override fun onCancel() {
+                            }
+                        })
+                    })
+                }
+                iv_cover.setImageURI(coverUrl.split(",").firstOrNull())
+                tv_title.text = title
+                tv_price.text = SpanBuilder("¥$generalPrice").size(0,1,11).build()
+                tv_money.text = SpanBuilder("¥$generalPrice").size(0,1,11).build()
+                tv_total.text = generalPrice
+                val s = "余额抵扣 当前可用 ¥$balance"
+                rb_balance.text = SpanBuilder(s).size(4,s.length,11).color(this@BuyVoiceActivity,4,s.length,R.color.textColor99).build()
+                rb_balance.isChecked = !(balance.isNullOrEmpty()||balance.toDouble() == 0.0)
+            }
+        }
     }
 
     private fun getData() {
@@ -91,10 +134,36 @@
                 tv_total.text = generalPrice
                 val s = "余额抵扣 当前可用 ¥$balance"
                 rb_balance.text = SpanBuilder(s).size(4,s.length,11).color(this@BuyVoiceActivity,4,s.length,R.color.textColor99).build()
+                rb_balance.isChecked = !(balance.isNullOrEmpty()||balance.toDouble() == 0.0)
             }
         }
     }
 
+    private fun checkPayStatus(){
+        if (!orderId.isNullOrEmpty()){
+            HttpManager.queryPayment(orderId).request(this){_,data->
+                when(data){
+                    "pending"->{
+                        toast("未查询到支付结果,如已付款请忽略")
+                    }
+                    "succeeded"->{
+                        toast("支付成功")
+                        onPaySuccess()
+                    }
+                    "failed"->{
+                        toast("支付失败")
+                    }
+                }
+            }
+            orderId = ""
+        }
+    }
+
+    override fun onResume() {
+        super.onResume()
+        checkPayStatus()
+    }
+
     override fun onPaySuccess() {
         setResult(RESULT_OK)
         startActivity<PaySuccessActivity>("data" to data,"fromDetail" to fromDetail,"isAudio" to true)

--
Gitblit v1.7.1