# PurchaseClient

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

`public abstract class BillingClient extends Object`\
`java.lang.Object`     \
&#x20;    `↳ com.gaa.sdk.iap.PurchaseClient`\
&#x20;\
library和用戶應用軟件代碼之間通信的基本接口。\
所有method均以異步處理， UI thread 需要調用， 所有回調都將返回 UI thread 。\
如果想要在實例（instance）化此class之後進行連接設置，請調用startConnection([PurchaseClientStateListener](../interfaces/purchaseclientstatelistener)) 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](../../interfaces/querypurchaseslistener#id-cn-querypurchaseslistener-querypurchaseslistener))及queryProductDetailsAsync([ProductDetailsParams](../productdetailsparams#id-cn-productdetailsparams-productdetailsparams), [ProductDetailsListener](../../interfaces/productdetailslistener#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](../../annotations/purchaseclient.connectionstate#purchaseclient.connectionstate)                                                     |
| @interface         | [PurchaseClient.ResponseCode](../../annotations/purchaseclient.responsecode#id-cn-purchaseclient.responsecode-purchaseclient.responsecode)                            |
| @interface         | [PurchaseClient.ProductType](../../annotations/purchaseclient.producttype#id-cn-purchaseclient.producttype-purchaseclient.producttype)                                |
| @interface         | [PurchaseClient.RecurringAction](../../annotations/purchaseclient.recurringaction#id-cn-purchaseclient.recurringaction-purchaseclient.recurringaction)                |
| @interface         | [PurchaseClient.FeatureType](../../annotations/purchaseclient.featuretype#id-cn-purchaseclient.featuretype-purchaseclient.featuretype)                                |
| class              | <p><a href="../purchaseclient.builder#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`](../purchaseclient.builder#id-cn-purchaseclient.builder-purchaseclient.builder) `newBuilder(Context context)`

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

| **Parameters:**                                                                                         | <p><br></p>                  |
| ------------------------------------------------------------------------------------------------------- | ---------------------------- |
| context                                                                                                 | 使用applicationContext来連接支付模塊。 |
| **Returns:**                                                                                            | <p><br></p>                  |
| [PurchaseClient.Builder](../purchaseclient.builder#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](../../annotations/purchaseclient.connectionstate#purchaseclient.connectionstate) 相對應的狀態值。 |

\


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

`@UiThread`\
`public abstract` [`IapResult`](../iapresult#id-cn-iapresult-iapresult) `isFeatureSupported(String feature)`

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

| **Parameters:** | <p><br></p>                                                                                                                                 |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| String          | [PurchaseClient.FeatureType](../../annotations/purchaseclient.featuretype#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`](../interfaces/purchaseclientstatelistener) `listener)`

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

| **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`](../../interfaces/iapresultlistener#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`](../iapresult#id-cn-iapresult-iapresult) `launchPurchaseFlow(Activity activity,` [`PurchaseFlowParams`](../purchaseflowparams#id-cn-purchaseflowparams-purchaseflowparams) `params)`

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

| **Parameters:** | <p><br></p>                                                                                        |
| --------------- | -------------------------------------------------------------------------------------------------- |
| activity        | Activity: 顯示購買介面，請參考activity。                                                                      |
| params          | [PurchaseFlowParams](../purchaseflowparams#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`](../consumeparams#id-cn-consumeparams-consumeparams) `params,` [`ConsumeListener`](file:///C:/display/~1000034/ConsumeListener) `listener)`

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

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

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

`@AnyThread`\
`public abstract void acknowledgeAsync(`[`AcknowledgeParams`](../acknowledgeparams#id-cn-acknowledgeparams-acknowledgeparams) `acknowledgeParams,` [`AcknowledgeListener`](../../interfaces/acknowledgelistener#id-cn-acknowledgelistener-acknowledgelistener) `listener)`

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

| **Parameters:**   | <p><br></p>                                                                                                            |
| ----------------- | ---------------------------------------------------------------------------------------------------------------------- |
| acknowledgeParams | [AcknowledgeParams](../acknowledgeparams#id-cn-acknowledgeparams-acknowledgeparams) 確認購買的參數。                           |
| listener          | [AcknowledgeListener](../../interfaces/acknowledgelistener#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](../annotations/purchaseclient.producttype) 指定商品類型。 |
| listener        | [QueryPurchasesListener](../interfaces/querypurchaseslistener) 接收購買訂單的结果。       |

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

`@AnyThread`\
`public abstract void queryProductDetailsAsync(`[`ProductDetailsParams`](productdetailsparams) `params,` [`ProductDetailsListener`](../interfaces/productdetailslistener) `listener)`

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

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

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

`@AnyThread`\
`public abstract void manageRecurringProductAsync(`[`RecurringProductParams`](recurringproductparams) `recurringProductParams,` [`RecurringProductListener`](../interfaces/recurringproductlistener) `listener)`

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

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

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

`@UiThread`\
`public abstract void launchLoginFlowAsync(Activity activity,` [`IapResultListener`](../interfaces/iapresultlistener) `listener)`

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

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

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

`@UiThread`\
`public abstract void launchManageSubscription(Activity activity,` [`SubscriptionParams`](subscriptionparams) `subscriptionParams)`

移動到訂閱商品管理選單

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

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

`@AnyThread`\
`public abstract void getStoreInfoAsync(`[`StoreInfoListener`](../interfaces/storeinfolistener) `listener)`

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

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

&#x20;\
&#x20;
