Skip to content

Commit

Permalink
add GetPurchasesHistoryAsync for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Feb 6, 2022
1 parent 603d7eb commit bd438fb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/Plugin.InAppBilling/Converters.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,22 @@ public static InAppBillingPurchase ToIABPurchase(this Purchase purchase)
};
return finalPurchase;
}

public static InAppBillingPurchase ToIABPurchase(this PurchaseHistoryRecord purchase)
{
return new InAppBillingPurchase
{
ConsumptionState = ConsumptionState.NoYetConsumed,
OriginalJson = purchase.OriginalJson,
Signature = purchase.Signature,
Payload = purchase.DeveloperPayload,
ProductId = purchase.Skus.FirstOrDefault(),
Quantity = purchase.Quantity,
ProductIds = purchase.Skus,
PurchaseToken = purchase.PurchaseToken,
TransactionDateUtc = DateTimeOffset.FromUnixTimeMilliseconds(purchase.PurchaseTime).DateTime,
State = PurchaseState.Unknown
};
}
}
}
23 changes: 22 additions & 1 deletion src/Plugin.InAppBilling/InAppBilling.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,32 @@ public override Task<IEnumerable<InAppBillingPurchase>> GetPurchasesAsync(ItemTy
return Task.FromResult(purchasesResult.PurchasesList.Select(p => p.ToIABPurchase()));
}

/// <summary>
/// Android only: Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.
/// </summary>
/// <param name="itemType">Type of product</param>
/// <returns>The current purchases</returns>
public override async Task<IEnumerable<InAppBillingPurchase>> GetPurchasesHistoryAsync(ItemType itemType)
{
if (BillingClient == null)
throw new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable, "You are not connected to the Google Play App store.");

var skuType = itemType switch
{
ItemType.InAppPurchase => BillingClient.SkuType.Inapp,
_ => BillingClient.SkuType.Subs
};

var purchasesResult = await BillingClient.QueryPurchaseHistoryAsync(skuType);


return purchasesResult?.PurchaseHistoryRecords?.Select(p => p.ToIABPurchase()) ?? new List<InAppBillingPurchase>();
}

/// <summary>
/// (Android specific) Upgrade/Downgrade/Change a previously purchased subscription
/// </summary>
/// <param name="newProductId">Sku or ID of product that will replace the old one</param>
/// <param name="oldProductId">Sku or ID of product that needs to be upgraded</param>
/// <param name="purchaseTokenOfOriginalSubscription">Purchase token of original subscription</param>
/// <param name="prorationMode">Proration mode (1 - ImmediateWithTimeProration, 2 - ImmediateAndChargeProratedPrice, 3 - ImmediateWithoutProration, 4 - Deferred)</param>
/// <returns>Purchase details</returns>
Expand Down
10 changes: 10 additions & 0 deletions src/Plugin.InAppBilling/Shared/BaseInAppBilling.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public abstract class BaseInAppBilling : IInAppBilling, IDisposable
/// <returns>The current purchases</returns>
public abstract Task<IEnumerable<InAppBillingPurchase>> GetPurchasesAsync(ItemType itemType);



/// <summary>
/// Android only: Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.
/// </summary>
/// <param name="itemType">Type of product</param>
/// <returns>The current purchases</returns>
public virtual Task<IEnumerable<InAppBillingPurchase>> GetPurchasesHistoryAsync(ItemType itemType) =>
Task.FromResult<IEnumerable<InAppBillingPurchase>>(new List<InAppBillingPurchase>());

/// <summary>
/// Purchase a specific product or subscription
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Plugin.InAppBilling/Shared/IInAppBilling.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ public interface IInAppBilling : IDisposable
/// Get all current purchases for a specific product type. If you use verification and it fails for some purchase, it's not contained in the result.
/// </summary>
/// <param name="itemType">Type of product</param>
/// <param name="verifyPurchase">Verify purchase implementation</param>
/// <returns>The current purchases</returns>
Task<IEnumerable<InAppBillingPurchase>> GetPurchasesAsync(ItemType itemType);


/// <summary>
/// Android only: Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.
/// </summary>
/// <param name="itemType">Type of product</param>
/// <returns>The current purchases</returns>
Task<IEnumerable<InAppBillingPurchase>> GetPurchasesHistoryAsync(ItemType itemType);

/// <summary>
/// Purchase a specific product or subscription
/// </summary>
Expand Down

0 comments on commit bd438fb

Please sign in to comment.