Commit f698e9cf authored by 王雪伟's avatar 王雪伟

[提交人]:王雪伟

[提交简述] :加入googlePay工具类
[实现方案] :生成订单ID 调起Google支付
parent 581ac570
...@@ -144,6 +144,12 @@ public class NetConfig { ...@@ -144,6 +144,12 @@ public class NetConfig {
public static final String USER_PHONE_CODE_LOGIN = "app/v3/auth/phoneCodeLogin";//验证码登录 public static final String USER_PHONE_CODE_LOGIN = "app/v3/auth/phoneCodeLogin";//验证码登录
public static final String URL_VERSION = "app/v1/version"; public static final String URL_VERSION = "app/v1/version";
public static final String URL_AUTH_GOOGLE_LOGIN = "app/v1/auth/googleLogin";//绑定谷歌登录 public static final String URL_AUTH_GOOGLE_LOGIN = "app/v1/auth/googleLogin";//绑定谷歌登录
public static final String URL_PAY_BASE = "http://pay.linking100.com/";
///unifiedpay/gateway/google/替换包名/notify/order
public static final String URL_GOOGLE_NOTIFY = URL_PAY_BASE +"unifiedpay/gateway/google/com.zhangxin.magicbox/notify/order";
} }
public static class Task { public static class Task {
......
...@@ -4,6 +4,7 @@ import android.util.Log ...@@ -4,6 +4,7 @@ import android.util.Log
import android.view.View import android.view.View
import com.android.billingclient.api.* import com.android.billingclient.api.*
import com.android.billingclient.api.BillingClient.SkuType import com.android.billingclient.api.BillingClient.SkuType
import com.ishin.google.bean.GooglePayResult
import com.zxhl.cms.AppContext import com.zxhl.cms.AppContext
import com.zxhl.cms.R import com.zxhl.cms.R
import com.zxhl.cms.common.base.BaseActivity import com.zxhl.cms.common.base.BaseActivity
...@@ -28,16 +29,35 @@ class GooglePayActivity : BaseActivity() { ...@@ -28,16 +29,35 @@ class GooglePayActivity : BaseActivity() {
} }
var googlePay: GooglePayHelper? = null
override fun layoutID(): Int { override fun layoutID(): Int {
return R.layout.activity_google_pay return R.layout.activity_google_pay
} }
override fun init() { override fun init() {
id_btn_google_pay.setOnClickListener { id_btn_google_pay.setOnClickListener {
googlePay?.googlePay("0.1", object : GooglePayCallBack {
override fun onPaySuccess(purchase: GooglePayResult) {
showToast("success" + purchase.orderId)
}
override fun onCancel() {
showToast("onCancel")
}
override fun onError(error: String) {
showToast("onError$error")
}
})
} }
// startConnection() // startConnection()
GooglePayHelper(this,"").init() googlePay= GooglePayHelper(this)
googlePay?.init()
}
override fun onDestroy() {
super.onDestroy()
googlePay?.release()
} }
// //
// //
......
package com.zxhl.cms.googlepay
import com.ishin.google.bean.GooglePayResult
/**
* @author (wangXuewei)
* @datetime 2022-03-14 16:49 GMT+8
* @detail :
*/
interface GooglePayCallBack {
fun onPaySuccess(purchase: GooglePayResult)
fun onCancel()
fun onError(error: String)
}
\ No newline at end of file
...@@ -5,11 +5,13 @@ import android.util.Log ...@@ -5,11 +5,13 @@ import android.util.Log
import com.android.billingclient.api.* import com.android.billingclient.api.*
import com.android.billingclient.api.BillingClient.BillingResponseCode import com.android.billingclient.api.BillingClient.BillingResponseCode
import com.android.billingclient.api.BillingClient.SkuType import com.android.billingclient.api.BillingClient.SkuType
import com.ishin.google.bean.GooglePayResult
import com.zxhl.cms.AppContext import com.zxhl.cms.AppContext
import com.zxhl.cms.net.ApiClient import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.uc.AliPayEntity import com.zxhl.cms.net.model.uc.AliPayEntity
import com.zxhl.cms.net.model.uc.GooglePayEntity
import com.zxhl.cms.pay.alipay.AlipayServer import com.zxhl.cms.pay.alipay.AlipayServer
import com.zxhl.cms.utils.EventUtils import com.zxhl.cms.utils.EventUtils
...@@ -22,11 +24,14 @@ import com.zxhl.cms.utils.EventUtils ...@@ -22,11 +24,14 @@ import com.zxhl.cms.utils.EventUtils
class GooglePayHelper { class GooglePayHelper {
private val TAG = "GooglePayHelper" private val TAG = "GooglePayHelper"
private var mActivity: Activity; private var mActivity: Activity;
private var mSku: String = "box0001" private var mSku: String = "box_text_0001_1cent"
private var mPayCallBack: GooglePayCallBack? = null;
private var mConsume: String = "2"
private var productId = ""
private var obfuscatedAccountid = ""
constructor(mActivity: Activity, mSku: String) { constructor(mActivity: Activity) {
this.mActivity = mActivity this.mActivity = mActivity
// this.mSku = mSku
} }
/** /**
...@@ -38,17 +43,34 @@ class GooglePayHelper { ...@@ -38,17 +43,34 @@ class GooglePayHelper {
Log.d(TAG, "购买监听 code ${billingResult.responseCode}") Log.d(TAG, "购买监听 code ${billingResult.responseCode}")
Log.d(TAG, "购买监听 size ${purchases?.size}") Log.d(TAG, "购买监听 size ${purchases?.size}")
if (billingResult.responseCode == BillingResponseCode.OK) { if (billingResult.responseCode == BillingResponseCode.OK) {
mConsume = "2"
//支付成功 同步后台 //支付成功 同步后台
if (!purchases.isNullOrEmpty()) { if (!purchases.isNullOrEmpty()) {
for (i in 0 until purchases.size) { for (i in 0 until purchases.size) {
updateToService() var purchase = purchases[i];
var googlePayResult = GooglePayResult()
googlePayResult.packageName = purchase.packageName
googlePayResult.orderId = purchase.orderId
googlePayResult.productId = productId
googlePayResult.obfuscatedAccountid = obfuscatedAccountid
googlePayResult.developerPayload = purchase.developerPayload
googlePayResult.purchaseTime = purchase.purchaseTime.toString()
googlePayResult.purchaseState = purchase.purchaseState.toString()
googlePayResult.purchaseToken = purchase.purchaseToken
mPayCallBack?.onPaySuccess(googlePayResult)//支付成功
//同步后台 消耗掉
updateToService(purchase.purchaseToken)
} }
} }
} else if (billingResult.responseCode == BillingResponseCode.ITEM_ALREADY_OWNED) { } else if (billingResult.responseCode == BillingResponseCode.ITEM_ALREADY_OWNED) {
//未消耗掉 //未消耗掉
//mConsume = "1" mConsume = "1"
queryPurchases() queryPurchases()
mPayCallBack?.onError("商品未消耗掉 查询补单")
} else if (billingResult.responseCode == BillingResponseCode.USER_CANCELED) {
mPayCallBack?.onCancel()
} else { } else {
mPayCallBack?.onError("支付异常: code:" + billingResult.responseCode + " msg:" + billingResult.debugMessage)
when (billingResult.responseCode) { when (billingResult.responseCode) {
BillingResponseCode.SERVICE_TIMEOUT -> { BillingResponseCode.SERVICE_TIMEOUT -> {
//服务连接超时 //服务连接超时
...@@ -89,11 +111,12 @@ class GooglePayHelper { ...@@ -89,11 +111,12 @@ class GooglePayHelper {
.enablePendingPurchases() .enablePendingPurchases()
.build() .build()
//检查之前的商品是否被消费
fun init() { fun init() {
//已经连接查询订单 //已经连接查询订单
if (billingClient.isReady) { if (billingClient.isReady) {
Log.d(TAG, "init 已连接") Log.d(TAG, "init 已连接")
getOrderId() queryPurchases()
} else { } else {
//开始连接 //开始连接
Log.d(TAG, "init 开始连接") Log.d(TAG, "init 开始连接")
...@@ -102,9 +125,10 @@ class GooglePayHelper { ...@@ -102,9 +125,10 @@ class GooglePayHelper {
if (billingResult.responseCode == BillingResponseCode.OK) { if (billingResult.responseCode == BillingResponseCode.OK) {
//BillingClient准备好了。你可以在这里查询购买情况。 //BillingClient准备好了。你可以在这里查询购买情况。
Log.d(TAG, "init 连接成功") Log.d(TAG, "init 连接成功")
getOrderId() queryPurchases()
} else { } else {
Log.d(TAG, "init 连接失败 code:" + billingResult.responseCode) Log.d(TAG, "init 连接失败 code:" + billingResult.responseCode)
mPayCallBack?.onError("初始化连接失败 code:" + billingResult.responseCode + " msg:" + billingResult.debugMessage)
} }
} }
...@@ -112,6 +136,7 @@ class GooglePayHelper { ...@@ -112,6 +136,7 @@ class GooglePayHelper {
// Try to restart the connection on the next request to // Try to restart the connection on the next request to
// Google Play by calling the startConnection() method. // Google Play by calling the startConnection() method.
Log.d(TAG, "init onBillingServiceDisconnected") Log.d(TAG, "init onBillingServiceDisconnected")
mPayCallBack?.onError("初始化连接失败 onBillingServiceDisconnected")
} }
}) })
} }
...@@ -121,10 +146,11 @@ class GooglePayHelper { ...@@ -121,10 +146,11 @@ class GooglePayHelper {
/** /**
* 购买商品 * 购买商品
*/ */
private fun purchase() { private fun purchase(sku: String, orderId: String) {
mSku = sku
if (billingClient.isReady) { if (billingClient.isReady) {
val skuList: MutableList<String> = ArrayList() val skuList: MutableList<String> = ArrayList()
skuList.add(mSku) skuList.add(sku)
val params = SkuDetailsParams.newBuilder() val params = SkuDetailsParams.newBuilder()
params.setSkusList(skuList).setType(SkuType.INAPP) params.setSkusList(skuList).setType(SkuType.INAPP)
billingClient.querySkuDetailsAsync( billingClient.querySkuDetailsAsync(
...@@ -140,7 +166,7 @@ class GooglePayHelper { ...@@ -140,7 +166,7 @@ class GooglePayHelper {
val flowParams = BillingFlowParams.newBuilder() val flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails) .setSkuDetails(skuDetails)
// .setObfuscatedProfileId() // .setObfuscatedProfileId()
// .setObfuscatedAccountId() .setObfuscatedAccountId(orderId)
.build() .build()
//吊起支付 //吊起支付
val responseCode = val responseCode =
...@@ -154,11 +180,15 @@ class GooglePayHelper { ...@@ -154,11 +180,15 @@ class GooglePayHelper {
} else { } else {
Log.d(TAG, "purchase 未查询到商品") Log.d(TAG, "purchase 未查询到商品")
mPayCallBack?.onError("GooglePay 商品未查询到")
} }
} else { } else {
Log.d(TAG, "purchase 查询连接失败 cede" + billingResult.responseCode) Log.d(TAG, "purchase 查询连接失败 cede" + billingResult.responseCode)
mPayCallBack?.onError("GooglePay error code:"+billingResult.responseCode+" msg:"+billingResult.debugMessage)
} }
} }
}else{
Log.d(TAG, "purchase 购买 未连接")
} }
} }
...@@ -230,21 +260,14 @@ class GooglePayHelper { ...@@ -230,21 +260,14 @@ class GooglePayHelper {
} else { } else {
for (i in 0 until mutableList.size) { for (i in 0 until mutableList.size) {
Log.d(TAG, "queryPurchases init " + mutableList[i].purchaseState) Log.d(TAG, "queryPurchases init " + mutableList[i].purchaseState)
if (mutableList[i].isAcknowledged) { if (mutableList[i].purchaseState == Purchase.PurchaseState.PURCHASED) {
//已确认/已验证,消耗即可 //已购买,消耗即可
consume(mutableList[i].purchaseToken) consume(mutableList[i].purchaseToken)
if (mutableList[i].purchaseState == Purchase.PurchaseState.PURCHASED) {
}
} else {
//后端同步
//支付成功后 google回调没有收到 再次检查 后端同步
} }
} }
} }
} else { } else {
Log.d(TAG, "queryPurchases null") Log.d(TAG, "queryPurchases error code" + billingResult.responseCode)
} }
} }
} else { } else {
...@@ -268,28 +291,50 @@ class GooglePayHelper { ...@@ -268,28 +291,50 @@ class GooglePayHelper {
} }
fun getOrderId() { /**
* google支付
*/
fun googlePay(goodsId: String, payCallBack: GooglePayCallBack) {
mPayCallBack = payCallBack
//向服务端获取订单ID //向服务端获取订单ID
ApiClient.userInfoAPi.aliPay("39", "4", "", "", "6") ApiClient.userInfoAPi.googlePay(goodsId, "4", "", "", "6")
.compose(RxSchedulers.observableIO2Main()) .compose(RxSchedulers.observableIO2Main())
.subscribe(object : BaseObserver<AliPayEntity>() { .subscribe(object : BaseObserver<GooglePayEntity>() {
override fun onSuccess(result: AliPayEntity?) { override fun onSuccess(result: GooglePayEntity?) {
if (result != null) {
Log.d(TAG,"请求支付")
purchase("box_text_0001_1cent", result.outTradeNo ?: "")
}else{
Log.d(TAG,"未获取到订单")
}
} }
override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) { override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
EventUtils.onEvent("member_page_pay_get_order_fail") // EventUtils.onEvent("GooglePay Fail:$errorMsg")
mPayCallBack?.onError("订单接口 Error:$errorMsg")
} }
}) })
purchase()
} }
fun updateToService() { fun updateToService(purchaseToken: String) {
//支付成功 向后端同步 网络问题再次重试 //支付成功 向后端同步 网络问题再次重试
// 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")
// }
// })
//同步成功 消耗掉 //同步成功 消耗掉
consume("") consume(purchaseToken)
} }
...@@ -297,6 +342,8 @@ class GooglePayHelper { ...@@ -297,6 +342,8 @@ class GooglePayHelper {
if (billingClient.isReady) { if (billingClient.isReady) {
billingClient.endConnection(); billingClient.endConnection();
} }
mPayCallBack = null
Log.d(TAG, "billingClient release")
} }
} }
\ No newline at end of file
package com.ishin.google.bean
class GooglePayResult {
var packageName: String = ""
var orderId: String = ""
var productId: String = ""
var developerPayload: String = ""
var obfuscatedAccountid: String = ""
var purchaseTime: String = ""
var purchaseState: String = ""
var purchaseToken: String = ""
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ package com.zxhl.cms.net.api; ...@@ -3,6 +3,7 @@ package com.zxhl.cms.net.api;
import com.zxhl.cms.ad.upload.model.Response; import com.zxhl.cms.ad.upload.model.Response;
import com.zxhl.cms.common.NetConfig; import com.zxhl.cms.common.NetConfig;
import com.zxhl.cms.net.model.uc.AliPayEntity; import com.zxhl.cms.net.model.uc.AliPayEntity;
import com.zxhl.cms.net.model.uc.GooglePayEntity;
import com.zxhl.cms.net.model.uc.PayMinEntity; import com.zxhl.cms.net.model.uc.PayMinEntity;
import com.zxhl.cms.net.model.uc.PayVerifyEntity; import com.zxhl.cms.net.model.uc.PayVerifyEntity;
import com.zxhl.cms.net.model.uc.UserInfo; import com.zxhl.cms.net.model.uc.UserInfo;
...@@ -80,6 +81,27 @@ public interface IUserInfoApi { ...@@ -80,6 +81,27 @@ public interface IUserInfoApi {
@POST(NetConfig.User.URL_PAY_ORDER) @POST(NetConfig.User.URL_PAY_ORDER)
Observable<Response<AliPayEntity>> aliPay(@Query("goodsId") String goodsId, @Query("payType") String payType, @Query("voucherId") String voucherId, @Query("appId") String appId, @Query("payMode") String payMode); Observable<Response<AliPayEntity>> aliPay(@Query("goodsId") String goodsId, @Query("payType") String payType, @Query("voucherId") String voucherId, @Query("appId") String appId, @Query("payMode") String payMode);
/**
* Google支付
*
* @return
*/
@POST(NetConfig.User.URL_PAY_ORDER)
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支付成功后 通知后台更新会员状态
*/
@POST(NetConfig.User.URL_GOOGLE_NOTIFY)
Observable<Response<AliPayEntity>> googlePaySuccess(
@Query("packageName") String packageName,
@Query("orderId") String orderId,
@Query("productId") String productId,
@Query("developerPayload") String developerPayload,
@Query("obfuscatedAccountid") String obfuscatedAccountid,
@Query("purchaseTime") String purchaseTime,
@Query("purchaseState") String purchaseState,
@Query("purchaseToken") String purchaseToken);
/** /**
* 商城商品支付宝支付 * 商城商品支付宝支付
......
package com.zxhl.cms.net.model.uc;
/**
* @author (wangXuewei)
* @datetime 2022-03-14 18:21 GMT+8
* @detail :
*/
public class GooglePayEntity {
/**
* payType : 4
* wakeup : {"orderNo":1503315110133714944}
* payPlatform : null
* outTradeNo : 1503315110133714944
*/
private int payType;
private WakeupBean wakeup;
private String outTradeNo;
public int getPayType() {
return payType;
}
public void setPayType(int payType) {
this.payType = payType;
}
public WakeupBean getWakeup() {
return wakeup;
}
public void setWakeup(WakeupBean wakeup) {
this.wakeup = wakeup;
}
public String getOutTradeNo() {
return outTradeNo;
}
public void setOutTradeNo(String outTradeNo) {
this.outTradeNo = outTradeNo;
}
public static class WakeupBean {
/**
* orderNo : 1503315110133714944
*/
private long orderNo;
public long getOrderNo() {
return orderNo;
}
public void setOrderNo(long orderNo) {
this.orderNo = orderNo;
}
}
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<Button <Button
android:id="@+id/id_btn_google_pay" android:id="@+id/id_btn_google_pay"
android:text="GooglePay" android:text="GooglePay"
android:layout_marginTop="100dp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/>
......
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