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 1 commit
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
21 changes: 19 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.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,7 @@
private val builder: BillingClient.Builder = BillingClient.newBuilder(reactContext).enablePendingPurchases(),
private val googleApiAvailability: GoogleApiAvailability = GoogleApiAvailability.getInstance(),
) : ReactContextBaseJavaModule(reactContext),
PurchasesUpdatedListener {
PurchasesUpdatedListener, UserChoiceBillingListener {

Check failure on line 46 in android/src/play/java/com/dooboolab/rniap/RNIapModule.kt

View workflow job for this annotation

GitHub Actions / ktlint

Missing newline after ","
private var billingClientCache: BillingClient? = null
private val skus: MutableMap<String, ProductDetails> = mutableMapOf()

Expand Down Expand Up @@ -145,7 +147,7 @@
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 +452,7 @@
type: String,
skuArr: ReadableArray,
purchaseToken: String?,
externalTransactionID: String?,
replacementMode: Int,
obfuscatedAccountId: String?,
obfuscatedProfileId: String?,
Expand Down Expand Up @@ -508,6 +511,9 @@
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 +725,7 @@

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 +743,14 @@
}
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 userChoiceBillingUpdateListener = (
Syedh30 marked this conversation as resolved.
Show resolved Hide resolved
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,
userChoiceBillingUpdateListener,
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 = userChoiceBillingUpdateListener(
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,
externalTransactionID,
prorationModeAndroid = -1,
obfuscatedAccountIdAndroid,
obfuscatedProfileIdAndroid,
Expand All @@ -807,6 +809,7 @@ export const requestSubscription = (
ANDROID_ITEM_TYPE_SUBSCRIPTION,
subscriptionOffers?.map((so) => so.sku),
purchaseTokenAndroid,
externalTransactionID,
prorationModeAndroid,
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,
externalTransactionID: string | undefined,
Syedh30 marked this conversation as resolved.
Show resolved Hide resolved
prorationMode: ProrationModesAndroid | -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
externalTransactionToken?: string;
Syedh30 marked this conversation as resolved.
Show resolved Hide resolved
}

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

export interface RequestSubscriptionAndroid extends RequestPurchaseBaseAndroid {
purchaseTokenAndroid?: string;
externalTransactionID?: string;
prorationModeAndroid?: ProrationModesAndroid;
subscriptionOffers: SubscriptionOffer[];
}
Expand Down
Loading