Improve Google Play subscription offer handling
This commit is contained in:
1 parent
bcc9e01179
commit
f27e47d83a
4 files changed
+45
-11
No files matched your search
@@ -9,6 +9,7 @@ public sealed class AndroidPlayBillingService : Java.Lang.Object, IPlayBillingSe
|
||||
private readonly SemaphoreSlim _connectionLock = new(1, 1);
|
||||
private readonly BillingClient _client;
|
||||
private TaskCompletionSource<PlayBillingResult>? _purchaseCompletion;
|
||||
private string? _lastCatalogFailure;
|
||||
|
||||
public AndroidPlayBillingService()
|
||||
{
|
||||
@@ -31,7 +32,12 @@ public sealed class AndroidPlayBillingService : Java.Lang.Object, IPlayBillingSe
|
||||
{
|
||||
var details = await GetProductDetailsAsync(cancellationToken);
|
||||
return details?.GetSubscriptionOfferDetails()?
|
||||
.Where(item => string.IsNullOrEmpty(item.OfferId))
|
||||
.Where(item => !string.IsNullOrWhiteSpace(item.BasePlanId)
|
||||
&& !string.IsNullOrWhiteSpace(item.OfferToken))
|
||||
.GroupBy(item => item.BasePlanId, StringComparer.Ordinal)
|
||||
.Select(group => group
|
||||
.OrderBy(item => string.IsNullOrEmpty(item.OfferId) ? 0 : 1)
|
||||
.First())
|
||||
.Select(item =>
|
||||
{
|
||||
var phase = item.PricingPhases.PricingPhaseList.LastOrDefault();
|
||||
@@ -99,13 +105,25 @@ public sealed class AndroidPlayBillingService : Java.Lang.Object, IPlayBillingSe
|
||||
|
||||
var details = await GetProductDetailsAsync(cancellationToken);
|
||||
var offer = details?.GetSubscriptionOfferDetails()?
|
||||
.FirstOrDefault(item => item.BasePlanId == basePlanId
|
||||
&& string.IsNullOrEmpty(item.OfferId)
|
||||
&& !string.IsNullOrWhiteSpace(item.OfferToken));
|
||||
if (details is null || offer is null)
|
||||
.Where(item => item.BasePlanId == basePlanId
|
||||
&& !string.IsNullOrWhiteSpace(item.OfferToken))
|
||||
.OrderBy(item => string.IsNullOrEmpty(item.OfferId) ? 0 : 1)
|
||||
.FirstOrDefault();
|
||||
if (details is null)
|
||||
{
|
||||
return PlayBillingResult.Failed(
|
||||
$"Piano {basePlanId} non disponibile su Google Play. Verifica che l'abbonamento lme_pro e il piano siano attivi per il tuo Paese.");
|
||||
return PlayBillingResult.Failed(_lastCatalogFailure
|
||||
?? "Google Play non ha restituito l'abbonamento lme_pro.");
|
||||
}
|
||||
|
||||
if (offer is null)
|
||||
{
|
||||
var availablePlans = string.Join(", ", details.GetSubscriptionOfferDetails()?
|
||||
.Select(item => item.BasePlanId)
|
||||
.Where(item => !string.IsNullOrWhiteSpace(item))
|
||||
.Distinct(StringComparer.Ordinal) ?? []);
|
||||
return PlayBillingResult.Failed(string.IsNullOrWhiteSpace(availablePlans)
|
||||
? $"Google Play ha restituito lme_pro senza offerte acquistabili per il piano {basePlanId}."
|
||||
: $"Piano {basePlanId} non restituito da Google Play. Piani ricevuti: {availablePlans}.");
|
||||
}
|
||||
|
||||
var productParams = BillingFlowParams.ProductDetailsParams.NewBuilder()
|
||||
@@ -191,7 +209,18 @@ public sealed class AndroidPlayBillingService : Java.Lang.Object, IPlayBillingSe
|
||||
throw new InvalidOperationException(ToMessage(result.Result));
|
||||
}
|
||||
|
||||
return result.ProductDetailsList?.FirstOrDefault();
|
||||
var details = result.ProductDetailsList?.FirstOrDefault();
|
||||
if (details is not null)
|
||||
{
|
||||
_lastCatalogFailure = null;
|
||||
return details;
|
||||
}
|
||||
|
||||
var unfetched = result.UnfetchedProductList?.FirstOrDefault();
|
||||
_lastCatalogFailure = unfetched is null
|
||||
? "Google Play ha restituito un catalogo vuoto per lme_pro. Verifica account Play Store, adesione al test e origine dell'installazione."
|
||||
: $"Google Play non ha caricato lme_pro (codice catalogo {unfetched.StatusCodeValue}).";
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task EnsureConnectedAsync(CancellationToken cancellationToken)
|
||||
|
||||
Reference in new issue
Block a user