罗明文
2 天以前 7bed3f6090b14391d7dad4ca4bd632fb28d847c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
package com.dollearn.student.wxapi
 
import android.content.Intent
import android.os.Bundle
import cn.sinata.xldutils.activity.BaseActivity
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.pay.PayUtil
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import org.jetbrains.anko.toast
 
/**
 * 微信支付
 */
class WXPayEntryActivity : BaseActivity(), IWXAPIEventHandler {
 
    private var api: IWXAPI? = null
 
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        api = WXAPIFactory.createWXAPI(this, Const.WX_APP_ID)
        api!!.handleIntent(intent, this)
    }
 
    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        api!!.handleIntent(intent, this)
    }
 
    override fun onReq(req: BaseReq) {
 
    }
 
    override fun onResp(resp: BaseResp) {
        if (resp.type == ConstantsAPI.COMMAND_PAY_BY_WX) {
 
            if (resp.errCode == 0) {
                toast("支付成功")
                PayUtil.payListeners.lastOrNull()?.onPaySuccess()
            } else {
 
                if (resp.errCode == -2) {
                    toast("取消支付")
                    PayUtil.payListeners.lastOrNull()?.onPayCancel()
                } else if (resp.errCode == -1) {
                    toast("支付出现错误!")
                }
            }
        }
        finish()
    }
}