PayViewModel.kt 10.9 KB
Newer Older
jiangjiantao's avatar
jiangjiantao committed
1 2 3 4 5 6
package com.miya.fastcashier.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
7
import com.blankj.utilcode.util.GsonUtils
jiangjiantao's avatar
jiangjiantao committed
8
import com.elvishew.xlog.XLog
9
import com.fastcashier.lib_common.function.account.AccountService
10 11
import com.fastcashier.lib_common.function.pay.PayRepository
import com.fastcashier.lib_common.function.print.PrintService
赵鹏翔's avatar
赵鹏翔 committed
12
import com.miya.fastcashier.dao.ViewPayOrderData
赵鹏翔's avatar
赵鹏翔 committed
13
import com.sdy.miya.moblie.component.pay.core.utils.PayLogFileUtils
jiangjiantao's avatar
jiangjiantao committed
14
import com.sdy.miya.moblie.component.pay.platform.bean.PayServiceResponse
赵鹏翔's avatar
赵鹏翔 committed
15 16 17 18
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
jiangjiantao's avatar
jiangjiantao committed
19 20 21 22 23 24


class PayViewModel : ViewModel() {

    lateinit var payResultJob: Job

jiangjiantao's avatar
jiangjiantao committed
25
    val refundLiveData: MutableLiveData<Result<PayServiceResponse>> = MutableLiveData()
jiangjiantao's avatar
jiangjiantao committed
26 27 28
    val payServiceResponseLiveData: MutableLiveData<Result<PayServiceResponse>> = MutableLiveData()
    val payResultLiveData: MutableLiveData<Result<PayServiceResponse>> = MutableLiveData()

29 30 31
    /**
     * 退款全额
     */
jiangjiantao's avatar
jiangjiantao committed
32
    fun refund(payServiceResponse: PayServiceResponse) {
33
        XLog.d("开始退款:${GsonUtils.toJson(payServiceResponse)}")
jiangjiantao's avatar
jiangjiantao committed
34 35 36 37 38 39
        viewModelScope.launch(Dispatchers.IO) {
            //退款
            var refundPayServiceResponse: PayServiceResponse? = null
            try {
                val refundParams = HashMap<String, String>()
                refundParams["oriOrderNo"] = payServiceResponse.shopTradeNo;
40 41 42 43
                val refundOrderNo =
                    AccountService.getAccountInfo()?.shopInfo?.saasid + System.currentTimeMillis()
                refundParams["refundOrderNo"] = refundOrderNo
                refundParams["refundPrice"] = payServiceResponse.tradPrice
jiangjiantao's avatar
jiangjiantao committed
44
                refundPayServiceResponse = PayRepository.refundByOrderNo(refundParams)
45
                XLog.d("退款成功:${GsonUtils.toJson(refundPayServiceResponse)}")
gaodapeng's avatar
gaodapeng committed
46
                //保存到数据库
gaodapeng's avatar
gaodapeng committed
47
                ViewPayOrderData.insert(refundPayServiceResponse)
gaodapeng's avatar
gaodapeng committed
48 49
                refundLiveData.postValue(Result.success(refundPayServiceResponse))

jiangjiantao's avatar
jiangjiantao committed
50
            } catch (e: Exception) {
51
                e.printStackTrace()
jiangjiantao's avatar
jiangjiantao committed
52
                XLog.d("退款异常:${e.message}")
jiangjiantao's avatar
jiangjiantao committed
53 54
                refundLiveData.postValue(Result.failure(e))
            }
jiangjiantao's avatar
jiangjiantao committed
55

jiangjiantao's avatar
jiangjiantao committed
56 57
            //退款打印
            try {
jiangjiantao's avatar
jiangjiantao committed
58
                XLog.d("开始退款打印")
jiangjiantao's avatar
jiangjiantao committed
59
                AccountService.getAccountInfo()
60 61 62 63 64 65 66 67 68 69
                    ?.let {
                        refundPayServiceResponse?.let { it1 ->
                            PrintService.refundPrint(
                                it,
                                it1
                            )
                        }
                    }
            } catch (e: Exception) {
                e.printStackTrace()
jiangjiantao's avatar
jiangjiantao committed
70
                refundLiveData.postValue(Result.failure(e))
赵鹏翔's avatar
赵鹏翔 committed
71
                XLog.d("退款打印异常${e.message}")
jiangjiantao's avatar
jiangjiantao committed
72
            }
jiangjiantao's avatar
jiangjiantao committed
73 74 75 76

        }
    }

77 78 79 80
    /**
     * 退款固定数额
     */
    fun refund(payServiceResponse: PayServiceResponse, refundPrice: String) {
赵鹏翔's avatar
赵鹏翔 committed
81 82
        XLog.d("退款:参数${GsonUtils.toJson(payServiceResponse)}")
        PayLogFileUtils.writeLog("退款:参数${GsonUtils.toJson(payServiceResponse)}")
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        viewModelScope.launch(Dispatchers.IO) {
            //退款
            var refundPayServiceResponse: PayServiceResponse? = null
            try {
                val refundParams = HashMap<String, String>()
                refundParams["oriOrderNo"] = payServiceResponse.shopTradeNo;
                val refundOrderNo =
                    AccountService.getAccountInfo()?.shopInfo?.saasid + System.currentTimeMillis()
                refundParams["refundOrderNo"] = refundOrderNo
                refundParams["refundPrice"] = refundPrice
                refundPayServiceResponse = PayRepository.refundByOrderNo(refundParams)
                //保存到数据库
                ViewPayOrderData.insert(refundPayServiceResponse)
                refundLiveData.postValue(Result.success(refundPayServiceResponse))

赵鹏翔's avatar
赵鹏翔 committed
98 99
                XLog.d("退款成功:${GsonUtils.toJson(refundPayServiceResponse)}")
                PayLogFileUtils.writeLog("退款成功:${GsonUtils.toJson(refundPayServiceResponse)}")
100 101 102
            } catch (e: Exception) {
                e.printStackTrace()
                refundLiveData.postValue(Result.failure(e))
赵鹏翔's avatar
赵鹏翔 committed
103
                XLog.d("退款异常:${e.message}")
104 105 106 107
            }

            //退款打印
            try {
赵鹏翔's avatar
赵鹏翔 committed
108 109
                XLog.d("退款打印")
                PayLogFileUtils.writeLog("退款打印")
110 111 112 113 114 115 116 117 118 119 120 121
                AccountService.getAccountInfo()
                    ?.let {
                        refundPayServiceResponse?.let { it1 ->
                            PrintService.refundPrint(
                                it,
                                it1
                            )
                        }
                    }
            } catch (e: Exception) {
                e.printStackTrace()
                refundLiveData.postValue(Result.failure(e))
赵鹏翔's avatar
赵鹏翔 committed
122 123 124

                XLog.d("退款打印异常${e.message}")
                PayLogFileUtils.writeLog("退款打印异常${e.message}")
125 126 127 128 129
            }

        }
    }

jiangjiantao's avatar
jiangjiantao committed
130 131

    /**
132
     * 生成付款码支付
jiangjiantao's avatar
jiangjiantao committed
133 134
     * payType 只支持微信或者支付宝
     * 2是微信 1是支付宝
jiangjiantao's avatar
jiangjiantao committed
135
     * price 传进来要是分,整数
jiangjiantao's avatar
jiangjiantao committed
136
     */
jiangjiantao's avatar
jiangjiantao committed
137
    fun generatePayCode(price: String, payType: String) {
赵鹏翔's avatar
赵鹏翔 committed
138
        XLog.d("主扫码:开始生成")
jiangjiantao's avatar
jiangjiantao committed
139
        if (TextUtils.isEmpty(price)) {
gaodapeng's avatar
gaodapeng committed
140
            payServiceResponseLiveData.value = Result.failure(RuntimeException("请输入金额!"))
jiangjiantao's avatar
jiangjiantao committed
141 142 143
            return
        }

jiangjiantao's avatar
jiangjiantao committed
144 145 146 147
        val priceFen: String
        try {
            priceFen = price.toDouble().times(100).toInt().toString()
        } catch (e: java.lang.Exception) {
148
            e.printStackTrace()
gaodapeng's avatar
gaodapeng committed
149
            payServiceResponseLiveData.value = Result.failure(RuntimeException("金额有误!"))
jiangjiantao's avatar
jiangjiantao committed
150 151 152 153
            return
        }

        val sassid = AccountService.getAccountInfo()?.shopInfo?.saasid
154
        val orderNo = sassid + System.currentTimeMillis()
jiangjiantao's avatar
jiangjiantao committed
155

赵鹏翔's avatar
赵鹏翔 committed
156 157
        XLog.d("主扫码:订单号${orderNo},类型${payType}")
        PayLogFileUtils.writeLog("主扫:主扫码订单号${orderNo},类型${payType}")
jiangjiantao's avatar
jiangjiantao committed
158
        viewModelScope.launch(Dispatchers.IO) {
jiangjiantao's avatar
jiangjiantao committed
159
            try {
jiangjiantao's avatar
jiangjiantao committed
160
                val payServiceResponse = PayRepository.generatePayCode(orderNo, priceFen, payType)
jiangjiantao's avatar
jiangjiantao committed
161
                payServiceResponseLiveData.postValue(Result.success(payServiceResponse))
162
                XLog.d("生成付款码成功${GsonUtils.toJson(payServiceResponse)}")
赵鹏翔's avatar
赵鹏翔 committed
163
                PayLogFileUtils.writeLog("主扫:生成付款码成功${GsonUtils.toJson(payServiceResponse)}")
jiangjiantao's avatar
jiangjiantao committed
164
            } catch (e: Exception) {
165
                e.printStackTrace()
jiangjiantao's avatar
jiangjiantao committed
166
                payServiceResponseLiveData.postValue(Result.failure(e))
赵鹏翔's avatar
赵鹏翔 committed
167
                XLog.d("生成付款码异常${e.message}")
jiangjiantao's avatar
jiangjiantao committed
168 169 170 171 172 173 174 175
            }
        }
    }


    /**
     * 订单查询
     */
jiangjiantao's avatar
jiangjiantao committed
176
    fun payResultQuery(payServiceResponse: PayServiceResponse) {
赵鹏翔's avatar
赵鹏翔 committed
177
        PayLogFileUtils.writeLog("支付查询订单号:${payServiceResponse.shopTradeNo}")
jiangjiantao's avatar
jiangjiantao committed
178 179 180
        payResultJob = viewModelScope.launch(Dispatchers.IO) {
            while (isActive) {
                try {
jiangjiantao's avatar
jiangjiantao committed
181
                    XLog.d("支付查询订单号:${payServiceResponse.shopTradeNo}")
182 183
                    val payServiceResponseResult =
                        PayRepository.payResultQuery(payServiceResponse.shopTradeNo)
gaodapeng's avatar
gaodapeng committed
184 185
                    //加入数据库
                    ViewPayOrderData.insert(payServiceResponseResult)
jiangjiantao's avatar
jiangjiantao committed
186
                    payResultLiveData.postValue(Result.success(payServiceResponseResult))
赵鹏翔's avatar
赵鹏翔 committed
187 188 189

                    XLog.d("支付查询结果:${GsonUtils.toJson(payServiceResponseResult)}")
                    PayLogFileUtils.writeLog("支付查询结果:${GsonUtils.toJson(payServiceResponseResult)}")
jiangjiantao's avatar
jiangjiantao committed
190 191
                    break
                } catch (e: Exception) {
jiangjiantao's avatar
jiangjiantao committed
192
                    XLog.d("支付查询异常:${e.message}")
jiangjiantao's avatar
jiangjiantao committed
193 194 195
                    e.printStackTrace()
                    payResultLiveData.postValue(Result.failure(e))
                    Thread.sleep(5000)
jiangjiantao's avatar
jiangjiantao committed
196
                }
jiangjiantao's avatar
jiangjiantao committed
197
            }
jiangjiantao's avatar
jiangjiantao committed
198 199 200 201
        }
    }


jiangjiantao's avatar
jiangjiantao committed
202 203 204 205
    /**
     * 订单查询,退款查询
     */
    fun payResultQuery(orderNo: String) {
赵鹏翔's avatar
赵鹏翔 committed
206
        PayLogFileUtils.writeLog("退款查询:订单号${orderNo}")
207
        viewModelScope.launch(Dispatchers.IO) {
jiangjiantao's avatar
jiangjiantao committed
208
            try {
jiangjiantao's avatar
jiangjiantao committed
209
                XLog.d("开始退款支付查询:${orderNo}")
jiangjiantao's avatar
jiangjiantao committed
210 211
                val payServiceResponseResult = PayRepository.payResultQuery(orderNo)
                payResultLiveData.postValue(Result.success(payServiceResponseResult))
赵鹏翔's avatar
赵鹏翔 committed
212 213 214

                XLog.d("退款查询成功:${GsonUtils.toJson(payServiceResponseResult)}")
                PayLogFileUtils.writeLog("退款查询成功:${GsonUtils.toJson(payServiceResponseResult)}")
jiangjiantao's avatar
jiangjiantao committed
215
            } catch (e: Exception) {
216
                e.printStackTrace()
jiangjiantao's avatar
jiangjiantao committed
217
                XLog.d("退款支付查询异常:${e.message}")
jiangjiantao's avatar
jiangjiantao committed
218 219 220 221
                payResultLiveData.postValue(Result.failure(e))
            }
        }
    }
jiangjiantao's avatar
jiangjiantao committed
222

jiangjiantao's avatar
jiangjiantao committed
223 224

    /**
225
     * 被扫支付,金额是元,首先要转成分
jiangjiantao's avatar
jiangjiantao committed
226
     */
227
    fun pay(price: String, payCode: String) {
jiangjiantao's avatar
jiangjiantao committed
228
        if (TextUtils.isEmpty(price)) {
gaodapeng's avatar
gaodapeng committed
229
            payResultLiveData.value = Result.failure(RuntimeException("请输入金额!"))
jiangjiantao's avatar
jiangjiantao committed
230 231 232 233
            return
        }

        if (TextUtils.isEmpty(payCode)) {
gaodapeng's avatar
gaodapeng committed
234
            payResultLiveData.value = Result.failure(RuntimeException("付款码为空!"))
jiangjiantao's avatar
jiangjiantao committed
235 236 237 238 239 240 241
            return
        }

        val priceFen: String
        try {
            priceFen = price.toDouble().times(100).toInt().toString()
        } catch (e: java.lang.Exception) {
242
            e.printStackTrace()
gaodapeng's avatar
gaodapeng committed
243
            payResultLiveData.value = Result.failure(RuntimeException("金额有误!"))
jiangjiantao's avatar
jiangjiantao committed
244 245 246 247
            return
        }

        val sassid = AccountService.getAccountInfo()?.shopInfo?.saasid
248
        val orderNo = sassid + System.currentTimeMillis()
jiangjiantao's avatar
jiangjiantao committed
249

赵鹏翔's avatar
赵鹏翔 committed
250 251
        XLog.d("被扫: 参数 orderNo = $orderNo price = $priceFen payCode = $payCode")
        PayLogFileUtils.writeLog("被扫:开始,参数orderNo = $orderNo price = $priceFen payCode = $payCode")
jiangjiantao's avatar
jiangjiantao committed
252 253 254
        viewModelScope.launch(Dispatchers.IO) {
            try {
                //0代表不分类别付款
255 256 257
                val payServiceResponse = PayRepository.pay(orderNo, priceFen, payCode, "0")
                //保存到数据库
                ViewPayOrderData.insert(payServiceResponse)
jiangjiantao's avatar
jiangjiantao committed
258
                payResultLiveData.postValue(Result.success(payServiceResponse))
赵鹏翔's avatar
赵鹏翔 committed
259
                PayLogFileUtils.writeLog("被扫:支付成功${GsonUtils.toJson(payServiceResponse)}")
260
                XLog.d("被扫支付成功${GsonUtils.toJson(payServiceResponse)}")
jiangjiantao's avatar
jiangjiantao committed
261
            } catch (e: Exception) {
262
                e.printStackTrace()
jiangjiantao's avatar
jiangjiantao committed
263
                payResultLiveData.postValue(Result.failure(e))
264
                XLog.d("被扫支付异常${GsonUtils.toJson(e.message)}")
jiangjiantao's avatar
jiangjiantao committed
265 266 267 268 269
            }
        }

    }

jiangjiantao's avatar
jiangjiantao committed
270
}