คู่มือนี้อธิบายวิธีผสานรวม 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 จะไม่จัดการการเรียกเก็บเงิน
คุณต้องแสดงหน้าจอข้อมูลต่อผู้ใช้โดยการเรียกใช้ API
showAlternativeBillingOnlyInformationDialog ก่อนเริ่มโฟลว์การเรียกเก็บเงินระบบอื่นทุกครั้ง หากผู้ใช้รับทราบกล่องโต้ตอบแล้ว การใช้ API นี้โดยปกติจะไม่ทำให้กล่องโต้ตอบแสดงอีกครั้ง
บางครั้งกล่องโต้ตอบอาจแสดงต่อผู้ใช้อีกครั้งในสถานการณ์ต่างๆ เช่น หากผู้ใช้ล้างแคชในอุปกรณ์
Kotlin
// An activity reference from which the alternative billing only information // dialog will be launched. val activity: Activity = this.activity val listener: AlternativeBillingOnlyInformationDialogListener = AlternativeBillingOnlyInformationDialogListener { 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 ConsoleDEVELOPER_ERROR: คำขอมีข้อผิดพลาด ใช้ข้อความแก้ไขข้อบกพร่อง เพื่อระบุและแก้ไขข้อผิดพลาดก่อนดำเนินการต่อNETWORK_ERROR, SERVICE_DISCONNECTED, SERVICE_UNAVAILABLE: ข้อผิดพลาดเหล่านี้เป็นข้อผิดพลาดชั่วคราวที่ควรลองอีกครั้ง ในกรณีที่SERVICE_DISCONNECTEDให้สร้างการเชื่อมต่อกับ Google Play อีกครั้งก่อน ลองอีกครั้ง
ทดสอบการเรียกเก็บเงินระบบอื่น
คุณควรใช้ผู้ทดสอบใบอนุญาตเพื่อทดสอบการผสานรวมการเรียกเก็บเงินระบบอื่น ระบบจะไม่เรียกเก็บเงินจากคุณสำหรับธุรกรรมที่เริ่มโดยบัญชีผู้ทดสอบใบอนุญาต ดูข้อมูลเพิ่มเติมเกี่ยวกับการกำหนดค่าผู้ทดสอบใบอนุญาตได้ที่ทดสอบการเรียกเก็บเงินสำหรับการซื้อในแอปด้วยการอนุญาตให้ใช้สิทธิแอปพลิเคชัน
ขั้นตอนถัดไป
เมื่อผสานรวมในแอปเสร็จแล้ว คุณก็พร้อมที่จะผสานรวมแบ็กเอนด์