คู่มือนี้อธิบายวิธีผสานรวมแอปกับ API ของ Play Billing Library เพื่อให้คุณเสนอตัวเลือกการเรียกเก็บเงินแก่ผู้ใช้ได้
การผสานรวมกับ PBL
คุณสามารถผสานรวมตัวเลือกการเรียกเก็บเงินกับ PBL ได้ใน 4 สถานการณ์ สถานการณ์จะแตกต่างกันไปตามผู้ที่แสดงผลหน้าจอตัวเลือกและสถานที่ที่จะชำระเงิน ตารางต่อไปนี้แสดงสถานการณ์การผสานรวม
| คุณต้องการแสดงหน้าจอตัวเลือกการเรียกเก็บเงินใด | |||
| Google Play | ของคุณเอง (ตามหลักเกณฑ์ UX) | ||
| การชำระเงินเกิดขึ้นที่ใด | ในแอป | สถานการณ์ 1A
Google จะแสดงหน้าจอตัวเลือกและการเรียกเก็บเงินระบบอื่นจะได้รับการจัดการภายในแอปของคุณ |
สถานการณ์ 1B
นักพัฒนาแอปจะแสดงหน้าจอตัวเลือกและการเรียกเก็บเงินระบบอื่นจะได้รับการจัดการภายในแอปของคุณ |
| เว็บลิงก์ภายนอก | สถานการณ์ 2ก
Google จะแสดงหน้าจอตัวเลือกและลิงก์ผู้ใช้ออกนอกแอปไปยังเว็บไซต์ของคุณเองเพื่อทำการซื้อ |
สถานการณ์ 2B
นักพัฒนาแอปแสดงหน้าจอตัวเลือกและผู้ใช้จะลิงก์ออกไปนอกแอปไปยังเว็บไซต์ของคุณเองเพื่อทำการซื้อ |
|
ภาพต่อไปนี้จะอธิบายขั้นตอนการเลือกการเรียกเก็บเงินสำหรับแต่ละสถานการณ์
สถานการณ์การผสานรวม PBL
ทำตามขั้นตอนในส่วนนี้เพื่อใช้ตัวเลือกการเรียกเก็บเงินในแอป ทั้งนี้ขึ้นอยู่กับสถานการณ์การผสานรวมของคุณ
จัดการสถานการณ์ 1A
Google จะแสดงหน้าจอตัวเลือกและการเรียกเก็บเงินระบบอื่นจะได้รับการจัดการภายในแอปของคุณ ทำตามขั้นตอนต่อไปนี้เพื่อเปิดใช้ตัวเลือกการเรียกเก็บเงินในสถานการณ์นี้
เรียกใช้
enableBillingProgramด้วยEnableBillingProgramParamsเมื่อสร้างอินสแตนซ์ BillingClient แล้วเริ่มการเชื่อมต่อ เช่น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();ตรวจสอบว่าตัวเลือกการเรียกเก็บเงินที่แสดงผลโดย Google พร้อมให้บริการแก่ผู้ใช้
โทรหา
isBillingProgramAvailableAsyncเพื่อตรวจสอบความพร้อมให้บริการของโปรแกรม แล้วโทรหาqueryProductDetailsAsyncเพื่อแสดงผลิตภัณฑ์ที่พร้อมให้บริการ เช่น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. } } );หมายเหตุ:
billingProgramAvailabilityDetailsจะบอกให้คุณทราบว่าหน้าจอตัวเลือกการเรียกเก็บเงินที่แสดงผลโดย Google หรือที่แสดงผลโดยนักพัฒนาแอปพร้อมใช้งานหรือไม่เรียกใช้
launchBillingFlowเพื่อทริกเกอร์ขั้นตอนการซื้อเมื่อผู้ใช้คลิกซื้อ หากตัวเลือกการเรียกเก็บเงินพร้อมใช้งาน ให้ส่งDeveloperBillingOptionParamsไปยังBillingFlowParamsเช่น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);หมายเหตุ: การควบคุมโดยผู้ปกครองจะแสดงสำหรับผู้ใช้ภายใต้การควบคุมดูแล
จัดการการเลือกประเภทการเรียกเก็บเงินของผู้ใช้ดังนี้
- หากผู้ใช้เลือก Google Play Billing ระบบจะแสดงผลการเรียกเก็บเงินไปยัง
PurchasesUpdatedListenerที่ลงทะเบียนไว้ในขั้นตอนที่ 1 - หากผู้ใช้เลือกการเรียกเก็บเงินระบบอื่นของคุณ ระบบจะแสดงผลลัพธ์การเรียกเก็บเงินไปยัง
DeveloperProvidedBillingListenerที่ลงทะเบียนไว้ในขั้นตอนที่ 1DeveloperProvidedBillingDetailsที่แสดงผลจะมีexternalTransactionTokenในกรณีนี้ ระบบจะใช้โทเค็นสำหรับการรายงานธุรกรรม
- หากผู้ใช้เลือก Google Play Billing ระบบจะแสดงผลการเรียกเก็บเงินไปยัง
จัดการสถานการณ์ 1B
นักพัฒนาแอปแสดงหน้าจอตัวเลือกและจัดการการเรียกเก็บเงินสำรองภายในแอปของคุณ ทำตามขั้นตอนต่อไปนี้เพื่อเปิดใช้ตัวเลือกการเรียกเก็บเงินในสถานการณ์นี้
เรียก
enableBillingProgramโดยไม่มีDeveloperProvidedBillingListenerในEnableBillingProgramParamsเมื่อสร้างอินสแตนซ์BillingClientแล้วเริ่มการเชื่อมต่อ เช่น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();ตรวจสอบว่าตัวเลือกการเรียกเก็บเงินที่แสดงโดยนักพัฒนาแอปพร้อมให้บริการแก่ผู้ใช้
โทรหา
isBillingProgramAvailableAsyncเพื่อตรวจสอบความพร้อมให้บริการของโปรแกรม แล้วโทรหาqueryProductDetailsAsyncเพื่อแสดงผลิตภัณฑ์ที่พร้อมให้บริการ เช่น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. } } );หมายเหตุ:
billingProgramAvailabilityDetailsจะบอกให้คุณทราบว่าหน้าจอตัวเลือกการเรียกเก็บเงินที่แสดงผลโดย Google หรือที่แสดงผลโดยนักพัฒนาแอปพร้อมใช้งานหรือไม่เรียกใช้เมธอด
getBillingChoiceInfoAsyncเพื่อรับแบนเนอร์ Play Billing และข้อมูลความภักดี เช่น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 } });สร้างโทเค็นธุรกรรมภายนอกโดยตั้งค่า DeveloperBillingType เป็น IN_APP เช่น
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 } } );เมื่อผู้ใช้คลิกซื้อ ให้เรียกใช้
showBillingProgramInformationDialogเพื่อแสดงกล่องโต้ตอบข้อมูล ตัวอย่างเช่น ดูกล่องโต้ตอบข้อมูลสำหรับผู้ใช้ ต้องตั้งค่า BillingProgram และ transactionToken จากขั้นตอนที่ 4 ในคำขอหมายเหตุ: การควบคุมโดยผู้ปกครองจะแสดงสำหรับผู้ใช้ภายใต้การควบคุมดูแล
เปิดหน้าจอตัวเลือกการเรียกเก็บเงินระบบอื่นหากผลลัพธ์จากขั้นตอนก่อนหน้าคือ
OKจัดการการเลือกประเภทการเรียกเก็บเงินของผู้ใช้ดังนี้
- หากผู้ใช้เลือก Play Billing ให้เรียกใช้
launchBillingFlowตามคำแนะนำมาตรฐานของ Play Billing ระบบจะส่งผลลัพธ์การเรียกเก็บเงินกลับไปยังPurchasesUpdatedListenerที่ลงทะเบียนไว้ในขั้นตอนที่ 1 - หากผู้ใช้เลือกการเรียกเก็บเงินระบบอื่น คุณต้องจัดการธุรกรรมด้วยตนเองและรายงานธุรกรรมดังกล่าวไปยัง Play โดยใช้โทเค็นที่สร้างขึ้นในขั้นตอนที่ 4
- หากผู้ใช้เลือก Play Billing ให้เรียกใช้
จัดการสถานการณ์ 2A
Google จะแสดงหน้าจอตัวเลือกและการเรียกเก็บเงินระบบอื่นจะได้รับการจัดการภายนอกแอปของคุณ ทำตามขั้นตอนต่อไปนี้เพื่อเปิดใช้ตัวเลือกการเรียกเก็บเงินในสถานการณ์นี้
เรียกใช้
enableBillingProgramด้วยEnableBillingProgramParamsเมื่อสร้างอินสแตนซ์ BillingClient แล้วเริ่มการเชื่อมต่อ เช่น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();ตรวจสอบความพร้อมใช้งานของสิ่งต่อไปนี้
- ตัวเลือกการเรียกเก็บเงินที่แสดงผลโดย Google
- เว็บลิงก์ภายนอก
โทรหา
isBillingProgramAvailableAsyncเพื่อตรวจสอบความพร้อมให้บริการของโปรแกรม แล้วโทรหาqueryProductDetailsAsyncเพื่อแสดงผลิตภัณฑ์ที่พร้อมให้บริการ เช่น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. } } );เรียกใช้
createBillingProgramReportingDetailsAsyncเพื่อสร้างโทเค็นธุรกรรมภายนอกเมื่อผู้ใช้แสดงเจตนาที่จะซื้อ เช่น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. } } );เรียกใช้
launchBillingFlowเพื่อทริกเกอร์ขั้นตอนการซื้อเมื่อผู้ใช้คลิกซื้อ หากผู้ใช้มีตัวเลือกการเรียกเก็บเงิน ให้ดำเนินการดังต่อไปนี้- ส่ง
DeveloperBillingOptionParamsไปยังBillingFlowParams - ส่งโทเค็นธุรกรรมภายนอกจากขั้นตอนที่ 3 ไปยัง
DeveloperBillingOptionParams
เช่น
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();หมายเหตุ: การควบคุมโดยผู้ปกครองจะแสดงสำหรับผู้ใช้ภายใต้การควบคุมดูแล
- ส่ง
จัดการการเลือกประเภทการเรียกเก็บเงินของผู้ใช้ดังนี้
- หากผู้ใช้เลือก Play Billing ให้เรียกใช้
launchBillingFlowตามคำแนะนำมาตรฐานของ Play Billing ระบบจะส่งผลลัพธ์การเรียกเก็บเงินกลับไปยังPurchasesUpdatedListenerที่ลงทะเบียนไว้ในขั้นตอนที่ 1 - หากผู้ใช้เลือกการเรียกเก็บเงินระบบอื่น ระบบจะแสดงผลลัพธ์การเรียกเก็บเงินไปยัง
DeveloperProvidedBillingListenerที่ลงทะเบียนไว้ในขั้นตอนที่DeveloperProvidedBillingDetailsที่แสดงผลจะมีexternalTransactionTokenที่ส่งไปยังDeveloperBillingOptionParamsในขั้นตอนที่ 4 หากเป็นโทเค็นที่ถูกต้อง
- หากผู้ใช้เลือก Play Billing ให้เรียกใช้
จัดการสถานการณ์ 2B
นักพัฒนาแอปแสดงหน้าจอตัวเลือกและการเรียกเก็บเงินทางเลือกจะได้รับการจัดการภายนอกแอป ทำตามขั้นตอนต่อไปนี้เพื่อเปิดใช้ตัวเลือกการเรียกเก็บเงินในสถานการณ์นี้
เรียกใช้
enableBillingProgramโดยไม่มีDeveloperProvidedBillingListenerในEnableBillingProgramParamsเมื่อสร้างอินสแตนซ์ BillingClient แล้วเริ่มการเชื่อมต่อ เช่น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();ตรวจสอบความพร้อมใช้งานของสิ่งต่อไปนี้
- ตัวเลือกการเรียกเก็บเงินที่แสดงผลโดย Google
- เว็บลิงก์ภายนอก
โทรหา
isBillingProgramAvailableAsyncเพื่อตรวจสอบความพร้อมให้บริการของโปรแกรม แล้วโทรหาqueryProductDetailsAsyncเพื่อแสดงผลิตภัณฑ์ที่พร้อมให้บริการ เช่น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. } } );เรียกใช้เมธอด
getBillingChoiceInfoAsyncเพื่อรับแบนเนอร์ Play Billing และข้อมูลความภักดี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 } });เรียกใช้
createBillingProgramReportingDetailsAsyncเพื่อสร้างโทเค็นธุรกรรมภายนอกเมื่อผู้ใช้แสดงเจตนาที่จะซื้อ เช่น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. } } );เปิดหน้าจอตัวเลือกทางเลือกเมื่อผู้ใช้คลิก "ซื้อ"
จัดการการเลือกประเภทการเรียกเก็บเงินของผู้ใช้ดังนี้
หากผู้ใช้เลือก Play Billing ให้เรียกใช้
launchBillingFlowตามคำแนะนำมาตรฐานของ Play Billing ระบบจะส่งผลลัพธ์การเรียกเก็บเงินกลับไปยังPurchasesUpdatedListenerที่ลงทะเบียนไว้ในขั้นตอนที่ 1การควบคุมโดยผู้ปกครองจะแสดงสำหรับผู้ใช้ภายใต้การควบคุมดูแล
หากผู้ใช้เลือกการเรียกเก็บเงินระบบอื่น ให้เรียกใช้ launchExternalLink เช่น
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);ส่งโทเค็นธุรกรรมภายนอกจากขั้นตอนที่ 4 ไปยัง
LaunchExternalLinkParamsหากแสดงผลเป็น OK ให้ดำเนินการธุรกรรมต่อและรายงานธุรกรรมไปยัง Google Playการควบคุมโดยผู้ปกครองจะแสดงสำหรับผู้ใช้ภายใต้การควบคุมดูแล
ตัวเลือกการเรียกเก็บเงินระหว่างการเปลี่ยนการสมัครใช้บริการ
สำหรับการแทนที่การสมัครใช้บริการ ระบบไม่ควรแสดงหน้าจอตัวเลือกของผู้ใช้เนื่องจากระบบจะเก็บตัวเลือกของผู้ใช้สำหรับการซื้อครั้งแรกไว้สำหรับการอัปเกรดและการดาวน์เกรด
หากการซื้อครั้งแรกดำเนินการผ่าน Google Play Billing คุณควรเรียกใช้ launchBillingFlow พร้อมข้อมูลการเปลี่ยนการสมัครใช้บริการ Google Play Billing มาตรฐาน
อย่างไรก็ตาม หากการซื้อครั้งแรกประมวลผลผ่านการเรียกเก็บเงินระบบอื่น การจัดการการแทนที่การสมัครใช้บริการจะแตกต่างกันเล็กน้อยตามสถานการณ์
การแทนที่การสมัครใช้บริการในสถานการณ์ที่ 1ก
ผู้ใช้ที่ขออัปเกรดหรือดาวน์เกรดควรดำเนินการผ่านระบบการเรียกเก็บเงินระบบอื่นของนักพัฒนาแอปโดยไม่ต้องผ่านตัวเลือกของผู้ใช้อีกครั้ง
โดยให้เรียกใช้ launchBillingFlow เมื่อผู้ใช้ขออัปเกรดหรือดาวน์เกรด ใช้ setOriginalExternalTransactionId ภายในออบเจ็กต์ SubscriptionUpdateParams ในพารามิเตอร์เพื่อระบุรหัสธุรกรรมภายนอกสําหรับการซื้อครั้งแรก ซึ่งจะไม่แสดงหน้าจอตัวเลือกของผู้ใช้ เนื่องจากระบบจะเก็บตัวเลือกของผู้ใช้สำหรับการซื้อครั้งแรกไว้สำหรับการอัปเกรดและดาวน์เกรด การเรียกใช้ launchBillingFlow ในกรณีนี้จะสร้างโทเค็นธุรกรรมภายนอกใหม่สำหรับธุรกรรมที่คุณสามารถดึงข้อมูลจาก Callback ได้
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.
เมื่อการอัปเกรดหรือดาวน์เกรดเสร็จสมบูรณ์ คุณต้องรายงานธุรกรรมใหม่โดยใช้โทเค็นธุรกรรมภายนอกที่ได้รับจากการเรียกใช้ก่อนหน้าสำหรับการซื้อการสมัครใช้บริการใหม่
การแทนที่การสมัครใช้บริการในสถานการณ์ที่ 1ข
ในสถานการณ์นี้ คุณต้องสร้างโทเค็นธุรกรรมภายนอกใหม่ ความแตกต่างเพียงอย่างเดียวจากการซื้อปกติคือในสถานการณ์นี้ ระบบจะคงตัวเลือกของผู้ใช้ไว้ และคุณไม่จำเป็นต้องแสดงหน้าจอตัวเลือกสำหรับการอัปเกรดหรือดาวน์เกรด อย่างไรก็ตาม คุณต้องแสดงกล่องโต้ตอบข้อมูลแบบครั้งเดียวและการรับทราบของผู้ปกครอง
ดูโค้ดการผสานรวมตัวอย่างได้ที่ขั้นตอนที่ 4 ในสถานการณ์ 1B: นักพัฒนาแอปแสดงหน้าจอตัวเลือกและการเรียกเก็บเงินอื่นจะได้รับการจัดการภายในแอปของคุณ
เมื่อการอัปเกรดหรือดาวน์เกรดเสร็จสมบูรณ์ คุณต้องรายงานธุรกรรมใหม่โดยใช้โทเค็นธุรกรรมภายนอกที่ได้รับจากการเรียกใช้ก่อนหน้าสำหรับการซื้อการสมัครใช้บริการใหม่
การแทนที่การสมัครใช้บริการในสถานการณ์ที่ 2ก
สำหรับการสมัครใช้บริการที่ซื้อผ่านเว็บไซต์ของนักพัฒนาแอปหรือแอปการชำระเงินตั้งแต่แรกหลังจากมีการนำเสนอตัวเลือกของผู้ใช้ ผู้ใช้ที่ขออัปเกรดหรือดาวน์เกรดควรดำเนินการผ่านเว็บไซต์ของนักพัฒนาแอปหรือแอปการชำระเงินโดยไม่ต้องผ่านประสบการณ์การนำเสนอตัวเลือกของผู้ใช้อีกครั้ง
โดยให้เรียกใช้ launchBillingFlow เมื่อผู้ใช้ขออัปเกรดหรือดาวน์เกรด แทนที่จะระบุพารามิเตอร์อื่นๆ ในออบเจ็กต์ SubscriptionUpdateParams ให้ใช้ setOriginalExternalTransactionId โดยระบุรหัสธุรกรรมภายนอกสําหรับการซื้อครั้งแรก
ต้องระบุ DeveloperBillingOptionParams ในการเรียกนี้ด้วย ซึ่งจะไม่แสดงหน้าจอตัวเลือกของผู้ใช้ เนื่องจากระบบจะเก็บตัวเลือกของผู้ใช้สำหรับการซื้อเดิมไว้สำหรับการอัปเกรดและดาวน์เกรด เช่น
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();
นอกจากนี้ คุณต้องสร้างโทเค็นธุรกรรมภายนอกใหม่ด้วย เช่น
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.
}
});
หลังจากสร้างโทเค็นใหม่แล้ว คุณต้องเรียกใช้เมธอด launchBillingFlow เพื่อเปิดขั้นตอนการซื้อ
เมื่อการอัปเกรดหรือดาวน์เกรดเสร็จสมบูรณ์ คุณต้องรายงานธุรกรรมใหม่โดยใช้โทเค็นธุรกรรมภายนอกที่ได้รับจากการเรียกใช้ก่อนหน้าสำหรับการซื้อการสมัครใช้บริการใหม่
การแทนที่การสมัครใช้บริการในสถานการณ์ที่ 2ข
ขั้นตอนในการจัดการการแทนที่การสมัครใช้บริการในสถานการณ์นี้คล้ายกับขั้นตอนที่อธิบายไว้ในการแทนที่การสมัครใช้บริการในสถานการณ์ที่ 2A ความแตกต่างเพียงอย่างเดียวคือหลังจากสร้างโทเค็นธุรกรรมแล้ว แทนที่จะเรียกใช้เมธอด launchBillingFlow คุณต้องเรียกใช้ launchExternalLink เพื่อแสดงกล่องโต้ตอบข้อจำกัดความรับผิดของการลิงก์ออก ในกรณีนี้ ระบบจะคงตัวเลือกของผู้ใช้ไว้ และคุณไม่จำเป็นต้องแสดงหน้าจอตัวเลือกสำหรับการอัปเกรดหรือดาวน์เกรด
ดูโค้ดการผสานรวมตัวอย่างได้ที่ขั้นตอนที่ 6 ในสถานการณ์ 2B: นักพัฒนาแอปแสดงหน้าจอตัวเลือกและการเรียกเก็บเงินระบบอื่นได้รับการจัดการภายในแอปของคุณ
เมื่อการอัปเกรดหรือดาวน์เกรดเสร็จสมบูรณ์ คุณต้องรายงานธุรกรรมใหม่โดยใช้โทเค็นธุรกรรมภายนอกที่ได้รับจากการเรียกใช้ก่อนหน้าสำหรับการซื้อการสมัครใช้บริการใหม่