Skip to content

Commit

Permalink
Merge pull request #25 from eggheadgames/amazon-sku-limit-fix
Browse files Browse the repository at this point in the history
Fix bug where Amazon can only request 100 IAP products
  • Loading branch information
mikemee authored May 29, 2020
2 parents 769576d + 903a9d8 commit 0d563c5
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 210 deletions.
7 changes: 2 additions & 5 deletions library/src/main/java/com/billing/BillingService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.billing;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.CallSuper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class BillingService {
protected Context context;

private List<PurchaseServiceListener> purchaseServiceListeners;
private List<SubscriptionServiceListener> subscriptionServiceListeners;

@SuppressWarnings("WeakerAccess")
public BillingService(Context context) {
this.context = context;
public BillingService() {
purchaseServiceListeners = new ArrayList<>();
subscriptionServiceListeners = new ArrayList<>();
}
Expand Down Expand Up @@ -87,7 +85,6 @@ public void updatePrices(Map<String, String> iapkeyPrices) {

@CallSuper
public void close() {
context = null;
subscriptionServiceListeners.clear();
purchaseServiceListeners.clear();
}
Expand Down
138 changes: 0 additions & 138 deletions library/src/main/java/com/billing/amazon/AmazonBillingListener.java

This file was deleted.

106 changes: 106 additions & 0 deletions library/src/main/java/com/billing/amazon/AmazonBillingListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.billing.amazon

import android.util.Log
import com.amazon.device.iap.PurchasingListener
import com.amazon.device.iap.PurchasingService
import com.amazon.device.iap.model.*

internal class AmazonBillingListener(private val amazonBillingService: AmazonBillingService) : PurchasingListener {

var mDebugLog = false

override fun onUserDataResponse(userDataResponse: UserDataResponse) {
logDebug("onUserDataResponse " + userDataResponse.requestStatus)
}

fun enableDebugLogging(enable: Boolean) {
mDebugLog = enable
}

override fun onProductDataResponse(response: ProductDataResponse) {
val status = response.requestStatus
logDebug("onProductDataResponse: RequestStatus ($status)")
when (status) {
ProductDataResponse.RequestStatus.SUCCESSFUL -> {
logDebug("onProductDataResponse: successful. The item data map in this response includes the valid SKUs")
val unavailableSkus = response.unavailableSkus
logDebug("onProductDataResponse: " + unavailableSkus.size + " unavailable skus")
val productData = response.productData
logDebug("onProductDataResponse productData : " + productData.size)
val iapkeyPrices = productData.map {
it.value.sku to it.value.price
}.toMap()
amazonBillingService.updatePrices(iapkeyPrices)
}
ProductDataResponse.RequestStatus.FAILED, ProductDataResponse.RequestStatus.NOT_SUPPORTED, null ->
logDebug("onProductDataResponse: failed, should retry request")
}
}

override fun onPurchaseResponse(response: PurchaseResponse) {
logDebug("onPurchaseResponse " + response.requestStatus)
val requestId = response.requestId.toString()
val userId = response.userData.userId
val status = response.requestStatus
val receipt: Receipt? = response.receipt
logDebug("onPurchaseResponse: requestId ($requestId) userId ($userId) purchaseRequestStatus ($status)")
when (status) {
PurchaseResponse.RequestStatus.SUCCESSFUL -> {
if (receipt != null) {
logDebug("onPurchaseResponse: receipt json:" + receipt.toJSON())
logDebug("onPurchaseResponse: getUserId:" + response.userData.userId)
logDebug("onPurchaseResponse: getMarketplace:" + response.userData.marketplace)
if (receipt.productType == ProductType.SUBSCRIPTION) {
amazonBillingService.subscriptionOwned(receipt.sku, false)
} else {
amazonBillingService.productOwned(receipt.sku, false)
}
PurchasingService.notifyFulfillment(receipt.receiptId, FulfillmentResult.FULFILLED)
}
}
PurchaseResponse.RequestStatus.ALREADY_PURCHASED -> {
logDebug("onPurchaseResponse: already purchased, you should verify the entitlement purchase on your side and make sure the purchase was granted to customer")
if (receipt != null && !receipt.isCanceled) {
if (receipt.productType == ProductType.SUBSCRIPTION) {
amazonBillingService.subscriptionOwned(receipt.sku, true)
} else {
amazonBillingService.productOwned(receipt.sku, true)
}
}
}
PurchaseResponse.RequestStatus.INVALID_SKU -> logDebug("onPurchaseResponse: invalid SKU! onProductDataResponse should have disabled buy button already.")
PurchaseResponse.RequestStatus.FAILED, PurchaseResponse.RequestStatus.NOT_SUPPORTED, null -> logDebug("onPurchaseResponse: failed so remove purchase request from local storage")
}
}

override fun onPurchaseUpdatesResponse(response: PurchaseUpdatesResponse?) {
logDebug("onPurchaseUpdatesResponse " + if (response == null) "null" else response.requestStatus)
if (response == null) return
if (response.requestStatus == PurchaseUpdatesResponse.RequestStatus.SUCCESSFUL) {
val receipts = response.receipts.toTypedArray()
for (receipt in receipts) {
if (receipt != null && !receipt.isCanceled) {
if (receipt.productType == ProductType.ENTITLED) {
amazonBillingService.productOwned(receipt.sku, true)
logDebug("onPurchaseUpdatesResponse productOwned: " + receipt.sku)
} else if (receipt.productType == ProductType.SUBSCRIPTION) {
amazonBillingService.subscriptionOwned(receipt.sku, true)
logDebug("onPurchaseUpdatesResponse subscriptionOwned: " + receipt.sku)
}
}
}
}
}

private fun logDebug(msg: String) {
if (mDebugLog) Log.d(TAG, msg)
}

companion object {
private val TAG = AmazonBillingListener::class.java.simpleName
}

init {
logDebug("IS_SANDBOX_MODE:" + PurchasingService.IS_SANDBOX_MODE)
}
}
65 changes: 0 additions & 65 deletions library/src/main/java/com/billing/amazon/AmazonBillingService.java

This file was deleted.

Loading

0 comments on commit 0d563c5

Please sign in to comment.