支付在直播系统源码中有很重要的作用,在直播平台中,送礼物、买东西等很多功能都需要用到支付这个功能,支付方式上的接入服务也是越来越来,下面就给大家介绍下载直播系统源码中接入谷歌支付的过程。
android同胞我相信很多人跟我一样谷歌支付运行自己的直播系统源码的时候调用支付发现都是出现一个问题签名不同我们今天就来解决这个问题先正常导入接入流程后面会提到问题的解决
1、直播系统源码中,首先,导入依赖
//谷歌支付
def billing_version = "5.0.0"
implementation "com.android.billingclient:billing-ktx:$billing_version"
2、直播系统源码中清淡文件添加权限
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="com.farsitel.bazaar.permission.PAY_THROUGH_BAZAAR" />
3、直播系统源码中代码接入kotlin的代码是在activity里面写
companion object {
private val billProxy = GoogleBillHelper()
private var billingListenerImpl: GoogleBillingListenerImpl? = null
}
//初始化
billingListenerImpl = GoogleBillingListenerImpl(this)
//建立连接
GoogleBillingManager.getInstance().createClient(this)
//调用方法
onClickGooglePlay()
/**
* 点击购买
* 换起Google 支付面前 ,需要查询是否有对应的APP商品
* @param view
*/
private fun onClickGooglePlay() {
billProxy
.onQuerySkuDetailsAsync(
billingListenerImpl,
BillingClient.ProductType.INAPP,
"fzvip_weak")
}
//事件监听
private class GoogleBillingListenerImpl(private val activity: Activity) : GoogleBillingListener {
/**
* 产品查询成功
*
* @param list
*/
override fun onProductDetailsSus(list: List<ProductDetails>) {
if (null == list || list.size <= 0) {
Log.e("TAG", "没有查询到相关产品~~~~")
return
}
//查询方法中只传了一个商品,所以直接取第一个了
//根据实际情况处理~
Log.e(TAG, "onProductDetailsSus: " )
list.forEach {
Log.e(TAG, "onProductDetailsSus: "+it )
billProxy.onOpenGooglePlay(this, activity, it)
}
}
/**
* 购买监听
*
* @param result
* @param purchases
*/
override fun onPurchasesUpdated(result: BillingResult?, purchases: MutableList<Purchase>?) {
Log.e(TAG, "onPurchasesUpdated: "+result?.responseCode )
Log.e(TAG, "onPurchasesUpdated: "+result?.debugMessage )
if (null == purchases || purchases.size == 0) {
return
}
//循环调用消耗
for (purchase in purchases) {
billProxy.onConsumeAsync(this, purchase)
}
}
/**
* 消费监听
* @param purchaseToken
*/
override fun onConsumeSus(purchaseToken: String) {
Log.e("TAG", "消费结束,处理自己的业务逻辑~~~")
//去与后台验证。处理APP的页面逻辑, 比如充值后发放金币啥的~~~
}
}
override fun onDestroy() {
super.onDestroy()
//结束连接
GoogleBillingManager.getInstance().endConn()
}
4、直播系统源码中支付的类和接口直接用就可以
1、第一个类
/**
* @author by JHY
* Date: 2023/4/20 15:40
* Desc:支付的具体操作
* 1.查询
* 2.购买
* 3.消费
*/
public class GoogleBillHelper {
public static final String TAG = GoogleBillHelper.class.getSimpleName();
/**
* 查询商品详情
*
* @param billingListener : 接口监听
* @param productIds :商品id 。对应Google 后台的
* @param productType :取值
* BillingClient.ProductType.INAPP(一次性商品)
* BillingClient.ProductType.SUBS(订阅)
*/
public void onQuerySkuDetailsAsync(GoogleBillingListener billingListener, String productType, String... productIds) {
if (null == productIds || productIds.length == 0
|| !GoogleBillingManager.getInstance().isReady()
) {
return;
}
List<QueryProductDetailsParams.Product> skuList = new ArrayList<>();
for (String productId : productIds) {
QueryProductDetailsParams.Product product = QueryProductDetailsParams
.Product.newBuilder()
.setProductId(productId)
.setProductType(productType)
.build();
//添加对应的 产品id 去查询详情
skuList.add(product);
}
QueryProductDetailsParams params = QueryProductDetailsParams
.newBuilder()
.setProductList(skuList)
.build();
GoogleBillingManager.getInstance().getBillingClient().queryProductDetailsAsync(params, (billingResult, list) -> {
if (BillingClient.BillingResponseCode.OK == billingResult.getResponseCode()) {
if (null != billingListener) {
billingListener.onProductDetailsSus(list);
}
} else {
Log.e("TAG", "code : " + billingResult.getResponseCode() + " message : " + billingResult.getDebugMessage());
}
});
}
/**
* 打开支付面板
*
* @param billingListener
* @param activity
* @param details
*/
public void onOpenGooglePlay(GoogleBillingListener billingListener, Activity activity, ProductDetails details) {
Log.e(TAG, "onOpenGooglePlay: ");
if (null == details) {
return;
}
List<BillingFlowParams.ProductDetailsParams> params = new ArrayList<>();
//根据自己的判断是否是连续包月还是一次性消费连续包月需要添加setOfferToken不加不能调谷歌支付面板
//就是一次性商品是一个商品连续包月的话里面有优惠卷
if (details.getProductId().equals("fzvip_android_succession_quarter")) {
//添加购买数据
BillingFlowParams.ProductDetailsParams productDetailsParams = BillingFlowParams.ProductDetailsParams
.newBuilder()
.setProductDetails(details)
.setOfferToken(details.getSubscriptionOfferDetails().get(0).getOfferToken())
.build();
params.add(productDetailsParams);
} else {
BillingFlowParams.ProductDetailsParams productDetailsParams = BillingFlowParams.ProductDetailsParams
.newBuilder()
.setProductDetails(details)
.build();
params.add(productDetailsParams);
}
SharedPreferences mProductId = activity.getSharedPreferences("ProductId", Context.MODE_PRIVATE);
String mProductId1 = mProductId.getString("mProductId", "");
//
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(params)
.setObfuscatedAccountId(mProductId1)//Uid
.build();
//响应code 码
int responseCode = GoogleBillingManager.getInstance().getBillingClient().launchBillingFlow(activity, billingFlowParams).getResponseCode();
Log.e(TAG, "onOpenGooglePlay: " + responseCode);
//成功换起
if (BillingClient.BillingResponseCode.OK == responseCode) {
//添加购买监听
GoogleBillingManager.getInstance().setBillingListener(billingListener);
}
}
/**
* 消费商品
* 对于购买类型的商品需要手动调用一次消费方法 (目的:用户可以再次购买此商品)
*
* @param billingListener
* @param purchase
*/
public void onConsumeAsync(GoogleBillingListener billingListener, Purchase purchase, Activity activity) {
QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
.setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
.setTipWord("正在加载")
.create();
tipDialog.show();
if (!GoogleBillingManager.getInstance().isReady()) {
return;
}
ConsumeParams consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
ConsumeResponseListener listener = (billingResult, purchaseToken) -> {
if (billingResult.getResponseCode() == 5 && BillingClient.BillingResponseCode.OK == 0) {
//连续订阅
if (null != billingListener) {
tipDialog.dismiss();
billingListener.onConsumeSus(purchaseToken);
} else {
tipDialog.dismiss();
Log.e(TAG, "消费失败 code : " + billingResult.getResponseCode() + " message : " + billingResult.getDebugMessage());
}
}
//一次性消费
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (null != billingListener) {
tipDialog.dismiss();
billingListener.onConsumeSus(purchaseToken);
} else {
tipDialog.dismiss();
Log.e(TAG, "消费失败 code : " + billingResult.getResponseCode() + " message : " + billingResult.getDebugMessage());
}
}
};
GoogleBillingManager.getInstance().getBillingClient().consumeAsync(consumeParams, listener);
}
}
2、第二个类
/**
* @author by JHY
* Date: 2023/4/20 15:40
* Desc:Google 支付管理
*/
public class GoogleBillingManager {
private static GoogleBillingManager instance;
private BillingClient billingClient;
private GoogleBillingListener billingListener;
private GoogleBillingManager() {
}
public static GoogleBillingManager getInstance() {
if (instance == null) {
synchronized (GoogleBillingManager.class) {
if (instance == null) {
instance = new GoogleBillingManager();
}
}
}
return instance;
}
/**
* 创建支付客户端
*/
public void createClient(Context context) {
if (null == context) {
return;
}
billingClient = BillingClient.newBuilder(context.getApplicationContext())
.enablePendingPurchases()
.setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> purchases) {
if (null != billingListener) {
billingListener.onPurchasesUpdated(billingResult, purchases);
}
}
})
.build();
//启动支付连接
startConn();
}
public BillingClient getBillingClient() {
return billingClient;
}
/**
* 添加监听事件
*/
public void setBillingListener(GoogleBillingListener billingListener) {
this.billingListener = billingListener;
}
/**
* 是否准备好了
*
* @return
*/
public boolean isReady() {
return !(null == billingClient || !billingClient.isReady());
}
/**
* 启动连接
*/
private void startConn() {
if (isReady()) {
return;
}
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Log.e("TAG", "连接成功,可以开始操作了~~~");
}
}
@Override
public void onBillingServiceDisconnected() {
//连接失败。 可以尝试调用 startConnection 重新建立连接
Log.e("TAG", "连接失败");
}
});
}
/**
* 结束连接
*/
public void endConn() {
if (null != billingClient) {
billingClient.endConnection();
}
}
}
3、接口
/**
* @author by JHY
* Date: 2023/4/20 15:40
* Desc:监听
*/
public interface GoogleBillingListener {
/**
* 购买监听
*
* @param result
* @param purchases
*/
void onPurchasesUpdated(BillingResult result, List<Purchase> purchases);
/**
* 查询商品详情成功
*
* @param list
*/
void onProductDetailsSus(List<ProductDetails> list);
/**
* 商品消费成功
*
* @param purchaseToken
*/
void onConsumeSus(String purchaseToken);
}
重点总结:这些东西写完发现调用完了之后开始支付如果运行我们android studio上面的app根本行不通无法支付只能出来弹框他不能成功唤起支付页面、解决的话很简单就是我们不安装自己的去安装Google play平台上面我们上传上去的app光说可能听不太懂我们直接上图片
1、我们直接点击自己的上传的app
2、查看自己需要的版本
3、下载apk就可以了
4、弹框下载apk就可以测试了
以上就是 在直播系统源码中接入谷歌支付,更多内容欢迎关注之后的文章。
标签:TAG,billingResult,谷歌,param,直播,billingListener,源码,null,public From: https://www.cnblogs.com/yunbaomengnan/p/17617081.html