cocos creator google pay

Introduction

Add the Google payment interface to the Android project; the corresponding js call is
the Google official website document:
https://developer.android.com/google/play/billing/billing_library_overview

01-Inject dependencies in build.gradle

Add to buildscript - repositories and allprojects - respositories:
maven { url 'https://maven.google.com' }
mavenLocal()
Insert image description here
Insert image description here

02-Introduce library files in build.gradle

Add dependencies in dependencies:
implementation 'com.android.billingclient:billing:2.0.1'
or
implementation 'com.android.billingclient:billing:2.0.3'
Insert image description here
Insert image description here

03-Google payment permissions

uses-permission android:name="com.android.vending.BILLING
Insert image description here

04-Google paid code

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());
        }
    }
}

Initialize the GooglePay class in the AppActivity OnCreate method
Insert image description here
Insert image description here
and write an external call to the static payment in AppActivity
Insert image description here

Latest library

// 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’
Insert image description here

Guess you like

Origin blog.csdn.net/qq_18924323/article/details/105564220