Commit 2c2232f5 authored by 王雪伟's avatar 王雪伟

[提交人]:王雪伟

[提交简述] :加入googlePay工具类
[实现方案] :加入loading
parent 87632f7b
......@@ -8,6 +8,7 @@ import com.ishin.google.bean.GooglePayResult
import com.zxhl.cms.AppContext
import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.widget.LoadingDialog
import kotlinx.android.synthetic.main.activity_google_pay.*
......@@ -33,20 +34,30 @@ class GooglePayActivity : BaseActivity() {
override fun layoutID(): Int {
return R.layout.activity_google_pay
}
private var mLoading: LoadingDialog? = null
override fun init() {
mLoading = LoadingDialog.getLoadingDialog(
this,
getString(R.string.wait_ing2),
false,
false
)
id_btn_google_pay.setOnClickListener {
mLoading?.show()
googlePay?.googlePay("0.1", object : GooglePayCallBack {
override fun onPaySuccess(purchase: GooglePayResult) {
showToast("success" + purchase.orderId)
mLoading?.setResult(true, "pay success", 1000)
}
override fun onCancel() {
showToast("onCancel")
mLoading?.setResult(true, "pay cancel", 1000)
}
override fun onError(error: String) {
showToast("onError$error")
mLoading?.setResult(true, "pay error", 1000)
}
})
}
......
......@@ -10,6 +10,7 @@ import com.zxhl.cms.AppContext
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.other.AnyEntity
import com.zxhl.cms.net.model.uc.AliPayEntity
import com.zxhl.cms.net.model.uc.GooglePayEntity
import com.zxhl.cms.pay.alipay.AlipayServer
......@@ -24,7 +25,6 @@ import com.zxhl.cms.utils.EventUtils
class GooglePayHelper {
private val TAG = "GooglePayHelper"
private var mActivity: Activity;
private var mSku: String = "box_text_0001_1cent"
private var mPayCallBack: GooglePayCallBack? = null;
private var mConsume: String = "2"
private var productId = ""
......@@ -57,9 +57,8 @@ class GooglePayHelper {
googlePayResult.purchaseTime = purchase.purchaseTime.toString()
googlePayResult.purchaseState = purchase.purchaseState.toString()
googlePayResult.purchaseToken = purchase.purchaseToken
mPayCallBack?.onPaySuccess(googlePayResult)//支付成功
//同步后台 消耗掉
updateToService(purchase.purchaseToken)
updateToService(googlePayResult)
}
}
} else if (billingResult.responseCode == BillingResponseCode.ITEM_ALREADY_OWNED) {
......@@ -71,38 +70,38 @@ class GooglePayHelper {
mPayCallBack?.onCancel()
} else {
mPayCallBack?.onError("支付异常: code:" + billingResult.responseCode + " msg:" + billingResult.debugMessage)
when (billingResult.responseCode) {
BillingResponseCode.SERVICE_TIMEOUT -> {
//服务连接超时
}
BillingResponseCode.FEATURE_NOT_SUPPORTED -> {
}
BillingResponseCode.SERVICE_DISCONNECTED -> {
//服务未连接
}
BillingResponseCode.USER_CANCELED -> {
//取消
}
BillingResponseCode.SERVICE_UNAVAILABLE -> {
//服务不可用
}
BillingResponseCode.BILLING_UNAVAILABLE -> {
//购买不可用
}
BillingResponseCode.ITEM_UNAVAILABLE -> {
//商品不存在
}
BillingResponseCode.DEVELOPER_ERROR -> {
//提供给 API 的无效参数
}
BillingResponseCode.ERROR -> {
//错误
}
BillingResponseCode.ITEM_NOT_OWNED -> {
//不可购买
}
}
// when (billingResult.responseCode) {
// BillingResponseCode.SERVICE_TIMEOUT -> {
// //服务连接超时
// }
// BillingResponseCode.FEATURE_NOT_SUPPORTED -> {
// }
// BillingResponseCode.SERVICE_DISCONNECTED -> {
// //服务未连接
// }
// BillingResponseCode.USER_CANCELED -> {
// //取消
// }
// BillingResponseCode.SERVICE_UNAVAILABLE -> {
// //服务不可用
// }
// BillingResponseCode.BILLING_UNAVAILABLE -> {
// //购买不可用
// }
// BillingResponseCode.ITEM_UNAVAILABLE -> {
// //商品不存在
//
// }
// BillingResponseCode.DEVELOPER_ERROR -> {
// //提供给 API 的无效参数
// }
// BillingResponseCode.ERROR -> {
// //错误
// }
// BillingResponseCode.ITEM_NOT_OWNED -> {
// //不可购买
// }
// }
}
}
......@@ -146,11 +145,12 @@ class GooglePayHelper {
/**
* 购买商品
*/
private fun purchase(sku: String, orderId: String) {
mSku = sku
private fun purchase(mSku: String, orderId: String) {
productId = mSku
obfuscatedAccountid = orderId
if (billingClient.isReady) {
val skuList: MutableList<String> = ArrayList()
skuList.add(sku)
skuList.add(mSku)
val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(SkuType.INAPP)
billingClient.querySkuDetailsAsync(
......@@ -184,11 +184,12 @@ class GooglePayHelper {
}
} else {
Log.d(TAG, "purchase 查询连接失败 cede" + billingResult.responseCode)
mPayCallBack?.onError("GooglePay error code:"+billingResult.responseCode+" msg:"+billingResult.debugMessage)
mPayCallBack?.onError("GooglePay error code:" + billingResult.responseCode + " msg:" + billingResult.debugMessage)
}
}
}else{
} else {
Log.d(TAG, "purchase 购买 未连接")
mPayCallBack?.onError("GooglePay purchase 购买 未连接")
}
}
......@@ -197,7 +198,68 @@ class GooglePayHelper {
*
* @param purchaseToken 商品token
*/
private fun consume(purchaseToken: String) {
private fun consume(googlePayResult: GooglePayResult) {
if (billingClient.isReady) {
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(googlePayResult.purchaseToken)
.build()
billingClient.consumeAsync(
consumeParams
) { billingResult, s ->
if (billingResult.responseCode == BillingResponseCode.OK) {
// Handle the success of the consume operation.
Log.d(TAG, "consume 消耗成功${googlePayResult.purchaseToken}")
mPayCallBack?.onPaySuccess(googlePayResult)//支付成功
productId = ""
obfuscatedAccountid = ""
} else {
Log.d(TAG, "consume 消耗失败 code:" + billingResult.responseCode)
mPayCallBack?.onError("consume 消耗失败 code:" + billingResult.responseCode + " msg:" + billingResult.debugMessage)//支付成功
}
}
} else {
billingClient.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingResponseCode.OK) {
//重连后 消耗掉
val consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(googlePayResult.purchaseToken)
.build()
billingClient.consumeAsync(
consumeParams
) { billingResult, s ->
if (billingResult.responseCode == BillingResponseCode.OK) {
// Handle the success of the consume operation.
Log.d(TAG, "consume 消耗成功${googlePayResult.purchaseToken}")
mPayCallBack?.onPaySuccess(googlePayResult)//支付成功
} else {
Log.d(TAG, "consume 消耗失败 code:" + billingResult.responseCode)
mPayCallBack?.onError("consume 消耗失败 code:" + billingResult.responseCode + " msg:" + billingResult.debugMessage)
}
}
} else {
// Log.d(TAG, "consume init 连接失败 code:" + billingResult.responseCode)
mPayCallBack?.onError("consume 消耗连接失败 code:" + billingResult.responseCode + " msg:" + billingResult.debugMessage)
}
}
override fun onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
mPayCallBack?.onError("consume onBillingServiceDisconnected" )
}
})
}
}
/**
* 补单消耗商品
*
* @param purchaseToken 商品token
*/
private fun consume2(purchaseToken: String) {
if (billingClient.isReady) {
val consumeParams =
ConsumeParams.newBuilder()
......@@ -262,7 +324,7 @@ class GooglePayHelper {
Log.d(TAG, "queryPurchases init " + mutableList[i].purchaseState)
if (mutableList[i].purchaseState == Purchase.PurchaseState.PURCHASED) {
//已购买,消耗即可
consume(mutableList[i].purchaseToken)
consume2(mutableList[i].purchaseToken)
}
}
}
......@@ -302,10 +364,11 @@ class GooglePayHelper {
.subscribe(object : BaseObserver<GooglePayEntity>() {
override fun onSuccess(result: GooglePayEntity?) {
if (result != null) {
Log.d(TAG,"请求支付")
Log.d(TAG, "请求支付")
purchase("box_text_0001_1cent", result.outTradeNo ?: "")
}else{
Log.d(TAG,"未获取到订单")
} else {
Log.d(TAG, "未获取到订单")
mPayCallBack?.onError("GooglePay 未获取到订单数据")
}
}
......@@ -316,25 +379,29 @@ class GooglePayHelper {
})
}
fun updateToService(purchaseToken: String) {
fun updateToService(purchase: GooglePayResult) {
//支付成功 向后端同步 网络问题再次重试
// ApiClient.userInfoAPi.googlePaySuccess(pkName, orderId, productId, developerPayload, obfuscatedAccountid, purchaseTime, purchaseState, purchaseToken).compose(RxSchedulers.observableIO2Main())
// .subscribe(object : BaseObserver<Any?>() {
// override fun onSuccess(result: Any?) {
// EventUtils.onEvent("memberpresenter_googlepaysuccess_onsuccess")
// AdJustUtils.onEvent("r7m4vr")
// mActivity?.finish()
//
//
// }
//
// override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
// EventUtils.onEvent("memberpresenter_googlepaysuccess_onfailure","${errorMsg}")
// AdJustUtils.onEvent("3ebyqo")
// }
// })
ApiClient.userInfoAPi.googlePaySuccess(
purchase.packageName,
purchase.orderId,
purchase.productId,
purchase.developerPayload,
purchase.obfuscatedAccountid,
purchase.purchaseTime,
purchase.purchaseState,
purchase.purchaseToken
).compose(RxSchedulers.observableIO2Main())
.subscribe(object : BaseObserver<AnyEntity?>() {
override fun onSuccess(result: AnyEntity?) {
EventUtils.onEvent("googlepaysuccess_onsuccess")
}
override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
EventUtils.onEvent("googlepaysuccess_onfailure", "${errorMsg}")
}
})
//同步成功 消耗掉
consume(purchaseToken)
consume(purchase)
}
......
......@@ -2,6 +2,7 @@ package com.zxhl.cms.net.api;
import com.zxhl.cms.ad.upload.model.Response;
import com.zxhl.cms.common.NetConfig;
import com.zxhl.cms.net.model.other.AnyEntity;
import com.zxhl.cms.net.model.uc.AliPayEntity;
import com.zxhl.cms.net.model.uc.GooglePayEntity;
import com.zxhl.cms.net.model.uc.PayMinEntity;
......@@ -90,10 +91,10 @@ public interface IUserInfoApi {
Observable<Response<GooglePayEntity>> googlePay(@Query("goodsId") String goodsId, @Query("payType") String payType, @Query("voucherId") String voucherId, @Query("appId") String appId, @Query("payMode") String payMode);
/**
* google支付成功后 通知后台更新会员状态
* google支付成功后 通知后台
*/
@POST(NetConfig.User.URL_GOOGLE_NOTIFY)
Observable<Response<AliPayEntity>> googlePaySuccess(
Observable<Response<AnyEntity>> googlePaySuccess(
@Query("packageName") String packageName,
@Query("orderId") String orderId,
@Query("productId") String productId,
......
package com.zxhl.cms.net.model.other;
/**
* @author (wangXuewei)
* @datetime 2022-03-15 10:25 GMT+8
* @detail :
*/
public class AnyEntity {
}
......@@ -8,6 +8,7 @@
<string name="auth_ing">授权中</string>
<string name="upload_ing">上传中,请稍后</string>
<string name="wait_ing">请稍后</string>
<string name="wait_ing2">Loading</string>
<string name="get_sms_code_success">验证码已发送</string>
<string name="nav_back_again_finish">再按一次退出程序</string>
<string name="album_not_available">请检查系统相册是否可用</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment