Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: google alternative billing #2803

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions android/src/play/java/com/dooboolab/rniap/RNIapModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import com.android.billingclient.api.PurchasesUpdatedListener
import com.android.billingclient.api.QueryProductDetailsParams
import com.android.billingclient.api.QueryPurchaseHistoryParams
import com.android.billingclient.api.QueryPurchasesParams
import com.android.billingclient.api.UserChoiceBillingListener
import com.android.billingclient.api.UserChoiceDetails
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.Promise
Expand All @@ -41,7 +43,8 @@ class RNIapModule(
private val builder: BillingClient.Builder = BillingClient.newBuilder(reactContext).enablePendingPurchases(),
private val googleApiAvailability: GoogleApiAvailability = GoogleApiAvailability.getInstance(),
) : ReactContextBaseJavaModule(reactContext),
PurchasesUpdatedListener {
PurchasesUpdatedListener,
UserChoiceBillingListener {
private var billingClientCache: BillingClient? = null
private val skus: MutableMap<String, ProductDetails> = mutableMapOf()

Expand Down Expand Up @@ -145,7 +148,7 @@ class RNIapModule(
promise.safeResolve(true)
return
}
builder.setListener(this).build().also {
builder.setListener(this).enableUserChoiceBilling(this).build().also {
billingClientCache = it
it.startConnection(
object : BillingClientStateListener {
Expand Down Expand Up @@ -450,6 +453,7 @@ class RNIapModule(
type: String,
skuArr: ReadableArray,
purchaseToken: String?,
externalTransactionID: String?,
replacementMode: Int,
obfuscatedAccountId: String?,
obfuscatedProfileId: String?,
Expand Down Expand Up @@ -508,6 +512,9 @@ class RNIapModule(
builder.setProductDetailsParamsList(productParamsList).setIsOfferPersonalized(isOfferPersonalized)

val subscriptionUpdateParamsBuilder = SubscriptionUpdateParams.newBuilder()
if (externalTransactionID != null) {
subscriptionUpdateParamsBuilder.setOriginalExternalTransactionId(externalTransactionID)
}
if (purchaseToken != null) {
subscriptionUpdateParamsBuilder.setOldPurchaseToken(purchaseToken)

Expand Down Expand Up @@ -719,6 +726,7 @@ class RNIapModule(

companion object {
private const val PROMISE_BUY_ITEM = "PROMISE_BUY_ITEM"
private const val USER_ALTER_ITEM = "USER_ALTER_ITEM"
const val TAG = "RNIapModule"
}

Expand All @@ -736,4 +744,14 @@ class RNIapModule(
}
reactContext.addLifecycleEventListener(lifecycleEventListener)
}

override fun userSelectedAlternativeBilling(userChoiceDetails: UserChoiceDetails) {
val products = userChoiceDetails.products
val externalToken = userChoiceDetails.externalTransactionToken
val result = Arguments.createMap()
result.putString("products", userChoiceDetails.products.toString())
result.putString("externalTransactionToken", userChoiceDetails.externalTransactionToken)
sendEvent(reactContext, "user-alternative-billing", result)
PromiseUtils.resolvePromisesForKey(USER_ALTER_ITEM, null)
}
}
21 changes: 21 additions & 0 deletions src/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ export const purchaseUpdatedListener = (
return emitterSubscription;
};

export const userChoiceBillingUpdateListenerAndroid = (
listener: (event: Purchase) => void,
) => {
const eventEmitter = new NativeEventEmitter(getNativeModule());
const proxyListener = isIosStorekit2()
? (event: Purchase) => {
listener(transactionSk2ToPurchaseMap(event as any));
}
: listener;
const emitterUserChoiceBilling = eventEmitter.addListener(
'user-alternative-billing',
proxyListener,
);

if (isAndroid) {
getAndroidModule().startListening();
}

return emitterUserChoiceBilling;
};

/**
* Add IAP purchase error event
* Register a callback that gets called when there has been an error with a purchase. Returns a React Native `EmitterSubscription` on which you can call `.remove()` to stop receiving updates.
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/withIAPContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
promotedProductListener,
purchaseErrorListener,
purchaseUpdatedListener,
userChoiceBillingUpdateListenerAndroid,
transactionListener,
} from '../eventEmitter';
import {IapIos, initConnection} from '../iap';
Expand Down Expand Up @@ -71,6 +72,9 @@ export function withIAPContext<T>(Component: React.ComponentType<T>) {

const [currentPurchaseError, setCurrentPurchaseError] =
useState<PurchaseError>();
const [alternativePurchase, setAlternativePurchase] = useState<Purchase>();
const [alternativePurchaseError, setAlternativePurchaseError] =
useState<PurchaseError>();

const [initConnectionError, setInitConnectionError] = useState<Error>();

Expand All @@ -85,13 +89,17 @@ export function withIAPContext<T>(Component: React.ComponentType<T>) {
currentPurchase,
currentTransaction,
currentPurchaseError,
alternativePurchase,
alternativePurchaseError,
initConnectionError,
setProducts,
setSubscriptions,
setPurchaseHistory,
setAvailablePurchases,
setCurrentPurchase,
setCurrentPurchaseError,
setAlternativePurchase,
setAlternativePurchaseError,
}),
[
connected,
Expand All @@ -103,13 +111,17 @@ export function withIAPContext<T>(Component: React.ComponentType<T>) {
currentPurchase,
currentTransaction,
currentPurchaseError,
alternativePurchase,
alternativePurchaseError,
initConnectionError,
setProducts,
setSubscriptions,
setPurchaseHistory,
setAvailablePurchases,
setCurrentPurchase,
setCurrentPurchaseError,
setAlternativePurchase,
setAlternativePurchaseError,
],
);

Expand All @@ -134,6 +146,13 @@ export function withIAPContext<T>(Component: React.ComponentType<T>) {
},
);

const userChoiceBillingUpdateSubscription = userChoiceBillingUpdateListenerAndroid(
async (purchase: ProductPurchase | SubscriptionPurchase) => {
setAlternativePurchaseError(undefined);
setAlternativePurchase(purchase);
},
);

const transactionUpdateSubscription = transactionListener(
async (transactionOrError: TransactionEvent) => {
setCurrentPurchaseError(transactionOrError?.error);
Expand Down Expand Up @@ -162,6 +181,7 @@ export function withIAPContext<T>(Component: React.ComponentType<T>) {
purchaseErrorSubscription.remove();
promotedProductSubscription?.remove();
transactionUpdateSubscription?.remove();
userChoiceBillingUpdateSubscription?.remove();
};
}, [connected]);

Expand Down
3 changes: 3 additions & 0 deletions src/iap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ export const requestPurchase = (
ANDROID_ITEM_TYPE_IAP,
skus,
undefined,
undefined,
-1,
obfuscatedAccountIdAndroid,
obfuscatedProfileIdAndroid,
Expand Down Expand Up @@ -797,6 +798,7 @@ export const requestSubscription = (
const {
subscriptionOffers,
purchaseTokenAndroid,
externalTransactionIdAndroid,
replacementModeAndroid = -1,
obfuscatedAccountIdAndroid,
obfuscatedProfileIdAndroid,
Expand All @@ -807,6 +809,7 @@ export const requestSubscription = (
ANDROID_ITEM_TYPE_SUBSCRIPTION,
subscriptionOffers?.map((so) => so.sku),
purchaseTokenAndroid,
externalTransactionIdAndroid,
replacementModeAndroid,
obfuscatedAccountIdAndroid,
obfuscatedProfileIdAndroid,
Expand Down
1 change: 1 addition & 0 deletions src/modules/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type BuyItemByType = (
type: string,
skus: Sku[],
purchaseToken: string | undefined,
externalTransactionIdAndroid: string | undefined,
replacementModeAndroid: ReplacementModesAndroid | -1,
obfuscatedAccountId: string | undefined,
obfuscatedProfileId: string | undefined,
Expand Down
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface ProductPurchase {
userMarketplaceAmazon?: string;
userJsonAmazon?: string;
isCanceledAmazon?: boolean;
//UserChoiceBilling
externalTransactionTokenAndroid?: string;
}

export interface PurchaseResult {
Expand Down Expand Up @@ -254,6 +256,7 @@ export interface SubscriptionOffer {

export interface RequestSubscriptionAndroid extends RequestPurchaseBaseAndroid {
purchaseTokenAndroid?: string;
externalTransactionIdAndroid?: string;
replacementModeAndroid?: ReplacementModesAndroid;
subscriptionOffers: SubscriptionOffer[];
}
Expand Down
Loading