คำแนะนำการผสานรวมในแอปสำหรับการเรียกเก็บเงินระบบอื่นเท่านั้น

คู่มือนี้อธิบายวิธีผสานรวม API เพื่อเสนอการเรียกเก็บเงินระบบอื่นเท่านั้น (กล่าวคือ แบบไม่ให้ผู้ใช้เลือก) ในแอปที่มีสิทธิ์ ดูข้อมูลเพิ่มเติมเกี่ยวกับโปรแกรมเหล่านี้ รวมถึงข้อกำหนดด้านสิทธิ์และขอบเขตทางภูมิศาสตร์ได้ที่ เกี่ยวกับการเรียกเก็บเงินระบบอื่น

การตั้งค่า Play Billing Library

เพิ่มทรัพยากร Dependency ของ Play Billing Library ลงในแอป Android หากต้องการใช้ API การเรียกเก็บเงินระบบอื่น คุณต้องใช้เวอร์ชัน 6.1 ขึ้นไป

เชื่อมต่อกับ Google Play

ขั้นตอนแรกในกระบวนการผสานรวมจะเหมือนกับที่อธิบายไว้ใน คู่มือการผสานรวม Google Play Billing โดยมีการแก้ไขเล็กน้อยเมื่อ เริ่มต้น BillingClient ดังนี้

  • คุณต้องเรียกใช้เมธอดใหม่เพื่อระบุว่าแอปของคุณใช้ระบบการเรียกเก็บเงินระบบอื่นเท่านั้น นั่นคือ enableAlternativeBillingOnly

ตัวอย่างต่อไปนี้แสดงการเริ่มต้น BillingClient ด้วยการแก้ไขเหล่านี้

Kotlin


var billingClient = BillingClient.newBuilder(context)
    .enableAlternativeBillingOnly()
    .build()

Java

private BillingClient billingClient = BillingClient.newBuilder(context)
    .enableAlternativeBillingOnly()
    .build();

หลังจากเริ่มต้น BillingClient แล้ว คุณต้อง สร้างการเชื่อมต่อกับ Google Play ตามที่อธิบายไว้ในคู่มือการผสานรวม

การตรวจสอบความพร้อมให้บริการ

แอปของคุณควรยืนยันว่าการเรียกเก็บเงินระบบอื่นเท่านั้นพร้อมใช้งานโดยการเรียกใช้ isAlternativeBillingOnlyAvailableAsync

API นี้จะแสดงผล BillingResponseCode.OK หากการเรียกเก็บเงินระบบอื่นเท่านั้นพร้อมใช้งาน โปรดดูรายละเอียดเกี่ยวกับวิธีที่แอปควร ตอบสนองต่อรหัสการตอบกลับอื่นๆ ได้ที่การจัดการการตอบกลับ

Kotlin


billingClient.isAlternativeBillingOnlyAvailableAsync(object:
    AlternativeBillingOnlyAvailabilityListener {
        override fun onAlternativeBillingOnlyAvailabilityResponse(
            billingResult: BillingResult) {
            if (billingResult.responseCode !=  BillingResponseCode.OK) {
                // Handle failures such as retrying due to network errors,
                // handling alternative billing only being unavailable, etc.
                return
            }

            // Alternative billing only is available. Continue with steps in
            // the guide.
        }
    });

Java


billingClient.isAlternativeBillingOnlyAvailable(
    new AlternativeBillingOnlyAvailabilityListener() {
        @Override
        public void onAlternativeBillingOnlyAvailabilityResponse(
            BillingResult billingResult) {
            if (billingResult.getResponseCode() != BillingResponseCode.OK) {
                 // Handle failures such as retrying due to network errors,
                 // handling alternative billing only being unavailable,
                 // etc.
                return;
            }

            // Alternative billing only is available. Continue with steps in
            // the guide.
        }
    });

กล่องโต้ตอบข้อมูลสำหรับผู้ใช้

หากต้องการผสานรวมกับการเรียกเก็บเงินระบบอื่นเท่านั้น แอปที่มีสิทธิ์ต้องแสดงหน้าจอข้อมูลซึ่งช่วยให้ผู้ใช้เข้าใจว่า Google Play จะไม่จัดการการเรียกเก็บเงิน ระบบต้องแสดงหน้าจอข้อมูลแก่ผู้ใช้โดยการเรียกใช้ showAlternativeBillingOnlyInformationDialog API ก่อนที่จะเริ่มขั้นตอนการเรียกเก็บเงินระบบอื่นในแต่ละครั้ง หากผู้ใช้รับทราบกล่องโต้ตอบแล้ว การใช้ API นี้โดยทั่วไปจะไม่ทำให้กล่องโต้ตอบแสดงขึ้นอีก อย่างไรก็ตาม อาจมีบางครั้งที่กล่องโต้ตอบแสดงขึ้นอีกครั้งต่อผู้ใช้ในบางสถานการณ์ เช่น หากผู้ใช้ล้างแคชในอุปกรณ์

Kotlin


// An activity reference from which the alternative billing only information
// dialog will be launched.
val activity : Activity = ...;

val listener : AlternativeBillingOnlyInformationDialogListener =
    AlternativeBillingOnlyInformationDialogListener { 
        override fun onAlternativeBillingOnlyInformationDialogResponse(
            billingResult: BillingResult) {
            // check billingResult
        }
}

val billingResult =
    billingClient.showAlternativeBillingOnlyInformationDialog(activity,
        listener)

Java


// An activity reference from which the alternative billing only information
// dialog will be launched.
Activity activity = ...;

AlternativeBillingOnlyInformationDialogListener listener =
    new AlternativeBillingOnlyInformationDialogListener() {
        @Override
        public void onAlternativeBillingOnlyInformationDialogResponse(
            BillingResult billingResult) {
                // check billingResult
            }
    };

BillingResult billingResult =
    billingClient.showAlternativeBillingOnlyInformationDialog(activity,
        listener);

หากเมธอดนี้แสดงผล BillingResponseCode.OK แสดงว่าแอปของคุณสามารถดำเนินการ ธุรกรรมต่อได้ ในกรณีที่ BillingResponseCode.USER_CANCELED แอปของคุณควรเรียกใช้ showAlternativeBillingOnlyInformationDialog เพื่อแสดง กล่องโต้ตอบแก่ผู้ใช้อีกครั้ง สำหรับรหัสการตอบกลับอื่นๆ โปรดดูส่วนการจัดการการตอบกลับ

การรายงานธุรกรรมไปยัง Google Play

คุณต้องรายงานธุรกรรมทั้งหมดที่ดำเนินการผ่านระบบการเรียกเก็บเงินระบบอื่นต้องรายงาน ไปยัง Google Play โดยการเรียกใช้ Google Play Developer API จากแบ็กเอนด์ภายใน 24 ชั่วโมง พร้อมระบุ externalTransactionToken ซึ่งได้รับโดยใช้ API ที่อธิบายไว้ด้านล่าง คุณควรสร้าง externalTransactionToken ใหม่สำหรับการซื้อครั้งเดียวแต่ละครั้ง การสมัครใช้บริการใหม่แต่ละรายการ รวมถึงการอัปเกรด/ดาวน์เกรดการสมัครใช้บริการที่มีอยู่ หากต้องการทราบวิธีรายงานธุรกรรมเมื่อได้รับ externalTransactionToken แล้ว โปรดดูคู่มือการผสานรวมแบ็กเอนด์

Kotlin

billingClient.createAlternativeBillingOnlyReportingDetailsAsync(object:
    AlternativeBillingOnlyReportingDetailsListener {
        override fun onAlternativeBillingOnlyTokenResponse(
            billingResult: BillingResult,
            alternativeBillingOnlyReportingDetails:
                AlternativeBillingOnlyReportingDetails?) {
            if (billingResult.responseCode !=  BillingResponseCode.OK) {
                // Handle failures such as retrying due to network errors.
                return
            }

            val externalTransactionToken =
                alternativeBillingOnlyReportingDetails?
                    .externalTransactionToken

            // Send transaction token to backend and report to Google Play.
        }
    });

Java


billingClient.createAlternativeBillingOnlyReportingDetailsAsync(
    new AlternativeBillingOnlyReportingDetailsListener() {
        @Override
        public void onAlternativeBillingOnlyTokenResponse(
            BillingResult billingResult,
            @Nullable AlternativeBillingOnlyReportingDetails
                alternativeBillingOnlyReportingDetails) {
            if (billingResult.getResponseCode() != BillingResponseCode.OK) {
                // Handle failures such as retrying due to network errors.
                return;
            }

            String transactionToken =
                alternativeBillingOnlyReportingDetails
                .getExternalTransactionToken();

            // Send transaction token to backend and report to Google Play.
        }
    });

การจัดการการตอบกลับ

เมธอด isAlternativeBillingOnlyAvailableAsync(), showAlternativeBillingOnlyInformationDialog() และ createAlternativeBillingOnlyReportingDetailsAsync() ข้างต้นอาจแสดงผลการตอบกลับที่ไม่ใช่ BillingResponseCode.OK ในกรณีที่เกิดข้อผิดพลาด วิธีจัดการข้อผิดพลาดที่แนะนำมีดังนี้

  • ERROR: ข้อผิดพลาดภายใน อย่าดำเนินการธุรกรรมต่อ ลองอีกครั้งโดยเรียกใช้ showAlternativeBillingOnlyInformationDialog() เพื่อแสดงกล่องโต้ตอบข้อมูลแก่ผู้ใช้ในครั้งถัดไปที่ผู้ใช้พยายามทำการซื้อ
  • FEATURE_NOT_SUPPORTED: Play Store ในอุปกรณ์ปัจจุบันไม่รองรับ API การเรียกเก็บเงินระบบอื่น อย่าดำเนินการธุรกรรมต่อ
  • USER_CANCELED: อย่าดำเนินการธุรกรรมต่อ เรียกใช้ showAlternativeBillingOnlyInformationDialog() อีกครั้งเพื่อแสดงกล่องโต้ตอบข้อมูลแก่ผู้ใช้ในครั้งถัดไปที่ผู้ใช้พยายามทำการซื้อ
  • BILLING_UNAVAILABLE: ธุรกรรมไม่มีสิทธิ์ใช้การเรียกเก็บเงินระบบอื่นเท่านั้น จึงไม่ควรดำเนินการต่อภายใต้โปรแกรมนี้ สาเหตุอาจเป็นเพราะผู้ใช้ไม่ได้อยู่ในประเทศที่มีสิทธิ์เข้าร่วมโปรแกรมนี้ หรือบัญชีของคุณลงทะเบียนเข้าร่วมโปรแกรมไม่สำเร็จ หากเป็นกรณีหลัง ให้ตรวจสอบสถานะการลงทะเบียนใน Play Developer Console
  • DEVELOPER_ERROR: เกิดข้อผิดพลาดกับคำขอ ใช้ข้อความดีบักเพื่อระบุและแก้ไขข้อผิดพลาดก่อนดำเนินการต่อ
  • NETWORK_ERROR, SERVICE_DISCONNECTED, SERVICE_UNAVAILABLE: ข้อผิดพลาดเหล่านี้เป็นข้อผิดพลาดชั่วคราวที่ควรลองอีกครั้ง ในกรณีที่เกิด SERVICE_DISCONNECTED ให้สร้างการเชื่อมต่อกับ Google Play อีกครั้งก่อนที่จะลองอีกครั้ง

ทดสอบการเรียกเก็บเงินระบบอื่น

คุณควรใช้ผู้ทดสอบใบอนุญาตเพื่อทดสอบการผสานรวมการเรียกเก็บเงินระบบอื่น ระบบจะไม่เรียกเก็บเงินจากคุณสำหรับธุรกรรมที่บัญชีผู้ทดสอบใบอนุญาตเริ่มต้น ดูข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่าผู้ทดสอบใบอนุญาตได้ที่ทดสอบการเรียกเก็บเงินสำหรับการซื้อในแอปด้วยการอนุญาตให้ใช้สิทธิแอปพลิเคชันสำหรับข้อมูลเพิ่มเติม

ขั้นตอนถัดไป

เมื่อผสานรวมในแอปเสร็จแล้ว คุณก็พร้อมที่จะผสานรวมแบ็กเอนด์