In diesem Leitfaden wird beschrieben, wie Sie Ihre App in die Play Billing Library APIs einbinden, um Ihren Nutzern die Abrechnung mit Auswahlmöglichkeit anzubieten.
Integration in PBL
Sie können die Abrechnungsauswahl in vier Szenarien in die PBL einbinden. Die Szenarien unterscheiden sich je nachdem, wer den Auswahlbildschirm rendert und wo die Zahlung erfolgt. In der folgenden Tabelle werden die Integrationsszenarien beschrieben:
| Welchen Bildschirm für die Abrechnungsauswahl möchtest du rendern? | |||
| Google Play | Eigene (gemäß den UX-Richtlinien) | ||
| Wo erfolgt die Zahlung? | In-App | Szenario 1A
Google rendert den Auswahlbildschirm und die alternative Abrechnung wird in Ihrer App abgewickelt. |
Szenario 1B
Der App-Entwickler rendert den Auswahlbildschirm und die alternative Abrechnung wird in Ihrer App abgewickelt. |
| Externer Weblink | Szenario 2A
Google rendert den Auswahlbildschirm und der Nutzer wird außerhalb Ihrer App zu Ihren eigenen Websites weitergeleitet, um dort Käufe zu tätigen. |
Szenario 2B
Der App-Entwickler rendert den Auswahlbildschirm und der Nutzer wird für Käufe außerhalb Ihrer App auf Ihre eigenen Websites weitergeleitet. |
|
In der folgenden Abbildung wird der Ablauf der Abrechnungsauswahl für die einzelnen Szenarien dargestellt:
Szenarien für die Integration von PBL
Je nach Integrationsszenario können Sie die Schritte in diesem Abschnitt ausführen, um die Abrechnungsauswahl in Ihrer App zu implementieren.
Szenario 1A verarbeiten
Google rendert den Auswahlbildschirm und die alternative Abrechnung wird in Ihrer App abgewickelt. Gehen Sie so vor, um die Abrechnungsauswahl in diesem Szenario zu aktivieren:
Rufen Sie beim Erstellen der BillingClient-Instanz
enableBillingProgrammitEnableBillingProgramParamsauf und starten Sie dann die Verbindung. Beispiel:Kotlin
// Build the parameters to enable the Billing Choice program and assign the listener // to handle user selection of the developer-provided billing option. val enableBillingProgramParams = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperProvidedBillingListener(developerProvidedBillingListener) .build() // Build the parameters to enable support for pending purchases. val pendingPurchasesParams = PendingPurchasesParams.newBuilder() .enableOneTimeProducts() .build() // Construct the BillingClient instance with the purchases updated listener, // pending purchases support, and the billing choice params. val billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases(pendingPurchasesParams) .enableBillingProgram(enableBillingProgramParams) .build() // Establish a connection to Google Play val billingResult = suspendCancellableCoroutine<BillingResult> { continuation -> billingClient.startConnection(object : BillingClientStateListener { // Called when the connection setup process completes. override fun onBillingSetupFinished(billingResult: BillingResult) { // Resume the coroutine and pass back the BillingResult to the caller. if (continuation.isActive) { continuation.resume(billingResult) } } // Called if the connection to the Play Store service is dropped. // This prevents the await or suspension point from hanging indefinitely. override fun onBillingServiceDisconnected() { if (continuation.isActive) { continuation.resume( BillingResult.newBuilder() .setResponseCode(BillingResponseCode.SERVICE_DISCONNECTED) .setDebugMessage("Billing service disconnected during connection setup") .build() ) } } }) }
Java
EnableBillingProgramParams params = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperProvidedBillingListener(developerProvidedBillingListener) .build(); BillingClient billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases( PendingPurchasesParams.newBuilder() .enableOneTimeProducts() .build() ) .enableBillingProgram(params) .build();Prüfen Sie, ob die von Google gerenderte Abrechnungsauswahl für den Nutzer verfügbar ist.
Rufen Sie
isBillingProgramAvailableAsyncauf, um die Verfügbarkeit des Programms zu prüfen, und rufen Sie dannqueryProductDetailsAsyncauf, um die verfügbaren Produkte anzuzeigen. Beispiel:Kotlin
val (billingResult, billingProgramAvailabilityDetails) = billingClient.isBillingProgramAvailable(BillingProgram.BILLING_CHOICE) if (billingResult.responseCode == BillingResponseCode.OK) { val billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.billingChoiceAvailabilityDetails if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.choiceScreenType == ChoiceScreenType.GOOGLE_RENDERED ) { // Billing choice is available. Query products and proceed. } else { // Fallback to other available programs. } } else { // Fallback to other available programs. }
Java
// ... billingClient.isBillingProgramAvailableAsync( BillingProgram.BILLING_CHOICE, (billingResult, billingProgramAvailabilityDetails) -> { if (billingResult.getResponseCode() == BillingResponseCode.OK) { BillingChoiceAvailabilityDetails billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.getBillingChoiceAvailabilityDetails(); if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.getChoiceScreenType() == ChoiceScreenType.GOOGLE_RENDERED) { // Billing choice is available. Query products and proceed. } else { // Fallback to other available programs. } } else { // Fallback to other available programs. } } );Hinweis:
billingProgramAvailabilityDetailsgibt an, ob der von Google gerenderte oder der vom Entwickler gerenderte Bildschirm für die Abrechnungsauswahl verfügbar ist.Rufe
launchBillingFlowauf, um den Kaufvorgang auszulösen, wenn der Nutzer auf „Kaufen“ klickt. Wenn die Abrechnungsauswahl verfügbar ist, übergeben SieDeveloperBillingOptionParamsanBillingFlowParams. Beispiel:Kotlin
val developerBillingOptionParams = DeveloperBillingOptionParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build() val billingFlowParams = BillingFlowParams.newBuilder() .setProductDetailsParamsList(productDetailsParamsList) .enableDeveloperBillingOption(developerBillingOptionParams) .build() val billingResult = billingClient.launchBillingFlow(activity, billingFlowParams)
Java
DeveloperBillingOptionParams developerBillingOptionParams = DeveloperBillingOptionParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build(); BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder() .setProductDetailsParamsList(productDetailsParamsList) .enableDeveloperBillingOption(developerBillingOptionParams) .build(); BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);Hinweis: Die Jugendschutzeinstellungen werden für Nutzer mit Elternaufsicht angezeigt.
Gehen Sie bei der Auswahl des Abrechnungstyps durch den Nutzer so vor:
- Wenn der Nutzer die Google Play-Abrechnung auswählt, wird das Abrechnungsergebnis an den in Schritt 1 registrierten
PurchasesUpdatedListenerzurückgegeben. - Wenn der Nutzer die alternative Abrechnung auswählt, wird das Abrechnungsergebnis an den in Schritt 1 registrierten
DeveloperProvidedBillingListenerzurückgegeben. Der zurückgegebeneDeveloperProvidedBillingDetailsenthält in diesem Fall einenexternalTransactionToken. Das Token wird für die Transaktionsberichterstellung verwendet.
- Wenn der Nutzer die Google Play-Abrechnung auswählt, wird das Abrechnungsergebnis an den in Schritt 1 registrierten
Szenario 1B verarbeiten
Der Entwickler rendert den Auswahlbildschirm und die alternative Abrechnung wird in deiner App abgewickelt. Gehe so vor, um die Abrechnungsauswahl in diesem Szenario zu aktivieren:
Rufen Sie
enableBillingProgramohneDeveloperProvidedBillingListenerinEnableBillingProgramParamsauf, wenn Sie eineBillingClient-Instanz erstellen, und starten Sie dann die Verbindung. Beispiel:Kotlin
// Build the parameters to enable the Billing Choice program. val enableBillingProgramParams = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build() // Build the parameters to enable support for pending purchases. val pendingPurchasesParams = PendingPurchasesParams.newBuilder() .enableOneTimeProducts() .build() // Construct the BillingClient instance with the purchases updated listener, // pending purchases support, and the billing choice params. val billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases(pendingPurchasesParams) .enableBillingProgram(enableBillingProgramParams) .build() // Establish a connection to Google Play val billingResult = suspendCancellableCoroutine<BillingResult> { continuation -> billingClient.startConnection(object : BillingClientStateListener { // Called when the connection setup process completes. override fun onBillingSetupFinished(billingResult: BillingResult) { // Resume the coroutine and pass back the BillingResult to the caller. if (continuation.isActive) { continuation.resume(billingResult) } } // Called if the connection to the Play Store service is dropped. // This prevents the await or suspension point from hanging indefinitely. override fun onBillingServiceDisconnected() { if (continuation.isActive) { continuation.resume( BillingResult.newBuilder() .setResponseCode(BillingResponseCode.SERVICE_DISCONNECTED) .setDebugMessage("Billing service disconnected during connection setup") .build() ) } } }) }
Java
EnableBillingProgramParams params = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build(); BillingClient billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases() .enableBillingProgram(params) .build();Prüfen Sie, ob die vom Entwickler gerenderte Abrechnungsauswahl für den Nutzer verfügbar ist.
Rufen Sie
isBillingProgramAvailableAsyncauf, um die Verfügbarkeit des Programms zu prüfen, und rufen Sie dannqueryProductDetailsAsyncauf, um die verfügbaren Produkte anzuzeigen. Beispiel:Kotlin
val (billingResult, billingProgramAvailabilityDetails) = billingClient.isBillingProgramAvailable(BillingProgram.BILLING_CHOICE) if (billingResult.responseCode == BillingResponseCode.OK) { val billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.billingChoiceAvailabilityDetails if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.choiceScreenType == ChoiceScreenType.DEVELOPER_RENDERED ) { // Billing choice is available. Query products and proceed. // You can inspect details such as: // - billingChoiceAvailabilityDetails.choiceScreenType // - billingChoiceAvailabilityDetails.isExternalLinkAvailable } else { // Fallback to other available programs. } } else { // Fallback to other available programs. }
Java
// ... billingClient.isBillingProgramAvailableAsync( BillingProgram.BILLING_CHOICE, (billingResult, billingProgramAvailabilityDetails) -> { if (billingResult.getResponseCode() == BillingResponseCode.OK) { BillingChoiceAvailabilityDetails billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.getBillingChoiceAvailabilityDetails(); if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.getChoiceScreenType() == ChoiceScreenType.DEVELOPER_RENDERED) { // Billing choice is available. Query products and proceed. // You can inspect details such as: // - billingChoiceAvailabilityDetails.getChoiceScreenType() // - billingChoiceAvailabilityDetails.isExternalLinkAvailable() } else { // Fallback to other available programs. } } else { // Fallback to other available programs. } } );Hinweis:
billingProgramAvailabilityDetailsgibt an, ob der von Google oder der von der Entwicklerin oder vom Entwickler gerenderte Bildschirm für die Abrechnungsauswahl verfügbar ist.Rufen Sie die Methode
getBillingChoiceInfoAsyncauf, um das Play Billing-Banner und die Treueinformationen abzurufen. Beispiel:Kotlin
// 1. Create the params required for the request val params = GetBillingChoiceInfoParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setPlayBillingChoiceImageLayout(GetBillingChoiceInfoParams.ImageLayout.RECTANGULAR_FOUR_BY_ONE) .build() // 2. Call the suspend method on your billingClient instance val (billingResult, playBillingChoiceInfo) = billingClient.getBillingChoiceInfo(params) if (billingResult.responseCode == BillingResponseCode.OK && playBillingChoiceInfo != null) { // Access the URL of the image associated with the Play Billing Choice val imageUrl = playBillingChoiceInfo.playBillingChoiceImageUrl // Access the Play Loyalty string information, if available val loyaltyInfo = playBillingChoiceInfo.playBillingLoyaltyInfo // Populate your developer-rendered UI elements playBillingLoyaltyTextView.text = loyaltyInfo loadImage(imageUrl, playBillingImageView) } else { // Handle error scenarios }
Java
// 1. Create the params required for the request GetBillingChoiceInfoParams params = GetBillingChoiceInfoParams.newBuilder() .setBillingProgram(BillingClient.BillingProgram.BILLING_CHOICE) .setPlayBillingChoiceImageLayout(GetBillingChoiceInfoParams.ImageLayout.RECTANGULAR_FOUR_BY_ONE) .build(); // 2. Call the method asynchronously on your billingClient instance billingClient.getBillingChoiceInfoAsync(params, (billingResult, playBillingChoiceInfo) -> { if (billingResult.getResponseCode() == BillingResponseCode.OK && playBillingChoiceInfo != null) { // Access the URL of the image associated with the Play Billing Choice String imageUrl = playBillingChoiceInfo.getPlayBillingChoiceImageUrl(); // Access the Play Loyalty string information, if available String loyaltyInfo = playBillingChoiceInfo.getPlayBillingLoyaltyInfo(); // Populate your developer-rendered UI elements playBillingLoyaltyTextView.setText(loyaltyInfo); loadImage(imageUrl, playBillingImageView); } else { // Handle error scenarios } });Erstellen Sie ein Token für externe Transaktionen, wobei DeveloperBillingType auf IN_APP festgelegt ist. Beispiel:
Kotlin
// Build the parameters specifying the billing program and that the billing type is IN_APP. val params = BillingProgramReportingDetailsParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperBillingType(DeveloperBillingType.IN_APP) .build() // Call the suspending extension function to request the reporting details val (billingResult, billingProgramReportingDetails) = billingClient.createBillingProgramReportingDetails(params) if (billingResult.responseCode != BillingResponseCode.OK) { // Handle failures such as retrying due to network errors. return } // Extract the transaction token from the returned reporting details val transactionToken = billingProgramReportingDetails?.externalTransactionToken // Persist the external transaction token locally. Pass it to // DeveloperBillingOptionParams when launchBillingFlow is called. // It can also be used as part of your external website
Java
BillingProgramReportingDetailsParams params = BillingProgramReportingDetailsParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperBillingType(DeveloperBillingType.IN_APP) .build(); billingClient.createBillingProgramReportingDetailsAsync( params, new BillingProgramReportingDetailsListener() { @Override public void onCreateBillingProgramReportingDetailsResponse( BillingResult billingResult, @Nullable BillingProgramReportingDetails billingProgramReportingDetails ) { if (billingResult.getResponseCode() != BillingResponseCode.OK) { // Handle failures such as retrying due to network errors. return; } String transactionToken = billingProgramReportingDetails.getExternalTransactionToken(); // Persist the external transaction token locally. Pass it to // DeveloperBillingOptionParams when launchBillingFlow is called. // It can also be used as part of your external website } } );Wenn der Nutzer auf „Kaufen“ klickt, rufe
showBillingProgramInformationDialogauf, um ein Informationsdialogfeld anzuzeigen. Beispiel: Informationsdialog für Nutzer. BillingProgram und transactionToken aus Schritt 4 müssen in der Anfrage festgelegt werden.Hinweis: Die Jugendschutzeinstellungen werden für Nutzer mit Elternaufsicht angezeigt.
Starte den Auswahlbildschirm für die alternative Abrechnung, wenn das Ergebnis des vorherigen Schritts
OKist.Gehen Sie bei der Auswahl des Abrechnungstyps durch den Nutzer so vor:
- Wenn der Nutzer Google Play Billing auswählt, rufe
launchBillingFlowgemäß der Standardanleitung für Google Play Billing auf. Das Abrechnungsergebnis wird an die in Schritt 1 registriertePurchasesUpdatedListenerzurückgegeben. - Wenn der Nutzer deine alternative Abrechnung auswählt, musst du die Transaktion selbst abwickeln und sie mit dem in Schritt 4 generierten Token an Google Play melden.
- Wenn der Nutzer Google Play Billing auswählt, rufe
Szenario 2A verarbeiten
Google rendert den Auswahlbildschirm und die alternative Abrechnung wird außerhalb Ihrer App abgewickelt. Gehen Sie so vor, um die Abrechnungsauswahl in diesem Szenario zu aktivieren:
Rufen Sie beim Erstellen der BillingClient-Instanz
enableBillingProgrammitEnableBillingProgramParamsauf und starten Sie dann die Verbindung. Beispiel:Kotlin
// Build the parameters to enable the Billing Choice program and assign the listener // to handle user selection of the developer-provided billing option. val enableBillingProgramParams = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperProvidedBillingListener(developerProvidedBillingListener) .build() // Build the parameters to enable support for pending purchases. val pendingPurchasesParams = PendingPurchasesParams.newBuilder() .enableOneTimeProducts() .build() // Construct the BillingClient instance with the purchases updated listener, // pending purchases support, and the billing choice params. val billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases(pendingPurchasesParams) .enableBillingProgram(enableBillingProgramParams) .build() // Establish a connection to Google Play val billingResult = suspendCancellableCoroutine<BillingResult> { continuation -> billingClient.startConnection(object : BillingClientStateListener { // Called when the connection setup process completes. override fun onBillingSetupFinished(billingResult: BillingResult) { // Resume the coroutine and pass back the BillingResult to the caller. if (continuation.isActive) { continuation.resume(billingResult) } } // Called if the connection to the Play Store service is dropped. // This prevents the await or suspension point from hanging indefinitely. override fun onBillingServiceDisconnected() { if (continuation.isActive) { continuation.resume( BillingResult.newBuilder() .setResponseCode(BillingResponseCode.SERVICE_DISCONNECTED) .setDebugMessage("Billing service disconnected during connection setup") .build() ) } } }) }
Java
EnableBillingProgramParams params = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperProvidedBillingListener(developerProvidedBillingListener) .build(); BillingClient billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases( PendingPurchasesParams.newBuilder() .enableOneTimeProducts() .build() ) .enableBillingProgram(params) .build();Prüfe die Verfügbarkeit der folgenden Elemente:
- Von Google gerenderte Abrechnungsauswahl
- Externer Weblink
Rufen Sie
isBillingProgramAvailableAsyncauf, um die Verfügbarkeit des Programms zu prüfen, und rufen Sie dannqueryProductDetailsAsyncauf, um die verfügbaren Produkte anzuzeigen. Beispiel:Kotlin
// Check the availability of the billing choice program asynchronously using coroutines val (billingResult, billingProgramAvailabilityDetails) = billingClient.isBillingProgramAvailable(BillingProgram.BILLING_CHOICE) // Ensure the billing program query succeeded if (billingResult.responseCode == BillingResponseCode.OK) { // Retrieve the availability details specific to the billing choice program val billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.billingChoiceAvailabilityDetails // Check if billing choice is available, renders via Google Play, and external link is supported if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.choiceScreenType == ChoiceScreenType.GOOGLE_RENDERED && billingChoiceAvailabilityDetails.isExternalLinkAvailable ) { // Billing choice is available and external transaction links are supported. Query products and proceed. } else { // Fallback to other available programs. } } else { // Fallback to other available programs. }
Java
// ... billingClient.isBillingProgramAvailableAsync( BillingProgram.BILLING_CHOICE, (billingResult, billingProgramAvailabilityDetails) -> { if (billingResult.getResponseCode() == BillingResponseCode.OK) { BillingChoiceAvailabilityDetails billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.getBillingChoiceAvailabilityDetails(); if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.getChoiceScreenType() == ChoiceScreenType.GOOGLE_RENDERED && billingChoiceAvailabilityDetails.isExternalLinkAvailable()) { // Billing choice is available and external transaction links are supported. // Query products and proceed. } else { // Fallback to other available programs. } } else { // Fallback to other available programs. } } );Rufe
createBillingProgramReportingDetailsAsyncauf, um ein externes Transaktionstoken zu erstellen, wenn der Nutzer die Absicht zeigt, etwas zu kaufen. Beispiel:Kotlin
// Build the parameters for creating reporting details val params = BillingProgramReportingDetailsParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperBillingType(DeveloperBillingType.EXTERNAL_LINK) .build() // Call the suspend function to create billing program reporting details val (billingResult, billingProgramReportingDetails) = billingClient.createBillingProgramReportingDetails(params) // Handle response failure cases if (billingResult.responseCode != BillingResponseCode.OK) { // Handle failures such as retrying due to network errors. return } // Retrieve the external transaction token val transactionToken = billingProgramReportingDetails?.externalTransactionToken // Persist the external transaction token locally. Pass it to // DeveloperBillingOptionParams when launchBillingFlow is called. // It can also be used as part of your external website
Java
BillingProgramReportingDetailsParams params = BillingProgramReportingDetailsParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperBillingType(DeveloperBillingType.EXTERNAL_LINK) .build(); billingClient.createBillingProgramReportingDetailsAsync( params, new BillingProgramReportingDetailsListener() { @Override public void onCreateBillingProgramReportingDetailsResponse( BillingResult billingResult, @Nullable BillingProgramReportingDetails billingProgramReportingDetails ) { if (billingResult.getResponseCode() != BillingResponseCode.OK) { // Handle failures such as retrying due to network errors. return; } String transactionToken = billingProgramReportingDetails.getExternalTransactionToken(); // Persist the external transaction token locally. Pass it to // DeveloperBillingOptionParams when launchBillingFlow is called. // It can also be used as part of your external website. } } );Rufe
launchBillingFlowauf, um den Kaufvorgang auszulösen, wenn der Nutzer auf „Kaufen“ klickt. Wenn die Abrechnungsauswahl für den Nutzer verfügbar ist, führe die folgenden Schritte aus:- Übergeben Sie
DeveloperBillingOptionParamsanBillingFlowParams. - Übergib das Token für die externe Transaktion aus Schritt 3 an
DeveloperBillingOptionParams.
Beispiel:
Kotlin
// Build the developer billing option parameters with the external link URI, // the transaction token, and browser/app launch mode. val developerBillingOptionParams = DeveloperBillingOptionParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setLinkUri(Uri.parse("https://www.example.com/external/purchase")) .setExternalTransactionToken(transactionToken) .setLaunchMode( DeveloperBillingOptionParams.LaunchMode.LAUNCH_IN_EXTERNAL_BROWSER_OR_APP ) .build() val billingFlowParams = BillingFlowParams.newBuilder() .setProductDetailsParamsList(productDetailsParamsList) .enableDeveloperBillingOption(developerBillingOptionParams) .build() val billingResult = billingClient.launchBillingFlow(activity, billingFlowParams)
Java
DeveloperBillingOptionParams developerBillingOptionParams = DeveloperBillingOptionParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setLinkUri(Uri.parse("https://www.example.com/external/purchase")) .setExternalTransactionToken(transactionToken) .setLaunchMode( DeveloperBillingOptionParams.LaunchMode.LAUNCH_IN_EXTERNAL_BROWSER_OR_APP) .build();Hinweis: Die Jugendschutzeinstellungen werden für Nutzer mit Elternaufsicht angezeigt.
- Übergeben Sie
Gehen Sie bei der Auswahl des Abrechnungstyps durch den Nutzer so vor:
- Wenn der Nutzer Google Play Billing auswählt, rufe
launchBillingFlowgemäß der Standardanleitung für Google Play Billing auf. Das Abrechnungsergebnis wird an die in Schritt 1 registriertePurchasesUpdatedListenerzurückgegeben. - Wenn der Nutzer die alternative Abrechnung auswählt, wird das Abrechnungsergebnis an den
DeveloperProvidedBillingListenerzurückgegeben, der in Schritt- registriert wurde.
- Das zurückgegebene
DeveloperProvidedBillingDetailsenthält dasexternalTransactionToken, das in Schritt 4 anDeveloperBillingOptionParamsübergeben wurde, sofern es sich um ein gültiges Token handelt.
- Das zurückgegebene
- Wenn der Nutzer Google Play Billing auswählt, rufe
Szenario 2B verarbeiten
Der Entwickler rendert den Auswahlbildschirm und die alternative Abrechnung wird außerhalb der App abgewickelt. Gehen Sie so vor, um die Abrechnungsauswahl in diesem Szenario zu aktivieren:
Rufe
enableBillingProgramohneDeveloperProvidedBillingListenerinEnableBillingProgramParamsauf, wenn du eine BillingClient-Instanz erstellst, und starte dann die Verbindung. Beispiel:Kotlin
// Build the parameters to enable the Billing Choice program. val enableBillingProgramParams = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build() // Build the parameters to enable support for pending purchases. val pendingPurchasesParams = PendingPurchasesParams.newBuilder() .enableOneTimeProducts() .build() // Construct the BillingClient instance with the purchases updated listener, // pending purchases support, and the billing choice params. val billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases(pendingPurchasesParams) .enableBillingProgram(enableBillingProgramParams) .build() // Establish a connection to Google Play val billingResult = suspendCancellableCoroutine<BillingResult> { continuation -> billingClient.startConnection(object : BillingClientStateListener { // Called when the connection setup process completes. override fun onBillingSetupFinished(billingResult: BillingResult) { // Resume the coroutine and pass back the BillingResult to the caller. if (continuation.isActive) { continuation.resume(billingResult) } } // Called if the connection to the Play Store service is dropped. // This prevents the await or suspension point from hanging indefinitely. override fun onBillingServiceDisconnected() { if (continuation.isActive) { continuation.resume( BillingResult.newBuilder() .setResponseCode(BillingResponseCode.SERVICE_DISCONNECTED) .setDebugMessage("Billing service disconnected during connection setup") .build() ) } } }) }
Java
EnableBillingProgramParams params = EnableBillingProgramParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build(); BillingClient billingClient = BillingClient.newBuilder(context) .setListener(purchasesUpdatedListener) .enablePendingPurchases() .enableBillingProgram(params) .build();Prüfe die Verfügbarkeit der folgenden Elemente:
- Von Google gerenderte Abrechnungsauswahl
- Externer Weblink
Rufen Sie
isBillingProgramAvailableAsyncauf, um die Verfügbarkeit des Programms zu prüfen, und rufen Sie dannqueryProductDetailsAsyncauf, um die verfügbaren Produkte anzuzeigen. Beispiel:Kotlin
// Check the availability of the billing choice program asynchronously using a coroutine val (billingResult, billingProgramAvailabilityDetails) = billingClient.isBillingProgramAvailable(BillingProgram.BILLING_CHOICE) // Ensure the response code is OK if (billingResult.responseCode == BillingResponseCode.OK) { // Retrieve the billing choice availability details val billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.billingChoiceAvailabilityDetails // Check if billing choice details are available, choice screen is developer-rendered, // and external transaction links are supported. if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.choiceScreenType == ChoiceScreenType.DEVELOPER_RENDERED && billingChoiceAvailabilityDetails.isExternalLinkAvailable ) { // Billing choice is available and external transaction links are supported. // Query products and proceed. } else { // Fallback to other available programs. } } else { // Fallback to other available programs. }
Java
// ... billingClient.isBillingProgramAvailableAsync( BillingProgram.BILLING_CHOICE, (billingResult, billingProgramAvailabilityDetails) -> { if (billingResult.getResponseCode() == BillingResponseCode.OK) { BillingChoiceAvailabilityDetails billingChoiceAvailabilityDetails = billingProgramAvailabilityDetails.getBillingChoiceAvailabilityDetails(); if (billingChoiceAvailabilityDetails != null && billingChoiceAvailabilityDetails.getChoiceScreenType() == ChoiceScreenType.DEVELOPER_RENDERED && billingChoiceAvailabilityDetails.isExternalLinkAvailable()) { // Billing choice is available and external transaction links are supported. Query products and proceed. } else { // Fallback to other available programs. } } else { // Fallback to other available programs. } } );Rufe die Methode
getBillingChoiceInfoAsyncauf, um das Play Billing-Banner und die Treueinformationen abzurufen.Kotlin
// 1. Create the params required for the request val params = GetBillingChoiceInfoParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setPlayBillingChoiceImageLayout(GetBillingChoiceInfoParams.ImageLayout.RECTANGULAR_FOUR_BY_ONE) .build() // 2. Call the suspend method on your billingClient instance val (billingResult, playBillingChoiceInfo) = billingClient.getBillingChoiceInfo(params) if (billingResult.responseCode == BillingResponseCode.OK && playBillingChoiceInfo != null) { // Access the URL of the image associated with the Play Billing Choice val imageUrl = playBillingChoiceInfo.playBillingChoiceImageUrl // Access the Play Loyalty string information, if available val loyaltyInfo = playBillingChoiceInfo.playBillingLoyaltyInfo // Populate your developer-rendered UI elements playBillingLoyaltyTextView.text = loyaltyInfo loadImage(imageUrl, playBillingImageView) } else { // Handle error scenarios }
Java
// 1. Create the params required for the request GetBillingChoiceInfoParams params = GetBillingChoiceInfoParams.newBuilder() .setBillingProgram(BillingClient.BillingProgram.BILLING_CHOICE) .setPlayBillingChoiceImageLayout(GetBillingChoiceInfoParams.ImageLayout.RECTANGULAR_FOUR_BY_ONE) .build(); // 2. Call the method asynchronously on your billingClient instance billingClient.getBillingChoiceInfoAsync(params, (billingResult, playBillingChoiceInfo) -> { if (billingResult.getResponseCode() == BillingResponseCode.OK && playBillingChoiceInfo != null) { // Access the URL of the image associated with the Play Billing Choice String imageUrl = playBillingChoiceInfo.getPlayBillingChoiceImageUrl(); // Access the Play Loyalty string information, if available String loyaltyInfo = playBillingChoiceInfo.getPlayBillingLoyaltyInfo(); // Populate your developer-rendered UI elements playBillingLoyaltyTextView.setText(loyaltyInfo); loadImage(imageUrl, playBillingImageView); } else { // Handle error scenarios } });Rufe
createBillingProgramReportingDetailsAsyncauf, um ein externes Transaktionstoken zu erstellen, wenn der Nutzer die Absicht zeigt, etwas zu kaufen. Beispiel:Kotlin
// Build the parameters for creating reporting details val params = BillingProgramReportingDetailsParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperBillingType(DeveloperBillingType.EXTERNAL_LINK) .build() // Call the suspend function to create billing program reporting details val (billingResult, billingProgramReportingDetails) = billingClient.createBillingProgramReportingDetails(params) // Handle response failure cases if (billingResult.responseCode != BillingResponseCode.OK) { // Handle failures such as retrying due to network errors. return } // Retrieve the external transaction token val transactionToken = billingProgramReportingDetails?.externalTransactionToken // Persist the external transaction token locally. Pass it to // DeveloperBillingOptionParams when launchBillingFlow is called. // It can also be used as part of your external website
Java
BillingProgramReportingDetailsParams params = BillingProgramReportingDetailsParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperBillingType(DeveloperBillingType.EXTERNAL_LINK) .build(); billingClient.createBillingProgramReportingDetailsAsync( params, new BillingProgramReportingDetailsListener() { @Override public void onCreateBillingProgramReportingDetailsResponse( BillingResult billingResult, @Nullable BillingProgramReportingDetails billingProgramReportingDetails ) { if (billingResult.getResponseCode() != BillingResponseCode.OK) { // Handle failures such as retrying due to network errors. return; } String transactionToken = billingProgramReportingDetails.getExternalTransactionToken(); // Persist the external transaction token locally. Pass it to // DeveloperBillingOptionParams when launchBillingFlow is called. // It can also be used as part of your external website. } } );Starten Sie den Bildschirm für die alternative Auswahl, wenn der Nutzer auf „Kaufen“ klickt.
Gehen Sie bei der Auswahl des Abrechnungstyps durch den Nutzer so vor:
Wenn der Nutzer Google Play Billing auswählt, rufe
launchBillingFlowgemäß der Standardanleitung für Google Play Billing auf. Das Abrechnungsergebnis wird an die in Schritt 1 registriertePurchasesUpdatedListenerzurückgegeben.Die Jugendschutzeinstellungen werden für Nutzer mit Elternaufsicht angezeigt.
Wenn der Nutzer die alternative Abrechnung auswählt, rufen Sie „launchExternalLink“ auf. Beispiel:
Kotlin
// An activity reference from which the purchase flow will be launched. val params = LaunchExternalLinkParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setExternalTransactionToken(transactionToken) // You can pass along the external transaction token from // BillingProgramReportingDetails as a URL parameter in the URI .setLinkUri(yourLinkUri) .setLinkType(LaunchExternalLinkParams.LinkType.LINK_TO_DIGITAL_CONTENT_OFFER) .setLaunchMode( LaunchExternalLinkParams.LaunchMode.LAUNCH_IN_EXTERNAL_BROWSER_OR_APP ) .build() // Call launchExternalLink with a callback billingClient.launchExternalLink(activity, params) { billingResult -> if (billingResult.responseCode == BillingResponseCode.OK) { // Proceed with the rest of the purchase flow. If the user // purchases an item, be sure to report the transaction to Google // Play. } else { // Handle failures such as retrying due to network errors. } }
Java
// An activity reference from which the purchase flow will be launched. Activity activity = ...; LaunchExternalLinkParams params = LaunchExternalLinkParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) // You can pass along the external transaction token from // BillingProgramReportingDetails as a URL parameter in the URI .setLinkUri(yourLinkUri) .setLinkType(LaunchExternalLinkParams.LinkType.LINK_TO_DIGITAL_CONTENT_OFFER) .setLaunchMode( LaunchExternalLinkParams.LaunchMode.LAUNCH_IN_EXTERNAL_BROWSER_OR_APP) .setExternalTransactionToken(transactionToken) .build(); LaunchExternalLinkResponseListener listener = new LaunchExternalLinkResponseListener() { @Override public void onLaunchExternalLinkResponse(BillingResult billingResult) { if (billingResult.getResponseCode() == BillingResponseCode.OK) { // Proceed with the rest of the purchase flow. If the user // purchases an item, be sure to report the transaction to Google // Play. } else { // Handle failures such as retrying due to network errors. } } }; billingClient.launchExternalLink(activity, params, listener);Übergeben Sie das Token für die externe Transaktion aus Schritt 4 an
LaunchExternalLinkParams. Wenn OK zurückgegeben wird, fahre mit der Transaktion fort und melde die Transaktion an Google Play.Die Jugendschutzeinstellungen werden für Nutzer mit Elternaufsicht angezeigt.
Abrechnungsauswahl beim Ersetzen eines Abos
Beim Ersetzen eines Abos sollte der Bildschirm für die Nutzerauswahl nicht angezeigt werden, da die Nutzerauswahl für den ursprünglichen Kauf bei Upgrades und Downgrades beibehalten wird.
Wenn der ursprüngliche Kauf über Google Play Billing abgewickelt wurde, solltest du launchBillingFlow mit den Standardinformationen zum Ersetzen von Google Play Billing-Abos aufrufen.
Wenn der ursprüngliche Kauf jedoch über eine alternative Abrechnung abgewickelt wurde, unterscheidet sich die Handhabung von Abo-Ersetzungen je nach Szenario geringfügig.
Abo-Ersatz in Szenario 1A
Nutzer, die ein Upgrade oder Downgrade anfordern, sollten das alternative Abrechnungssystem des Entwicklers verwenden, ohne die Nutzerauswahl noch einmal durchlaufen zu müssen.
Rufe dazu launchBillingFlow auf, wenn der Nutzer ein Upgrade oder Downgrade anfordert. Verwenden Sie setOriginalExternalTransactionId im Objekt SubscriptionUpdateParams in den Parametern, um die externe Transaktions-ID für den ursprünglichen Kauf anzugeben. Der Bildschirm für die Nutzerauswahl wird nicht angezeigt, da die Nutzerauswahl für den ursprünglichen Kauf bei Upgrades und Downgrades beibehalten wird. Der Aufruf von launchBillingFlow generiert in diesem Fall ein neues Token für die externe Transaktion, das Sie über den Callback abrufen können.
Kotlin
// The external transaction ID from the current // alternative billing subscription. val externalTransactionId = "external_transaction_id" val developerBillingOptionParams = DeveloperBillingOptionParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build() val billingFlowParams = BillingFlowParams.newBuilder() .setProductDetailsParamsList( listOf( BillingFlowParams.ProductDetailsParams.newBuilder() // Fetched using queryProductDetailsAsync. .setProductDetails(productDetailsNewPlan) // offerIdToken can be found in // ProductDetails=>SubscriptionOfferDetails. .setOfferToken(offerTokenNewPlan) .build() ) ) .setSubscriptionUpdateParams( SubscriptionUpdateParams.newBuilder() .setOriginalExternalTransactionId(externalTransactionId) .build() ) .enableDeveloperBillingOption(developerBillingOptionParams) .build() val billingResult = billingClient.launchBillingFlow(activity, billingFlowParams) // When the user selects the alternative billing flow, // the DeveloperProvidedBillingListener is triggered.
Java
// The external transaction ID from the current
// alternative billing subscription.
String externalTransactionId = //... ;
DeveloperBillingOptionParams developerBillingOptionParams =
DeveloperBillingOptionParams.newBuilder()
.setBillingProgram(BillingProgram.BILLING_CHOICE)
.build();
List<ProductDetailsParams> productDetailsParamsList = new ArrayList<>();
productDetailsParamsList.add(
ProductDetailsParams.newBuilder()
// Fetched using queryProductDetailsAsync.
.setProductDetails(productDetailsNewPlan)
// offerIdToken can be found in
// ProductDetails=>SubscriptionOfferDetails
.setOfferToken(offerTokenNewPlan)
.build());
BillingFlowParams billingFlowParams =
BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.setSubscriptionUpdateParams(
SubscriptionUpdateParams.newBuilder()
.setOriginalExternalTransactionId(externalTransactionId)
.build())
.enableDeveloperBillingOption(developerBillingOptionParams)
.build();
BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);
// When the user selects the alternative billing flow,
// the DeveloperProvidedBillingListener is triggered.
Wenn das Upgrade oder Downgrade abgeschlossen ist, musst du eine neue Transaktion melden. Verwende dazu das Token für externe Transaktionen, das du beim vorherigen Aufruf für den neuen Abokauf erhalten hast.
Aboersatz in Szenario 1B
In diesem Szenario muss ein neues externes Transaktionstoken generiert werden. Der einzige Unterschied zu einem normalen Kauf besteht darin, dass in diesem Szenario die Nutzerauswahl beibehalten wird und du den Auswahlbildschirm für das Upgrade oder Downgrade nicht anzeigen musst. Du musst jedoch das einmalige Informationsdialogfeld und die Bestätigung der Eltern anzeigen.
Beispielcode für die Integration finden Sie in Schritt 4 von Szenario 1B: Der Entwickler rendert den Auswahlbildschirm und die alternative Abrechnung wird in Ihrer App abgewickelt.
Wenn das Upgrade oder Downgrade abgeschlossen ist, musst du eine neue Transaktion melden. Verwende dazu das Token für externe Transaktionen, das du beim vorherigen Aufruf für den neuen Abokauf erhalten hast.
Abo-Ersatz in Szenario 2A
Bei Abos, die ursprünglich über die Website des Entwicklers oder eine Zahlungs-App nach der Nutzerauswahl gekauft wurden, sollten Nutzer, die ein Upgrade oder Downgrade beantragen, die Website des Entwicklers oder eine Zahlungs-App verwenden, ohne die Nutzerauswahl noch einmal durchlaufen zu müssen.
Rufe dazu launchBillingFlow auf, wenn der Nutzer ein Upgrade oder Downgrade anfordert. Anstatt andere Parameter im Objekt SubscriptionUpdateParams anzugeben, verwenden Sie setOriginalExternalTransactionId und geben Sie die ID der externen Transaktion für den ursprünglichen Kauf an.
DeveloperBillingOptionParams muss ebenfalls in diesem Aufruf angegeben werden. Der Bildschirm für die Nutzerauswahl wird nicht angezeigt, da die Nutzerauswahl für den ursprünglichen Kauf für Upgrades und Downgrades beibehalten wird. Beispiel:
Kotlin
val externalTransactionId = "external_transaction_id" // 1. Construct DeveloperBillingOptionParams indicating the billing program val developerBillingOptionParams = DeveloperBillingOptionParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .build() // 2. Build BillingFlowParams combining DeveloperBillingOptionParams and SubscriptionUpdateParams val billingFlowParams = BillingFlowParams.newBuilder() .setProductDetailsParamsList( listOf( BillingFlowParams.ProductDetailsParams.newBuilder() // Fetched using queryProductDetailsAsync. .setProductDetails(productDetailsNewPlan) // offerIdToken can be found in ProductDetails=>SubscriptionOfferDetails. .setOfferToken(offerTokenNewPlan) .build() ) ) .setSubscriptionUpdateParams( SubscriptionUpdateParams.newBuilder() .setOriginalExternalTransactionId(externalTransactionId) .build() ) .enableDeveloperBillingOption(developerBillingOptionParams) .build()
Java
String externalTransactionId = //... ;
// 1. Construct DeveloperBillingOptionParams indicating the billing program
DeveloperBillingOptionParams developerBillingOptionParams =
DeveloperBillingOptionParams.newBuilder()
.setBillingProgram(BillingClient.BillingProgram.BILLING_CHOICE)
.build();
// 2. Add ProductDetailsParams
List productDetailsParamsList = new ArrayList<>();
productDetailsParamsList.add(
ProductDetailsParams.newBuilder()
// Fetched using queryProductDetailsAsync.
.setProductDetails(productDetailsNewPlan)
// offerIdToken can be found in ProductDetails=>SubscriptionOfferDetails
.setOfferToken(offerTokenNewPlan)
.build());
// 3. Build BillingFlowParams combining DeveloperBillingOptionParams and SubscriptionUpdateParams
BillingFlowParams billingFlowParams =
BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.setSubscriptionUpdateParams(
SubscriptionUpdateParams.newBuilder()
.setOriginalExternalTransactionId(externalTransactionId)
.build())
.enableDeveloperBillingOption(developerBillingOptionParams)
.build();
Außerdem müssen Sie ein neues Token für externe Transaktionen generieren. Beispiel:
Kotlin
val params = BillingProgramReportingDetailsParams.newBuilder() .setBillingProgram(BillingProgram.BILLING_CHOICE) .setDeveloperBillingType(DeveloperBillingType.EXTERNAL_LINK) .build() val (billingResult, billingProgramReportingDetails) = billingClient.createBillingProgramReportingDetails(params) if (billingResult.responseCode != BillingResponseCode.OK) { // Handle failures such as retrying due to network errors. return } val externalTransactionToken = billingProgramReportingDetails?.externalTransactionToken // Persist the external transaction token locally. Pass it to // the external website using DeveloperBillingOptionParams when // launchBillingFlow is called.
Java
BillingProgramReportingDetailsParams params =
BillingProgramReportingDetailsParams.newBuilder()
.setBillingProgram(BillingProgram.BILLING_CHOICE)
.setDeveloperBillingType(DeveloperBillingType.EXTERNAL_LINK)
.build();
billingClient.createBillingProgramReportingDetailsAsync(
params,
new BillingProgramReportingDetailsListener() {
@Override
public void onCreateBillingProgramReportingDetailsResponse(
BillingResult billingResult,
@Nullable BillingProgramReportingDetails billingProgramReportingDetails) {
if (billingResult.getResponseCode() != BillingResponseCode.OK) {
// Handle failures such as retrying due to network errors.
return;
}
String transactionToken =
billingProgramReportingDetails.getExternalTransactionToken();
// Persist the external transaction token locally. Pass it to
// the external website using DeveloperBillingOptionParams when
// launchBillingFlow is called.
}
});
Nachdem Sie das neue Token generiert haben, müssen Sie die Methode launchBillingFlow aufrufen, um den Kaufvorgang zu starten.
Wenn das Upgrade oder Downgrade abgeschlossen ist, musst du eine neue Transaktion melden. Verwende dazu das externe Transaktionstoken, das du beim vorherigen Aufruf für den neuen Abokauf erhalten hast.
Aboersatz in Szenario 2B
Die Schritte zum Ersetzen des Abos in diesem Szenario ähneln den Schritten, die unter Abo-Ersatz in Szenario 2A beschrieben sind. Der einzige Unterschied besteht darin, dass Sie nach dem Generieren des Transaktionstokens nicht die Methode launchBillingFlow, sondern „launchExternalLink“ aufrufen müssen, um das Dialogfeld mit dem Haftungsausschluss für den Linkout anzuzeigen. In diesem Szenario bleibt die Nutzerauswahl erhalten und Sie müssen den Auswahlbildschirm für das Upgrade oder Downgrade nicht anzeigen.
Beispielcode für die Integration finden Sie in Schritt 6 in Szenario 2B: Der Entwickler rendert den Auswahlbildschirm und die alternative Abrechnung wird in Ihrer App abgewickelt.
Wenn das Upgrade oder Downgrade abgeschlossen ist, musst du eine neue Transaktion melden. Verwende dazu das Token für externe Transaktionen, das du beim vorherigen Aufruf für den neuen Abokauf erhalten hast.