admin管理员组文章数量:1568433
cocos creator 谷歌支付
- 导读
- 01-在build.gradle 注入依赖
- 02-在build.gradle中引入库文件
- 03-Google 支付权限
- 04-Google 支付的代码
- 最新库
导读
安卓项目中加入google 支付的接口;对应的是js 的调用
google 官网文档:
https://developer.android/google/play/billing/billing_library_overview
01-在build.gradle 注入依赖
在buildscript - repositories 和 allprojects - respositories 中加入 :
maven { url ‘https://maven.google’ }
mavenLocal()
02-在build.gradle中引入库文件
在 dependencies 中添加依赖:
implementation ‘com.android.billingclient:billing:2.0.1’
或者
implementation ‘com.android.billingclient:billing:2.0.3’
03-Google 支付权限
uses-permission android:name="com.android.vending.BILLING
04-Google 支付的代码
package org.cocos2dx.javascript.Google;
import android.content.Intent;
import android.support.annotation.Nullable;
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import org.cocos2dx.javascript.AppActivity;
import org.cocos2dx.lib.Cocos2dxActivity;
import java.util.ArrayList;
import java.util.List;
public class GooglePay implements PurchasesUpdatedListener {
private static final String TAG = "GooglePay";
private AppActivity app;
private BillingClient mBillingClient;
// private AppActivity mGooglePay;
public void Init(AppActivity activity){
app = activity;
// mGooglePay = this;
}
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// }
//01—开启谷歌支付
public void StartGooglePay(final String id){
app.CallToJS("01—开启谷歌支付");
mBillingClient = BillingClient.newBuilder(app)
.enablePendingPurchases()//启用待处理的购买交易(在外部交易)
.setListener(this)
.build();
//建立连接
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
//建立连接成功回调
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// The BillingClient is ready. You can query purchases here.
//请求商品闲情
List<String>skuList = new ArrayList<>();
skuList.add(id);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
//商品详情回调
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
// Process the result.
if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK){
app.CallToJS("开启谷歌支付 获取商品列表");
for(SkuDetails skuDetails:skuDetailsList){
String sku = skuDetails.getSku();
app.CallToJS("发起购买 支付 sku:"+sku);
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetails)
.build();
mBillingClient.launchBillingFlow(app,flowParams);
// if(sku == id){
//
// }
// else{
// app.CallToJS("开启谷歌支付 sku:"+sku);
// app.CallToJS("开启谷歌支付 id:"+id);
// }
}
}
else{
app.CallToJS("开启谷歌支付 getResponseCode():"+billingResult.getResponseCode());
}
}
});
}
else{
app.CallToJS("建立连接成功回调 getResponseCode() :"+billingResult.getResponseCode());
}
}
@Override
//建立连接失败回调
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
app.CallToJS("建立连接失败回调");
}
});
}
@Override
//02-支付操作回调
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
app.CallToJS("02-支付操作回调");
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& purchases != null) {
//支付成功
app.CallToJS("支付成功");
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
//取消支付
// Handle an error caused by a user cancelling the purchase flow.
app.CallToJS("取消支付");
} else {
// Handle any other error codes.
app.CallToJS("支付操作回调 .getResponseCode():"+billingResult.getResponseCode() );
}
}
//03-物品处理
void handlePurchase(Purchase purchase) {
app.CallToJS("03-物品处理");
//购买完成
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// Grant entitlement to the user.
// Acknowledge the purchase if it hasn't already been acknowledged.
//未确认物品
if (!purchase.isAcknowledged()) {
//非消耗品
// AcknowledgePurchaseParams acknowledgePurchaseParams =
// AcknowledgePurchaseParams.newBuilder()
// .setPurchaseToken(purchase.getPurchaseToken())
// .build();
// mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener(){
// @Override
// public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
//
// }
// });
//消耗品
ConsumeParams consumeParams =
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.setDeveloperPayload(purchase.getDeveloperPayload())
.build();
mBillingClient.consumeAsync(consumeParams, new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
app.CallToJS("消耗品 Token:"+purchaseToken);
}
});
}
//已确认物品
else{
app.CallToJS("已确认过物品");
}
}
else if(purchase.getPurchaseState() == Purchase.PurchaseState.PENDING){
app.CallToJS("未完成支付");
//todo
}
else{
app.CallToJS("物品处理 getPurchaseState:"+purchase.getPurchaseState());
}
}
}
在 AppActivity OnCreate 方法中 中初始化 GooglePay 的类
写一个 外部调用 AppActivity 中的静态支付
最新库
// Dependency for Google Sign-In
implementation ‘com.google.android.gms:play-services-auth:15.0.1’
//gogle pay
implementation ‘com.android.billingclient:billing:2.0.1’
版权声明:本文标题:cocos creator 谷歌支付 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1725993002a1051712.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论