@@ -54,14 +54,6 @@ private class Integration(
5454 private val purchasesUpdatedListener : PurchasesUpdatedListener
5555) {
5656
57- private lateinit var productDetailsParamsList: List <BillingFlowParams .ProductDetailsParams >
58- private lateinit var playBillingLoyaltyTextView: TextView
59- private lateinit var playBillingImageView: ImageView
60- private lateinit var yourLinkUri: Uri
61- private lateinit var transactionToken: String
62- private lateinit var productDetailsNewPlan: ProductDetails
63- private lateinit var offerTokenNewPlan: String
64-
6557 private fun loadImage (url : String , imageView : ImageView ) {}
6658
6759 // Scenario 1A: Google-rendered, In-app
@@ -93,18 +85,22 @@ private class Integration(
9385 // Called when the connection setup process completes.
9486 override fun onBillingSetupFinished (billingResult : BillingResult ) {
9587 // Resume the coroutine and pass back the BillingResult to the caller.
96- continuation.resume(billingResult)
88+ if (continuation.isActive) {
89+ continuation.resume(billingResult)
90+ }
9791 }
9892
9993 // Called if the connection to the Play Store service is dropped.
10094 // This prevents the await or suspension point from hanging indefinitely.
10195 override fun onBillingServiceDisconnected () {
102- continuation.resume(
103- BillingResult .newBuilder()
104- .setResponseCode(BillingClient .BillingResponseCode .SERVICE_DISCONNECTED )
105- .setDebugMessage(" Billing service disconnected during connection setup" )
106- .build()
107- )
96+ if (continuation.isActive) {
97+ continuation.resume(
98+ BillingResult .newBuilder()
99+ .setResponseCode(BillingClient .BillingResponseCode .SERVICE_DISCONNECTED )
100+ .setDebugMessage(" Billing service disconnected during connection setup" )
101+ .build()
102+ )
103+ }
108104 }
109105 })
110106 }
@@ -131,7 +127,9 @@ private class Integration(
131127 // [END android_playbilling_billingchoice_verify_google_rendered_in_app]
132128 }
133129
134- fun launchBillingFlowGoogleRenderedInApp () {
130+ fun launchBillingFlowGoogleRenderedInApp (
131+ productDetailsParamsList : List <BillingFlowParams .ProductDetailsParams >
132+ ) {
135133 // [START android_playbilling_billingchoice_launch_google_rendered_in_app]
136134 val developerBillingOptionParams = DeveloperBillingOptionParams .newBuilder()
137135 .setBillingProgram(BillingClient .BillingProgram .BILLING_CHOICE )
@@ -154,11 +152,16 @@ private class Integration(
154152 .setBillingProgram(BillingClient .BillingProgram .BILLING_CHOICE )
155153 .build()
156154
155+ // Build the parameters to enable support for pending purchases.
156+ val pendingPurchasesParams = PendingPurchasesParams .newBuilder()
157+ .enableOneTimeProducts()
158+ .build()
159+
157160 // Construct the BillingClient instance with the purchases updated listener,
158161 // pending purchases support, and the billing choice params.
159162 val billingClient = BillingClient .newBuilder(context)
160163 .setListener(purchasesUpdatedListener)
161- .enablePendingPurchases(PendingPurchasesParams .newBuilder().enableOneTimeProducts().build() )
164+ .enablePendingPurchases(pendingPurchasesParams )
162165 .enableBillingProgram(params)
163166 .build()
164167
@@ -168,18 +171,22 @@ private class Integration(
168171 // Called when the connection setup process completes.
169172 override fun onBillingSetupFinished (billingResult : BillingResult ) {
170173 // Resume the coroutine and pass back the BillingResult to the caller.
171- continuation.resume(billingResult)
174+ if (continuation.isActive) {
175+ continuation.resume(billingResult)
176+ }
172177 }
173178
174179 // Called if the connection to the Play Store service is dropped.
175180 // This prevents the await or suspension point from hanging indefinitely.
176181 override fun onBillingServiceDisconnected () {
177- continuation.resume(
178- BillingResult .newBuilder()
179- .setResponseCode(BillingClient .BillingResponseCode .SERVICE_DISCONNECTED )
180- .setDebugMessage(" Billing service disconnected during connection setup" )
181- .build()
182- )
182+ if (continuation.isActive) {
183+ continuation.resume(
184+ BillingResult .newBuilder()
185+ .setResponseCode(BillingClient .BillingResponseCode .SERVICE_DISCONNECTED )
186+ .setDebugMessage(" Billing service disconnected during connection setup" )
187+ .build()
188+ )
189+ }
183190 }
184191 })
185192 }
@@ -211,7 +218,10 @@ private class Integration(
211218 // [END android_playbilling_billingchoice_verify_developer_rendered_in_app]
212219 }
213220
214- suspend fun getBillingChoiceInfoDeveloperRenderedInApp () {
221+ suspend fun getBillingChoiceInfoDeveloperRenderedInApp (
222+ playBillingLoyaltyTextView : TextView ,
223+ playBillingImageView : ImageView
224+ ) {
215225 // [START android_playbilling_billingchoice_info_developer_rendered_in_app]
216226 // 1. Create the params required for the request
217227 val params = GetBillingChoiceInfoParams .newBuilder()
@@ -321,7 +331,7 @@ private class Integration(
321331 // [END android_playbilling_billingchoice_create_token_google_rendered_external_link]
322332 }
323333
324- fun launchBillingFlowGoogleRenderedExternalLink () {
334+ fun launchBillingFlowGoogleRenderedExternalLink (transactionToken : String ) {
325335 // [START android_playbilling_billingchoice_launch_google_rendered_external_link]
326336 // Build the developer billing option parameters with the external link URI,
327337 // the transaction token, and browser/app launch mode.
@@ -367,7 +377,7 @@ private class Integration(
367377 // [END android_playbilling_billingchoice_verify_developer_rendered_external_link]
368378 }
369379
370- fun launchExternalLinkDeveloperRenderedExternalLink () {
380+ fun launchExternalLinkDeveloperRenderedExternalLink (yourLinkUri : Uri ) {
371381 // [START android_playbilling_billingchoice_launch_external_developer_rendered_external_link]
372382 // An activity reference from which the purchase flow will be launched.
373383 val activity: Activity = activity
@@ -397,7 +407,11 @@ private class Integration(
397407 }
398408
399409 // Subscription Replacement
400- fun subsReplacementGoogleRenderedInApp (externalTransactionId : String ) {
410+ fun subsReplacementGoogleRenderedInApp (
411+ externalTransactionId : String ,
412+ productDetailsNewPlan : ProductDetails ,
413+ offerTokenNewPlan : String
414+ ) {
401415 // [START android_playbilling_billingchoice_subs_replace_google_rendered_in_app]
402416 // The external transaction ID from the current
403417 // alternative billing subscription.
@@ -434,7 +448,11 @@ private class Integration(
434448 // [END android_playbilling_billingchoice_subs_replace_google_rendered_in_app]
435449 }
436450
437- fun subsReplacementGoogleRenderedExternalLinkSample1 (externalTransactionId : String ) {
451+ fun subsReplacementGoogleRenderedExternalLinkSample1 (
452+ externalTransactionId : String ,
453+ productDetailsNewPlan : ProductDetails ,
454+ offerTokenNewPlan : String
455+ ) {
438456 // [START android_playbilling_billingchoice_subs_replace_google_rendered_external_link_sample1]
439457 val externalTransactionId = externalTransactionId
440458
@@ -473,25 +491,17 @@ private class Integration(
473491 .setDeveloperBillingType(DeveloperBillingType .EXTERNAL_LINK )
474492 .build()
475493
476- billingClient.createBillingProgramReportingDetailsAsync(
477- params,
478- object : BillingProgramReportingDetailsListener {
479- override fun onCreateBillingProgramReportingDetailsResponse (
480- billingResult : BillingResult ,
481- billingProgramReportingDetails : BillingProgramReportingDetails ?
482- ) {
483- if (billingResult.responseCode != BillingResponseCode .OK ) {
484- // Handle failures such as retrying due to network errors.
485- return
486- }
487- val externalTransactionToken =
488- billingProgramReportingDetails?.externalTransactionToken
489- // Persist the external transaction token locally. Pass it to
490- // the external website using DeveloperBillingOptionParams when
491- // launchBillingFlow is called.
492- }
494+ billingClient.createBillingProgramReportingDetailsAsync(params) { billingResult, billingProgramReportingDetails ->
495+ if (billingResult.responseCode != BillingResponseCode .OK ) {
496+ // Handle failures such as retrying due to network errors.
497+ return @createBillingProgramReportingDetailsAsync
493498 }
494- )
499+ val externalTransactionToken =
500+ billingProgramReportingDetails?.externalTransactionToken
501+ // Persist the external transaction token locally. Pass it to
502+ // the external website using DeveloperBillingOptionParams when
503+ // launchBillingFlow is called.
504+ }
495505 // [END android_playbilling_billingchoice_subs_replace_google_rendered_external_link_sample2]
496506 }
497507}
0 commit comments