Skip to content

Commit

Permalink
Creating java BillingClient.Builder in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuguohua committed Dec 9, 2024
1 parent 91d8684 commit 851a663
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 180 deletions.
28 changes: 25 additions & 3 deletions @types/jsb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,18 @@ declare namespace jsb {
onThermalStatusChanged?: (previousStatus: number, newStatus: number, statusMin: number, statusMax: number) => void;
} | undefined;

export interface UserChoiceDetails {

export interface UserChoiceDetailsProduct {
hashCode(): number;
getId(): string;
getOfferToken(): string;
getType(): string;
toStr(): string;
equals(product: Product): boolean;
}
export class UserChoiceDetails {
getExternalTransactionToken(): string;
getOriginalExternalTransactionId(): string;
getProducts(): UserChoiceDetailsProduct[];
}

/**
Expand Down Expand Up @@ -751,6 +761,7 @@ declare namespace jsb {
}

export class PendingPurchasesParams {
private constructor();
public static newBuilder(): PendingPurchasesParamsBuilder;
}

Expand All @@ -760,6 +771,7 @@ declare namespace jsb {
build: () => Product;
}
export class Product {
private constructor();
public static newBuilder(): ProductBuilder;
}

Expand All @@ -768,6 +780,7 @@ declare namespace jsb {
build: () => QueryProductDetailsParams;
}
export class QueryProductDetailsParams {
private constructor();
public static newBuilder(): QueryProductDetailsParamsBuilder;
}

Expand Down Expand Up @@ -855,7 +868,7 @@ declare namespace jsb {
* @zh
* Billing client的连接状态
*/
enum ConnectionState {
export enum ConnectionState {
/**
* @en
* This client was not yet connected to billing service or was already closed.
Expand Down Expand Up @@ -1111,6 +1124,7 @@ declare namespace jsb {
build: () => ProductDetailsParams;
}
export class ProductDetailsParams {
private constructor();
static newBuilder: () => ProductDetailsParamsBuilder;
}

Expand All @@ -1121,13 +1135,15 @@ declare namespace jsb {
build: () => SubscriptionUpdateParams;
}
export class SubscriptionUpdateParams {
private constructor();
static newBuilder(): () => SubscriptionUpdateParamsBuilder;
}

export class BillingFlowParams {
static ProductDetailsParams: typeof jsb.ProductDetailsParams;
static SubscriptionUpdateParams: typeof jsb.SubscriptionUpdateParams;
static Builder: BillingFlowParamsBuilder;
private constructor();
public static newBuilder(): BillingFlowParamsBuilder;
}

Expand All @@ -1145,13 +1161,15 @@ declare namespace jsb {
build: () => ConsumeParams;
}
export class ConsumeParams {
private constructor();
public static newBuilder(): ConsumeParamsBuilder;
}
export interface AcknowledgePurchaseParamsBuilder {
setPurchaseToken: (purchaseToken: string) => AcknowledgePurchaseParamsBuilder;
build: () => AcknowledgePurchaseParams;
}
export class AcknowledgePurchaseParams {
private constructor();
public static newBuilder(): AcknowledgePurchaseParamsBuilder;
}

Expand All @@ -1160,6 +1178,7 @@ declare namespace jsb {
build: () => QueryPurchasesParams;
}
export class QueryPurchasesParams {
private constructor();
public static newBuilder(): QueryPurchasesParamsBuilder;
}

Expand All @@ -1169,16 +1188,19 @@ declare namespace jsb {
build: () => InAppMessageParams;
}
export class InAppMessageParams {
private constructor();
public static newBuilder(): InAppMessageParamsBuilder;
}

export interface GetBillingConfigParamsBuilder {
build: () => GetBillingConfigParams;
}
export class GetBillingConfigParams {
private constructor();
public static newBuilder(): GetBillingConfigParamsBuilder;
}
export class BillingClient {
private constructor();
static Builder: BillingFlowParamsBuilder;
static ConnectionState: typeof jsb.ConnectionState;
static BillingResponseCode: typeof jsb.BillingResponseCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ of this software and associated documentation files (the "Software"), to deal
import com.android.billingclient.api.UserChoiceDetails;
import com.cocos.lib.GlobalObject;
import com.cocos.lib.CocosHelper;
import com.google.android.gms.common.internal.Asserts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -81,33 +83,9 @@ public class GoogleBilling implements PurchasesUpdatedListener, UserChoiceBillin
*/
private BillingClient _billingClient;

public GoogleBilling(boolean enableAlternativeBillingOnly,
boolean enableExternalOffer,
boolean enableOneTimeProducts,
boolean enablePrepaidPlans,
int tag) {
public GoogleBilling(int tag, BillingClient.Builder builder) {
assert tag >= 0 && builder != null;
this._tag = tag;
BillingClient.Builder builder = BillingClient.newBuilder(GlobalObject.getActivity());
// Callbacks are set by default, if ts doesn't set callbacks, nothing is done after the callbacks
builder.setListener(this);
builder.enableUserChoiceBilling(this);

if(enableAlternativeBillingOnly) {
builder.enableAlternativeBillingOnly();
}
if(enableExternalOffer) {
builder.enableExternalOffer();
}
if(enableOneTimeProducts || enablePrepaidPlans) {
PendingPurchasesParams.Builder tempBuilder = PendingPurchasesParams.newBuilder();
if(enableOneTimeProducts) {
tempBuilder.enableOneTimeProducts();
}
if(enablePrepaidPlans) {
tempBuilder.enablePrepaidPlans();
}
builder.enablePendingPurchases(tempBuilder.build());
}
_billingClient = builder.build();
}

Expand Down Expand Up @@ -241,39 +219,6 @@ public void queryProductDetailsAsync(String[] productIds, String[] productTypes,
public void launchBillingFlow(BillingFlowParams params) {
_billingClient.launchBillingFlow(GlobalObject.getActivity(), params);
}
// public void launchBillingFlow(int[] productDetailsHashs, String selectedOfferToken) {
// if(!isConnected()) {
// Log.w(TAG, "Must be connected before use this interface");
// return;
// }
//
// List<BillingFlowParams.ProductDetailsParams> productDetailsParamsList = new ArrayList<>();
// for (int productDetailsHash: productDetailsHashs) {
// if(_productDetails.containsKey(productDetailsHash)) {
// if(selectedOfferToken.isEmpty()) {
// productDetailsParamsList.add(
// BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(_productDetails.get(productDetailsHash)).build()
// );
// } else {
// productDetailsParamsList.add(
// BillingFlowParams.ProductDetailsParams.newBuilder()
// .setProductDetails(_productDetails.get(productDetailsHash))
// .setOfferToken(selectedOfferToken)
// .build()
// );
// }
// } else {
// Log.w(TAG, "Purchased product ID does not exist");
// }
//
// }
// if(productDetailsParamsList.isEmpty()) {
// Log.w(TAG, "Purchased product ID does not exist");
// return;
// }
// BillingFlowParams params = BillingFlowParams.newBuilder().setProductDetailsParamsList(productDetailsParamsList).build();
// _billingClient.launchBillingFlow(GlobalObject.getActivity(), params);
// }

public void queryPurchasesAsync(String type, @NonNull PurchasesResponseListener listener) {
if(!isConnected()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ of this software and associated documentation files (the "Software"), to deal
import com.android.billingclient.api.AlternativeBillingOnlyInformationDialogListener;
import com.android.billingclient.api.AlternativeBillingOnlyReportingDetails;
import com.android.billingclient.api.AlternativeBillingOnlyReportingDetailsListener;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingConfig;
import com.android.billingclient.api.BillingConfigResponseListener;
import com.android.billingclient.api.BillingFlowParams;
Expand Down Expand Up @@ -68,19 +69,17 @@ public class GoogleBillingHelper {
private static SparseArray<GoogleBilling> googleBillings = new SparseArray<GoogleBilling>();
private static int billingTag = 0;

public static int createBillingClient(boolean enableAlternativeBillingOnly,
boolean enableExternalOffer,
boolean enableOneTimeProducts,
boolean enablePrepaidPlans) {
final int index = billingTag;
public static int newTag() {
return billingTag++;
}
public static void createBillingClient(int tag, BillingClient.Builder builder) {
GlobalObject.runOnUiThread(new Runnable() {
@Override
public void run() {
GoogleBilling billing = new GoogleBilling(enableAlternativeBillingOnly, enableExternalOffer, enableOneTimeProducts, enablePrepaidPlans, index);
googleBillings.put(index, billing);
GoogleBilling billing = new GoogleBilling(tag, builder);
googleBillings.put(tag, billing);
}
});
return billingTag++;
}

public static int removeBillingClient(int tag) {
Expand All @@ -94,7 +93,7 @@ public void run() {
return billingTag++;
}

class BillingClientPurchasesUpdatedListener implements PurchasesUpdatedListener {
public static final class BillingClientPurchasesUpdatedListener implements PurchasesUpdatedListener {
private int _tag;
BillingClientPurchasesUpdatedListener(int tag) {
this._tag = tag;
Expand All @@ -115,7 +114,7 @@ public void run() {
}
}

class BillingClientUserChoiceBillingListener implements UserChoiceBillingListener {
public static final class BillingClientUserChoiceBillingListener implements UserChoiceBillingListener {
private int _tag;
BillingClientUserChoiceBillingListener(int tag) {
this._tag = tag;
Expand Down
14 changes: 1 addition & 13 deletions native/vendor/google/billing/GoogleBilling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,7 @@ BillingClient::BillingClient(Builder* builder) {
this->purchasesUpdatedListener = builder->purchasesUpdatedListener;
this->userChoiceBillingListener = builder->userChoiceBillingListener;

if (this->_pendingPurchasesParams) {
_tag = GoogleBillingHelper::createBillingClient(
builder->_enableAlternativeBillingOnly,
builder->_enableExternalOffer,
builder->_pendingPurchasesParams->_enableOneTimeProducts,
builder->_pendingPurchasesParams->_enablePrepaidPlans);
} else {
_tag = GoogleBillingHelper::createBillingClient(
builder->_enableAlternativeBillingOnly,
builder->_enableExternalOffer,
false,
false);
}
_tag = GoogleBillingHelper::createBillingClient(builder);

CC_ASSERT(_tag >= 0);
GoogleBillingManager::getInstance()->pushBillingClient(_tag, this);
Expand Down
12 changes: 8 additions & 4 deletions native/vendor/google/billing/GoogleBilling.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ class UserChoiceDetails : public cc::RefCounted {
std::string getType() const {
return _offerToken;
}
std::string toString() const {
std::string toStr() const {
return _toStr;
}
bool equals(const Product& p) {
Expand All @@ -478,6 +478,7 @@ class UserChoiceDetails : public cc::RefCounted {
}

private:
friend class GoogleBillingToNative;
int _hashCode{0};
std::string _id;
std::string _type;
Expand Down Expand Up @@ -544,6 +545,7 @@ class PendingPurchasesParams {
}

private:
friend class GoogleBillingToNative;
friend class BillingClient;
bool _enableOneTimeProducts{false};
bool _enablePrepaidPlans{false};
Expand Down Expand Up @@ -956,6 +958,7 @@ class CC_DLL BillingClient : public cc::RefCounted {

private:
friend class BillingClient;
friend class GoogleBillingToNative;
bool _enableAlternativeBillingOnly;
bool _enableExternalOffer;
PendingPurchasesParams* _pendingPurchasesParams;
Expand All @@ -965,8 +968,7 @@ class CC_DLL BillingClient : public cc::RefCounted {
static Builder* newBuilder() {
return new Builder();
}
BillingClient(Builder* builder);
~BillingClient();

void startConnection(se::Object* listener);
void endConnection();
int getConnectionState() const;
Expand All @@ -986,7 +988,9 @@ class CC_DLL BillingClient : public cc::RefCounted {
BillingResult* showAlternativeBillingOnlyInformationDialog(se::Object* listener);
BillingResult* showExternalOfferInformationDialog(se::Object* listener);
BillingResult* showInAppMessages(InAppMessageParams* params, se::Object* listener);

private:
BillingClient(Builder* builder);
~BillingClient();
private:
friend class GoogleBillingHelper;
int _tag{-1};
Expand Down
21 changes: 9 additions & 12 deletions native/vendor/google/billing/GoogleBillingHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,15 @@ template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, Alterna
template void callJSfunc(se::Object* obj, const char*, BillingResult*&&, ExternalOfferReportingDetails*&&);
template void callJSfunc(se::Object* obj, const char*, InAppMessageResult*&&);

int GoogleBillingHelper::createBillingClient(bool enableAlternativeBillingOnly,
bool enableExternalOffer,
bool enableOneTimeProducts,
bool enablePrepaidPlans) {
int tag = JniHelper::callStaticIntMethod(JCLS_BILLING,
"createBillingClient",
enableAlternativeBillingOnly,
enableExternalOffer,
enableOneTimeProducts,
enablePrepaidPlans);
auto* bb = cc::BillingFlowParams::newBuilder()->setIsOfferPersonalized(false).setObfuscatedAccountId("qqq").setObfuscatedProfileId("ddd").build();
//GoogleBillingHelper::launchBillingFlow(tag, bb);
int GoogleBillingHelper::createBillingClient(void* params) {
int tag = JniHelper::callStaticIntMethod(JCLS_BILLING, "newTag");
auto* builder = reinterpret_cast<BillingClient::Builder*>(params);
jobject buildObj = GoogleBillingToNative::newBillingClientBuilderObject(tag, builder);

cc::JniMethodInfo t;
cc::JniHelper::getStaticMethodInfo(t, JCLS_BILLING, "createBillingClient", "(ILcom/android/billingclient/api/BillingClient$Builder;)V");
t.env->CallStaticVoidMethod(t.classID, t.methodID, tag, buildObj);

return tag;
}

Expand Down
5 changes: 1 addition & 4 deletions native/vendor/google/billing/GoogleBillingHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ class AcknowledgePurchaseParams;

class CC_DLL GoogleBillingHelper {
public:
static int createBillingClient(bool enableAlternativeBillingOnly,
bool enableExternalOffer,
bool enableOneTimeProducts,
bool enablePrepaidPlans);
static int createBillingClient(void* builder);
static void removeBillingClient(int tag);
static void removeProductDetails(int tag, int productDetailsID);
static void removePurchase(int tag, int purchaseID);
Expand Down
Loading

0 comments on commit 851a663

Please sign in to comment.