Skip to content

Commit

Permalink
Set offer token
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Jun 1, 2023
1 parent e8c4e4a commit 3e9ab6b
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/Plugin.InAppBilling/InAppBilling.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public async override Task<IEnumerable<InAppBillingProduct>> GetProductInfoAsync
_ => ProductType.Subs
};

if(skuType == ProductType.Subs)
if (skuType == ProductType.Subs)
{
var result = BillingClient.IsFeatureSupported(FeatureType.Subscriptions);
ParseBillingResult(result);
Expand All @@ -170,8 +170,8 @@ public async override Task<IEnumerable<InAppBillingProduct>> GetProductInfoAsync
return skuDetailsResult.ProductDetails.Select(product => product.ToIAPProduct());
}

public override async Task<IEnumerable<InAppBillingPurchase>> GetPurchasesAsync(ItemType itemType)

public override async Task<IEnumerable<InAppBillingPurchase>> GetPurchasesAsync(ItemType itemType)
{
if (BillingClient == null)
throw new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable, "You are not connected to the Google Play App store.");
Expand Down Expand Up @@ -222,7 +222,7 @@ public override async Task<IEnumerable<InAppBillingPurchase>> GetPurchasesHistor
/// <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>
public override async Task<InAppBillingPurchase> UpgradePurchasedSubscriptionAsync(string newProductId, string purchaseTokenOfOriginalSubscription,SubscriptionProrationMode prorationMode = SubscriptionProrationMode.ImmediateWithTimeProration)
public override async Task<InAppBillingPurchase> UpgradePurchasedSubscriptionAsync(string newProductId, string purchaseTokenOfOriginalSubscription, SubscriptionProrationMode prorationMode = SubscriptionProrationMode.ImmediateWithTimeProration)
{
if (BillingClient == null || !IsConnected)
{
Expand Down Expand Up @@ -273,7 +273,7 @@ async Task<InAppBillingPurchase> UpgradePurchasedSubscriptionInternalAsync(strin
.SetOldPurchaseToken(purchaseTokenOfOriginalSubscription)
.SetReplaceProrationMode((int)prorationMode)
.Build();

var prodDetailsParams = BillingFlowParams.ProductDetailsParams.NewBuilder()
.SetProductDetails(skuDetails)
.SetOfferToken(skuDetails.GetSubscriptionOfferDetails()?.FirstOrDefault()?.OfferToken)
Expand Down Expand Up @@ -350,16 +350,20 @@ async Task<InAppBillingPurchase> PurchaseAsync(string productSku, string itemTyp
.SetProductId(productSku)
.Build();

var skuDetailsParams = QueryProductDetailsParams.NewBuilder().SetProductList(new[] {productList});
var skuDetailsParams = QueryProductDetailsParams.NewBuilder().SetProductList(new[] { productList });

var skuDetailsResult = await BillingClient.QueryProductDetailsAsync(skuDetailsParams.Build());

ParseBillingResult(skuDetailsResult.Result);

var skuDetails = skuDetailsResult.ProductDetails.FirstOrDefault() ?? throw new ArgumentException($"{productSku} does not exist");
var productDetailsParamsList = BillingFlowParams.ProductDetailsParams.NewBuilder()
var productDetailsParamsList = itemType == ProductType.Subs ?
BillingFlowParams.ProductDetailsParams.NewBuilder()
.SetProductDetails(skuDetails)
.SetOfferToken(skuDetails.GetSubscriptionOfferDetails()?.FirstOrDefault()?.OfferToken)
.Build()
: BillingFlowParams.ProductDetailsParams.NewBuilder()
.SetProductDetails(skuDetails)
//OFFER TOKEN NEEDED?
.Build();

var billingFlowParams = BillingFlowParams.NewBuilder()
Expand All @@ -382,7 +386,7 @@ async Task<InAppBillingPurchase> PurchaseAsync(string productSku, string itemTyp

var responseCode = BillingClient.LaunchBillingFlow(Activity, flowParams);

ParseBillingResult(responseCode);
ParseBillingResult(responseCode);

var result = await tcsPurchase.Task;
ParseBillingResult(result.billingResult);
Expand Down Expand Up @@ -438,20 +442,20 @@ public override async Task<bool> ConsumePurchaseAsync(string productId, string t
throw new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable, "You are not connected to the Google Play App store.");
}


var consumeParams = ConsumeParams.NewBuilder()
.SetPurchaseToken(transactionIdentifier)
.Build();

var result = await BillingClient.ConsumeAsync(consumeParams);


return ParseBillingResult(result.BillingResult);
return ParseBillingResult(result.BillingResult);
}

static bool ParseBillingResult(BillingResult result)
{
if(result == null)
if (result == null)
throw new InAppBillingPurchaseException(PurchaseError.GeneralError);

if ((int)result.ResponseCode == Android.BillingClient.Api.BillingClient.BillingResponseCode.NetworkError)
Expand All @@ -473,7 +477,7 @@ static bool ParseBillingResult(BillingResult result)
BillingResponseCode.ItemUnavailable => throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable),
_ => false,
};
}
}
}
}

0 comments on commit 3e9ab6b

Please sign in to comment.