# PurchaseClient

## PurchaseClient <a href="#id-cn-purchaseclient-purchaseclient" id="id-cn-purchaseclient-purchaseclient"></a>

```java
public abstract class BillingClient extends Object

java.lang.Object
    ↳ com.gaa.sdk.iap.PurchaseClient
```

\
&#x20;\
library和用戶應用軟件代碼之間通信的基本接口。\
所有method均以異步處理， UI thread 需要調用， 所有回調都將返回 UI thread 。\
如果想要在實例（instance）化此class之後進行連接設置，請調用startConnection([PurchaseClientStateListener](/dev/cht/tools/billing/v21/references/interfaces/purchaseclientstatelistener.md)) method，並在設置完成後通過listener接收通知。

```java
class YourPurchaseManager implements PurchaseClientStateListener {
    private PurchaseClient mPurchaseClient;
 
    public YourPurchaseManager() {
        mPurchaseClient = PurchaseClient.newBuilder(activity)
            .setBase64PublicKey(/* your public key */)
            .setListener(this)
            .build();
    }
 ...
}
```

一般情况下，設定完成後，會調用購買訂單查詢和In-App商品的詳細資訊。請參考queryPurchasesAsync(String, [QueryPurchasesListener](/dev/cht/tools/billing/v21/references/interfaces/querypurchaseslistener.md#id-cn-querypurchaseslistener-querypurchaseslistener))及queryProductDetailsAsync([ProductDetailsParams](/dev/cht/tools/billing/v21/references/classes/productdetailsparams.md#id-cn-productdetailsparams-productdetailsparams), [ProductDetailsListener](/dev/cht/tools/billing/v21/references/interfaces/productdetailslistener.md#id-cn-productdetailslistener-productdetailslistener))。\
使用該對象完成所有操作並關閉應用軟件時，不要忘記調用endConnection()。必須解除與支付模塊的連接和資源。建議為止為Activity.onDestroy()。\
要查看library的詳細登錄日誌，只需在Logger.setLogLevel(int) 中輸入android.util.Log.VERBOSE調用即可。&#x20;

> <mark style="color:red;">**注意! ! !**</mark>
>
> <mark style="color:red;">一定要在發布應用軟件的發行版（release版本）之前!刪除Logger.setLogLevel(int)</mark>。

## Summary <a href="#id-cn-purchaseclient-summary" id="id-cn-purchaseclient-summary"></a>

| **Nested classes** | <p><br></p>                                                                                                                                                                                                   |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| @interface         | [PurchaseClient.ConnectionState](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/v83ZrjbolWJ04wVGjNOp#purchaseclient.connectionstate)                                      |
| @interface         | [PurchaseClient.ResponseCode](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/ffN8NCzx5o6qKXzIdVZC#id-cn-purchaseclient.responsecode-purchaseclient.responsecode)          |
| @interface         | [PurchaseClient.ProductType](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/GzTZSxN4YNICgDrEwrN2#id-cn-purchaseclient.producttype-purchaseclient.producttype)             |
| @interface         | [PurchaseClient.RecurringAction](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/9HSzvI8pZw7OTjoHRnQO#id-cn-purchaseclient.recurringaction-purchaseclient.recurringaction) |
| @interface         | [PurchaseClient.FeatureType](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/1GhgVaYHl7CLGw5w0buc#id-cn-purchaseclient.featuretype-purchaseclient.featuretype)             |
| class              | <p><a href="/pages/fG1YHZu7W9S13UpFmc4V#id-cn-purchaseclient.builder-purchaseclient.builder">PurchaseClient.Builder</a><br>是易於創建PurchaseClient實例（instance）的builder。</p>                                       |

## Public methods <a href="#id-cn-purchaseclient-publicmethods" id="id-cn-purchaseclient-publicmethods"></a>

### newBuilder <a href="#id-cn-purchaseclient-newbuilder" id="id-cn-purchaseclient-newbuilder"></a>

`@AnyThread` \
`public static` [`PurchaseClient.Builder`](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/fG1YHZu7W9S13UpFmc4V#id-cn-purchaseclient.builder-purchaseclient.builder) `newBuilder(Context context)`

生成用創建PurchaseClient實例（instance）的builder。

| **Parameters:**                                                                                                                                                                       | <p><br></p>                  |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- |
| context                                                                                                                                                                               | 使用applicationContext来連接支付模塊。 |
| **Returns:**                                                                                                                                                                          | <p><br></p>                  |
| [PurchaseClient.Builder](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/fG1YHZu7W9S13UpFmc4V#id-cn-purchaseclient.builder-purchaseclient.builder) | <p><br></p>                  |

### isReady <a href="#id-cn-purchaseclient-isready" id="id-cn-purchaseclient-isready"></a>

`@AnyThread` \
`public abstract boolean isReady()`

檢查客戶端是否已連接到當前服務，從而確認對其他函數的請求是否成功 。

| **Returns:** |
| ------------ |
| boolean      |

### getConnectionState <a href="#id-cn-purchaseclient-getconnectionstate" id="id-cn-purchaseclient-getconnectionstate"></a>

`@AnyThread` \
`public abstract int getConnectionState()`

返回當前PurchaseClient的連接狀態。

| **Returns:** | <p><br></p>                                                                                                                                                                        |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| int          | 與[PurchaseClient.ConnectionState](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/v83ZrjbolWJ04wVGjNOp#purchaseclient.connectionstate) 相對應的狀態值。 |

<br>

### isFeatureSupported <a href="#id-cn-purchaseclient-isfeaturesupported" id="id-cn-purchaseclient-isfeaturesupported"></a>

`@UiThread`\
`public abstract` [`IapResult`](/dev/cht/tools/billing/v21/references/classes/iapresult.md#id-cn-iapresult-iapresult) `isFeatureSupported(String feature)`

檢查指定的功能是否在服務中支持 。

| **Parameters:** | <p><br></p>                                                                                                                                                                                            |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| String          | [PurchaseClient.FeatureType](https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/pages/1GhgVaYHl7CLGw5w0buc#id-cn-purchaseclient.featuretype-purchaseclient.featuretype)常數之一。 |
| **Returns:**    | <p><br></p>                                                                                                                                                                                            |
| IapResult       | 支持功能時， PurchaseClient.ResponseCode.RESULT\_OK 不支持功能時，PurchaseClient.ResponseCode.ERROR\_FEATURE\_NOT\_SUPPORTED                                                                                        |

### startConnection <a href="#id-cn-purchaseclient-startconnection" id="id-cn-purchaseclient-startconnection"></a>

`@AnyThread` \
`public abstract void startConnection(`[`PurchaseClientStateListener`](/dev/cht/tools/billing/v21/references/interfaces/purchaseclientstatelistener.md) `listener)`

異步啟動PurchaseClient設置進程。 完成後，通過[PurchaseClientStateListener](/dev/cht/tools/billing/v21/references/interfaces/purchaseclientstatelistener.md)得到回應。

| **Parameters:** | <p><br></p>                      |
| --------------- | -------------------------------- |
| listener        | PurchaseClient設置和支付模塊連接完成時，接收回應。 |

### endConnection <a href="#id-cn-purchaseclient-endconnection" id="id-cn-purchaseclient-endconnection"></a>

`@AnyThread` \
`public abstract void endConnection()`

斷開與支付模塊的連接，並解除PurchaseClient的保留資源。

### launchUpdateOrInstallFlow <a href="#id-cn-purchaseclient-launchupdateorinstallflow" id="id-cn-purchaseclient-launchupdateorinstallflow"></a>

`@UiThread` \
`public abstract void launchUpdateOrInstallFlow(Activity activity,` [`IapResultListener`](/dev/cht/tools/billing/v21/references/interfaces/iapresultlistener.md#id-cn-iapresultlistener-iapresultlistener) `listener)`

IapResult.getResponseCode()值為PurchaseClient.ResponseCode.RESULT\_NEED\_UPDATE時，必須調用以更新或安装支付模塊。

| **Parameters:** | <p><br></p>                                                        |
| --------------- | ------------------------------------------------------------------ |
| activity        | Activity: 參照activity以了解结支付模塊的更新和安装流程。                              |
| listener        | [IapResultListener](file:///C:/display/~1000034/IapResultListener) |

### launchPurchaseFlow <a href="#id-cn-purchaseclient-launchpurchaseflow" id="id-cn-purchaseclient-launchpurchaseflow"></a>

`@UiThread`\
`public abstract` [`IapResult`](/dev/cht/tools/billing/v21/references/classes/iapresult.md#id-cn-iapresult-iapresult) `launchPurchaseFlow(Activity activity,` [`PurchaseFlowParams`](/dev/cht/tools/billing/v21/references/classes/purchaseflowparams.md#id-cn-purchaseflowparams-purchaseflowparams) `params)`

開始購買In-App商品。 顯示購買界面，通過初始化PurchaseClient時指定的[PurchasesUpdatedListener](/dev/cht/tools/billing/v21/references/interfaces/purchasesupdatedlistener.md#id-cn-purchasesupdatedlistener-purchasesupdatedlistener)得到回應。

| **Parameters:** | <p><br></p>                                                                                                                                      |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| activity        | Activity: 顯示購買介面，請參考activity。                                                                                                                    |
| params          | [PurchaseFlowParams](/dev/cht/tools/billing/v21/references/classes/purchaseflowparams.md#id-cn-purchaseflowparams-purchaseflowparams) 顯示購買介面的參數。 |
| **Returns:**    | <p><br></p>                                                                                                                                      |
| IapResult       | <p><br></p>                                                                                                                                      |

### consumeAsync <a href="#id-cn-purchaseclient-consumeasync" id="id-cn-purchaseclient-consumeasync"></a>

`@AnyThread public abstract void consumeAsync(`[`ConsumeParams`](/dev/cht/tools/billing/v21/references/classes/consumeparams.md#id-cn-consumeparams-consumeparams) `params,` [`ConsumeListener`](file:///C:/display/~1000034/ConsumeListener) `listener)`

使用購買的商品。\
只能執行擁有的商品，已經進行使用的商品必須重新進行購買。\
警告!從API v6開始，如果在購買後3天内不進行使用(consume)或確認(acknowledge)，就會退款。

| **Parameters:** | <p><br></p>                                                                                                                             |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| params          | [ConsumeParams](/dev/cht/tools/billing/v21/references/classes/consumeparams.md#id-cn-consumeparams-consumeparams) 使用的參數。                |
| listener        | [ConsumeListener](/dev/cht/tools/billing/v21/references/interfaces/consumelistener.md#id-cn-consumelistener-consumelistener) 接收使用操作的结果。 |

### acknowledgeAsync <a href="#id-cn-purchaseclient-acknowledgeasync" id="id-cn-purchaseclient-acknowledgeasync"></a>

`@AnyThread`\
`public abstract void acknowledgeAsync(`[`AcknowledgeParams`](/dev/cht/tools/billing/v21/references/classes/acknowledgeparams.md#id-cn-acknowledgeparams-acknowledgeparams) `acknowledgeParams,` [`AcknowledgeListener`](/dev/cht/tools/billing/v21/references/interfaces/acknowledgelistener.md#id-cn-acknowledgelistener-acknowledgelistener) `listener)`

確認購買的商品。\
只能執行所擁有的商品，已經確認的商品如果不使用就不能重新購買。\
管理型商品或包月型商品都均可適用。\
特别是管理型商品，可以作為確認(acknowledge)並在一定時間後進行使用(consume)的固定期限商品。\
警告!從API v6開始，如果在購買後3天内不進行使用(consume)或確認(acknowledge)，就會退款。

| **Parameters:**   | <p><br></p>                                                                                                                                               |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| acknowledgeParams | [AcknowledgeParams](/dev/cht/tools/billing/v21/references/classes/acknowledgeparams.md#id-cn-acknowledgeparams-acknowledgeparams) 確認購買的參數。                |
| listener          | [AcknowledgeListener](/dev/cht/tools/billing/v21/references/interfaces/acknowledgelistener.md#id-cn-acknowledgelistener-acknowledgelistener) 接收確認購買的操作结果。 |

### queryPurchasesAsync <a href="#id-cn-purchaseclient-querypurchasesasync" id="id-cn-purchaseclient-querypurchasesasync"></a>

`@AnyThread`\
`public abstract void queryPurchasesAsync(String productType,` [`QueryPurchasesListener`](#id-cn-purchaseclient-querypurchasesasync) `listener)`

獲取在應用軟件中購買的所有商品的購買資訊。

| **Parameters:** | <p><br></p>                                                                                                           |
| --------------- | --------------------------------------------------------------------------------------------------------------------- |
| productType     | [PurchaseClient.ProductType](/dev/cht/tools/billing/v21/references/annotations/purchaseclient.producttype.md) 指定商品類型。 |
| listener        | [QueryPurchasesListener](/dev/cht/tools/billing/v21/references/interfaces/querypurchaseslistener.md) 接收購買訂單的结果。       |

### queryProductDetailsAsync <a href="#id-cn-purchaseclient-queryproductdetailsasync" id="id-cn-purchaseclient-queryproductdetailsasync"></a>

`@AnyThread`\
`public abstract void queryProductDetailsAsync(`[`ProductDetailsParams`](/dev/cht/tools/billing/v21/references/classes/productdetailsparams.md) `params,` [`ProductDetailsListener`](/dev/cht/tools/billing/v21/references/interfaces/productdetailslistener.md) `listener)`

獲取在開發者中心註冊的In-App商品的詳細資訊。

| **Parameters:** | <p><br></p>                                                                                                                                                           |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| params          | [ProductDetailsParams](/dev/cht/tools/billing/v21/references/classes/productdetailsparams.md#id-cn-productdetailsparams-productdetailsparams) 獲取商品詳細資訊的參數。            |
| listener        | [ProductDetailsListener](/dev/cht/tools/billing/v21/references/interfaces/productdetailslistener.md#id-cn-productdetailslistener-productdetailslistener) 接收商品詳細資訊的结果。 |

### manageRecurringProductAsync <a href="#id-cn-purchaseclient-managerecurringproductasync" id="id-cn-purchaseclient-managerecurringproductasync"></a>

`@AnyThread`\
`public abstract void manageRecurringProductAsync(`[`RecurringProductParams`](/dev/cht/tools/billing/v21/references/classes/recurringproductparams.md) `recurringProductParams,` [`RecurringProductListener`](/dev/cht/tools/billing/v21/references/interfaces/recurringproductlistener.md) `listener)`

變更包月型商品的狀態。 每月取消自動支付或重新恢復取消的自動支付的操作。

| **Parameters:**        | <p><br></p>                                                                                                                                                           |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| recurringProductParams | [RecurringProductParams](/dev/cht/tools/billing/v21/references/classes/recurringproductparams.md#id-cn-recurringproductparams-recurringproductparams)   變更包月型商品狀態的參數。 |
| listener               | [RecurringProductListener](/dev/cht/tools/billing/v21/references/interfaces/recurringproductlistener.md) 變更包月型商品狀態的參數。                                                |

### launchLoginFlowAsync <a href="#id-cn-purchaseclient-launchloginflowasync" id="id-cn-purchaseclient-launchloginflowasync"></a>

`@UiThread`\
`public abstract void launchLoginFlowAsync(Activity activity,` [`IapResultListener`](/dev/cht/tools/billing/v21/references/interfaces/iapresultlistener.md) `listener)`

IapResult.getResponseCode值發生PurchaseClient.ResponseCode.RESULT\_NEED\_LOGIN时，進行store登錄。

| **Parameters:** | <p><br></p>                                                                                              |
| --------------- | -------------------------------------------------------------------------------------------------------- |
| activity        | Activity: 查看activity以了解store登錄情况。                                                                        |
| listener        | [IapResultListener](/dev/cht/tools/billing/v21/references/interfaces/iapresultlistener.md) 接收登錄成功或失敗的结果。 |

### launchManageSubscription <a href="#id-cn-purchaseclient-launchmanagesubscription" id="id-cn-purchaseclient-launchmanagesubscription"></a>

`@UiThread`\
`public abstract void launchManageSubscription(Activity activity,` [`SubscriptionParams`](/dev/cht/tools/billing/v21/references/classes/subscriptionparams.md) `subscriptionParams)`

移動到訂閱商品管理選單

| **Parameters:**    | <p><br></p>                                                                                                                       |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
| activity           | Activity: 查看activity以移動到訂閱商品的管理選單。                                                                                                |
| subscriptionParams | [SubscriptionParams](/dev/cht/tools/billing/v21/references/classes/subscriptionparams.md) 為null時移動到管理選單界面，有 PurchaseData時移動到詳情界面。 |

### getStoreInfoAsync <a href="#id-cn-purchaseclient-getstoreinfoasync" id="id-cn-purchaseclient-getstoreinfoasync"></a>

`@AnyThread`\
`public abstract void getStoreInfoAsync(`[`StoreInfoListener`](/dev/cht/tools/billing/v21/references/interfaces/storeinfolistener.md) `listener)`

獲取市場分類代碼。 此值是使用server to server API所需的值。

| **Parameters:** | <p><br></p>                                                                                          |
| --------------- | ---------------------------------------------------------------------------------------------------- |
| listener        | [StoreInfoListener](/dev/cht/tools/billing/v21/references/interfaces/storeinfolistener.md) 接收市場分類代碼。 |

&#x20;\
&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://onestore-dev.gitbook.io/dev/cht/tools/billing/v21/references/classes/purchaseclient.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
