Skip to content

Commit

Permalink
Merge pull request #128 from AndreiMisiukevich/issue_125
Browse files Browse the repository at this point in the history
Issue #125
  • Loading branch information
jamesmontemagno committed Mar 17, 2018
2 parents 52833c0 + 0d2505a commit d72b28b
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/Plugin.InAppBilling.Android/InAppBillingImplementation.cs
Expand Up @@ -286,46 +286,51 @@ public async override Task<InAppBillingPurchase> PurchaseAsync(string productId,

async Task<Purchase> PurchaseAsync(string productSku, string itemType, string payload, IInAppBillingVerifyPurchase verifyPurchase)
{
lock (purchaseLocker)
{
if (tcsPurchase != null && !tcsPurchase.Task.IsCompleted)
return null;

if (tcsPurchase != null && !tcsPurchase.Task.IsCompleted)
return null;
Bundle buyIntentBundle = serviceConnection.Service.GetBuyIntent(3, Context.PackageName, productSku, itemType, payload);
var response = GetResponseCodeFromBundle(buyIntentBundle);

tcsPurchase = new TaskCompletionSource<PurchaseResponse>();
switch (response)
{
case 0:
//OK to purchase
break;
case 1:
//User Cancelled, should try again
throw new InAppBillingPurchaseException(PurchaseError.UserCancelled);
case 2:
//Network connection is down
throw new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable);
case 3:
//Billing Unavailable
throw new InAppBillingPurchaseException(PurchaseError.BillingUnavailable);
case 4:
//Item Unavailable
throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable);
case 5:
//Developer Error
throw new InAppBillingPurchaseException(PurchaseError.DeveloperError);
case 6:
//Generic Error
throw new InAppBillingPurchaseException(PurchaseError.GeneralError);
case 7:
//already purchased
throw new InAppBillingPurchaseException(PurchaseError.AlreadyOwned);
}

Bundle buyIntentBundle = serviceConnection.Service.GetBuyIntent(3, Context.PackageName, productSku, itemType, payload);
var response = GetResponseCodeFromBundle(buyIntentBundle);

switch (response)
{
case 0:
//OK to purchase
break;
case 1:
//User Cancelled, should try again
throw new InAppBillingPurchaseException(PurchaseError.UserCancelled);
case 2:
//Network connection is down
throw new InAppBillingPurchaseException(PurchaseError.ServiceUnavailable);
case 3:
//Billing Unavailable
throw new InAppBillingPurchaseException(PurchaseError.BillingUnavailable);
case 4:
//Item Unavailable
throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable);
case 5:
//Developer Error
throw new InAppBillingPurchaseException(PurchaseError.DeveloperError);
case 6:
//Generic Error
var pendingIntent = buyIntentBundle.GetParcelable(RESPONSE_BUY_INTENT) as PendingIntent;
if (pendingIntent == null)
throw new InAppBillingPurchaseException(PurchaseError.GeneralError);
case 7:
//already purchased
throw new InAppBillingPurchaseException(PurchaseError.AlreadyOwned);
}

var pendingIntent = buyIntentBundle.GetParcelable(RESPONSE_BUY_INTENT) as PendingIntent;
if (pendingIntent != null)
tcsPurchase = new TaskCompletionSource<PurchaseResponse>();

Context.StartIntentSenderForResult(pendingIntent.IntentSender, PURCHASE_REQUEST_CODE, new Intent(), 0, 0, 0);
}

var result = await tcsPurchase.Task;

Expand Down Expand Up @@ -565,6 +570,7 @@ class PurchaseResponse

InAppBillingServiceConnection serviceConnection;
static TaskCompletionSource<PurchaseResponse> tcsPurchase;
static readonly object purchaseLocker = new object();

static bool ValidOwnedItems(Bundle purchased)
{
Expand Down

0 comments on commit d72b28b

Please sign in to comment.