> For the complete documentation index, see [llms.txt](https://onestore-dev.gitbook.io/dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://onestore-dev.gitbook.io/dev/chi/tools/billing/v21/references/classes/purchaseclient.md).

# PurchaseClient

```java
public abstract class BillingClient extends Object

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

\
library和用户应用软件代码之间通信的基本接口。\
所有method均以异步处理， UI thread 需要调用， 所有回调都将返回 UI thread 。\
如果想要在实例（instance）化此class之后进行连接设置，请调用startConnection([PurchaseClientStateListener](/dev/chi/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/chi/tools/billing/v21/references/interfaces/querypurchaseslistener.md))及queryProductDetailsAsync([ProductDetailsParams](/dev/chi/tools/billing/v21/references/classes/productdetailsparams.md), [ProductDetailsListener](/dev/chi/tools/billing/v21/references/interfaces/purchaseslistener-deprecated.md))。\
使用该对象完成所有操作并关闭应用软件时，不要忘记调用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](/dev/chi/tools/billing/v21/references/annotations/purchaseclient.connectionstate.md)                                                                                         |
| @interface         | [PurchaseClient.ResponseCode](/dev/chi/tools/billing/v21/references/annotations/purchaseclient.responsecode.md)                                                                                               |
| @interface         | [PurchaseClient.ProductType](/dev/chi/tools/billing/v21/references/annotations/purchaseclient.producttype.md)                                                                                                 |
| @interface         | [PurchaseClient.RecurringAction](https://onestore-dev.gitbook.io/dev/chi/tools/billing/v21/references/classes/pages/9HSzvI8pZw7OTjoHRnQO#id-cn-purchaseclient.recurringaction-purchaseclient.recurringaction) |
| @interface         | [PurchaseClient.FeatureType](/dev/chi/tools/billing/v21/references/annotations/purchaseclient.featuretype.md)                                                                                                 |
| class              | <p><a href="/pages/fG1YHZu7W9S13UpFmc4V">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`](/dev/chi/tools/billing/v21/references/classes/purchaseclient.builder.md) `newBuilder(Context context)`

生成用于创建PurchaseClient实例（instance）的builder。

| **Parameters:**                                                                                   | <p><br></p>                  |
| ------------------------------------------------------------------------------------------------- | ---------------------------- |
| context                                                                                           | 使用applicationContext来连接支付模块。 |
| **Returns:**                                                                                      | <p><br></p>                  |
| [PurchaseClient.Builder](/dev/chi/tools/billing/v21/references/classes/purchaseclient.builder.md) | <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/chi/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/chi/tools/billing/v21/references/classes/iapresult.md) `isFeatureSupported(String feature)`

检查指定的功能是否在服务中支持 。

| **Parameters:** | <p><br></p>                                                                                                        |
| --------------- | ------------------------------------------------------------------------------------------------------------------ |
| String          | [PurchaseClient.FeatureType](/dev/chi/tools/billing/v21/references/annotations/purchaseclient.featuretype.md)常数之一。 |
| **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/chi/tools/billing/v21/references/interfaces/purchaseclientstatelistener.md) `listener)`

异步启动PurchaseClient设置进程。 完成后，通过[PurchaseClientStateListener](/dev/chi/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/chi/tools/billing/v21/references/interfaces/iapresultlistener.md) `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/chi/tools/billing/v21/references/classes/iapresult.md) `launchPurchaseFlow(Activity activity,` [`PurchaseFlowParams`](/dev/chi/tools/billing/v21/references/classes/purchaseflowparams.md) `params)`

开始购买In-App商品。 显示购买界面，通过初始化PurchaseClient时指定的[PurchasesUpdatedListener](/dev/chi/tools/billing/v21/references/interfaces/purchasesupdatedlistener.md)得到响应。

| **Parameters:** | <p><br></p>                                                                                          |
| --------------- | ---------------------------------------------------------------------------------------------------- |
| activity        | Activity: 显示购买界面，请参考activity。                                                                        |
| params          | [PurchaseFlowParams](/dev/chi/tools/billing/v21/references/classes/purchaseflowparams.md) 显示购买界面的参数。 |
| **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/chi/tools/billing/v21/references/classes/consumeparams.md) `params,` [`ConsumeListener`](file:///C:/display/~1000034/ConsumeListener) `listener)`

消费购买的商品。\
只能执行拥有的商品，已经进行消费的商品必须重新进行购买。\
警告!从API v6开始，如果在购买后3天内不进行消费(consume)或确认(acknowledge)，就会退款。

| **Parameters:** | <p><br></p>                                                                                       |
| --------------- | ------------------------------------------------------------------------------------------------- |
| params          | [ConsumeParams](/dev/chi/tools/billing/v21/references/classes/consumeparams.md) 消费的参数。            |
| listener        | [ConsumeListener](/dev/chi/tools/billing/v21/references/interfaces/consumelistener.md) 接收消费操作的结果。 |

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

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

确认购买的商品。\
只能执行所拥有的商品，已经确认的商品如果不消费就不能重新购买。\
管理型商品或包月型商品都均可适用。\
特别是管理型商品，可以作为确认(acknowledge)并在一定时间后进行消费(consume)的固定期限商品。\
警告!从API v6开始，如果在购买后3天内不进行消费(consume)或确认(acknowledge)，就会退款。

| **Parameters:**   | <p><br></p>                                                                                                 |
| ----------------- | ----------------------------------------------------------------------------------------------------------- |
| acknowledgeParams | [AcknowledgeParams](/dev/chi/tools/billing/v21/references/classes/acknowledgeparams.md) 确认购买的参数。            |
| listener          | [AcknowledgeListener](/dev/chi/tools/billing/v21/references/interfaces/acknowledgelistener.md) 接收确认购买的操作结果。 |

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

`@AnyThread`\
`public abstract void queryPurchasesAsync(String productType,` [`QueryPurchasesListener`](/dev/chi/tools/billing/v21/references/interfaces/querypurchaseslistener.md) `listener)`

获取在应用软件中购买的所有商品的购买信息。

| **Parameters:** | <p><br></p>                                                                                                           |
| --------------- | --------------------------------------------------------------------------------------------------------------------- |
| productType     | [PurchaseClient.ProductType](/dev/chi/tools/billing/v21/references/annotations/purchaseclient.producttype.md) 指定商品类型。 |
| listener        | [QueryPurchasesListener](/dev/chi/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/chi/tools/billing/v21/references/classes/productdetailsparams.md) `params,` [`ProductDetailsListener`](/dev/chi/tools/billing/v21/references/interfaces/productdetailslistener.md) `listener)`

获取在开发者中心注册的In-App商品的详细信息。

| **Parameters:** | <p><br></p>                                                                                                       |
| --------------- | ----------------------------------------------------------------------------------------------------------------- |
| params          | [ProductDetailsParams](/dev/chi/tools/billing/v21/references/classes/productdetailsparams.md) 获取商品详细信息的参数。        |
| listener        | [ProductDetailsListener](/dev/chi/tools/billing/v21/references/interfaces/productdetailslistener.md) 接收商品详细信息的结果。 |

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

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

变更包月型商品的状态。 每月取消自动支付或重新恢复取消的自动支付的操作。

| **Parameters:**        | <p><br></p>                                                                                                            |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| recurringProductParams | [RecurringProductParams](/dev/chi/tools/billing/v21/references/classes/recurringproductparams.md)   变更包月型商品状态的参数。      |
| listener               | [RecurringProductListener](/dev/chi/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/chi/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/chi/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/chi/tools/billing/v21/references/classes/subscriptionparams.md) `subscriptionParams)`

구독 상품 관리 메뉴로 이동합니다.

| **Parameters:**    | <p><br></p>                                                                                                                       |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
| activity           | Activity: 查看activity以移动到订阅商品的管理菜单。                                                                                                |
| subscriptionParams | [SubscriptionParams](/dev/chi/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/chi/tools/billing/v21/references/interfaces/storeinfolistener.md) `listener)`

获取市场分类代码。 此值是使用server to server API所需的值。

| **Parameters:** | <p><br></p>                                                                                          |
| --------------- | ---------------------------------------------------------------------------------------------------- |
| listener        | [StoreInfoListener](/dev/chi/tools/billing/v21/references/interfaces/storeinfolistener.md) 接收市场分类代码。 |

&#x20;\
&#x20;
