<manifest>
<!--
if your binary use ONE store's In-app SDK,
Please make sure to declare the following query on Androidmanifest.xml.
Refer to the notice for more information.
https://dev.onestore.net/devpoc/support/news/noticeView.omp?noticeId=32968
-->
<queries>
<intent>
<action android:name="com.onestore.ipc.iap.IapService.ACTION" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="onestore" />
</intent>
</queries>
...
<application>
...
</application>
</manifest>
發行全球store的測試選項設置
此是強制SDK和全球store聯動的選項。本功能從SDK v21.01.00版本開始適用。
<manifest>
...
<application>
<activity>
...
</activity>
...
<!-- Options for in-app testing on your global store -->
<meta-data android:name="onestore:dev_option" android:value="global" />
</application>
</manifest>
/**
* Set the log level.<br/>
* {@link Log#VERBOSE}, {@link Log#DEBUG}, {@link Log#INFO},
* {@link Log#WARN}, {@link Log#ERROR}
* @param level int
*/
com.gaa.sdk.base.Logger.setLogLevel(2)
常數
值
VERBOSE
2
DEBUG
3
INFO (default)
4
WARN
5
ERROR
6
發行版本網絡安全比較弱,本選項需要刪除。
錯誤處理
ONE store 支付程序庫以IapResult形式反饋錯誤。IapResult包含分類App内可能出現的支付相關錯誤的ResponseCode。例如,如果IapResult收到RESULT_NEED_LOGIN 或 RESULT_NEED_UPDATE等錯誤代碼(code),應用軟件需要做出相應的處理。
private val listener = PurchasesUpdatedListener { iapResult, purchases ->
// To be implemented in a later section.
}
private var purchaseClient = PurchaseClient.newBuilder(activity)
.setListener(listener)
.setBase64PublicKey(/*your public key*/) // optional
.build()
private PurchasesUpdatedListener listener = new PurchasesUpdatedListener {
@Override
public void onPurchasesUpdated(IapResult iapResult, List<PurchaseData>) {
// To be implemented in a later section.
}
};
private PurchaseClient purchaseClient = PurchaseClient.newBuilder(activity)
.setListener(listener)
.setBase64PublicKey(/*your public key*/) // optional
.build();
purchaseClient.startConnection(object : PurchaseClientStateListener {
override fun onSetupFinished(iapResult: IapResult) {
if (iapResult.isSuccess) {
// The PurchaseClient is ready. You can query purchases here.
}
}
override fun onServiceDisconnected() {
// Try to restart the connection on the next request to
// PurchaseClient by calling the startConnection() method.
}
})
purchaseClient.startConnection(new PurchaseClientStateListener {
@Override
public void onSetupFinished(IapResult iapResult) {
if (iapResult.isSuccess()) {
// The PurchaseClient is ready. You can query purchases here.
}
}
@Override
public void onServiceDisconnected() {
// Try to restart the connection on the next request to
// PurchaseClient by calling the startConnection() method.
}
});
override fun onPurchasesUpdated(iapResult: IapResult, purchases: List<PurchaseData>?) {
if (iapResult.isSuccess && purchases != null) {
for (purchase in purchases) {
handlePurchase(purchase)
}
} else if (iapResult.responseCode == ResponseCode.NEED_UPDATE) {
// PurchaseClient by calling the launchUpdateOrInstallFlow() method.
} else if (iapResult.responseCode == ReponseCode.NEED_LOGIN) {
// PurchaseClient by calling the launchLoginFlow() method.
} else {
// Handle any other error codes.
}
}
@Override
public void onPurchasesUpdated(IapResult iapResult, List<PurchaseData> purchases) {
if (iapResult.isSuccess() && purchases != null) {
for (purchase in purchases) {
handlePurchase(purchase);
}
} else if (iapResult.getResponseCode() == ResponseCode.NEED_UPDATE) {
// PurchaseClient by calling the launchUpdateOrInstallFlow() method.
} else if (iapResult.getResponseCode() == ReponseCode.NEED_LOGIN) {
// PurchaseClient by calling the launchLoginFlow() method.
} else {
// Handle any other error codes.
}
}
fun handlePurchase(purchase: PurchaseData) {
// Purchase retrieved from PurchaseClient#queryPurchasesAsync
// or your PurchasesUpdatedListener.
val purchase: PurchaseData = ...
// Verify the purchase.
// Ensure entitlement was not already granted for this purchaseToken.
// Grant entitlement to the user.
val consumeParams = ConsumeParams.newBuilder()
.setPurchaseData(purchase)
.build()
purchaseClient.consumeAsync(consumeParams) { iapResult, purchaseData ->
// Process the result.
}
}
private void handlePurchase(PurchaseData purchase) {
// Purchase retrieved from PurchaseClient#queryPurchasesAsync or your PurchasesUpdatedListener.
PurchaseData purchase = ...
// Verify the purchase.
// Ensure entitlement was not already granted for this purchaseToken.
// Grant entitlement to the user.
ConsumeParams consumeParams = ConsumeParams.newBuilder()
.setPurchaseData(purchase)
.build();
purchaseClient.consumeAsync(consumeParams, new ConsumeListener() {
@Override
public void onConsumeResponse(IapResult iapResult, PurchaseData purchaseData) {
// Process the result.
}
});
}
// Purchase retrieved from PurchaseClient#queryPurchasesAsync or your PurchasesUpdatedListener.
fun handlePurchase(PurchaseData purchase) {
if (purchase.purchaseState == PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged) {
val acknowledgeParams = AcknowledgeParams.newBuilder()
.setPurchaseData(purchase)
.build()
purchaseClient.acknowledgeAsync(acknowledgeParams) { iapResult, purchaseData ->
// PurchaseClient by calling the queryPurchasesAsync() method.
}
}
}
}
// Purchase retrieved from PurchaseClient#queryPurchasesAsync or your PurchasesUpdatedListener.
private void handlePurchase(purchase: PurchaseData) {
if (purchase.getPurchaseState() == PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
AcknowledgeParams acknowledgeParams = AcknowledgeParams.newBuilder()
.setPurchaseData(purchase)
.build();
purchaseClient.acknowledgeAsync(acknowledgeParams, new AcknowledgeListener() {
@Override
public void onAcknowledgeResponse(IapResult iapResult, PurchaseData purchaseData) {
// PurchaseClient by calling the queryPurchasesAsync() method.
}
});
}
}
}
purchaseClient.getStoreInfoAsync { iapResult, storeCode ->
// Save storecode and use it in Server to Server API.
}
purchaseClient.getStoreInfoAsync(new StoreInfoListener() {
@Override
public void onStoreInfoResponse(IapResult iapResult, String storeCode) {
// Save storecode and use it in Server to Server API.
}
});
安裝ONE store服務
ONE store服務的版本低或者沒有時,無法使用app内支付。 通過PurchaseClient.startConnection()連接時,可以在IapResult.getResponseCode()確認。如果發生RESULT_NEED_UPDATE,需要調用launchUpdateOrInstallFlow()方法。
val activity: Activity = ...
purchaseClient.launchUpdateOrInstallFlow(activity) { iapResult ->
if (iapResult.isSuccess) {
// If the installation is completed successfully,
// you should try to reconnect with the ONE store service.
// PurchaseClient by calling the startConnection() method.
}
}